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