| 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_FILES_FILE_H_ | 5 #ifndef BASE_FILES_FILE_H_ |
| 6 #define BASE_FILES_FILE_H_ | 6 #define BASE_FILES_FILE_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <string> | 10 #include <string> |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 class BASE_EXPORT File { | 56 class BASE_EXPORT File { |
| 57 public: | 57 public: |
| 58 // FLAG_(OPEN|CREATE).* are mutually exclusive. You should specify exactly one | 58 // FLAG_(OPEN|CREATE).* are mutually exclusive. You should specify exactly one |
| 59 // of the five (possibly combining with other flags) when opening or creating | 59 // of the five (possibly combining with other flags) when opening or creating |
| 60 // a file. | 60 // a file. |
| 61 // FLAG_(WRITE|APPEND) are mutually exclusive. This is so that APPEND behavior | 61 // FLAG_(WRITE|APPEND) are mutually exclusive. This is so that APPEND behavior |
| 62 // will be consistent with O_APPEND on POSIX. | 62 // will be consistent with O_APPEND on POSIX. |
| 63 // FLAG_EXCLUSIVE_(READ|WRITE) only grant exclusive access to the file on | 63 // FLAG_EXCLUSIVE_(READ|WRITE) only grant exclusive access to the file on |
| 64 // creation on POSIX; for existing files, consider using Lock(). | 64 // creation on POSIX; for existing files, consider using Lock(). |
| 65 enum Flags { | 65 enum Flags { |
| 66 FLAG_OPEN = 1 << 0, // Opens a file, only if it exists. | 66 FLAG_OPEN = 1 << 0, // Opens a file, only if it exists. |
| 67 FLAG_CREATE = 1 << 1, // Creates a new file, only if it does not | 67 FLAG_CREATE = 1 << 1, // Creates a new file, only if it does not |
| 68 // already exist. | 68 // already exist. |
| 69 FLAG_OPEN_ALWAYS = 1 << 2, // May create a new file. | 69 FLAG_OPEN_ALWAYS = 1 << 2, // May create a new file. |
| 70 FLAG_CREATE_ALWAYS = 1 << 3, // May overwrite an old file. | 70 FLAG_CREATE_ALWAYS = 1 << 3, // May overwrite an old file. |
| 71 FLAG_OPEN_TRUNCATED = 1 << 4, // Opens a file and truncates it, only if it | 71 FLAG_OPEN_TRUNCATED = 1 << 4, // Opens a file and truncates it, only if it |
| 72 // exists. | 72 // exists. |
| 73 FLAG_READ = 1 << 5, | 73 FLAG_READ = 1 << 5, |
| 74 FLAG_WRITE = 1 << 6, | 74 FLAG_WRITE = 1 << 6, |
| 75 FLAG_APPEND = 1 << 7, | 75 FLAG_APPEND = 1 << 7, |
| 76 FLAG_EXCLUSIVE_READ = 1 << 8, // EXCLUSIVE is opposite of Windows SHARE. | 76 FLAG_EXCLUSIVE_READ = 1 << 8, // EXCLUSIVE is opposite of Windows SHARE. |
| 77 FLAG_EXCLUSIVE_WRITE = 1 << 9, | 77 FLAG_EXCLUSIVE_WRITE = 1 << 9, |
| 78 FLAG_ASYNC = 1 << 10, | 78 FLAG_ASYNC = 1 << 10, |
| 79 FLAG_TEMPORARY = 1 << 11, // Used on Windows only. | 79 FLAG_TEMPORARY = 1 << 11, // Used on Windows only. |
| 80 FLAG_HIDDEN = 1 << 12, // Used on Windows only. | 80 FLAG_HIDDEN = 1 << 12, // Used on Windows only. |
| 81 FLAG_DELETE_ON_CLOSE = 1 << 13, | 81 FLAG_DELETE_ON_CLOSE = 1 << 13, |
| 82 FLAG_WRITE_ATTRIBUTES = 1 << 14, // Used on Windows only. | 82 FLAG_WRITE_ATTRIBUTES = 1 << 14, // Used on Windows only. |
| 83 FLAG_SHARE_DELETE = 1 << 15, // Used on Windows only. | 83 FLAG_SHARE_DELETE = 1 << 15, // Used on Windows only. |
| 84 FLAG_TERMINAL_DEVICE = 1 << 16, // Serial port flags. | 84 FLAG_TERMINAL_DEVICE = 1 << 16, // Serial port flags. |
| 85 FLAG_BACKUP_SEMANTICS = 1 << 17, // Used on Windows only. | 85 FLAG_BACKUP_SEMANTICS = 1 << 17, // Used on Windows only. |
| 86 FLAG_EXECUTE = 1 << 18, // Used on Windows only. | 86 FLAG_EXECUTE = 1 << 18, // Used on Windows only. |
| 87 FLAG_SEQUENTIAL_SCAN = 1 << 19, // Used on Windows only. | 87 FLAG_SEQUENTIAL_SCAN = 1 << 19, // Used on Windows only. |
| 88 FLAG_CAN_DELETE_ON_CLOSE = 1 << 20, // Requests permission to delete a file |
| 89 // via DeleteOnClose() (Windows only). |
| 90 // See DeleteOnClose() for details. |
| 88 }; | 91 }; |
| 89 | 92 |
| 90 // This enum has been recorded in multiple histograms. If the order of the | 93 // This enum has been recorded in multiple histograms. If the order of the |
| 91 // fields needs to change, please ensure that those histograms are obsolete or | 94 // fields needs to change, please ensure that those histograms are obsolete or |
| 92 // have been moved to a different enum. | 95 // have been moved to a different enum. |
| 93 // | 96 // |
| 94 // FILE_ERROR_ACCESS_DENIED is returned when a call fails because of a | 97 // FILE_ERROR_ACCESS_DENIED is returned when a call fails because of a |
| 95 // filesystem restriction. FILE_ERROR_SECURITY is returned when a browser | 98 // filesystem restriction. FILE_ERROR_SECURITY is returned when a browser |
| 96 // policy doesn't allow the operation to be executed. | 99 // policy doesn't allow the operation to be executed. |
| 97 enum Error { | 100 enum Error { |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 // Returns a new object referencing this file for use within the current | 301 // Returns a new object referencing this file for use within the current |
| 299 // process. Handling of FLAG_DELETE_ON_CLOSE varies by OS. On POSIX, the File | 302 // process. Handling of FLAG_DELETE_ON_CLOSE varies by OS. On POSIX, the File |
| 300 // object that was created or initialized with this flag will have unlinked | 303 // object that was created or initialized with this flag will have unlinked |
| 301 // the underlying file when it was created or opened. On Windows, the | 304 // the underlying file when it was created or opened. On Windows, the |
| 302 // underlying file is deleted when the last handle to it is closed. | 305 // underlying file is deleted when the last handle to it is closed. |
| 303 File Duplicate() const; | 306 File Duplicate() const; |
| 304 | 307 |
| 305 bool async() const { return async_; } | 308 bool async() const { return async_; } |
| 306 | 309 |
| 307 #if defined(OS_WIN) | 310 #if defined(OS_WIN) |
| 311 // Sets or clears the DeleteFile disposition on the handle. Returns true if |
| 312 // the disposition was set or cleared, as indicated by |delete_on_close|. |
| 313 // |
| 314 // Microsoft Windows deletes a file only when the last handle to the |
| 315 // underlying kernel object is closed when the DeleteFile disposition has been |
| 316 // set by any handle holder. This disposition is be set by: |
| 317 // - Calling the Win32 DeleteFile function with the path to a file. |
| 318 // - Opening/creating a file with FLAG_DELETE_ON_CLOSE. |
| 319 // - Opening/creating a file with FLAG_CAN_DELETE_ON_CLOSE and subsequently |
| 320 // calling DeleteOnClose(true). |
| 321 // |
| 322 // In all cases, all pre-existing handles to the file must have been opened |
| 323 // with FLAG_SHARE_DELETE. |
| 324 // |
| 325 // So: |
| 326 // - Use FLAG_SHARE_DELETE when creating/opening a file to allow another |
| 327 // entity on the system to cause it to be deleted when it is closed. (Note: |
| 328 // another entity can delete the file the moment after it is closed, so not |
| 329 // using this permission doesn't provide any protections.) |
| 330 // - Use FLAG_DELETE_ON_CLOSE for any file that is to be deleted after use. |
| 331 // The OS will ensure it is deleted even in the face of process termination. |
| 332 // - Use FLAG_CAN_DELETE_ON_CLOSE in conjunction with DeleteOnClose() to alter |
| 333 // the DeleteFile disposition on an open handle. This fine-grained control |
| 334 // allows for marking a file for deletion during processing so that it is |
| 335 // deleted in the event of untimely process termination, and then clearing |
| 336 // this state once the file is suitable for persistence. |
| 337 bool DeleteOnClose(bool delete_on_close); |
| 338 #endif |
| 339 |
| 340 #if defined(OS_WIN) |
| 308 static Error OSErrorToFileError(DWORD last_error); | 341 static Error OSErrorToFileError(DWORD last_error); |
| 309 #elif defined(OS_POSIX) | 342 #elif defined(OS_POSIX) |
| 310 static Error OSErrorToFileError(int saved_errno); | 343 static Error OSErrorToFileError(int saved_errno); |
| 311 #endif | 344 #endif |
| 312 | 345 |
| 313 // Converts an error value to a human-readable form. Used for logging. | 346 // Converts an error value to a human-readable form. Used for logging. |
| 314 static std::string ErrorToString(Error error); | 347 static std::string ErrorToString(Error error); |
| 315 | 348 |
| 316 private: | 349 private: |
| 317 friend class FileTracing::ScopedTrace; | 350 friend class FileTracing::ScopedTrace; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 339 bool created_; | 372 bool created_; |
| 340 bool async_; | 373 bool async_; |
| 341 | 374 |
| 342 DISALLOW_COPY_AND_ASSIGN(File); | 375 DISALLOW_COPY_AND_ASSIGN(File); |
| 343 }; | 376 }; |
| 344 | 377 |
| 345 } // namespace base | 378 } // namespace base |
| 346 | 379 |
| 347 #endif // BASE_FILES_FILE_H_ | 380 #endif // BASE_FILES_FILE_H_ |
| 348 | 381 |
| OLD | NEW |