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