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 | |
| 10 | #include "base/callback.h" |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 11 | #include "base/file_path.h" |
| 12 | #include "base/file_util.h" |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 13 | #include "base/platform_file.h" |
| 14 | #include "base/ref_counted.h" |
| 15 | #include "base/tracked_objects.h" |
| 16 | |
| 17 | namespace base { |
| 18 | |
| 19 | class MessageLoopProxy; |
dumi@chromium.org | 97ae261 | 2010-09-03 11:28:37 +0900 | [diff] [blame] | 20 | class Time; |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 21 | |
| 22 | // This class provides asynchronous access to common file routines. |
| 23 | class FileUtilProxy { |
| 24 | public: |
kkanetkar@chromium.org | 4871068 | 2010-11-03 05:36:52 +0900 | [diff] [blame] | 25 | // Holds metadata for file or directory entry. Used by ReadDirectoryCallback. |
| 26 | struct Entry { |
| 27 | FilePath::StringType name; |
| 28 | bool is_directory; |
| 29 | }; |
| 30 | |
dumi@chromium.org | 6dedd6d | 2010-08-25 05:26:23 +0900 | [diff] [blame] | 31 | // This callback is used by methods that report only an error code. It is |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 32 | // valid to pass NULL as the callback parameter to any function that takes a |
| 33 | // StatusCallback, in which case the operation will complete silently. |
kkanetkar@chromium.org | 4871068 | 2010-11-03 05:36:52 +0900 | [diff] [blame] | 34 | typedef Callback1<PlatformFileError /* error code */>::Type StatusCallback; |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 35 | |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 36 | typedef Callback3<PlatformFileError /* error code */, |
| 37 | PassPlatformFile, |
| 38 | bool /* created */>::Type CreateOrOpenCallback; |
| 39 | typedef Callback3<PlatformFileError /* error code */, |
| 40 | PassPlatformFile, |
| 41 | FilePath>::Type CreateTemporaryCallback; |
| 42 | typedef Callback2<PlatformFileError /* error code */, |
| 43 | bool /* created */>::Type EnsureFileExistsCallback; |
| 44 | typedef Callback2<PlatformFileError /* error code */, |
| 45 | const PlatformFileInfo& /* file_info */ |
| 46 | >::Type GetFileInfoCallback; |
| 47 | typedef Callback2<PlatformFileError /* error code */, |
| 48 | const std::vector<Entry>&>::Type ReadDirectoryCallback; |
darin@chromium.org | 44a9973 | 2011-02-04 09:39:34 +0900 | [diff] [blame^] | 49 | typedef Callback3<PlatformFileError /* error code */, |
| 50 | const char* /* data */, |
| 51 | int /* bytes read/written */>::Type ReadCallback; |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 52 | typedef Callback2<PlatformFileError /* error code */, |
darin@chromium.org | 44a9973 | 2011-02-04 09:39:34 +0900 | [diff] [blame^] | 53 | int /* bytes written */>::Type WriteCallback; |
erg@google.com | d5fffd4 | 2011-01-08 03:06:45 +0900 | [diff] [blame] | 54 | |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 55 | // Creates or opens a file with the given flags. It is invalid to pass NULL |
| 56 | // for the callback. |
kinuko@chromium.org | 850eb6d | 2010-10-15 09:37:34 +0900 | [diff] [blame] | 57 | // If PLATFORM_FILE_CREATE is set in |file_flags| it always tries to create |
| 58 | // a new file at the given |file_path| and calls back with |
| 59 | // PLATFORM_FILE_ERROR_FILE_EXISTS if the |file_path| already exists. |
dumi@chromium.org | 6dedd6d | 2010-08-25 05:26:23 +0900 | [diff] [blame] | 60 | static bool CreateOrOpen(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 61 | const FilePath& file_path, |
| 62 | int file_flags, |
| 63 | CreateOrOpenCallback* callback); |
| 64 | |
| 65 | // Creates a temporary file for writing. The path and an open file handle |
| 66 | // are returned. It is invalid to pass NULL for the callback. |
dumi@chromium.org | 6dedd6d | 2010-08-25 05:26:23 +0900 | [diff] [blame] | 67 | static bool CreateTemporary( |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 68 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 69 | CreateTemporaryCallback* callback); |
| 70 | |
| 71 | // Close the given file handle. |
dumi@chromium.org | 6dedd6d | 2010-08-25 05:26:23 +0900 | [diff] [blame] | 72 | static bool Close(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
kkanetkar@chromium.org | 4871068 | 2010-11-03 05:36:52 +0900 | [diff] [blame] | 73 | PlatformFile, |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 74 | StatusCallback* callback); |
| 75 | |
kinuko@chromium.org | 850eb6d | 2010-10-15 09:37:34 +0900 | [diff] [blame] | 76 | // Ensures that the given |file_path| exist. This creates a empty new file |
| 77 | // at |file_path| if the |file_path| does not exist. |
| 78 | // If a new file han not existed and is created at the |file_path|, |
| 79 | // |created| of the callback argument is set true and |error code| |
| 80 | // is set PLATFORM_FILE_OK. |
| 81 | // If the file already exists, |created| is set false and |error code| |
| 82 | // is set PLATFORM_FILE_OK. |
| 83 | // If the file hasn't existed but it couldn't be created for some other |
| 84 | // reasons, |created| is set false and |error code| indicates the error. |
kinuko@chromium.org | 850eb6d | 2010-10-15 09:37:34 +0900 | [diff] [blame] | 85 | static bool EnsureFileExists( |
| 86 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 87 | const FilePath& file_path, |
| 88 | EnsureFileExistsCallback* callback); |
| 89 | |
jianli@chromium.org | 9ed1f9d | 2010-08-31 11:42:36 +0900 | [diff] [blame] | 90 | // Retrieves the information about a file. It is invalid to pass NULL for the |
| 91 | // callback. |
jianli@chromium.org | 9ed1f9d | 2010-08-31 11:42:36 +0900 | [diff] [blame] | 92 | static bool GetFileInfo( |
| 93 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 94 | const FilePath& file_path, |
| 95 | GetFileInfoCallback* callback); |
| 96 | |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 97 | static bool GetFileInfoFromPlatformFile( |
| 98 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
kkanetkar@chromium.org | 4871068 | 2010-11-03 05:36:52 +0900 | [diff] [blame] | 99 | PlatformFile file, |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 100 | GetFileInfoCallback* callback); |
| 101 | |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 102 | static bool ReadDirectory(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 103 | const FilePath& file_path, |
| 104 | ReadDirectoryCallback* callback); |
| 105 | |
erg@google.com | 37c078e | 2011-01-11 09:50:59 +0900 | [diff] [blame] | 106 | // Creates directory at given path. It's an error to create |
| 107 | // if |exclusive| is true and dir already exists. |
| 108 | static bool CreateDirectory( |
| 109 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 110 | const FilePath& file_path, |
| 111 | bool exclusive, |
| 112 | bool recursive, |
| 113 | StatusCallback* callback); |
| 114 | |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 115 | // Copies a file or a directory from |src_file_path| to |dest_file_path| |
| 116 | // Error cases: |
| 117 | // If destination file doesn't exist or destination's parent |
| 118 | // doesn't exists. |
| 119 | // If source dir exists but destination path is an existing file. |
kinuko@chromium.org | 1a07804 | 2010-10-07 17:35:09 +0900 | [diff] [blame] | 120 | // If source file exists but destination path is an existing directory. |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 121 | // If source is a parent of destination. |
| 122 | // If source doesn't exists. |
| 123 | static bool Copy(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 124 | const FilePath& src_file_path, |
| 125 | const FilePath& dest_file_path, |
| 126 | StatusCallback* callback); |
| 127 | |
erg@google.com | 37c078e | 2011-01-11 09:50:59 +0900 | [diff] [blame] | 128 | // Moves a file or a directory from src_file_path to dest_file_path. |
| 129 | // Error cases are similar to Copy method's error cases. |
| 130 | static bool Move( |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 131 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
erg@google.com | 37c078e | 2011-01-11 09:50:59 +0900 | [diff] [blame] | 132 | const FilePath& src_file_path, |
| 133 | const FilePath& dest_file_path, |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 134 | StatusCallback* callback); |
| 135 | |
kinuko@chromium.org | 1a07804 | 2010-10-07 17:35:09 +0900 | [diff] [blame] | 136 | // Deletes a file or a directory. |
| 137 | // 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] | 138 | static bool Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 139 | const FilePath& file_path, |
kinuko@chromium.org | 1a07804 | 2010-10-07 17:35:09 +0900 | [diff] [blame] | 140 | bool recursive, |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 141 | StatusCallback* callback); |
| 142 | |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 143 | // Deletes a directory and all of its contents. |
| 144 | static bool RecursiveDelete( |
| 145 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 146 | const FilePath& file_path, |
| 147 | StatusCallback* callback); |
| 148 | |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 149 | // Reads from a file. On success, the file pointer is moved to position |
| 150 | // |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] | 151 | static bool Read( |
| 152 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
kkanetkar@chromium.org | 4871068 | 2010-11-03 05:36:52 +0900 | [diff] [blame] | 153 | PlatformFile file, |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 154 | int64 offset, |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 155 | int bytes_to_read, |
darin@chromium.org | 44a9973 | 2011-02-04 09:39:34 +0900 | [diff] [blame^] | 156 | ReadCallback* callback); |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 157 | |
| 158 | // Writes to a file. If |offset| is greater than the length of the file, |
| 159 | // |false| is returned. On success, the file pointer is moved to position |
ericu@google.com | 6a65222 | 2010-10-05 11:26:47 +0900 | [diff] [blame] | 160 | // |offset + bytes_to_write| in the file. The callback can be NULL. |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 161 | static bool Write( |
| 162 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
kkanetkar@chromium.org | 4871068 | 2010-11-03 05:36:52 +0900 | [diff] [blame] | 163 | PlatformFile file, |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 164 | int64 offset, |
| 165 | const char* buffer, |
| 166 | int bytes_to_write, |
darin@chromium.org | 44a9973 | 2011-02-04 09:39:34 +0900 | [diff] [blame^] | 167 | WriteCallback* callback); |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 168 | |
| 169 | // Touches a file. The callback can be NULL. |
| 170 | static bool Touch( |
| 171 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
kkanetkar@chromium.org | 4871068 | 2010-11-03 05:36:52 +0900 | [diff] [blame] | 172 | PlatformFile file, |
| 173 | const Time& last_access_time, |
| 174 | const Time& last_modified_time, |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 175 | StatusCallback* callback); |
| 176 | |
dumi@chromium.org | 12c2e2b | 2010-09-24 10:09:32 +0900 | [diff] [blame] | 177 | // Touches a file. The callback can be NULL. |
| 178 | static bool Touch( |
| 179 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 180 | const FilePath& file_path, |
kkanetkar@chromium.org | 4871068 | 2010-11-03 05:36:52 +0900 | [diff] [blame] | 181 | const Time& last_access_time, |
| 182 | const Time& last_modified_time, |
dumi@chromium.org | 12c2e2b | 2010-09-24 10:09:32 +0900 | [diff] [blame] | 183 | StatusCallback* callback); |
| 184 | |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 185 | // Truncates a file to the given length. If |length| is greater than the |
| 186 | // current length of the file, the file will be extended with zeroes. |
| 187 | // The callback can be NULL. |
| 188 | static bool Truncate( |
| 189 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
kkanetkar@chromium.org | 4871068 | 2010-11-03 05:36:52 +0900 | [diff] [blame] | 190 | PlatformFile file, |
ericu@google.com | 6a65222 | 2010-10-05 11:26:47 +0900 | [diff] [blame] | 191 | int64 length, |
| 192 | StatusCallback* callback); |
| 193 | |
| 194 | // Truncates a file to the given length. If |length| is greater than the |
| 195 | // current length of the file, the file will be extended with zeroes. |
| 196 | // The callback can be NULL. |
| 197 | static bool Truncate( |
| 198 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 199 | const FilePath& path, |
| 200 | int64 length, |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 201 | StatusCallback* callback); |
| 202 | |
| 203 | // Flushes a file. The callback can be NULL. |
| 204 | static bool Flush( |
| 205 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
kkanetkar@chromium.org | 4871068 | 2010-11-03 05:36:52 +0900 | [diff] [blame] | 206 | PlatformFile file, |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 207 | StatusCallback* callback); |
| 208 | |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 209 | private: |
| 210 | DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy); |
| 211 | }; |
| 212 | |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 213 | } // namespace base |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 214 | |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 215 | #endif // BASE_FILE_UTIL_PROXY_H_ |