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