Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_FILES_BASE_FILE_H_ |
| 6 #define BASE_PLATFORM_FILE_H_ | 6 #define BASE_FILES_BASE_FILE_H_ |
| 7 | 7 |
| 8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
| 9 #if defined(OS_WIN) | 9 #if defined(OS_WIN) |
| 10 #include <windows.h> | 10 #include <windows.h> |
| 11 #endif | 11 #endif |
| 12 | 12 |
| 13 #include <string> | 13 #include <string> |
| 14 | 14 |
| 15 #include "base/base_export.h" | 15 #include "base/base_export.h" |
| 16 #include "base/basictypes.h" | 16 #include "base/basictypes.h" |
| 17 #include "base/files/file_path.h" | 17 #include "base/files/file_path.h" |
| 18 #include "base/move.h" | |
| 18 #include "base/time/time.h" | 19 #include "base/time/time.h" |
| 19 | 20 |
| 21 #if defined(OS_WIN) | |
| 22 #include "base/win/scoped_handle.h" | |
| 23 #endif | |
| 24 | |
| 20 namespace base { | 25 namespace base { |
| 21 | 26 |
| 22 // PLATFORM_FILE_(OPEN|CREATE).* are mutually exclusive. You should specify | 27 // BASE_FILE_(OPEN|CREATE).* are mutually exclusive. You should specify |
| 23 // exactly one of the five (possibly combining with other flags) when opening | 28 // exactly one of the five (possibly combining with other flags) when opening |
| 24 // or creating a file. | 29 // or creating a file. |
| 25 // PLATFORM_FILE_(WRITE|APPEND) are mutually exclusive. This is so that APPEND | 30 // BASE_FILE_(WRITE|APPEND) are mutually exclusive. This is so that APPEND |
| 26 // behavior will be consistent with O_APPEND on POSIX. | 31 // behavior will be consistent with O_APPEND on POSIX. |
| 27 // PLATFORM_FILE_EXCLUSIVE_(READ|WRITE) only grant exclusive access to the file | 32 // BASE_FILE_EXCLUSIVE_(READ|WRITE) only grant exclusive access to the file |
| 28 // on creation on POSIX; for existing files, consider using LockPlatformFile(). | 33 // on creation on POSIX; for existing files, consider using LockPlatformFile(). |
| 29 enum PlatformFileFlags { | 34 enum BaseFileFlags { |
|
brettw
2013/11/26 23:04:49
Can these be in the BaseFile class? If I was doing
rvargas (doing something else)
2013/11/26 23:45:53
Sure... I assume you are talking only about the en
| |
| 30 PLATFORM_FILE_OPEN = 1 << 0, // Opens a file, only if it exists. | 35 BASE_FILE_OPEN = 1 << 0, // Opens a file, only if it exists. |
| 31 PLATFORM_FILE_CREATE = 1 << 1, // Creates a new file, only if it | 36 BASE_FILE_CREATE = 1 << 1, // Creates a new file, only if it |
| 32 // does not already exist. | 37 // does not already exist. |
| 33 PLATFORM_FILE_OPEN_ALWAYS = 1 << 2, // May create a new file. | 38 BASE_FILE_OPEN_ALWAYS = 1 << 2, // May create a new file. |
| 34 PLATFORM_FILE_CREATE_ALWAYS = 1 << 3, // May overwrite an old file. | 39 BASE_FILE_CREATE_ALWAYS = 1 << 3, // May overwrite an old file. |
| 35 PLATFORM_FILE_OPEN_TRUNCATED = 1 << 4, // Opens a file and truncates it, | 40 BASE_FILE_OPEN_TRUNCATED = 1 << 4, // Opens a file and truncates it, |
| 36 // only if it exists. | 41 // only if it exists. |
| 37 PLATFORM_FILE_READ = 1 << 5, | 42 BASE_FILE_READ = 1 << 5, |
| 38 PLATFORM_FILE_WRITE = 1 << 6, | 43 BASE_FILE_WRITE = 1 << 6, |
| 39 PLATFORM_FILE_APPEND = 1 << 7, | 44 BASE_FILE_APPEND = 1 << 7, |
| 40 PLATFORM_FILE_EXCLUSIVE_READ = 1 << 8, // EXCLUSIVE is opposite of Windows | 45 BASE_FILE_EXCLUSIVE_READ = 1 << 8, // EXCLUSIVE is opposite of Windows |
| 41 // SHARE | 46 // SHARE |
| 42 PLATFORM_FILE_EXCLUSIVE_WRITE = 1 << 9, | 47 BASE_FILE_EXCLUSIVE_WRITE = 1 << 9, |
| 43 PLATFORM_FILE_ASYNC = 1 << 10, | 48 BASE_FILE_ASYNC = 1 << 10, |
| 44 PLATFORM_FILE_TEMPORARY = 1 << 11, // Used on Windows only | 49 BASE_FILE_TEMPORARY = 1 << 11, // Used on Windows only |
| 45 PLATFORM_FILE_HIDDEN = 1 << 12, // Used on Windows only | 50 BASE_FILE_HIDDEN = 1 << 12, // Used on Windows only |
| 46 PLATFORM_FILE_DELETE_ON_CLOSE = 1 << 13, | 51 BASE_FILE_DELETE_ON_CLOSE = 1 << 13, |
| 47 | 52 |
| 48 PLATFORM_FILE_WRITE_ATTRIBUTES = 1 << 14, // Used on Windows only | 53 BASE_FILE_WRITE_ATTRIBUTES = 1 << 14, // Used on Windows only |
| 49 | 54 |
| 50 PLATFORM_FILE_SHARE_DELETE = 1 << 15, // Used on Windows only | 55 BASE_FILE_SHARE_DELETE = 1 << 15, // Used on Windows only |
| 51 | 56 |
| 52 PLATFORM_FILE_TERMINAL_DEVICE = 1 << 16, // Serial port flags | 57 BASE_FILE_TERMINAL_DEVICE = 1 << 16, // Serial port flags |
| 53 PLATFORM_FILE_BACKUP_SEMANTICS = 1 << 17, // Used on Windows only | 58 BASE_FILE_BACKUP_SEMANTICS = 1 << 17, // Used on Windows only |
| 54 | 59 |
| 55 PLATFORM_FILE_EXECUTE = 1 << 18, // Used on Windows only | 60 BASE_FILE_EXECUTE = 1 << 18, // Used on Windows only |
| 56 }; | 61 }; |
| 57 | 62 |
| 58 // This enum has been recorded in multiple histograms. If the order of the | 63 // This enum has been recorded in multiple histograms. If the order of the |
| 59 // fields needs to change, please ensure that those histograms are obsolete or | 64 // fields needs to change, please ensure that those histograms are obsolete or |
| 60 // have been moved to a different enum. | 65 // have been moved to a different enum. |
| 61 // | 66 // |
| 62 // PLATFORM_FILE_ERROR_ACCESS_DENIED is returned when a call fails because of | 67 // BASE_FILE_ERROR_ACCESS_DENIED is returned when a call fails because of |
| 63 // a filesystem restriction. PLATFORM_FILE_ERROR_SECURITY is returned when a | 68 // a filesystem restriction. BASE_FILE_ERROR_SECURITY is returned when a |
| 64 // browser policy doesn't allow the operation to be executed. | 69 // browser policy doesn't allow the operation to be executed. |
| 65 enum PlatformFileError { | 70 enum BaseFileError { |
| 66 PLATFORM_FILE_OK = 0, | 71 BASE_FILE_OK = 0, |
| 67 PLATFORM_FILE_ERROR_FAILED = -1, | 72 BASE_FILE_ERROR_FAILED = -1, |
| 68 PLATFORM_FILE_ERROR_IN_USE = -2, | 73 BASE_FILE_ERROR_IN_USE = -2, |
| 69 PLATFORM_FILE_ERROR_EXISTS = -3, | 74 BASE_FILE_ERROR_EXISTS = -3, |
| 70 PLATFORM_FILE_ERROR_NOT_FOUND = -4, | 75 BASE_FILE_ERROR_NOT_FOUND = -4, |
| 71 PLATFORM_FILE_ERROR_ACCESS_DENIED = -5, | 76 BASE_FILE_ERROR_ACCESS_DENIED = -5, |
| 72 PLATFORM_FILE_ERROR_TOO_MANY_OPENED = -6, | 77 BASE_FILE_ERROR_TOO_MANY_OPENED = -6, |
| 73 PLATFORM_FILE_ERROR_NO_MEMORY = -7, | 78 BASE_FILE_ERROR_NO_MEMORY = -7, |
| 74 PLATFORM_FILE_ERROR_NO_SPACE = -8, | 79 BASE_FILE_ERROR_NO_SPACE = -8, |
| 75 PLATFORM_FILE_ERROR_NOT_A_DIRECTORY = -9, | 80 BASE_FILE_ERROR_NOT_A_DIRECTORY = -9, |
| 76 PLATFORM_FILE_ERROR_INVALID_OPERATION = -10, | 81 BASE_FILE_ERROR_INVALID_OPERATION = -10, |
| 77 PLATFORM_FILE_ERROR_SECURITY = -11, | 82 BASE_FILE_ERROR_SECURITY = -11, |
| 78 PLATFORM_FILE_ERROR_ABORT = -12, | 83 BASE_FILE_ERROR_ABORT = -12, |
| 79 PLATFORM_FILE_ERROR_NOT_A_FILE = -13, | 84 BASE_FILE_ERROR_NOT_A_FILE = -13, |
| 80 PLATFORM_FILE_ERROR_NOT_EMPTY = -14, | 85 BASE_FILE_ERROR_NOT_EMPTY = -14, |
| 81 PLATFORM_FILE_ERROR_INVALID_URL = -15, | 86 BASE_FILE_ERROR_INVALID_URL = -15, |
| 82 PLATFORM_FILE_ERROR_IO = -16, | 87 BASE_FILE_ERROR_IO = -16, |
| 83 // Put new entries here and increment PLATFORM_FILE_ERROR_MAX. | 88 // Put new entries here and increment BASE_FILE_ERROR_MAX. |
| 84 PLATFORM_FILE_ERROR_MAX = -17 | 89 BASE_FILE_ERROR_MAX = -17 |
| 85 }; | 90 }; |
| 86 | 91 |
| 87 // This explicit mapping matches both FILE_ on Windows and SEEK_ on Linux. | 92 // This explicit mapping matches both FILE_ on Windows and SEEK_ on Linux. |
| 88 enum PlatformFileWhence { | 93 enum BaseFileWhence { |
| 89 PLATFORM_FILE_FROM_BEGIN = 0, | 94 BASE_FILE_FROM_BEGIN = 0, |
| 90 PLATFORM_FILE_FROM_CURRENT = 1, | 95 BASE_FILE_FROM_CURRENT = 1, |
| 91 PLATFORM_FILE_FROM_END = 2 | 96 BASE_FILE_FROM_END = 2 |
| 92 }; | 97 }; |
| 93 | 98 |
| 94 // Used to hold information about a given file. | 99 // Used to hold information about a given file. |
| 95 // If you add more fields to this structure (platform-specific fields are OK), | 100 // If you add more fields to this structure (platform-specific fields are OK), |
| 96 // make sure to update all functions that use it in file_util_{win|posix}.cc | 101 // make sure to update all functions that use it in file_util_{win|posix}.cc |
| 97 // too, and the ParamTraits<base::PlatformFileInfo> implementation in | 102 // too, and the ParamTraits<base::PlatformFileInfo> implementation in |
| 98 // chrome/common/common_param_traits.cc. | 103 // chrome/common/common_param_traits.cc. |
| 99 struct BASE_EXPORT PlatformFileInfo { | 104 struct BASE_EXPORT BaseFileInfo { |
| 100 PlatformFileInfo(); | 105 BaseFileInfo(); |
| 101 ~PlatformFileInfo(); | 106 ~BaseFileInfo(); |
| 102 | 107 |
| 103 // The size of the file in bytes. Undefined when is_directory is true. | 108 // The size of the file in bytes. Undefined when is_directory is true. |
| 104 int64 size; | 109 int64 size; |
| 105 | 110 |
| 106 // True if the file corresponds to a directory. | 111 // True if the file corresponds to a directory. |
| 107 bool is_directory; | 112 bool is_directory; |
| 108 | 113 |
| 109 // True if the file corresponds to a symbolic link. | 114 // True if the file corresponds to a symbolic link. |
| 110 bool is_symbolic_link; | 115 bool is_symbolic_link; |
| 111 | 116 |
| 112 // The last modified time of a file. | 117 // The last modified time of a file. |
| 113 base::Time last_modified; | 118 base::Time last_modified; |
| 114 | 119 |
| 115 // The last accessed time of a file. | 120 // The last accessed time of a file. |
| 116 base::Time last_accessed; | 121 base::Time last_accessed; |
| 117 | 122 |
| 118 // The creation time of a file. | 123 // The creation time of a file. |
| 119 base::Time creation_time; | 124 base::Time creation_time; |
| 120 }; | 125 }; |
| 121 | 126 |
| 122 #if defined(OS_WIN) | 127 #if defined(OS_WIN) |
| 123 typedef HANDLE PlatformFile; | 128 typedef HANDLE PlatformFile; |
| 124 const PlatformFile kInvalidPlatformFileValue = INVALID_HANDLE_VALUE; | 129 BASE_EXPORT BaseFileError ErrorToBaseFileError(DWORD last_error); |
| 125 BASE_EXPORT PlatformFileError LastErrorToPlatformFileError(DWORD last_error); | |
| 126 #elif defined(OS_POSIX) | 130 #elif defined(OS_POSIX) |
| 127 typedef int PlatformFile; | 131 typedef int PlatformFile; |
| 128 const PlatformFile kInvalidPlatformFileValue = -1; | 132 BASE_EXPORT BaseFileError ErrorToBaseFileError(int saved_errno); |
| 129 BASE_EXPORT PlatformFileError ErrnoToPlatformFileError(int saved_errno); | |
| 130 #endif | 133 #endif |
| 131 | 134 |
| 132 // Creates or opens the given file. If |created| is provided, it will be set to | 135 // Thin wrapper around an OS-level file. |
| 133 // true if a new file was created [or an old one truncated to zero length to | 136 // Note that this class does not provide any support for asynchronous IO, other |
| 134 // simulate a new file, which can happen with PLATFORM_FILE_CREATE_ALWAYS], and | 137 // than the ability to create asynchronous handles on Windows. |
| 135 // false otherwise. |error| can be NULL. | 138 class BASE_EXPORT BaseFile { |
| 136 // | 139 MOVE_ONLY_TYPE_FOR_CPP_03(BaseFile, RValue) |
| 137 // This function fails with 'access denied' if the |name| contains path | |
| 138 // traversal ('..') components. | |
| 139 BASE_EXPORT PlatformFile CreatePlatformFile(const FilePath& name, | |
| 140 int flags, | |
| 141 bool* created, | |
| 142 PlatformFileError* error); | |
| 143 | 140 |
| 144 // Same as CreatePlatformFile but allows paths with traversal (like \..\) | 141 public: |
| 145 // components. Use only with extreme care. | 142 BaseFile(); |
| 146 BASE_EXPORT PlatformFile CreatePlatformFileUnsafe(const FilePath& name, | |
| 147 int flags, | |
| 148 bool* created, | |
| 149 PlatformFileError* error); | |
| 150 | 143 |
| 151 BASE_EXPORT FILE* FdopenPlatformFile(PlatformFile file, const char* mode); | 144 // Creates or opens the given file. This will fail with 'access denied' if the |
| 145 // |name| contains path traversal ('..') components. | |
| 146 BaseFile(const FilePath& name, int flags); | |
| 152 | 147 |
| 153 // Closes a file handle. Returns |true| on success and |false| otherwise. | 148 // Takes ownership of |platform_file|. |
| 154 BASE_EXPORT bool ClosePlatformFile(PlatformFile file); | 149 explicit BaseFile(PlatformFile platform_file); |
| 155 | 150 |
| 156 // Changes current position in the file to an |offset| relative to an origin | 151 // Move constructor for C++03 move emulation of this type. |
| 157 // defined by |whence|. Returns the resultant current position in the file | 152 BaseFile(RValue other); |
| 158 // (relative to the start) or -1 in case of error. | |
| 159 BASE_EXPORT int64 SeekPlatformFile(PlatformFile file, | |
| 160 PlatformFileWhence whence, | |
| 161 int64 offset); | |
| 162 | 153 |
| 163 // Reads the given number of bytes (or until EOF is reached) starting with the | 154 ~BaseFile(); |
| 164 // given offset. Returns the number of bytes read, or -1 on error. Note that | |
| 165 // this function makes a best effort to read all data on all platforms, so it is | |
| 166 // not intended for stream oriented files but instead for cases when the normal | |
| 167 // expectation is that actually |size| bytes are read unless there is an error. | |
| 168 BASE_EXPORT int ReadPlatformFile(PlatformFile file, int64 offset, | |
| 169 char* data, int size); | |
| 170 | 155 |
| 171 // Same as above but without seek. | 156 // Move operator= for C++03 move emulation of this type. |
| 172 BASE_EXPORT int ReadPlatformFileAtCurrentPos(PlatformFile file, | 157 BaseFile& operator=(RValue other); |
| 173 char* data, int size); | |
| 174 | 158 |
| 175 // Reads the given number of bytes (or until EOF is reached) starting with the | 159 // Creates or opens the given file, allowing paths with traversal ('..') |
| 176 // given offset, but does not make any effort to read all data on all platforms. | 160 // components. Use only with extreme care. |
| 177 // Returns the number of bytes read, or -1 on error. | 161 void CreateBaseFileUnsafe(const FilePath& name, int flags); |
| 178 BASE_EXPORT int ReadPlatformFileNoBestEffort(PlatformFile file, int64 offset, | |
| 179 char* data, int size); | |
| 180 | 162 |
| 181 // Same as above but without seek. | 163 bool IsValid() const; |
| 182 BASE_EXPORT int ReadPlatformFileCurPosNoBestEffort(PlatformFile file, | |
| 183 char* data, int size); | |
| 184 | 164 |
| 185 // Writes the given buffer into the file at the given offset, overwritting any | 165 // Returns true if a new file was created (or an old one truncated to zero |
| 186 // data that was previously there. Returns the number of bytes written, or -1 | 166 // length to simulate a new file, which can happen with |
| 187 // on error. Note that this function makes a best effort to write all data on | 167 // BASE_FILE_CREATE_ALWAYS), and false otherwise. |
| 188 // all platforms. | 168 bool created() const { return created_; } |
| 189 // Ignores the offset and writes to the end of the file if the file was opened | |
| 190 // with PLATFORM_FILE_APPEND. | |
| 191 BASE_EXPORT int WritePlatformFile(PlatformFile file, int64 offset, | |
| 192 const char* data, int size); | |
| 193 | 169 |
| 194 // Save as above but without seek. | 170 // Returns the OS result of opening this file. |
| 195 BASE_EXPORT int WritePlatformFileAtCurrentPos(PlatformFile file, | 171 BaseFileError error() const { return error_; } |
| 196 const char* data, int size); | |
| 197 | 172 |
| 198 // Save as above but does not make any effort to write all data on all | 173 PlatformFile GetPlatformFile() const { return file_; } |
| 199 // platforms. Returns the number of bytes written, or -1 on error. | 174 PlatformFile TakePlatformFile(); |
| 200 BASE_EXPORT int WritePlatformFileCurPosNoBestEffort(PlatformFile file, | |
| 201 const char* data, int size); | |
| 202 | 175 |
| 203 // Truncates the given file to the given length. If |length| is greater than | 176 // Destroying this object closes the file automatically. |
| 204 // the current size of the file, the file is extended with zeros. If the file | 177 void Close(); |
| 205 // doesn't exist, |false| is returned. | |
| 206 BASE_EXPORT bool TruncatePlatformFile(PlatformFile file, int64 length); | |
| 207 | 178 |
| 208 // Flushes the buffers of the given file. | 179 // Changes current position in the file to an |offset| relative to an origin |
| 209 BASE_EXPORT bool FlushPlatformFile(PlatformFile file); | 180 // defined by |whence|. Returns the resultant current position in the file |
| 181 // (relative to the start) or -1 in case of error. | |
| 182 int64 Seek(BaseFileWhence whence, int64 offset); | |
| 210 | 183 |
| 211 // Touches the given file. | 184 // Reads the given number of bytes (or until EOF is reached) starting with the |
| 212 BASE_EXPORT bool TouchPlatformFile(PlatformFile file, | 185 // given offset. Returns the number of bytes read, or -1 on error. Note that |
| 213 const Time& last_access_time, | 186 // this function makes a best effort to read all data on all platforms, so it |
| 214 const Time& last_modified_time); | 187 // is not intended for stream oriented files but instead for cases when the |
| 188 // normal expectation is that actually |size| bytes are read unless there is | |
| 189 // an error. | |
| 190 int Read(int64 offset, char* data, int size); | |
| 215 | 191 |
| 216 // Returns some information for the given file. | 192 // Same as above but without seek. |
| 217 BASE_EXPORT bool GetPlatformFileInfo(PlatformFile file, PlatformFileInfo* info); | 193 int ReadAtCurrentPos(char* data, int size); |
| 218 | 194 |
| 219 // Attempts to take an exclusive write lock on the file. Returns immediately | 195 // Reads the given number of bytes (or until EOF is reached) starting with the |
| 220 // (i.e. does not wait for another process to unlock the file). If the lock | 196 // given offset, but does not make any effort to read all data on all |
| 221 // was obtained, the result will be PLATFORM_FILE_OK. A lock only guarantees | 197 // platforms. Returns the number of bytes read, or -1 on error. |
| 222 // that other processes may not also take a lock on the same file with the | 198 int ReadNoBestEffort(int64 offset, char* data, int size); |
| 223 // same API - it may still be opened, renamed, unlinked, etc. | |
| 224 // | |
| 225 // Common semantics: | |
| 226 // * Locks are held by processes, but not inherited by child processes. | |
| 227 // * Locks are released by the OS on file handle close or process termination. | |
| 228 // * Locks are reliable only on local filesystems. | |
| 229 // * Duplicated file handles may also write to locked files. | |
| 230 // Windows-specific semantics: | |
| 231 // * Locks are mandatory for read/write APIs, advisory for mapping APIs. | |
| 232 // * Within a process, locking the same file (by the same or new handle) | |
| 233 // will fail. | |
| 234 // POSIX-specific semantics: | |
| 235 // * Locks are advisory only. | |
| 236 // * Within a process, locking the same file (by the same or new handle) | |
| 237 // will succeed. | |
| 238 // * Closing any descriptor on a given file releases the lock. | |
| 239 BASE_EXPORT PlatformFileError LockPlatformFile(PlatformFile file); | |
| 240 | 199 |
| 241 // Unlock a file previously locked with LockPlatformFile. | 200 // Same as above but without seek. |
| 242 BASE_EXPORT PlatformFileError UnlockPlatformFile(PlatformFile file); | 201 int ReadAtCurrentPosNoBestEffort(char* data, int size); |
| 243 | 202 |
| 244 // Use this class to pass ownership of a PlatformFile to a receiver that may or | 203 // Writes the given buffer into the file at the given offset, overwritting any |
| 245 // may not want to accept it. This class does not own the storage for the | 204 // data that was previously there. Returns the number of bytes written, or -1 |
| 246 // PlatformFile. | 205 // on error. Note that this function makes a best effort to write all data on |
| 247 // | 206 // all platforms. |
| 248 // EXAMPLE: | 207 // Ignores the offset and writes to the end of the file if the file was opened |
| 249 // | 208 // with BASE_FILE_APPEND. |
| 250 // void MaybeProcessFile(PassPlatformFile pass_file) { | 209 int Write(int64 offset, const char* data, int size); |
| 251 // if (...) { | |
| 252 // PlatformFile file = pass_file.ReleaseValue(); | |
| 253 // // Now, we are responsible for closing |file|. | |
| 254 // } | |
| 255 // } | |
| 256 // | |
| 257 // void OpenAndMaybeProcessFile(const FilePath& path) { | |
| 258 // PlatformFile file = CreatePlatformFile(path, ...); | |
| 259 // MaybeProcessFile(PassPlatformFile(&file)); | |
| 260 // if (file != kInvalidPlatformFileValue) | |
| 261 // ClosePlatformFile(file); | |
| 262 // } | |
| 263 // | |
| 264 class BASE_EXPORT PassPlatformFile { | |
| 265 public: | |
| 266 explicit PassPlatformFile(PlatformFile* value) : value_(value) { | |
| 267 } | |
| 268 | 210 |
| 269 // Called to retrieve the PlatformFile stored in this object. The caller | 211 // Save as above but without seek. |
| 270 // gains ownership of the PlatformFile and is now responsible for closing it. | 212 int WriteAtCurrentPos(const char* data, int size); |
| 271 // Any subsequent calls to this method will return an invalid PlatformFile. | 213 |
| 272 PlatformFile ReleaseValue() { | 214 // Save as above but does not make any effort to write all data on all |
| 273 PlatformFile temp = *value_; | 215 // platforms. Returns the number of bytes written, or -1 on error. |
| 274 *value_ = kInvalidPlatformFileValue; | 216 int WriteAtCurrentPosNoBestEffort(const char* data, int size); |
| 275 return temp; | 217 |
| 276 } | 218 // Truncates the file to the given length. If |length| is greater than the |
| 219 // current size of the file, the file is extended with zeros. If the file | |
| 220 // doesn't exist, |false| is returned. | |
| 221 bool Truncate(int64 length); | |
| 222 | |
| 223 // Flushes the buffers. | |
| 224 bool Flush(); | |
| 225 | |
| 226 // Updates the file times. | |
| 227 bool SetTime(Time last_access_time, Time last_modified_time); | |
| 228 | |
| 229 // Returns some basic information for the given file. | |
| 230 bool GetInfo(BaseFileInfo* info); | |
| 231 | |
| 232 // Attempts to take an exclusive write lock on the file. Returns immediately | |
| 233 // (i.e. does not wait for another process to unlock the file). If the lock | |
| 234 // was obtained, the result will be BASE_FILE_OK. A lock only guarantees | |
| 235 // that other processes may not also take a lock on the same file with the | |
| 236 // same API - it may still be opened, renamed, unlinked, etc. | |
| 237 // | |
| 238 // Common semantics: | |
| 239 // * Locks are held by processes, but not inherited by child processes. | |
| 240 // * Locks are released by the OS on file close or process termination. | |
| 241 // * Locks are reliable only on local filesystems. | |
| 242 // * Duplicated file handles may also write to locked files. | |
| 243 // Windows-specific semantics: | |
| 244 // * Locks are mandatory for read/write APIs, advisory for mapping APIs. | |
| 245 // * Within a process, locking the same file (by the same or new handle) | |
| 246 // will fail. | |
| 247 // POSIX-specific semantics: | |
| 248 // * Locks are advisory only. | |
| 249 // * Within a process, locking the same file (by the same or new handle) | |
| 250 // will succeed. | |
| 251 // * Closing any descriptor on a given file releases the lock. | |
| 252 BaseFileError Lock(); | |
| 253 | |
| 254 // Unlock a file previously locked. | |
| 255 BaseFileError Unlock(); | |
| 277 | 256 |
| 278 private: | 257 private: |
| 279 PlatformFile* value_; | 258 void SetPlatformFile(PlatformFile file); |
| 259 | |
| 260 #if defined(OS_WIN) | |
| 261 win::ScopedHandle file_; | |
| 262 #elif defined(OS_POSIX) | |
| 263 PlatformFile file_; | |
| 264 #endif | |
| 265 | |
| 266 BaseFileError error_; | |
| 267 bool created_; | |
| 268 bool async_; | |
| 280 }; | 269 }; |
| 281 | 270 |
| 282 } // namespace base | 271 } // namespace base |
| 283 | 272 |
| 284 #endif // BASE_PLATFORM_FILE_H_ | 273 #endif // BASE_FILES_BASE_FILE_H_ |
| OLD | NEW |