erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [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 | |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 5 | #ifndef BASE_FILE_UTIL_PROXY_H_ |
| 6 | #define BASE_FILE_UTIL_PROXY_H_ |
| 7 | |
| 8 | #include <vector> |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 9 | |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 10 | #include "base/base_export.h" |
jhawkins@chromium.org | 3a6573d | 2011-10-18 03:40:30 +0900 | [diff] [blame] | 11 | #include "base/callback.h" |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 12 | #include "base/file_path.h" |
| 13 | #include "base/file_util.h" |
levin@chromium.org | 5c52868 | 2011-03-28 10:54:15 +0900 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 15 | #include "base/platform_file.h" |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 16 | #include "base/tracked_objects.h" |
| 17 | |
| 18 | namespace base { |
| 19 | |
| 20 | class MessageLoopProxy; |
dumi@chromium.org | 97ae261 | 2010-09-03 11:28:37 +0900 | [diff] [blame] | 21 | class Time; |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 22 | |
| 23 | // This class provides asynchronous access to common file routines. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 24 | class BASE_EXPORT FileUtilProxy { |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 25 | public: |
kinuko@chromium.org | 4f65c99 | 2011-10-19 16:21:57 +0900 | [diff] [blame] | 26 | // Holds metadata for file or directory entry. |
kkanetkar@chromium.org | 4871068 | 2010-11-03 05:36:52 +0900 | [diff] [blame] | 27 | struct Entry { |
| 28 | FilePath::StringType name; |
| 29 | bool is_directory; |
tzik@chromium.org | 09987c3 | 2011-07-19 14:03:03 +0900 | [diff] [blame] | 30 | int64 size; |
| 31 | base::Time last_modified_time; |
kkanetkar@chromium.org | 4871068 | 2010-11-03 05:36:52 +0900 | [diff] [blame] | 32 | }; |
| 33 | |
dumi@chromium.org | 6dedd6d | 2010-08-25 05:26:23 +0900 | [diff] [blame] | 34 | // This callback is used by methods that report only an error code. It is |
kinuko@chromium.org | d250f59 | 2011-11-04 22:27:49 +0900 | [diff] [blame^] | 35 | // valid to pass StatusCallback() to any function that takes a StatusCallback, |
jhawkins@chromium.org | 162de39 | 2011-10-18 06:33:35 +0900 | [diff] [blame] | 36 | // in which case the operation will complete silently. |
kinuko@chromium.org | 4f65c99 | 2011-10-19 16:21:57 +0900 | [diff] [blame] | 37 | typedef Callback<void(PlatformFileError)> StatusCallback; |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 38 | |
kinuko@chromium.org | 4f65c99 | 2011-10-19 16:21:57 +0900 | [diff] [blame] | 39 | typedef Callback<void(PlatformFileError, |
| 40 | PassPlatformFile, |
| 41 | bool /* created */)> CreateOrOpenCallback; |
| 42 | typedef Callback<void(PlatformFileError, |
| 43 | PassPlatformFile, |
| 44 | FilePath)> CreateTemporaryCallback; |
| 45 | typedef Callback<void(PlatformFileError, |
| 46 | const PlatformFileInfo& |
| 47 | )> GetFileInfoCallback; |
| 48 | typedef Callback<void(PlatformFileError, |
| 49 | const char* /* data */, |
| 50 | int /* bytes read */)> ReadCallback; |
| 51 | typedef Callback<void(PlatformFileError, |
| 52 | int /* bytes written */)> WriteCallback; |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 53 | |
jhawkins@chromium.org | d08a33e | 2011-10-18 05:44:47 +0900 | [diff] [blame] | 54 | // Creates or opens a file with the given flags. It is invalid to pass a null |
| 55 | // callback. If PLATFORM_FILE_CREATE is set in |file_flags| it always tries to |
| 56 | // create a new file at the given |file_path| and calls back with |
kinuko@chromium.org | 4f65c99 | 2011-10-19 16:21:57 +0900 | [diff] [blame] | 57 | // PLATFORM_FILE_ERROR_FILE_EXISTS if the |file_path| already exists. |
dumi@chromium.org | 6dedd6d | 2010-08-25 05:26:23 +0900 | [diff] [blame] | 58 | static bool CreateOrOpen(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 59 | const FilePath& file_path, |
| 60 | int file_flags, |
jhawkins@chromium.org | 3a6573d | 2011-10-18 03:40:30 +0900 | [diff] [blame] | 61 | const CreateOrOpenCallback& callback); |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 62 | |
jhawkins@chromium.org | d08a33e | 2011-10-18 05:44:47 +0900 | [diff] [blame] | 63 | // Creates a temporary file for writing. The path and an open file handle are |
| 64 | // returned. It is invalid to pass a null callback. The additional file flags |
| 65 | // will be added on top of the default file flags which are: |
noelutz@google.com | f56dab2 | 2011-06-14 05:29:50 +0900 | [diff] [blame] | 66 | // base::PLATFORM_FILE_CREATE_ALWAYS |
| 67 | // base::PLATFORM_FILE_WRITE |
| 68 | // base::PLATFORM_FILE_TEMPORARY. |
| 69 | // Set |additional_file_flags| to 0 for synchronous writes and set to |
| 70 | // base::PLATFORM_FILE_ASYNC to support asynchronous file operations. |
dumi@chromium.org | 6dedd6d | 2010-08-25 05:26:23 +0900 | [diff] [blame] | 71 | static bool CreateTemporary( |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 72 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
noelutz@google.com | f56dab2 | 2011-06-14 05:29:50 +0900 | [diff] [blame] | 73 | int additional_file_flags, |
jhawkins@chromium.org | d90c4f5 | 2011-10-18 04:29:29 +0900 | [diff] [blame] | 74 | const CreateTemporaryCallback& callback); |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 75 | |
| 76 | // Close the given file handle. |
dumi@chromium.org | 6dedd6d | 2010-08-25 05:26:23 +0900 | [diff] [blame] | 77 | static bool Close(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
kkanetkar@chromium.org | 4871068 | 2010-11-03 05:36:52 +0900 | [diff] [blame] | 78 | PlatformFile, |
jhawkins@chromium.org | 162de39 | 2011-10-18 06:33:35 +0900 | [diff] [blame] | 79 | const StatusCallback& callback); |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 80 | |
jhawkins@chromium.org | d08a33e | 2011-10-18 05:44:47 +0900 | [diff] [blame] | 81 | // Retrieves the information about a file. It is invalid to pass a null |
jianli@chromium.org | 9ed1f9d | 2010-08-31 11:42:36 +0900 | [diff] [blame] | 82 | // callback. |
jianli@chromium.org | 9ed1f9d | 2010-08-31 11:42:36 +0900 | [diff] [blame] | 83 | static bool GetFileInfo( |
| 84 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 85 | const FilePath& file_path, |
jhawkins@chromium.org | 56771f0 | 2011-10-18 05:12:05 +0900 | [diff] [blame] | 86 | const GetFileInfoCallback& callback); |
jianli@chromium.org | 9ed1f9d | 2010-08-31 11:42:36 +0900 | [diff] [blame] | 87 | |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 88 | static bool GetFileInfoFromPlatformFile( |
| 89 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
kkanetkar@chromium.org | 4871068 | 2010-11-03 05:36:52 +0900 | [diff] [blame] | 90 | PlatformFile file, |
jhawkins@chromium.org | 56771f0 | 2011-10-18 05:12:05 +0900 | [diff] [blame] | 91 | const GetFileInfoCallback& callback); |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 92 | |
kinuko@chromium.org | 1a07804 | 2010-10-07 17:35:09 +0900 | [diff] [blame] | 93 | // Deletes a file or a directory. |
| 94 | // It is an error to delete a non-empty directory with recursive=false. |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 95 | static bool Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 96 | const FilePath& file_path, |
kinuko@chromium.org | 1a07804 | 2010-10-07 17:35:09 +0900 | [diff] [blame] | 97 | bool recursive, |
jhawkins@chromium.org | 162de39 | 2011-10-18 06:33:35 +0900 | [diff] [blame] | 98 | const StatusCallback& callback); |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 99 | |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 100 | // Deletes a directory and all of its contents. |
| 101 | static bool RecursiveDelete( |
| 102 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 103 | const FilePath& file_path, |
jhawkins@chromium.org | 162de39 | 2011-10-18 06:33:35 +0900 | [diff] [blame] | 104 | const StatusCallback& callback); |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 105 | |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 106 | // Reads from a file. On success, the file pointer is moved to position |
jhawkins@chromium.org | d08a33e | 2011-10-18 05:44:47 +0900 | [diff] [blame] | 107 | // |offset + bytes_to_read| in the file. The callback can be null. |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 108 | static bool Read( |
| 109 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
kkanetkar@chromium.org | 4871068 | 2010-11-03 05:36:52 +0900 | [diff] [blame] | 110 | PlatformFile file, |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 111 | int64 offset, |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 112 | int bytes_to_read, |
jhawkins@chromium.org | d08a33e | 2011-10-18 05:44:47 +0900 | [diff] [blame] | 113 | const ReadCallback& callback); |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 114 | |
| 115 | // Writes to a file. If |offset| is greater than the length of the file, |
| 116 | // |false| is returned. On success, the file pointer is moved to position |
jhawkins@chromium.org | 553ef50 | 2011-10-18 05:56:52 +0900 | [diff] [blame] | 117 | // |offset + bytes_to_write| in the file. The callback can be null. |
sanga@chromium.org | 21d251f | 2011-08-18 01:45:48 +0900 | [diff] [blame] | 118 | // |bytes_to_write| must be greater than zero. |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 119 | static bool Write( |
| 120 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
kkanetkar@chromium.org | 4871068 | 2010-11-03 05:36:52 +0900 | [diff] [blame] | 121 | PlatformFile file, |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 122 | int64 offset, |
| 123 | const char* buffer, |
| 124 | int bytes_to_write, |
jhawkins@chromium.org | 553ef50 | 2011-10-18 05:56:52 +0900 | [diff] [blame] | 125 | const WriteCallback& callback); |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 126 | |
jhawkins@chromium.org | 553ef50 | 2011-10-18 05:56:52 +0900 | [diff] [blame] | 127 | // Touches a file. The callback can be null. |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 128 | static bool Touch( |
| 129 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
kkanetkar@chromium.org | 4871068 | 2010-11-03 05:36:52 +0900 | [diff] [blame] | 130 | PlatformFile file, |
| 131 | const Time& last_access_time, |
| 132 | const Time& last_modified_time, |
jhawkins@chromium.org | 162de39 | 2011-10-18 06:33:35 +0900 | [diff] [blame] | 133 | const StatusCallback& callback); |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 134 | |
jhawkins@chromium.org | 553ef50 | 2011-10-18 05:56:52 +0900 | [diff] [blame] | 135 | // Touches a file. The callback can be null. |
dumi@chromium.org | 12c2e2b | 2010-09-24 10:09:32 +0900 | [diff] [blame] | 136 | static bool Touch( |
| 137 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 138 | const FilePath& file_path, |
kkanetkar@chromium.org | 4871068 | 2010-11-03 05:36:52 +0900 | [diff] [blame] | 139 | const Time& last_access_time, |
| 140 | const Time& last_modified_time, |
jhawkins@chromium.org | 162de39 | 2011-10-18 06:33:35 +0900 | [diff] [blame] | 141 | const StatusCallback& callback); |
dumi@chromium.org | 12c2e2b | 2010-09-24 10:09:32 +0900 | [diff] [blame] | 142 | |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 143 | // Truncates a file to the given length. If |length| is greater than the |
| 144 | // current length of the file, the file will be extended with zeroes. |
jhawkins@chromium.org | 553ef50 | 2011-10-18 05:56:52 +0900 | [diff] [blame] | 145 | // The callback can be null. |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 146 | static bool Truncate( |
| 147 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
kkanetkar@chromium.org | 4871068 | 2010-11-03 05:36:52 +0900 | [diff] [blame] | 148 | PlatformFile file, |
ericu@google.com | 6a65222 | 2010-10-05 11:26:47 +0900 | [diff] [blame] | 149 | int64 length, |
jhawkins@chromium.org | 162de39 | 2011-10-18 06:33:35 +0900 | [diff] [blame] | 150 | const StatusCallback& callback); |
ericu@google.com | 6a65222 | 2010-10-05 11:26:47 +0900 | [diff] [blame] | 151 | |
| 152 | // Truncates a file to the given length. If |length| is greater than the |
| 153 | // current length of the file, the file will be extended with zeroes. |
jhawkins@chromium.org | 553ef50 | 2011-10-18 05:56:52 +0900 | [diff] [blame] | 154 | // The callback can be null. |
ericu@google.com | 6a65222 | 2010-10-05 11:26:47 +0900 | [diff] [blame] | 155 | static bool Truncate( |
| 156 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 157 | const FilePath& path, |
| 158 | int64 length, |
jhawkins@chromium.org | 162de39 | 2011-10-18 06:33:35 +0900 | [diff] [blame] | 159 | const StatusCallback& callback); |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 160 | |
jhawkins@chromium.org | 553ef50 | 2011-10-18 05:56:52 +0900 | [diff] [blame] | 161 | // Flushes a file. The callback can be null. |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 162 | static bool Flush( |
| 163 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
kkanetkar@chromium.org | 4871068 | 2010-11-03 05:36:52 +0900 | [diff] [blame] | 164 | PlatformFile file, |
jhawkins@chromium.org | 162de39 | 2011-10-18 06:33:35 +0900 | [diff] [blame] | 165 | const StatusCallback& callback); |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 166 | |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 167 | private: |
| 168 | DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy); |
| 169 | }; |
| 170 | |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 171 | } // namespace base |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 172 | |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 173 | #endif // BASE_FILE_UTIL_PROXY_H_ |