[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 5 | #ifndef BASE_FILES_FILE_H_ |
| 6 | #define BASE_FILES_FILE_H_ |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 7 | |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 8 | #include <stdint.h> |
[email protected] | f745d72 | 2014-04-05 02:39:18 | [diff] [blame] | 9 | |
[email protected] | b73427e | 2014-05-30 10:07:30 | [diff] [blame] | 10 | #include <string> |
| 11 | |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 12 | #include "base/base_export.h" |
dbeam | 492dc31b | 2015-05-11 07:53:47 | [diff] [blame] | 13 | #include "base/files/file_path.h" |
| 14 | #include "base/files/file_tracing.h" |
Brett Wilson | c7d73a99 | 2017-07-14 01:56:13 | [diff] [blame] | 15 | #include "base/files/platform_file.h" |
[email protected] | 49ec031 | 2014-03-18 02:39:03 | [diff] [blame] | 16 | #include "base/files/scoped_file.h" |
dcheng | 1a2fd6cd | 2016-06-07 21:39:12 | [diff] [blame] | 17 | #include "base/macros.h" |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 18 | #include "base/time/time.h" |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 19 | #include "build/build_config.h" |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 20 | |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 21 | #if defined(OS_POSIX) |
| 22 | #include <sys/stat.h> |
| 23 | #endif |
| 24 | |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 25 | namespace base { |
| 26 | |
Brian Sheedy | c5ef3eda | 2017-12-19 19:10:07 | [diff] [blame^] | 27 | #if defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL) |
[email protected] | f745d72 | 2014-04-05 02:39:18 | [diff] [blame] | 28 | typedef struct stat stat_wrapper_t; |
Brett Wilson | c7d73a99 | 2017-07-14 01:56:13 | [diff] [blame] | 29 | #elif defined(OS_POSIX) |
[email protected] | f745d72 | 2014-04-05 02:39:18 | [diff] [blame] | 30 | typedef struct stat64 stat_wrapper_t; |
| 31 | #endif |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 32 | |
| 33 | // Thin wrapper around an OS-level file. |
| 34 | // Note that this class does not provide any support for asynchronous IO, other |
| 35 | // than the ability to create asynchronous handles on Windows. |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 36 | // |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 37 | // Note about const: this class does not attempt to determine if the underlying |
| 38 | // file system object is affected by a particular method in order to consider |
| 39 | // that method const or not. Only methods that deal with member variables in an |
| 40 | // obvious non-modifying way are marked as const. Any method that forward calls |
| 41 | // to the OS is not considered const, even if there is no apparent change to |
| 42 | // member variables. |
| 43 | class BASE_EXPORT File { |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 44 | public: |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 45 | // FLAG_(OPEN|CREATE).* are mutually exclusive. You should specify exactly one |
| 46 | // of the five (possibly combining with other flags) when opening or creating |
| 47 | // a file. |
| 48 | // FLAG_(WRITE|APPEND) are mutually exclusive. This is so that APPEND behavior |
| 49 | // will be consistent with O_APPEND on POSIX. |
| 50 | // FLAG_EXCLUSIVE_(READ|WRITE) only grant exclusive access to the file on |
| 51 | // creation on POSIX; for existing files, consider using Lock(). |
| 52 | enum Flags { |
grt | 37dd923 | 2017-01-13 21:52:45 | [diff] [blame] | 53 | FLAG_OPEN = 1 << 0, // Opens a file, only if it exists. |
| 54 | FLAG_CREATE = 1 << 1, // Creates a new file, only if it does not |
| 55 | // already exist. |
| 56 | FLAG_OPEN_ALWAYS = 1 << 2, // May create a new file. |
| 57 | FLAG_CREATE_ALWAYS = 1 << 3, // May overwrite an old file. |
| 58 | FLAG_OPEN_TRUNCATED = 1 << 4, // Opens a file and truncates it, only if it |
| 59 | // exists. |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 60 | FLAG_READ = 1 << 5, |
| 61 | FLAG_WRITE = 1 << 6, |
| 62 | FLAG_APPEND = 1 << 7, |
grt | 37dd923 | 2017-01-13 21:52:45 | [diff] [blame] | 63 | FLAG_EXCLUSIVE_READ = 1 << 8, // EXCLUSIVE is opposite of Windows SHARE. |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 64 | FLAG_EXCLUSIVE_WRITE = 1 << 9, |
| 65 | FLAG_ASYNC = 1 << 10, |
grt | 37dd923 | 2017-01-13 21:52:45 | [diff] [blame] | 66 | FLAG_TEMPORARY = 1 << 11, // Used on Windows only. |
| 67 | FLAG_HIDDEN = 1 << 12, // Used on Windows only. |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 68 | FLAG_DELETE_ON_CLOSE = 1 << 13, |
grt | 37dd923 | 2017-01-13 21:52:45 | [diff] [blame] | 69 | FLAG_WRITE_ATTRIBUTES = 1 << 14, // Used on Windows only. |
| 70 | FLAG_SHARE_DELETE = 1 << 15, // Used on Windows only. |
| 71 | FLAG_TERMINAL_DEVICE = 1 << 16, // Serial port flags. |
| 72 | FLAG_BACKUP_SEMANTICS = 1 << 17, // Used on Windows only. |
| 73 | FLAG_EXECUTE = 1 << 18, // Used on Windows only. |
| 74 | FLAG_SEQUENTIAL_SCAN = 1 << 19, // Used on Windows only. |
| 75 | FLAG_CAN_DELETE_ON_CLOSE = 1 << 20, // Requests permission to delete a file |
| 76 | // via DeleteOnClose() (Windows only). |
| 77 | // See DeleteOnClose() for details. |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 78 | }; |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 79 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 80 | // This enum has been recorded in multiple histograms. If the order of the |
| 81 | // fields needs to change, please ensure that those histograms are obsolete or |
| 82 | // have been moved to a different enum. |
| 83 | // |
| 84 | // FILE_ERROR_ACCESS_DENIED is returned when a call fails because of a |
| 85 | // filesystem restriction. FILE_ERROR_SECURITY is returned when a browser |
| 86 | // policy doesn't allow the operation to be executed. |
| 87 | enum Error { |
| 88 | FILE_OK = 0, |
| 89 | FILE_ERROR_FAILED = -1, |
| 90 | FILE_ERROR_IN_USE = -2, |
| 91 | FILE_ERROR_EXISTS = -3, |
| 92 | FILE_ERROR_NOT_FOUND = -4, |
| 93 | FILE_ERROR_ACCESS_DENIED = -5, |
| 94 | FILE_ERROR_TOO_MANY_OPENED = -6, |
| 95 | FILE_ERROR_NO_MEMORY = -7, |
| 96 | FILE_ERROR_NO_SPACE = -8, |
| 97 | FILE_ERROR_NOT_A_DIRECTORY = -9, |
| 98 | FILE_ERROR_INVALID_OPERATION = -10, |
| 99 | FILE_ERROR_SECURITY = -11, |
| 100 | FILE_ERROR_ABORT = -12, |
| 101 | FILE_ERROR_NOT_A_FILE = -13, |
| 102 | FILE_ERROR_NOT_EMPTY = -14, |
| 103 | FILE_ERROR_INVALID_URL = -15, |
| 104 | FILE_ERROR_IO = -16, |
| 105 | // Put new entries here and increment FILE_ERROR_MAX. |
| 106 | FILE_ERROR_MAX = -17 |
| 107 | }; |
| 108 | |
| 109 | // This explicit mapping matches both FILE_ on Windows and SEEK_ on Linux. |
| 110 | enum Whence { |
| 111 | FROM_BEGIN = 0, |
| 112 | FROM_CURRENT = 1, |
| 113 | FROM_END = 2 |
| 114 | }; |
| 115 | |
| 116 | // Used to hold information about a given file. |
| 117 | // If you add more fields to this structure (platform-specific fields are OK), |
tnagel | 718a46e | 2015-03-17 22:39:43 | [diff] [blame] | 118 | // make sure to update all functions that use it in file_util_{win|posix}.cc, |
| 119 | // too, and the ParamTraits<base::File::Info> implementation in |
| 120 | // ipc/ipc_message_utils.cc. |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 121 | struct BASE_EXPORT Info { |
| 122 | Info(); |
| 123 | ~Info(); |
[email protected] | f745d72 | 2014-04-05 02:39:18 | [diff] [blame] | 124 | #if defined(OS_POSIX) |
| 125 | // Fills this struct with values from |stat_info|. |
| 126 | void FromStat(const stat_wrapper_t& stat_info); |
| 127 | #endif |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 128 | |
| 129 | // The size of the file in bytes. Undefined when is_directory is true. |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 130 | int64_t size; |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 131 | |
| 132 | // True if the file corresponds to a directory. |
| 133 | bool is_directory; |
| 134 | |
tnagel | 718a46e | 2015-03-17 22:39:43 | [diff] [blame] | 135 | // True if the file corresponds to a symbolic link. For Windows currently |
| 136 | // not supported and thus always false. |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 137 | bool is_symbolic_link; |
| 138 | |
| 139 | // The last modified time of a file. |
dbeam | 38d002a2 | 2015-04-23 21:42:34 | [diff] [blame] | 140 | Time last_modified; |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 141 | |
| 142 | // The last accessed time of a file. |
dbeam | 38d002a2 | 2015-04-23 21:42:34 | [diff] [blame] | 143 | Time last_accessed; |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 144 | |
| 145 | // The creation time of a file. |
dbeam | 38d002a2 | 2015-04-23 21:42:34 | [diff] [blame] | 146 | Time creation_time; |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | File(); |
| 150 | |
| 151 | // Creates or opens the given file. This will fail with 'access denied' if the |
dbeam | 492dc31b | 2015-05-11 07:53:47 | [diff] [blame] | 152 | // |path| contains path traversal ('..') components. |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 153 | File(const FilePath& path, uint32_t flags); |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 154 | |
| 155 | // Takes ownership of |platform_file|. |
| 156 | explicit File(PlatformFile platform_file); |
| 157 | |
[email protected] | f4bc9f1 | 2014-03-26 09:59:31 | [diff] [blame] | 158 | // Creates an object with a specific error_details code. |
| 159 | explicit File(Error error_details); |
| 160 | |
dcheng | e1b0277c | 2015-12-01 12:09:52 | [diff] [blame] | 161 | File(File&& other); |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 162 | |
| 163 | ~File(); |
| 164 | |
reillyg | aa435e7 | 2015-06-16 00:48:48 | [diff] [blame] | 165 | // Takes ownership of |platform_file|. |
| 166 | static File CreateForAsyncHandle(PlatformFile platform_file); |
| 167 | |
dcheng | e1b0277c | 2015-12-01 12:09:52 | [diff] [blame] | 168 | File& operator=(File&& other); |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 169 | |
[email protected] | 58ba3ea | 2014-01-03 22:14:15 | [diff] [blame] | 170 | // Creates or opens the given file. |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 171 | void Initialize(const FilePath& path, uint32_t flags); |
[email protected] | 58ba3ea | 2014-01-03 22:14:15 | [diff] [blame] | 172 | |
lukasza | 7b718e8 | 2015-10-21 21:27:13 | [diff] [blame] | 173 | // Returns |true| if the handle / fd wrapped by this object is valid. This |
| 174 | // method doesn't interact with the file system (and is safe to be called from |
| 175 | // ThreadRestrictions::SetIOAllowed(false) threads). |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 176 | bool IsValid() const; |
| 177 | |
| 178 | // Returns true if a new file was created (or an old one truncated to zero |
| 179 | // length to simulate a new file, which can happen with |
| 180 | // FLAG_CREATE_ALWAYS), and false otherwise. |
| 181 | bool created() const { return created_; } |
| 182 | |
[email protected] | 48abb28 | 2014-02-21 17:25:53 | [diff] [blame] | 183 | // Returns the OS result of opening this file. Note that the way to verify |
| 184 | // the success of the operation is to use IsValid(), not this method: |
dbeam | 492dc31b | 2015-05-11 07:53:47 | [diff] [blame] | 185 | // File file(path, flags); |
[email protected] | 48abb28 | 2014-02-21 17:25:53 | [diff] [blame] | 186 | // if (!file.IsValid()) |
| 187 | // return; |
[email protected] | ee8808c | 2014-01-08 22:30:21 | [diff] [blame] | 188 | Error error_details() const { return error_details_; } |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 189 | |
[email protected] | 49ec031 | 2014-03-18 02:39:03 | [diff] [blame] | 190 | PlatformFile GetPlatformFile() const; |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 191 | PlatformFile TakePlatformFile(); |
| 192 | |
| 193 | // Destroying this object closes the file automatically. |
| 194 | void Close(); |
| 195 | |
| 196 | // Changes current position in the file to an |offset| relative to an origin |
| 197 | // defined by |whence|. Returns the resultant current position in the file |
| 198 | // (relative to the start) or -1 in case of error. |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 199 | int64_t Seek(Whence whence, int64_t offset); |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 200 | |
| 201 | // Reads the given number of bytes (or until EOF is reached) starting with the |
| 202 | // given offset. Returns the number of bytes read, or -1 on error. Note that |
| 203 | // this function makes a best effort to read all data on all platforms, so it |
| 204 | // is not intended for stream oriented files but instead for cases when the |
| 205 | // normal expectation is that actually |size| bytes are read unless there is |
| 206 | // an error. |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 207 | int Read(int64_t offset, char* data, int size); |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 208 | |
| 209 | // Same as above but without seek. |
| 210 | int ReadAtCurrentPos(char* data, int size); |
| 211 | |
| 212 | // Reads the given number of bytes (or until EOF is reached) starting with the |
| 213 | // given offset, but does not make any effort to read all data on all |
| 214 | // platforms. Returns the number of bytes read, or -1 on error. |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 215 | int ReadNoBestEffort(int64_t offset, char* data, int size); |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 216 | |
| 217 | // Same as above but without seek. |
| 218 | int ReadAtCurrentPosNoBestEffort(char* data, int size); |
| 219 | |
| 220 | // Writes the given buffer into the file at the given offset, overwritting any |
| 221 | // data that was previously there. Returns the number of bytes written, or -1 |
| 222 | // on error. Note that this function makes a best effort to write all data on |
Sergey Ulanov | 3c6541d | 2017-08-31 23:59:52 | [diff] [blame] | 223 | // all platforms. |data| can be nullptr when |size| is 0. |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 224 | // Ignores the offset and writes to the end of the file if the file was opened |
| 225 | // with FLAG_APPEND. |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 226 | int Write(int64_t offset, const char* data, int size); |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 227 | |
| 228 | // Save as above but without seek. |
| 229 | int WriteAtCurrentPos(const char* data, int size); |
| 230 | |
| 231 | // Save as above but does not make any effort to write all data on all |
| 232 | // platforms. Returns the number of bytes written, or -1 on error. |
| 233 | int WriteAtCurrentPosNoBestEffort(const char* data, int size); |
| 234 | |
[email protected] | 58ba3ea | 2014-01-03 22:14:15 | [diff] [blame] | 235 | // Returns the current size of this file, or a negative number on failure. |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 236 | int64_t GetLength(); |
[email protected] | 58ba3ea | 2014-01-03 22:14:15 | [diff] [blame] | 237 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 238 | // Truncates the file to the given length. If |length| is greater than the |
| 239 | // current size of the file, the file is extended with zeros. If the file |
| 240 | // doesn't exist, |false| is returned. |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 241 | bool SetLength(int64_t length); |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 242 | |
tnagel | 0e46019 | 2014-10-20 09:37:00 | [diff] [blame] | 243 | // Instructs the filesystem to flush the file to disk. (POSIX: fsync, Windows: |
| 244 | // FlushFileBuffers). |
tnagel | 301e81e | 2016-07-22 23:55:18 | [diff] [blame] | 245 | // Calling Flush() does not guarantee file integrity and thus is not a valid |
| 246 | // substitute for file integrity checks and recovery codepaths for malformed |
| 247 | // files. It can also be *really* slow, so avoid blocking on Flush(), |
| 248 | // especially please don't block shutdown on Flush(). |
| 249 | // Latency percentiles of Flush() across all platforms as of July 2016: |
| 250 | // 50 % > 5 ms |
| 251 | // 10 % > 58 ms |
| 252 | // 1 % > 357 ms |
| 253 | // 0.1 % > 1.8 seconds |
| 254 | // 0.01 % > 7.6 seconds |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 255 | bool Flush(); |
| 256 | |
| 257 | // Updates the file times. |
| 258 | bool SetTimes(Time last_access_time, Time last_modified_time); |
| 259 | |
| 260 | // Returns some basic information for the given file. |
| 261 | bool GetInfo(Info* info); |
| 262 | |
Kevin Marshall | 2804dbe2 | 2017-07-14 02:58:07 | [diff] [blame] | 263 | #if !defined(OS_FUCHSIA) // Fuchsia's POSIX API does not support file locking. |
| 264 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 265 | // Attempts to take an exclusive write lock on the file. Returns immediately |
| 266 | // (i.e. does not wait for another process to unlock the file). If the lock |
| 267 | // was obtained, the result will be FILE_OK. A lock only guarantees |
| 268 | // that other processes may not also take a lock on the same file with the |
| 269 | // same API - it may still be opened, renamed, unlinked, etc. |
| 270 | // |
| 271 | // Common semantics: |
| 272 | // * Locks are held by processes, but not inherited by child processes. |
| 273 | // * Locks are released by the OS on file close or process termination. |
| 274 | // * Locks are reliable only on local filesystems. |
| 275 | // * Duplicated file handles may also write to locked files. |
| 276 | // Windows-specific semantics: |
| 277 | // * Locks are mandatory for read/write APIs, advisory for mapping APIs. |
| 278 | // * Within a process, locking the same file (by the same or new handle) |
| 279 | // will fail. |
| 280 | // POSIX-specific semantics: |
| 281 | // * Locks are advisory only. |
| 282 | // * Within a process, locking the same file (by the same or new handle) |
| 283 | // will succeed. |
| 284 | // * Closing any descriptor on a given file releases the lock. |
| 285 | Error Lock(); |
| 286 | |
| 287 | // Unlock a file previously locked. |
| 288 | Error Unlock(); |
| 289 | |
Kevin Marshall | 2804dbe2 | 2017-07-14 02:58:07 | [diff] [blame] | 290 | #endif // !defined(OS_FUCHSIA) |
| 291 | |
grt | bad5603 | 2015-03-19 17:54:40 | [diff] [blame] | 292 | // Returns a new object referencing this file for use within the current |
| 293 | // process. Handling of FLAG_DELETE_ON_CLOSE varies by OS. On POSIX, the File |
| 294 | // object that was created or initialized with this flag will have unlinked |
| 295 | // the underlying file when it was created or opened. On Windows, the |
| 296 | // underlying file is deleted when the last handle to it is closed. |
kulshin | 00e2bd6 | 2016-07-20 20:57:35 | [diff] [blame] | 297 | File Duplicate() const; |
grt | bad5603 | 2015-03-19 17:54:40 | [diff] [blame] | 298 | |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 299 | bool async() const { return async_; } |
| 300 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 301 | #if defined(OS_WIN) |
grt | 37dd923 | 2017-01-13 21:52:45 | [diff] [blame] | 302 | // Sets or clears the DeleteFile disposition on the handle. Returns true if |
| 303 | // the disposition was set or cleared, as indicated by |delete_on_close|. |
| 304 | // |
| 305 | // Microsoft Windows deletes a file only when the last handle to the |
| 306 | // underlying kernel object is closed when the DeleteFile disposition has been |
| 307 | // set by any handle holder. This disposition is be set by: |
| 308 | // - Calling the Win32 DeleteFile function with the path to a file. |
| 309 | // - Opening/creating a file with FLAG_DELETE_ON_CLOSE. |
| 310 | // - Opening/creating a file with FLAG_CAN_DELETE_ON_CLOSE and subsequently |
| 311 | // calling DeleteOnClose(true). |
| 312 | // |
| 313 | // In all cases, all pre-existing handles to the file must have been opened |
| 314 | // with FLAG_SHARE_DELETE. |
| 315 | // |
| 316 | // So: |
| 317 | // - Use FLAG_SHARE_DELETE when creating/opening a file to allow another |
| 318 | // entity on the system to cause it to be deleted when it is closed. (Note: |
| 319 | // another entity can delete the file the moment after it is closed, so not |
| 320 | // using this permission doesn't provide any protections.) |
| 321 | // - Use FLAG_DELETE_ON_CLOSE for any file that is to be deleted after use. |
| 322 | // The OS will ensure it is deleted even in the face of process termination. |
| 323 | // - Use FLAG_CAN_DELETE_ON_CLOSE in conjunction with DeleteOnClose() to alter |
| 324 | // the DeleteFile disposition on an open handle. This fine-grained control |
| 325 | // allows for marking a file for deletion during processing so that it is |
| 326 | // deleted in the event of untimely process termination, and then clearing |
| 327 | // this state once the file is suitable for persistence. |
| 328 | bool DeleteOnClose(bool delete_on_close); |
| 329 | #endif |
| 330 | |
| 331 | #if defined(OS_WIN) |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 332 | static Error OSErrorToFileError(DWORD last_error); |
| 333 | #elif defined(OS_POSIX) |
| 334 | static Error OSErrorToFileError(int saved_errno); |
| 335 | #endif |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 336 | |
Chris Mumford | 5ba7cac1 | 2017-12-04 21:23:00 | [diff] [blame] | 337 | // Gets the last global error (errno or GetLastError()) and converts it to the |
| 338 | // closest base::File::Error equivalent via OSErrorToFileError(). The returned |
| 339 | // value is only trustworthy immediately after another base::File method |
| 340 | // fails. base::File never resets the global error to zero. |
| 341 | static Error GetLastFileError(); |
| 342 | |
[email protected] | b73427e | 2014-05-30 10:07:30 | [diff] [blame] | 343 | // Converts an error value to a human-readable form. Used for logging. |
| 344 | static std::string ErrorToString(Error error); |
| 345 | |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 346 | private: |
dbeam | 492dc31b | 2015-05-11 07:53:47 | [diff] [blame] | 347 | friend class FileTracing::ScopedTrace; |
| 348 | |
dbeam | fd5b108 | 2015-07-02 03:33:08 | [diff] [blame] | 349 | // Creates or opens the given file. Only called if |path| has no |
dbeam | 492dc31b | 2015-05-11 07:53:47 | [diff] [blame] | 350 | // traversal ('..') components. |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 351 | void DoInitialize(const FilePath& path, uint32_t flags); |
dbeam | 492cd47 | 2015-04-24 01:27:53 | [diff] [blame] | 352 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 353 | void SetPlatformFile(PlatformFile file); |
| 354 | |
Brett Wilson | c7d73a99 | 2017-07-14 01:56:13 | [diff] [blame] | 355 | ScopedPlatformFile file_; |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 356 | |
dbeam | fd5b108 | 2015-07-02 03:33:08 | [diff] [blame] | 357 | // A path to use for tracing purposes. Set if file tracing is enabled during |
| 358 | // |Initialize()|. |
| 359 | FilePath tracing_path_; |
dbeam | 492dc31b | 2015-05-11 07:53:47 | [diff] [blame] | 360 | |
| 361 | // Object tied to the lifetime of |this| that enables/disables tracing. |
| 362 | FileTracing::ScopedEnabler trace_enabler_; |
| 363 | |
[email protected] | ee8808c | 2014-01-08 22:30:21 | [diff] [blame] | 364 | Error error_details_; |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 365 | bool created_; |
| 366 | bool async_; |
dcheng | 1a2fd6cd | 2016-06-07 21:39:12 | [diff] [blame] | 367 | |
| 368 | DISALLOW_COPY_AND_ASSIGN(File); |
[email protected] | 9ad009a | 2013-11-28 01:31:31 | [diff] [blame] | 369 | }; |
| 370 | |
| 371 | } // namespace base |
| 372 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 373 | #endif // BASE_FILES_FILE_H_ |
gavinp | ac9c196f | 2015-09-28 22:58:12 | [diff] [blame] | 374 | |