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