| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_PLATFORM_FILE_H_ | 5 #ifndef BASE_PLATFORM_FILE_H_ |
| 6 #define BASE_PLATFORM_FILE_H_ | 6 #define BASE_PLATFORM_FILE_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "base/basictypes.h" | |
| 10 #include "build/build_config.h" | 9 #include "build/build_config.h" |
| 11 #include "base/file_path.h" | |
| 12 #include "base/time.h" | |
| 13 #if defined(OS_WIN) | 10 #if defined(OS_WIN) |
| 14 #include <windows.h> | 11 #include <windows.h> |
| 15 #endif | 12 #endif |
| 16 | 13 |
| 17 #include <string> | 14 #include <string> |
| 18 | 15 |
| 16 #include "base/base_api.h" |
| 17 #include "base/basictypes.h" |
| 18 #include "base/file_path.h" |
| 19 #include "base/time.h" |
| 20 |
| 21 class FilePath; |
| 22 |
| 19 namespace base { | 23 namespace base { |
| 20 | 24 |
| 21 #if defined(OS_WIN) | 25 #if defined(OS_WIN) |
| 22 typedef HANDLE PlatformFile; | 26 typedef HANDLE PlatformFile; |
| 23 const PlatformFile kInvalidPlatformFileValue = INVALID_HANDLE_VALUE; | 27 const PlatformFile kInvalidPlatformFileValue = INVALID_HANDLE_VALUE; |
| 24 #elif defined(OS_POSIX) | 28 #elif defined(OS_POSIX) |
| 25 typedef int PlatformFile; | 29 typedef int PlatformFile; |
| 26 const PlatformFile kInvalidPlatformFileValue = -1; | 30 const PlatformFile kInvalidPlatformFileValue = -1; |
| 27 #endif | 31 #endif |
| 28 | 32 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 PLATFORM_FILE_ERROR_ABORT = -12, | 66 PLATFORM_FILE_ERROR_ABORT = -12, |
| 63 PLATFORM_FILE_ERROR_NOT_A_FILE = -13, | 67 PLATFORM_FILE_ERROR_NOT_A_FILE = -13, |
| 64 PLATFORM_FILE_ERROR_NOT_EMPTY = -14, | 68 PLATFORM_FILE_ERROR_NOT_EMPTY = -14, |
| 65 }; | 69 }; |
| 66 | 70 |
| 67 // Used to hold information about a given file. | 71 // Used to hold information about a given file. |
| 68 // If you add more fields to this structure (platform-specific fields are OK), | 72 // If you add more fields to this structure (platform-specific fields are OK), |
| 69 // make sure to update all functions that use it in file_util_{win|posix}.cc | 73 // make sure to update all functions that use it in file_util_{win|posix}.cc |
| 70 // too, and the ParamTraits<base::PlatformFileInfo> implementation in | 74 // too, and the ParamTraits<base::PlatformFileInfo> implementation in |
| 71 // chrome/common/common_param_traits.cc. | 75 // chrome/common/common_param_traits.cc. |
| 72 struct PlatformFileInfo { | 76 struct BASE_API PlatformFileInfo { |
| 73 PlatformFileInfo(); | 77 PlatformFileInfo(); |
| 74 ~PlatformFileInfo(); | 78 ~PlatformFileInfo(); |
| 75 | 79 |
| 76 // The size of the file in bytes. Undefined when is_directory is true. | 80 // The size of the file in bytes. Undefined when is_directory is true. |
| 77 int64 size; | 81 int64 size; |
| 78 | 82 |
| 79 // True if the file corresponds to a directory. | 83 // True if the file corresponds to a directory. |
| 80 bool is_directory; | 84 bool is_directory; |
| 81 | 85 |
| 82 // True if the file corresponds to a symbolic link. | 86 // True if the file corresponds to a symbolic link. |
| 83 bool is_symbolic_link; | 87 bool is_symbolic_link; |
| 84 | 88 |
| 85 // The last modified time of a file. | 89 // The last modified time of a file. |
| 86 base::Time last_modified; | 90 base::Time last_modified; |
| 87 | 91 |
| 88 // The last accessed time of a file. | 92 // The last accessed time of a file. |
| 89 base::Time last_accessed; | 93 base::Time last_accessed; |
| 90 | 94 |
| 91 // The creation time of a file. | 95 // The creation time of a file. |
| 92 base::Time creation_time; | 96 base::Time creation_time; |
| 93 | 97 |
| 94 // The full path of a file. Currently only used by FileSystemFileUtil during | 98 // The full path of a file. Currently only used by FileSystemFileUtil during |
| 95 // a GetMetadata operation. | 99 // a GetMetadata operation. |
| 96 FilePath path; | 100 FilePath path; |
| 97 }; | 101 }; |
| 98 | 102 |
| 99 // Creates or opens the given file. If PLATFORM_FILE_OPEN_ALWAYS is used, and | 103 // Creates or opens the given file. If PLATFORM_FILE_OPEN_ALWAYS is used, and |
| 100 // |created| is provided, |created| will be set to true if the file was created | 104 // |created| is provided, |created| will be set to true if the file was created |
| 101 // or to false in case the file was just opened. |error_code| can be NULL. | 105 // or to false in case the file was just opened. |error_code| can be NULL. |
| 102 PlatformFile CreatePlatformFile(const FilePath& name, | 106 BASE_API PlatformFile CreatePlatformFile(const FilePath& name, |
| 103 int flags, | 107 int flags, |
| 104 bool* created, | 108 bool* created, |
| 105 PlatformFileError* error_code); | 109 PlatformFileError* error_code); |
| 106 | 110 |
| 107 // Closes a file handle. Returns |true| on success and |false| otherwise. | 111 // Closes a file handle. Returns |true| on success and |false| otherwise. |
| 108 bool ClosePlatformFile(PlatformFile file); | 112 BASE_API bool ClosePlatformFile(PlatformFile file); |
| 109 | 113 |
| 110 // Reads the given number of bytes (or until EOF is reached) starting with the | 114 // Reads the given number of bytes (or until EOF is reached) starting with the |
| 111 // given offset. Returns the number of bytes read, or -1 on error. | 115 // given offset. Returns the number of bytes read, or -1 on error. |
| 112 int ReadPlatformFile(PlatformFile file, int64 offset, char* data, int size); | 116 BASE_API int ReadPlatformFile(PlatformFile file, int64 offset, |
| 117 char* data, int size); |
| 113 | 118 |
| 114 // Writes the given buffer into the file at the given offset, overwritting any | 119 // Writes the given buffer into the file at the given offset, overwritting any |
| 115 // data that was previously there. Returns the number of bytes written, or -1 | 120 // data that was previously there. Returns the number of bytes written, or -1 |
| 116 // on error. | 121 // on error. |
| 117 int WritePlatformFile(PlatformFile file, int64 offset, | 122 BASE_API int WritePlatformFile(PlatformFile file, int64 offset, |
| 118 const char* data, int size); | 123 const char* data, int size); |
| 119 | 124 |
| 120 // Truncates the given file to the given length. If |length| is greater than | 125 // Truncates the given file to the given length. If |length| is greater than |
| 121 // the current size of the file, the file is extended with zeros. If the file | 126 // the current size of the file, the file is extended with zeros. If the file |
| 122 // doesn't exist, |false| is returned. | 127 // doesn't exist, |false| is returned. |
| 123 bool TruncatePlatformFile(PlatformFile file, int64 length); | 128 BASE_API bool TruncatePlatformFile(PlatformFile file, int64 length); |
| 124 | 129 |
| 125 // Flushes the buffers of the given file. | 130 // Flushes the buffers of the given file. |
| 126 bool FlushPlatformFile(PlatformFile file); | 131 BASE_API bool FlushPlatformFile(PlatformFile file); |
| 127 | 132 |
| 128 // Touches the given file. | 133 // Touches the given file. |
| 129 bool TouchPlatformFile(PlatformFile file, const Time& last_access_time, | 134 BASE_API bool TouchPlatformFile(PlatformFile file, const Time& last_access_time, |
| 130 const Time& last_modified_time); | 135 const Time& last_modified_time); |
| 131 | 136 |
| 132 // Returns some information for the given file. | 137 // Returns some information for the given file. |
| 133 bool GetPlatformFileInfo(PlatformFile file, PlatformFileInfo* info); | 138 BASE_API bool GetPlatformFileInfo(PlatformFile file, PlatformFileInfo* info); |
| 134 | 139 |
| 135 // Use this class to pass ownership of a PlatformFile to a receiver that may or | 140 // Use this class to pass ownership of a PlatformFile to a receiver that may or |
| 136 // may not want to accept it. This class does not own the storage for the | 141 // may not want to accept it. This class does not own the storage for the |
| 137 // PlatformFile. | 142 // PlatformFile. |
| 138 // | 143 // |
| 139 // EXAMPLE: | 144 // EXAMPLE: |
| 140 // | 145 // |
| 141 // void MaybeProcessFile(PassPlatformFile pass_file) { | 146 // void MaybeProcessFile(PassPlatformFile pass_file) { |
| 142 // if (...) { | 147 // if (...) { |
| 143 // PlatformFile file = pass_file.ReleaseValue(); | 148 // PlatformFile file = pass_file.ReleaseValue(); |
| 144 // // Now, we are responsible for closing |file|. | 149 // // Now, we are responsible for closing |file|. |
| 145 // } | 150 // } |
| 146 // } | 151 // } |
| 147 // | 152 // |
| 148 // void OpenAndMaybeProcessFile(const FilePath& path) { | 153 // void OpenAndMaybeProcessFile(const FilePath& path) { |
| 149 // PlatformFile file = CreatePlatformFile(path, ...); | 154 // PlatformFile file = CreatePlatformFile(path, ...); |
| 150 // MaybeProcessFile(PassPlatformFile(&file)); | 155 // MaybeProcessFile(PassPlatformFile(&file)); |
| 151 // if (file != kInvalidPlatformFileValue) | 156 // if (file != kInvalidPlatformFileValue) |
| 152 // ClosePlatformFile(file); | 157 // ClosePlatformFile(file); |
| 153 // } | 158 // } |
| 154 // | 159 // |
| 155 class PassPlatformFile { | 160 class BASE_API PassPlatformFile { |
| 156 public: | 161 public: |
| 157 explicit PassPlatformFile(PlatformFile* value) : value_(value) { | 162 explicit PassPlatformFile(PlatformFile* value) : value_(value) { |
| 158 } | 163 } |
| 159 | 164 |
| 160 // Called to retrieve the PlatformFile stored in this object. The caller | 165 // Called to retrieve the PlatformFile stored in this object. The caller |
| 161 // gains ownership of the PlatformFile and is now responsible for closing it. | 166 // gains ownership of the PlatformFile and is now responsible for closing it. |
| 162 // Any subsequent calls to this method will return an invalid PlatformFile. | 167 // Any subsequent calls to this method will return an invalid PlatformFile. |
| 163 PlatformFile ReleaseValue() { | 168 PlatformFile ReleaseValue() { |
| 164 PlatformFile temp = *value_; | 169 PlatformFile temp = *value_; |
| 165 *value_ = kInvalidPlatformFileValue; | 170 *value_ = kInvalidPlatformFileValue; |
| 166 return temp; | 171 return temp; |
| 167 } | 172 } |
| 168 | 173 |
| 169 private: | 174 private: |
| 170 PlatformFile* value_; | 175 PlatformFile* value_; |
| 171 }; | 176 }; |
| 172 | 177 |
| 173 } // namespace base | 178 } // namespace base |
| 174 | 179 |
| 175 #endif // BASE_PLATFORM_FILE_H_ | 180 #endif // BASE_PLATFORM_FILE_H_ |
| OLD | NEW |