[email protected] | 2404a2827 | 2012-05-30 02:31:14 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_PLATFORM_FILE_H_ |
| 6 | #define BASE_PLATFORM_FILE_H_ |
| 7 | |
| 8 | #include "build/build_config.h" |
| 9 | #if defined(OS_WIN) |
| 10 | #include <windows.h> |
| 11 | #endif |
| 12 | |
| 13 | #include <string> |
| 14 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 15 | #include "base/base_export.h" |
[email protected] | 90509cb | 2011-03-25 18:46:38 | [diff] [blame] | 16 | #include "base/basictypes.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 17 | #include "base/files/file_path.h" |
[email protected] | 8f9a3a5 | 2013-06-28 15:14:18 | [diff] [blame] | 18 | #include "base/time/time.h" |
[email protected] | 90509cb | 2011-03-25 18:46:38 | [diff] [blame] | 19 | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 20 | namespace base { |
| 21 | |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 22 | // *************************************************************************** |
| 23 | // ***** Don't use anything from this file anymore. It is being removed! |
[email protected] | 2412aa8 | 2014-03-13 20:15:39 | [diff] [blame] | 24 | // ***** Use base/files/file.h instead |
[email protected] | f62ac7d | 2013-12-04 00:46:32 | [diff] [blame] | 25 | // *************************************************************************** |
| 26 | |
[email protected] | b2f2308d | 2011-05-23 22:00:04 | [diff] [blame] | 27 | // PLATFORM_FILE_(OPEN|CREATE).* are mutually exclusive. You should specify |
| 28 | // exactly one of the five (possibly combining with other flags) when opening |
| 29 | // or creating a file. |
[email protected] | 42a3b3e1 | 2013-06-10 15:28:09 | [diff] [blame] | 30 | // PLATFORM_FILE_(WRITE|APPEND) are mutually exclusive. This is so that APPEND |
| 31 | // behavior will be consistent with O_APPEND on POSIX. |
[email protected] | 3b95e1d0 | 2013-10-10 23:02:39 | [diff] [blame] | 32 | // PLATFORM_FILE_EXCLUSIVE_(READ|WRITE) only grant exclusive access to the file |
| 33 | // on creation on POSIX; for existing files, consider using LockPlatformFile(). |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 34 | enum PlatformFileFlags { |
[email protected] | 2404a2827 | 2012-05-30 02:31:14 | [diff] [blame] | 35 | PLATFORM_FILE_OPEN = 1 << 0, // Opens a file, only if it exists. |
| 36 | PLATFORM_FILE_CREATE = 1 << 1, // Creates a new file, only if it |
| 37 | // does not already exist. |
| 38 | PLATFORM_FILE_OPEN_ALWAYS = 1 << 2, // May create a new file. |
| 39 | PLATFORM_FILE_CREATE_ALWAYS = 1 << 3, // May overwrite an old file. |
| 40 | PLATFORM_FILE_OPEN_TRUNCATED = 1 << 4, // Opens a file and truncates it, |
| 41 | // only if it exists. |
| 42 | PLATFORM_FILE_READ = 1 << 5, |
| 43 | PLATFORM_FILE_WRITE = 1 << 6, |
[email protected] | 42a3b3e1 | 2013-06-10 15:28:09 | [diff] [blame] | 44 | PLATFORM_FILE_APPEND = 1 << 7, |
| 45 | PLATFORM_FILE_EXCLUSIVE_READ = 1 << 8, // EXCLUSIVE is opposite of Windows |
[email protected] | 2404a2827 | 2012-05-30 02:31:14 | [diff] [blame] | 46 | // SHARE |
[email protected] | 42a3b3e1 | 2013-06-10 15:28:09 | [diff] [blame] | 47 | PLATFORM_FILE_EXCLUSIVE_WRITE = 1 << 9, |
| 48 | PLATFORM_FILE_ASYNC = 1 << 10, |
| 49 | PLATFORM_FILE_TEMPORARY = 1 << 11, // Used on Windows only |
| 50 | PLATFORM_FILE_HIDDEN = 1 << 12, // Used on Windows only |
| 51 | PLATFORM_FILE_DELETE_ON_CLOSE = 1 << 13, |
[email protected] | b2f2308d | 2011-05-23 22:00:04 | [diff] [blame] | 52 | |
[email protected] | 42a3b3e1 | 2013-06-10 15:28:09 | [diff] [blame] | 53 | PLATFORM_FILE_WRITE_ATTRIBUTES = 1 << 14, // Used on Windows only |
[email protected] | ad0f81dd | 2011-06-22 19:29:23 | [diff] [blame] | 54 | |
[email protected] | 45d5c60 | 2013-10-07 18:33:22 | [diff] [blame] | 55 | PLATFORM_FILE_SHARE_DELETE = 1 << 15, // Used on Windows only |
[email protected] | 2404a2827 | 2012-05-30 02:31:14 | [diff] [blame] | 56 | |
[email protected] | 45d5c60 | 2013-10-07 18:33:22 | [diff] [blame] | 57 | PLATFORM_FILE_TERMINAL_DEVICE = 1 << 16, // Serial port flags |
| 58 | PLATFORM_FILE_BACKUP_SEMANTICS = 1 << 17, // Used on Windows only |
[email protected] | 33b9ffd0 | 2013-04-04 14:12:27 | [diff] [blame] | 59 | |
[email protected] | 45d5c60 | 2013-10-07 18:33:22 | [diff] [blame] | 60 | PLATFORM_FILE_EXECUTE = 1 << 18, // Used on Windows only |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 61 | }; |
| 62 | |
[email protected] | e35e173 | 2013-10-19 22:13:42 | [diff] [blame] | 63 | // This enum has been recorded in multiple histograms. If the order of the |
| 64 | // fields needs to change, please ensure that those histograms are obsolete or |
| 65 | // have been moved to a different enum. |
| 66 | // |
[email protected] | 5746640 | 2010-09-22 03:29:14 | [diff] [blame] | 67 | // PLATFORM_FILE_ERROR_ACCESS_DENIED is returned when a call fails because of |
| 68 | // a filesystem restriction. PLATFORM_FILE_ERROR_SECURITY is returned when a |
| 69 | // browser policy doesn't allow the operation to be executed. |
[email protected] | ed65fec | 2010-08-31 19:30:27 | [diff] [blame] | 70 | enum PlatformFileError { |
[email protected] | d480a3e | 2010-08-24 20:26:23 | [diff] [blame] | 71 | PLATFORM_FILE_OK = 0, |
[email protected] | ed65fec | 2010-08-31 19:30:27 | [diff] [blame] | 72 | PLATFORM_FILE_ERROR_FAILED = -1, |
| 73 | PLATFORM_FILE_ERROR_IN_USE = -2, |
| 74 | PLATFORM_FILE_ERROR_EXISTS = -3, |
| 75 | PLATFORM_FILE_ERROR_NOT_FOUND = -4, |
| 76 | PLATFORM_FILE_ERROR_ACCESS_DENIED = -5, |
| 77 | PLATFORM_FILE_ERROR_TOO_MANY_OPENED = -6, |
| 78 | PLATFORM_FILE_ERROR_NO_MEMORY = -7, |
[email protected] | 61da1db | 2010-09-02 03:43:36 | [diff] [blame] | 79 | PLATFORM_FILE_ERROR_NO_SPACE = -8, |
| 80 | PLATFORM_FILE_ERROR_NOT_A_DIRECTORY = -9, |
[email protected] | 5746640 | 2010-09-22 03:29:14 | [diff] [blame] | 81 | PLATFORM_FILE_ERROR_INVALID_OPERATION = -10, |
| 82 | PLATFORM_FILE_ERROR_SECURITY = -11, |
[email protected] | 81b18ca | 2010-10-07 08:35:09 | [diff] [blame] | 83 | PLATFORM_FILE_ERROR_ABORT = -12, |
| 84 | PLATFORM_FILE_ERROR_NOT_A_FILE = -13, |
| 85 | PLATFORM_FILE_ERROR_NOT_EMPTY = -14, |
[email protected] | 24dceaf | 2011-04-20 09:05:52 | [diff] [blame] | 86 | PLATFORM_FILE_ERROR_INVALID_URL = -15, |
[email protected] | 065499f7 | 2013-05-02 18:38:22 | [diff] [blame] | 87 | PLATFORM_FILE_ERROR_IO = -16, |
[email protected] | 10d69cdb | 2012-12-15 20:17:52 | [diff] [blame] | 88 | // Put new entries here and increment PLATFORM_FILE_ERROR_MAX. |
[email protected] | 065499f7 | 2013-05-02 18:38:22 | [diff] [blame] | 89 | PLATFORM_FILE_ERROR_MAX = -17 |
[email protected] | d480a3e | 2010-08-24 20:26:23 | [diff] [blame] | 90 | }; |
| 91 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 92 | // This explicit mapping matches both FILE_ on Windows and SEEK_ on Linux. |
| 93 | enum PlatformFileWhence { |
| 94 | PLATFORM_FILE_FROM_BEGIN = 0, |
| 95 | PLATFORM_FILE_FROM_CURRENT = 1, |
| 96 | PLATFORM_FILE_FROM_END = 2 |
| 97 | }; |
| 98 | |
[email protected] | 2f0193c2 | 2010-09-03 02:28:37 | [diff] [blame] | 99 | // Used to hold information about a given file. |
| 100 | // If you add more fields to this structure (platform-specific fields are OK), |
| 101 | // make sure to update all functions that use it in file_util_{win|posix}.cc |
| 102 | // too, and the ParamTraits<base::PlatformFileInfo> implementation in |
| 103 | // chrome/common/common_param_traits.cc. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 104 | struct BASE_EXPORT PlatformFileInfo { |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 105 | PlatformFileInfo(); |
| 106 | ~PlatformFileInfo(); |
| 107 | |
[email protected] | 2f0193c2 | 2010-09-03 02:28:37 | [diff] [blame] | 108 | // The size of the file in bytes. Undefined when is_directory is true. |
| 109 | int64 size; |
| 110 | |
| 111 | // True if the file corresponds to a directory. |
| 112 | bool is_directory; |
| 113 | |
[email protected] | 2e733d10 | 2010-11-30 00:43:37 | [diff] [blame] | 114 | // True if the file corresponds to a symbolic link. |
| 115 | bool is_symbolic_link; |
| 116 | |
[email protected] | 2f0193c2 | 2010-09-03 02:28:37 | [diff] [blame] | 117 | // The last modified time of a file. |
| 118 | base::Time last_modified; |
| 119 | |
| 120 | // The last accessed time of a file. |
| 121 | base::Time last_accessed; |
| 122 | |
| 123 | // The creation time of a file. |
| 124 | base::Time creation_time; |
| 125 | }; |
| 126 | |
[email protected] | 6f539941 | 2013-05-08 22:02:36 | [diff] [blame] | 127 | #if defined(OS_WIN) |
| 128 | typedef HANDLE PlatformFile; |
| 129 | const PlatformFile kInvalidPlatformFileValue = INVALID_HANDLE_VALUE; |
[email protected] | 6c6862b | 2013-10-24 04:42:43 | [diff] [blame] | 130 | BASE_EXPORT PlatformFileError LastErrorToPlatformFileError(DWORD last_error); |
[email protected] | 6f539941 | 2013-05-08 22:02:36 | [diff] [blame] | 131 | #elif defined(OS_POSIX) |
| 132 | typedef int PlatformFile; |
| 133 | const PlatformFile kInvalidPlatformFileValue = -1; |
[email protected] | e35e173 | 2013-10-19 22:13:42 | [diff] [blame] | 134 | BASE_EXPORT PlatformFileError ErrnoToPlatformFileError(int saved_errno); |
[email protected] | 6f539941 | 2013-05-08 22:02:36 | [diff] [blame] | 135 | #endif |
| 136 | |
[email protected] | ca8e74e | 2013-02-25 23:33:34 | [diff] [blame] | 137 | BASE_EXPORT FILE* FdopenPlatformFile(PlatformFile file, const char* mode); |
| 138 | |
[email protected] | 5018448 | 2010-09-27 18:51:20 | [diff] [blame] | 139 | // Closes a file handle. Returns |true| on success and |false| otherwise. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 140 | BASE_EXPORT bool ClosePlatformFile(PlatformFile file); |
[email protected] | ee8d4c8 | 2009-08-28 21:58:28 | [diff] [blame] | 141 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 142 | // Changes current position in the file to an |offset| relative to an origin |
| 143 | // defined by |whence|. Returns the resultant current position in the file |
| 144 | // (relative to the start) or -1 in case of error. |
| 145 | BASE_EXPORT int64 SeekPlatformFile(PlatformFile file, |
| 146 | PlatformFileWhence whence, |
| 147 | int64 offset); |
| 148 | |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 149 | // Reads the given number of bytes (or until EOF is reached) starting with the |
[email protected] | a37b56a | 2011-09-01 23:04:07 | [diff] [blame] | 150 | // given offset. Returns the number of bytes read, or -1 on error. Note that |
| 151 | // this function makes a best effort to read all data on all platforms, so it is |
| 152 | // not intended for stream oriented files but instead for cases when the normal |
| 153 | // expectation is that actually |size| bytes are read unless there is an error. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 154 | BASE_EXPORT int ReadPlatformFile(PlatformFile file, int64 offset, |
| 155 | char* data, int size); |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 156 | |
[email protected] | 2404a2827 | 2012-05-30 02:31:14 | [diff] [blame] | 157 | // Same as above but without seek. |
| 158 | BASE_EXPORT int ReadPlatformFileAtCurrentPos(PlatformFile file, |
| 159 | char* data, int size); |
| 160 | |
[email protected] | a37b56a | 2011-09-01 23:04:07 | [diff] [blame] | 161 | // Reads the given number of bytes (or until EOF is reached) starting with the |
| 162 | // given offset, but does not make any effort to read all data on all platforms. |
| 163 | // Returns the number of bytes read, or -1 on error. |
| 164 | BASE_EXPORT int ReadPlatformFileNoBestEffort(PlatformFile file, int64 offset, |
| 165 | char* data, int size); |
| 166 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 167 | // Same as above but without seek. |
| 168 | BASE_EXPORT int ReadPlatformFileCurPosNoBestEffort(PlatformFile file, |
| 169 | char* data, int size); |
| 170 | |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 171 | // Writes the given buffer into the file at the given offset, overwritting any |
| 172 | // data that was previously there. Returns the number of bytes written, or -1 |
[email protected] | 29b5121 | 2011-08-26 22:17:19 | [diff] [blame] | 173 | // on error. Note that this function makes a best effort to write all data on |
| 174 | // all platforms. |
[email protected] | 42a3b3e1 | 2013-06-10 15:28:09 | [diff] [blame] | 175 | // Ignores the offset and writes to the end of the file if the file was opened |
| 176 | // with PLATFORM_FILE_APPEND. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 177 | BASE_EXPORT int WritePlatformFile(PlatformFile file, int64 offset, |
| 178 | const char* data, int size); |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 179 | |
[email protected] | 2404a2827 | 2012-05-30 02:31:14 | [diff] [blame] | 180 | // Save as above but without seek. |
| 181 | BASE_EXPORT int WritePlatformFileAtCurrentPos(PlatformFile file, |
| 182 | const char* data, int size); |
| 183 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 184 | // Save as above but does not make any effort to write all data on all |
| 185 | // platforms. Returns the number of bytes written, or -1 on error. |
| 186 | BASE_EXPORT int WritePlatformFileCurPosNoBestEffort(PlatformFile file, |
| 187 | const char* data, int size); |
| 188 | |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 189 | // Truncates the given file to the given length. If |length| is greater than |
| 190 | // the current size of the file, the file is extended with zeros. If the file |
| 191 | // doesn't exist, |false| is returned. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 192 | BASE_EXPORT bool TruncatePlatformFile(PlatformFile file, int64 length); |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 193 | |
| 194 | // Flushes the buffers of the given file. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 195 | BASE_EXPORT bool FlushPlatformFile(PlatformFile file); |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 196 | |
| 197 | // Touches the given file. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 198 | BASE_EXPORT bool TouchPlatformFile(PlatformFile file, |
| 199 | const Time& last_access_time, |
| 200 | const Time& last_modified_time); |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 201 | |
| 202 | // Returns some information for the given file. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 203 | BASE_EXPORT bool GetPlatformFileInfo(PlatformFile file, PlatformFileInfo* info); |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 204 | |
[email protected] | 3b95e1d0 | 2013-10-10 23:02:39 | [diff] [blame] | 205 | // Attempts to take an exclusive write lock on the file. Returns immediately |
| 206 | // (i.e. does not wait for another process to unlock the file). If the lock |
| 207 | // was obtained, the result will be PLATFORM_FILE_OK. A lock only guarantees |
| 208 | // that other processes may not also take a lock on the same file with the |
| 209 | // same API - it may still be opened, renamed, unlinked, etc. |
| 210 | // |
| 211 | // Common semantics: |
| 212 | // * Locks are held by processes, but not inherited by child processes. |
| 213 | // * Locks are released by the OS on file handle close or process termination. |
| 214 | // * Locks are reliable only on local filesystems. |
| 215 | // * Duplicated file handles may also write to locked files. |
| 216 | // Windows-specific semantics: |
| 217 | // * Locks are mandatory for read/write APIs, advisory for mapping APIs. |
| 218 | // * Within a process, locking the same file (by the same or new handle) |
| 219 | // will fail. |
| 220 | // POSIX-specific semantics: |
| 221 | // * Locks are advisory only. |
| 222 | // * Within a process, locking the same file (by the same or new handle) |
| 223 | // will succeed. |
| 224 | // * Closing any descriptor on a given file releases the lock. |
| 225 | BASE_EXPORT PlatformFileError LockPlatformFile(PlatformFile file); |
| 226 | |
| 227 | // Unlock a file previously locked with LockPlatformFile. |
| 228 | BASE_EXPORT PlatformFileError UnlockPlatformFile(PlatformFile file); |
| 229 | |
[email protected] | 21ae1f9 | 2010-07-30 23:05:15 | [diff] [blame] | 230 | // Use this class to pass ownership of a PlatformFile to a receiver that may or |
| 231 | // may not want to accept it. This class does not own the storage for the |
| 232 | // PlatformFile. |
| 233 | // |
| 234 | // EXAMPLE: |
| 235 | // |
| 236 | // void MaybeProcessFile(PassPlatformFile pass_file) { |
| 237 | // if (...) { |
| 238 | // PlatformFile file = pass_file.ReleaseValue(); |
| 239 | // // Now, we are responsible for closing |file|. |
| 240 | // } |
| 241 | // } |
| 242 | // |
| 243 | // void OpenAndMaybeProcessFile(const FilePath& path) { |
| 244 | // PlatformFile file = CreatePlatformFile(path, ...); |
| 245 | // MaybeProcessFile(PassPlatformFile(&file)); |
| 246 | // if (file != kInvalidPlatformFileValue) |
| 247 | // ClosePlatformFile(file); |
| 248 | // } |
| 249 | // |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 250 | class BASE_EXPORT PassPlatformFile { |
[email protected] | 21ae1f9 | 2010-07-30 23:05:15 | [diff] [blame] | 251 | public: |
| 252 | explicit PassPlatformFile(PlatformFile* value) : value_(value) { |
| 253 | } |
| 254 | |
| 255 | // Called to retrieve the PlatformFile stored in this object. The caller |
| 256 | // gains ownership of the PlatformFile and is now responsible for closing it. |
| 257 | // Any subsequent calls to this method will return an invalid PlatformFile. |
| 258 | PlatformFile ReleaseValue() { |
| 259 | PlatformFile temp = *value_; |
| 260 | *value_ = kInvalidPlatformFileValue; |
| 261 | return temp; |
| 262 | } |
| 263 | |
| 264 | private: |
| 265 | PlatformFile* value_; |
| 266 | }; |
| 267 | |
[email protected] | 21da6eb | 2008-11-03 17:18:14 | [diff] [blame] | 268 | } // namespace base |
| 269 | |
| 270 | #endif // BASE_PLATFORM_FILE_H_ |