finnur@chromium.org | 899d4e0 | 2011-03-15 18:56:27 +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 | |
| 5 | #include "base/file_util_proxy.h" |
| 6 | |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 7 | #include "base/message_loop_proxy.h" |
| 8 | |
jianli@chromium.org | 9ed1f9d | 2010-08-31 11:42:36 +0900 | [diff] [blame] | 9 | // TODO(jianli): Move the code from anonymous namespace to base namespace so |
| 10 | // that all of the base:: prefixes would be unnecessary. |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 11 | namespace { |
| 12 | |
kinuko@chromium.org | 1a07804 | 2010-10-07 17:35:09 +0900 | [diff] [blame] | 13 | namespace { |
| 14 | |
| 15 | // Performs common checks for move and copy. |
| 16 | // This also removes the destination directory if it's non-empty and all other |
kinuko@chromium.org | 850eb6d | 2010-10-15 09:37:34 +0900 | [diff] [blame] | 17 | // checks are passed (so that the copy/move correctly overwrites the |
| 18 | // destination). |
kinuko@chromium.org | 1a07804 | 2010-10-07 17:35:09 +0900 | [diff] [blame] | 19 | static base::PlatformFileError PerformCommonCheckAndPreparationForMoveAndCopy( |
| 20 | const FilePath& src_file_path, |
| 21 | const FilePath& dest_file_path) { |
| 22 | // Exits earlier if the source path does not exist. |
| 23 | if (!file_util::PathExists(src_file_path)) |
| 24 | return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 25 | |
| 26 | // The parent of the |dest_file_path| does not exist. |
| 27 | if (!file_util::DirectoryExists(dest_file_path.DirName())) |
| 28 | return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 29 | |
| 30 | // It is an error to try to copy/move an entry into its child. |
| 31 | if (src_file_path.IsParent(dest_file_path)) |
| 32 | return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; |
| 33 | |
| 34 | // Now it is ok to return if the |dest_file_path| does not exist. |
| 35 | if (!file_util::PathExists(dest_file_path)) |
| 36 | return base::PLATFORM_FILE_OK; |
| 37 | |
| 38 | // |src_file_path| exists and is a directory. |
| 39 | // |dest_file_path| exists and is a file. |
| 40 | bool src_is_directory = file_util::DirectoryExists(src_file_path); |
| 41 | bool dest_is_directory = file_util::DirectoryExists(dest_file_path); |
| 42 | if (src_is_directory && !dest_is_directory) |
| 43 | return base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY; |
| 44 | |
| 45 | // |src_file_path| exists and is a file. |
| 46 | // |dest_file_path| exists and is a directory. |
| 47 | if (!src_is_directory && dest_is_directory) |
| 48 | return base::PLATFORM_FILE_ERROR_NOT_A_FILE; |
| 49 | |
| 50 | // It is an error to copy/move an entry into the same path. |
| 51 | if (src_file_path.value() == dest_file_path.value()) |
| 52 | return base::PLATFORM_FILE_ERROR_EXISTS; |
| 53 | |
| 54 | if (dest_is_directory) { |
| 55 | // It is an error to copy/move an entry to a non-empty directory. |
| 56 | // Otherwise the copy/move attempt must overwrite the destination, but |
| 57 | // the file_util's Copy or Move method doesn't perform overwrite |
| 58 | // on all platforms, so we delete the destination directory here. |
| 59 | // TODO(kinuko): may be better to change the file_util::{Copy,Move}. |
| 60 | if (!file_util::Delete(dest_file_path, false /* recursive */)) { |
| 61 | if (!file_util::IsDirectoryEmpty(dest_file_path)) |
| 62 | return base::PLATFORM_FILE_ERROR_NOT_EMPTY; |
| 63 | return base::PLATFORM_FILE_ERROR_FAILED; |
| 64 | } |
| 65 | } |
| 66 | return base::PLATFORM_FILE_OK; |
| 67 | } |
| 68 | |
kkanetkar@chromium.org | 4871068 | 2010-11-03 05:36:52 +0900 | [diff] [blame] | 69 | } // anonymous namespace |
kinuko@chromium.org | 1a07804 | 2010-10-07 17:35:09 +0900 | [diff] [blame] | 70 | |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 71 | class MessageLoopRelay |
| 72 | : public base::RefCountedThreadSafe<MessageLoopRelay> { |
| 73 | public: |
| 74 | MessageLoopRelay() |
| 75 | : origin_message_loop_proxy_( |
nduca@chromium.org | ba04861 | 2011-08-16 05:33:46 +0900 | [diff] [blame] | 76 | base::MessageLoopProxy::current()), |
dumi@chromium.org | 6dedd6d | 2010-08-25 05:26:23 +0900 | [diff] [blame] | 77 | error_code_(base::PLATFORM_FILE_OK) { |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 78 | } |
| 79 | |
dumi@chromium.org | 6dedd6d | 2010-08-25 05:26:23 +0900 | [diff] [blame] | 80 | bool Start(scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 81 | const tracked_objects::Location& from_here) { |
dumi@chromium.org | 6dedd6d | 2010-08-25 05:26:23 +0900 | [diff] [blame] | 82 | return message_loop_proxy->PostTask( |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 83 | from_here, |
| 84 | NewRunnableMethod(this, &MessageLoopRelay::ProcessOnTargetThread)); |
| 85 | } |
| 86 | |
| 87 | protected: |
| 88 | friend class base::RefCountedThreadSafe<MessageLoopRelay>; |
| 89 | virtual ~MessageLoopRelay() {} |
| 90 | |
| 91 | // Called to perform work on the FILE thread. |
| 92 | virtual void RunWork() = 0; |
| 93 | |
| 94 | // Called to notify the callback on the origin thread. |
| 95 | virtual void RunCallback() = 0; |
| 96 | |
dumi@chromium.org | 50f197d | 2010-09-01 04:30:27 +0900 | [diff] [blame] | 97 | void set_error_code(base::PlatformFileError error_code) { |
dumi@chromium.org | 6dedd6d | 2010-08-25 05:26:23 +0900 | [diff] [blame] | 98 | error_code_ = error_code; |
| 99 | } |
| 100 | |
dumi@chromium.org | 50f197d | 2010-09-01 04:30:27 +0900 | [diff] [blame] | 101 | base::PlatformFileError error_code() const { |
dumi@chromium.org | 6dedd6d | 2010-08-25 05:26:23 +0900 | [diff] [blame] | 102 | return error_code_; |
| 103 | } |
| 104 | |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 105 | private: |
| 106 | void ProcessOnTargetThread() { |
| 107 | RunWork(); |
| 108 | origin_message_loop_proxy_->PostTask( |
| 109 | FROM_HERE, |
| 110 | NewRunnableMethod(this, &MessageLoopRelay::RunCallback)); |
| 111 | } |
| 112 | |
| 113 | scoped_refptr<base::MessageLoopProxy> origin_message_loop_proxy_; |
dumi@chromium.org | 50f197d | 2010-09-01 04:30:27 +0900 | [diff] [blame] | 114 | base::PlatformFileError error_code_; |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | class RelayCreateOrOpen : public MessageLoopRelay { |
| 118 | public: |
| 119 | RelayCreateOrOpen( |
| 120 | scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 121 | const FilePath& file_path, |
| 122 | int file_flags, |
jhawkins@chromium.org | 3a6573d | 2011-10-18 03:40:30 +0900 | [diff] [blame] | 123 | const base::FileUtilProxy::CreateOrOpenCallback& callback) |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 124 | : message_loop_proxy_(message_loop_proxy), |
| 125 | file_path_(file_path), |
| 126 | file_flags_(file_flags), |
| 127 | callback_(callback), |
| 128 | file_handle_(base::kInvalidPlatformFileValue), |
| 129 | created_(false) { |
jhawkins@chromium.org | 3a6573d | 2011-10-18 03:40:30 +0900 | [diff] [blame] | 130 | DCHECK_EQ(false, callback.is_null()); |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | protected: |
| 134 | virtual ~RelayCreateOrOpen() { |
| 135 | if (file_handle_ != base::kInvalidPlatformFileValue) |
| 136 | base::FileUtilProxy::Close(message_loop_proxy_, file_handle_, NULL); |
| 137 | } |
| 138 | |
| 139 | virtual void RunWork() { |
kinuko@chromium.org | af334b2 | 2010-10-12 11:17:14 +0900 | [diff] [blame] | 140 | if (!file_util::DirectoryExists(file_path_.DirName())) { |
| 141 | // If its parent does not exist, should return NOT_FOUND error. |
| 142 | set_error_code(base::PLATFORM_FILE_ERROR_NOT_FOUND); |
| 143 | return; |
| 144 | } |
dumi@chromium.org | 50f197d | 2010-09-01 04:30:27 +0900 | [diff] [blame] | 145 | base::PlatformFileError error_code = base::PLATFORM_FILE_OK; |
| 146 | file_handle_ = base::CreatePlatformFile(file_path_, file_flags_, |
| 147 | &created_, &error_code); |
| 148 | set_error_code(error_code); |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | virtual void RunCallback() { |
jhawkins@chromium.org | 3a6573d | 2011-10-18 03:40:30 +0900 | [diff] [blame] | 152 | callback_.Run(error_code(), base::PassPlatformFile(&file_handle_), |
| 153 | created_); |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | private: |
| 157 | scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; |
| 158 | FilePath file_path_; |
| 159 | int file_flags_; |
jhawkins@chromium.org | 3a6573d | 2011-10-18 03:40:30 +0900 | [diff] [blame] | 160 | base::FileUtilProxy::CreateOrOpenCallback callback_; |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 161 | base::PlatformFile file_handle_; |
| 162 | bool created_; |
| 163 | }; |
| 164 | |
| 165 | class RelayCreateTemporary : public MessageLoopRelay { |
| 166 | public: |
| 167 | RelayCreateTemporary( |
| 168 | scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
noelutz@google.com | f56dab2 | 2011-06-14 05:29:50 +0900 | [diff] [blame] | 169 | int additional_file_flags, |
jhawkins@chromium.org | d90c4f5 | 2011-10-18 04:29:29 +0900 | [diff] [blame] | 170 | const base::FileUtilProxy::CreateTemporaryCallback& callback) |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 171 | : message_loop_proxy_(message_loop_proxy), |
noelutz@google.com | f56dab2 | 2011-06-14 05:29:50 +0900 | [diff] [blame] | 172 | additional_file_flags_(additional_file_flags), |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 173 | callback_(callback), |
| 174 | file_handle_(base::kInvalidPlatformFileValue) { |
jhawkins@chromium.org | d90c4f5 | 2011-10-18 04:29:29 +0900 | [diff] [blame] | 175 | DCHECK_EQ(false, callback.is_null()); |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | protected: |
| 179 | virtual ~RelayCreateTemporary() { |
| 180 | if (file_handle_ != base::kInvalidPlatformFileValue) |
| 181 | base::FileUtilProxy::Close(message_loop_proxy_, file_handle_, NULL); |
| 182 | } |
| 183 | |
| 184 | virtual void RunWork() { |
| 185 | // TODO(darin): file_util should have a variant of CreateTemporaryFile |
| 186 | // that returns a FilePath and a PlatformFile. |
| 187 | file_util::CreateTemporaryFile(&file_path_); |
| 188 | |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 189 | int file_flags = |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 190 | base::PLATFORM_FILE_WRITE | |
noelutz@google.com | f56dab2 | 2011-06-14 05:29:50 +0900 | [diff] [blame] | 191 | base::PLATFORM_FILE_TEMPORARY | |
| 192 | base::PLATFORM_FILE_CREATE_ALWAYS | |
| 193 | additional_file_flags_; |
| 194 | |
dumi@chromium.org | 50f197d | 2010-09-01 04:30:27 +0900 | [diff] [blame] | 195 | base::PlatformFileError error_code = base::PLATFORM_FILE_OK; |
| 196 | file_handle_ = base::CreatePlatformFile(file_path_, file_flags, |
| 197 | NULL, &error_code); |
| 198 | set_error_code(error_code); |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | virtual void RunCallback() { |
jhawkins@chromium.org | d90c4f5 | 2011-10-18 04:29:29 +0900 | [diff] [blame] | 202 | callback_.Run(error_code(), base::PassPlatformFile(&file_handle_), |
| 203 | file_path_); |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | private: |
| 207 | scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; |
noelutz@google.com | f56dab2 | 2011-06-14 05:29:50 +0900 | [diff] [blame] | 208 | int additional_file_flags_; |
jhawkins@chromium.org | d90c4f5 | 2011-10-18 04:29:29 +0900 | [diff] [blame] | 209 | base::FileUtilProxy::CreateTemporaryCallback callback_; |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 210 | base::PlatformFile file_handle_; |
| 211 | FilePath file_path_; |
| 212 | }; |
| 213 | |
| 214 | class RelayWithStatusCallback : public MessageLoopRelay { |
| 215 | public: |
| 216 | explicit RelayWithStatusCallback( |
| 217 | base::FileUtilProxy::StatusCallback* callback) |
dumi@chromium.org | 6dedd6d | 2010-08-25 05:26:23 +0900 | [diff] [blame] | 218 | : callback_(callback) { |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 219 | // It is OK for callback to be NULL. |
| 220 | } |
| 221 | |
| 222 | protected: |
| 223 | virtual void RunCallback() { |
| 224 | // The caller may not have been interested in the result. |
| 225 | if (callback_) { |
dumi@chromium.org | 6dedd6d | 2010-08-25 05:26:23 +0900 | [diff] [blame] | 226 | callback_->Run(error_code()); |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 227 | delete callback_; |
| 228 | } |
| 229 | } |
| 230 | |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 231 | private: |
| 232 | base::FileUtilProxy::StatusCallback* callback_; |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 233 | }; |
| 234 | |
| 235 | class RelayClose : public RelayWithStatusCallback { |
| 236 | public: |
| 237 | RelayClose(base::PlatformFile file_handle, |
| 238 | base::FileUtilProxy::StatusCallback* callback) |
| 239 | : RelayWithStatusCallback(callback), |
| 240 | file_handle_(file_handle) { |
| 241 | } |
| 242 | |
| 243 | protected: |
| 244 | virtual void RunWork() { |
dumi@chromium.org | 6dedd6d | 2010-08-25 05:26:23 +0900 | [diff] [blame] | 245 | if (!base::ClosePlatformFile(file_handle_)) |
dumi@chromium.org | 50f197d | 2010-09-01 04:30:27 +0900 | [diff] [blame] | 246 | set_error_code(base::PLATFORM_FILE_ERROR_FAILED); |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | private: |
| 250 | base::PlatformFile file_handle_; |
| 251 | }; |
| 252 | |
kinuko@chromium.org | 850eb6d | 2010-10-15 09:37:34 +0900 | [diff] [blame] | 253 | class RelayEnsureFileExists : public MessageLoopRelay { |
| 254 | public: |
| 255 | RelayEnsureFileExists( |
| 256 | scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 257 | const FilePath& file_path, |
jhawkins@chromium.org | 70bffc1 | 2011-10-18 04:39:59 +0900 | [diff] [blame^] | 258 | const base::FileUtilProxy::EnsureFileExistsCallback& callback) |
kinuko@chromium.org | 850eb6d | 2010-10-15 09:37:34 +0900 | [diff] [blame] | 259 | : message_loop_proxy_(message_loop_proxy), |
| 260 | file_path_(file_path), |
| 261 | callback_(callback), |
| 262 | created_(false) { |
jhawkins@chromium.org | 70bffc1 | 2011-10-18 04:39:59 +0900 | [diff] [blame^] | 263 | DCHECK_EQ(false, callback.is_null()); |
kinuko@chromium.org | 850eb6d | 2010-10-15 09:37:34 +0900 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | protected: |
| 267 | virtual void RunWork() { |
| 268 | if (!file_util::DirectoryExists(file_path_.DirName())) { |
| 269 | // If its parent does not exist, should return NOT_FOUND error. |
| 270 | set_error_code(base::PLATFORM_FILE_ERROR_NOT_FOUND); |
| 271 | return; |
| 272 | } |
| 273 | base::PlatformFileError error_code = base::PLATFORM_FILE_OK; |
| 274 | // Tries to create the |file_path_| exclusively. This should fail |
| 275 | // with PLATFORM_FILE_ERROR_EXISTS if the path already exists. |
| 276 | base::PlatformFile handle = base::CreatePlatformFile( |
| 277 | file_path_, |
| 278 | base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_READ, |
| 279 | &created_, &error_code); |
| 280 | if (error_code == base::PLATFORM_FILE_ERROR_EXISTS) { |
| 281 | // Make sure created_ is false. |
| 282 | created_ = false; |
| 283 | error_code = base::PLATFORM_FILE_OK; |
| 284 | } |
| 285 | if (handle != base::kInvalidPlatformFileValue) |
| 286 | base::ClosePlatformFile(handle); |
| 287 | set_error_code(error_code); |
| 288 | } |
| 289 | |
| 290 | virtual void RunCallback() { |
jhawkins@chromium.org | 70bffc1 | 2011-10-18 04:39:59 +0900 | [diff] [blame^] | 291 | callback_.Run(error_code(), created_); |
kinuko@chromium.org | 850eb6d | 2010-10-15 09:37:34 +0900 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | private: |
| 295 | scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; |
| 296 | FilePath file_path_; |
jhawkins@chromium.org | 70bffc1 | 2011-10-18 04:39:59 +0900 | [diff] [blame^] | 297 | base::FileUtilProxy::EnsureFileExistsCallback callback_; |
kinuko@chromium.org | 850eb6d | 2010-10-15 09:37:34 +0900 | [diff] [blame] | 298 | bool created_; |
| 299 | }; |
| 300 | |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 301 | class RelayDelete : public RelayWithStatusCallback { |
| 302 | public: |
| 303 | RelayDelete(const FilePath& file_path, |
| 304 | bool recursive, |
| 305 | base::FileUtilProxy::StatusCallback* callback) |
| 306 | : RelayWithStatusCallback(callback), |
| 307 | file_path_(file_path), |
| 308 | recursive_(recursive) { |
| 309 | } |
| 310 | |
| 311 | protected: |
| 312 | virtual void RunWork() { |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 313 | if (!file_util::PathExists(file_path_)) { |
| 314 | set_error_code(base::PLATFORM_FILE_ERROR_NOT_FOUND); |
| 315 | return; |
| 316 | } |
| 317 | if (!file_util::Delete(file_path_, recursive_)) { |
| 318 | if (!recursive_ && !file_util::IsDirectoryEmpty(file_path_)) { |
kinuko@chromium.org | 1a07804 | 2010-10-07 17:35:09 +0900 | [diff] [blame] | 319 | set_error_code(base::PLATFORM_FILE_ERROR_NOT_EMPTY); |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 320 | return; |
| 321 | } |
dumi@chromium.org | 50f197d | 2010-09-01 04:30:27 +0900 | [diff] [blame] | 322 | set_error_code(base::PLATFORM_FILE_ERROR_FAILED); |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 323 | } |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | private: |
| 327 | FilePath file_path_; |
| 328 | bool recursive_; |
| 329 | }; |
| 330 | |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 331 | class RelayCopy : public RelayWithStatusCallback { |
| 332 | public: |
| 333 | RelayCopy(const FilePath& src_file_path, |
| 334 | const FilePath& dest_file_path, |
| 335 | base::FileUtilProxy::StatusCallback* callback) |
| 336 | : RelayWithStatusCallback(callback), |
| 337 | src_file_path_(src_file_path), |
| 338 | dest_file_path_(dest_file_path) { |
| 339 | } |
| 340 | |
| 341 | protected: |
| 342 | virtual void RunWork() { |
kinuko@chromium.org | 1a07804 | 2010-10-07 17:35:09 +0900 | [diff] [blame] | 343 | set_error_code(PerformCommonCheckAndPreparationForMoveAndCopy( |
| 344 | src_file_path_, dest_file_path_)); |
| 345 | if (error_code() != base::PLATFORM_FILE_OK) |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 346 | return; |
kinuko@chromium.org | 31583c3 | 2010-10-07 06:44:35 +0900 | [diff] [blame] | 347 | if (!file_util::CopyDirectory(src_file_path_, dest_file_path_, |
kinuko@chromium.org | 1a07804 | 2010-10-07 17:35:09 +0900 | [diff] [blame] | 348 | true /* recursive */)) |
kinuko@chromium.org | 31583c3 | 2010-10-07 06:44:35 +0900 | [diff] [blame] | 349 | set_error_code(base::PLATFORM_FILE_ERROR_FAILED); |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | private: |
| 353 | FilePath src_file_path_; |
| 354 | FilePath dest_file_path_; |
| 355 | }; |
| 356 | |
| 357 | class RelayMove : public RelayWithStatusCallback { |
| 358 | public: |
| 359 | RelayMove(const FilePath& src_file_path, |
| 360 | const FilePath& dest_file_path, |
| 361 | base::FileUtilProxy::StatusCallback* callback) |
| 362 | : RelayWithStatusCallback(callback), |
| 363 | src_file_path_(src_file_path), |
| 364 | dest_file_path_(dest_file_path) { |
| 365 | } |
| 366 | |
| 367 | protected: |
| 368 | virtual void RunWork() { |
kinuko@chromium.org | 1a07804 | 2010-10-07 17:35:09 +0900 | [diff] [blame] | 369 | set_error_code(PerformCommonCheckAndPreparationForMoveAndCopy( |
| 370 | src_file_path_, dest_file_path_)); |
| 371 | if (error_code() != base::PLATFORM_FILE_OK) |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 372 | return; |
kinuko@chromium.org | 1a07804 | 2010-10-07 17:35:09 +0900 | [diff] [blame] | 373 | if (!file_util::Move(src_file_path_, dest_file_path_)) |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 374 | set_error_code(base::PLATFORM_FILE_ERROR_FAILED); |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | private: |
| 378 | FilePath src_file_path_; |
| 379 | FilePath dest_file_path_; |
| 380 | }; |
| 381 | |
| 382 | class RelayCreateDirectory : public RelayWithStatusCallback { |
| 383 | public: |
| 384 | RelayCreateDirectory( |
| 385 | const FilePath& file_path, |
| 386 | bool exclusive, |
kinuko@chromium.org | c483d79 | 2010-09-08 09:50:41 +0900 | [diff] [blame] | 387 | bool recursive, |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 388 | base::FileUtilProxy::StatusCallback* callback) |
| 389 | : RelayWithStatusCallback(callback), |
| 390 | file_path_(file_path), |
kinuko@chromium.org | c483d79 | 2010-09-08 09:50:41 +0900 | [diff] [blame] | 391 | exclusive_(exclusive), |
| 392 | recursive_(recursive) { |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | protected: |
| 396 | virtual void RunWork() { |
| 397 | bool path_exists = file_util::PathExists(file_path_); |
| 398 | // If parent dir of file doesn't exist. |
kinuko@chromium.org | c483d79 | 2010-09-08 09:50:41 +0900 | [diff] [blame] | 399 | if (!recursive_ && !file_util::PathExists(file_path_.DirName())) { |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 400 | set_error_code(base::PLATFORM_FILE_ERROR_NOT_FOUND); |
| 401 | return; |
| 402 | } |
| 403 | if (exclusive_ && path_exists) { |
| 404 | set_error_code(base::PLATFORM_FILE_ERROR_EXISTS); |
| 405 | return; |
| 406 | } |
| 407 | // If file exists at the path. |
| 408 | if (path_exists && !file_util::DirectoryExists(file_path_)) { |
| 409 | set_error_code(base::PLATFORM_FILE_ERROR_EXISTS); |
| 410 | return; |
| 411 | } |
| 412 | if (!file_util::CreateDirectory(file_path_)) |
| 413 | set_error_code(base::PLATFORM_FILE_ERROR_FAILED); |
| 414 | } |
| 415 | |
| 416 | private: |
| 417 | FilePath file_path_; |
| 418 | bool exclusive_; |
kinuko@chromium.org | c483d79 | 2010-09-08 09:50:41 +0900 | [diff] [blame] | 419 | bool recursive_; |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 420 | }; |
| 421 | |
| 422 | class RelayReadDirectory : public MessageLoopRelay { |
| 423 | public: |
| 424 | RelayReadDirectory(const FilePath& file_path, |
| 425 | base::FileUtilProxy::ReadDirectoryCallback* callback) |
| 426 | : callback_(callback), file_path_(file_path) { |
| 427 | DCHECK(callback); |
| 428 | } |
| 429 | |
| 430 | protected: |
| 431 | virtual void RunWork() { |
| 432 | // TODO(kkanetkar): Implement directory read in multiple chunks. |
| 433 | if (!file_util::DirectoryExists(file_path_)) { |
| 434 | set_error_code(base::PLATFORM_FILE_ERROR_NOT_FOUND); |
| 435 | return; |
| 436 | } |
| 437 | |
| 438 | file_util::FileEnumerator file_enum( |
tfarina@chromium.org | a3a4db7 | 2011-08-15 22:09:27 +0900 | [diff] [blame] | 439 | file_path_, false, static_cast<file_util::FileEnumerator::FileType>( |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 440 | file_util::FileEnumerator::FILES | |
| 441 | file_util::FileEnumerator::DIRECTORIES)); |
| 442 | FilePath current; |
| 443 | while (!(current = file_enum.Next()).empty()) { |
kkanetkar@chromium.org | 4871068 | 2010-11-03 05:36:52 +0900 | [diff] [blame] | 444 | base::FileUtilProxy::Entry entry; |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 445 | file_util::FileEnumerator::FindInfo info; |
| 446 | file_enum.GetFindInfo(&info); |
kinuko@chromium.org | 83604d1 | 2010-09-03 07:28:49 +0900 | [diff] [blame] | 447 | entry.is_directory = file_enum.IsDirectory(info); |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 448 | // This will just give the entry's name instead of entire path |
| 449 | // if we use current.value(). |
| 450 | entry.name = file_util::FileEnumerator::GetFilename(info).value(); |
tzik@chromium.org | 09987c3 | 2011-07-19 14:03:03 +0900 | [diff] [blame] | 451 | entry.size = file_util::FileEnumerator::GetFilesize(info); |
| 452 | entry.last_modified_time = |
| 453 | file_util::FileEnumerator::GetLastModifiedTime(info); |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 454 | entries_.push_back(entry); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | virtual void RunCallback() { |
| 459 | callback_->Run(error_code(), entries_); |
| 460 | delete callback_; |
| 461 | } |
| 462 | |
| 463 | private: |
| 464 | base::FileUtilProxy::ReadDirectoryCallback* callback_; |
| 465 | FilePath file_path_; |
kkanetkar@chromium.org | 4871068 | 2010-11-03 05:36:52 +0900 | [diff] [blame] | 466 | std::vector<base::FileUtilProxy::Entry> entries_; |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 467 | }; |
| 468 | |
jianli@chromium.org | 9ed1f9d | 2010-08-31 11:42:36 +0900 | [diff] [blame] | 469 | class RelayGetFileInfo : public MessageLoopRelay { |
| 470 | public: |
| 471 | RelayGetFileInfo(const FilePath& file_path, |
| 472 | base::FileUtilProxy::GetFileInfoCallback* callback) |
| 473 | : callback_(callback), |
dumi@chromium.org | 50f197d | 2010-09-01 04:30:27 +0900 | [diff] [blame] | 474 | file_path_(file_path) { |
jianli@chromium.org | 9ed1f9d | 2010-08-31 11:42:36 +0900 | [diff] [blame] | 475 | DCHECK(callback); |
| 476 | } |
| 477 | |
| 478 | protected: |
| 479 | virtual void RunWork() { |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 480 | if (!file_util::PathExists(file_path_)) { |
| 481 | set_error_code(base::PLATFORM_FILE_ERROR_NOT_FOUND); |
| 482 | return; |
| 483 | } |
dumi@chromium.org | 50f197d | 2010-09-01 04:30:27 +0900 | [diff] [blame] | 484 | if (!file_util::GetFileInfo(file_path_, &file_info_)) |
| 485 | set_error_code(base::PLATFORM_FILE_ERROR_FAILED); |
jianli@chromium.org | 9ed1f9d | 2010-08-31 11:42:36 +0900 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | virtual void RunCallback() { |
dumi@chromium.org | 50f197d | 2010-09-01 04:30:27 +0900 | [diff] [blame] | 489 | callback_->Run(error_code(), file_info_); |
jianli@chromium.org | 9ed1f9d | 2010-08-31 11:42:36 +0900 | [diff] [blame] | 490 | delete callback_; |
| 491 | } |
| 492 | |
| 493 | private: |
| 494 | base::FileUtilProxy::GetFileInfoCallback* callback_; |
| 495 | FilePath file_path_; |
dumi@chromium.org | 97ae261 | 2010-09-03 11:28:37 +0900 | [diff] [blame] | 496 | base::PlatformFileInfo file_info_; |
jianli@chromium.org | 9ed1f9d | 2010-08-31 11:42:36 +0900 | [diff] [blame] | 497 | }; |
| 498 | |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 499 | class RelayGetFileInfoFromPlatformFile : public MessageLoopRelay { |
| 500 | public: |
| 501 | RelayGetFileInfoFromPlatformFile( |
| 502 | base::PlatformFile file, |
| 503 | base::FileUtilProxy::GetFileInfoCallback* callback) |
| 504 | : callback_(callback), |
| 505 | file_(file) { |
| 506 | DCHECK(callback); |
| 507 | } |
| 508 | |
| 509 | protected: |
| 510 | virtual void RunWork() { |
| 511 | if (!base::GetPlatformFileInfo(file_, &file_info_)) |
| 512 | set_error_code(base::PLATFORM_FILE_ERROR_FAILED); |
| 513 | } |
| 514 | |
| 515 | virtual void RunCallback() { |
| 516 | callback_->Run(error_code(), file_info_); |
| 517 | delete callback_; |
| 518 | } |
| 519 | |
| 520 | private: |
| 521 | base::FileUtilProxy::GetFileInfoCallback* callback_; |
| 522 | base::PlatformFile file_; |
| 523 | base::PlatformFileInfo file_info_; |
| 524 | }; |
| 525 | |
| 526 | class RelayRead : public MessageLoopRelay { |
| 527 | public: |
| 528 | RelayRead(base::PlatformFile file, |
| 529 | int64 offset, |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 530 | int bytes_to_read, |
darin@chromium.org | 44a9973 | 2011-02-04 09:39:34 +0900 | [diff] [blame] | 531 | base::FileUtilProxy::ReadCallback* callback) |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 532 | : file_(file), |
| 533 | offset_(offset), |
darin@chromium.org | 44a9973 | 2011-02-04 09:39:34 +0900 | [diff] [blame] | 534 | buffer_(new char[bytes_to_read]), |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 535 | bytes_to_read_(bytes_to_read), |
| 536 | callback_(callback), |
| 537 | bytes_read_(0) { |
| 538 | } |
| 539 | |
| 540 | protected: |
| 541 | virtual void RunWork() { |
darin@chromium.org | 44a9973 | 2011-02-04 09:39:34 +0900 | [diff] [blame] | 542 | bytes_read_ = base::ReadPlatformFile(file_, offset_, buffer_.get(), |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 543 | bytes_to_read_); |
| 544 | if (bytes_read_ < 0) |
| 545 | set_error_code(base::PLATFORM_FILE_ERROR_FAILED); |
| 546 | } |
| 547 | |
| 548 | virtual void RunCallback() { |
| 549 | if (callback_) { |
darin@chromium.org | 44a9973 | 2011-02-04 09:39:34 +0900 | [diff] [blame] | 550 | callback_->Run(error_code(), buffer_.get(), bytes_read_); |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 551 | delete callback_; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | private: |
| 556 | base::PlatformFile file_; |
| 557 | int64 offset_; |
darin@chromium.org | 44a9973 | 2011-02-04 09:39:34 +0900 | [diff] [blame] | 558 | scoped_array<char> buffer_; |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 559 | int bytes_to_read_; |
darin@chromium.org | 44a9973 | 2011-02-04 09:39:34 +0900 | [diff] [blame] | 560 | base::FileUtilProxy::ReadCallback* callback_; |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 561 | int bytes_read_; |
| 562 | }; |
| 563 | |
| 564 | class RelayWrite : public MessageLoopRelay { |
| 565 | public: |
| 566 | RelayWrite(base::PlatformFile file, |
ericu@google.com | 6a65222 | 2010-10-05 11:26:47 +0900 | [diff] [blame] | 567 | int64 offset, |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 568 | const char* buffer, |
| 569 | int bytes_to_write, |
darin@chromium.org | 44a9973 | 2011-02-04 09:39:34 +0900 | [diff] [blame] | 570 | base::FileUtilProxy::WriteCallback* callback) |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 571 | : file_(file), |
| 572 | offset_(offset), |
darin@chromium.org | 44a9973 | 2011-02-04 09:39:34 +0900 | [diff] [blame] | 573 | buffer_(new char[bytes_to_write]), |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 574 | bytes_to_write_(bytes_to_write), |
finnur@chromium.org | 899d4e0 | 2011-03-15 18:56:27 +0900 | [diff] [blame] | 575 | callback_(callback), |
| 576 | bytes_written_(0) { |
darin@chromium.org | 44a9973 | 2011-02-04 09:39:34 +0900 | [diff] [blame] | 577 | memcpy(buffer_.get(), buffer, bytes_to_write); |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | protected: |
| 581 | virtual void RunWork() { |
darin@chromium.org | 44a9973 | 2011-02-04 09:39:34 +0900 | [diff] [blame] | 582 | bytes_written_ = base::WritePlatformFile(file_, offset_, buffer_.get(), |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 583 | bytes_to_write_); |
| 584 | if (bytes_written_ < 0) |
| 585 | set_error_code(base::PLATFORM_FILE_ERROR_FAILED); |
| 586 | } |
| 587 | |
| 588 | virtual void RunCallback() { |
| 589 | if (callback_) { |
| 590 | callback_->Run(error_code(), bytes_written_); |
| 591 | delete callback_; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | private: |
| 596 | base::PlatformFile file_; |
| 597 | int64 offset_; |
darin@chromium.org | 44a9973 | 2011-02-04 09:39:34 +0900 | [diff] [blame] | 598 | scoped_array<char> buffer_; |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 599 | int bytes_to_write_; |
darin@chromium.org | 44a9973 | 2011-02-04 09:39:34 +0900 | [diff] [blame] | 600 | base::FileUtilProxy::WriteCallback* callback_; |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 601 | int bytes_written_; |
| 602 | }; |
| 603 | |
| 604 | class RelayTouch : public RelayWithStatusCallback { |
| 605 | public: |
| 606 | RelayTouch(base::PlatformFile file, |
| 607 | const base::Time& last_access_time, |
| 608 | const base::Time& last_modified_time, |
| 609 | base::FileUtilProxy::StatusCallback* callback) |
| 610 | : RelayWithStatusCallback(callback), |
| 611 | file_(file), |
| 612 | last_access_time_(last_access_time), |
| 613 | last_modified_time_(last_modified_time) { |
| 614 | } |
| 615 | |
| 616 | protected: |
| 617 | virtual void RunWork() { |
| 618 | if (!base::TouchPlatformFile(file_, last_access_time_, last_modified_time_)) |
| 619 | set_error_code(base::PLATFORM_FILE_ERROR_FAILED); |
| 620 | } |
| 621 | |
| 622 | private: |
| 623 | base::PlatformFile file_; |
| 624 | base::Time last_access_time_; |
| 625 | base::Time last_modified_time_; |
| 626 | }; |
| 627 | |
dumi@chromium.org | 12c2e2b | 2010-09-24 10:09:32 +0900 | [diff] [blame] | 628 | class RelayTouchFilePath : public RelayWithStatusCallback { |
| 629 | public: |
| 630 | RelayTouchFilePath(const FilePath& file_path, |
| 631 | const base::Time& last_access_time, |
| 632 | const base::Time& last_modified_time, |
| 633 | base::FileUtilProxy::StatusCallback* callback) |
| 634 | : RelayWithStatusCallback(callback), |
evan@chromium.org | d719ede | 2010-09-25 05:07:41 +0900 | [diff] [blame] | 635 | file_path_(file_path), |
dumi@chromium.org | 12c2e2b | 2010-09-24 10:09:32 +0900 | [diff] [blame] | 636 | last_access_time_(last_access_time), |
| 637 | last_modified_time_(last_modified_time) { |
| 638 | } |
| 639 | |
| 640 | protected: |
| 641 | virtual void RunWork() { |
| 642 | if (!file_util::TouchFile( |
| 643 | file_path_, last_access_time_, last_modified_time_)) |
| 644 | set_error_code(base::PLATFORM_FILE_ERROR_FAILED); |
| 645 | } |
| 646 | |
| 647 | private: |
| 648 | FilePath file_path_; |
| 649 | base::Time last_access_time_; |
| 650 | base::Time last_modified_time_; |
| 651 | }; |
| 652 | |
ericu@google.com | 6a65222 | 2010-10-05 11:26:47 +0900 | [diff] [blame] | 653 | class RelayTruncatePlatformFile : public RelayWithStatusCallback { |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 654 | public: |
ericu@google.com | 6a65222 | 2010-10-05 11:26:47 +0900 | [diff] [blame] | 655 | RelayTruncatePlatformFile(base::PlatformFile file, |
| 656 | int64 length, |
| 657 | base::FileUtilProxy::StatusCallback* callback) |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 658 | : RelayWithStatusCallback(callback), |
| 659 | file_(file), |
| 660 | length_(length) { |
| 661 | } |
| 662 | |
| 663 | protected: |
| 664 | virtual void RunWork() { |
| 665 | if (!base::TruncatePlatformFile(file_, length_)) |
| 666 | set_error_code(base::PLATFORM_FILE_ERROR_FAILED); |
| 667 | } |
| 668 | |
| 669 | private: |
| 670 | base::PlatformFile file_; |
| 671 | int64 length_; |
| 672 | }; |
| 673 | |
ericu@google.com | 6a65222 | 2010-10-05 11:26:47 +0900 | [diff] [blame] | 674 | class RelayTruncate : public RelayWithStatusCallback { |
| 675 | public: |
| 676 | RelayTruncate(const FilePath& path, |
| 677 | int64 length, |
| 678 | base::FileUtilProxy::StatusCallback* callback) |
| 679 | : RelayWithStatusCallback(callback), |
| 680 | path_(path), |
| 681 | length_(length) { |
| 682 | } |
| 683 | |
| 684 | protected: |
| 685 | virtual void RunWork() { |
| 686 | base::PlatformFileError error_code(base::PLATFORM_FILE_ERROR_FAILED); |
| 687 | base::PlatformFile file = |
| 688 | base::CreatePlatformFile( |
| 689 | path_, |
| 690 | base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, |
| 691 | NULL, |
| 692 | &error_code); |
| 693 | if (error_code != base::PLATFORM_FILE_OK) { |
| 694 | set_error_code(error_code); |
| 695 | return; |
| 696 | } |
| 697 | if (!base::TruncatePlatformFile(file, length_)) |
| 698 | set_error_code(base::PLATFORM_FILE_ERROR_FAILED); |
| 699 | base::ClosePlatformFile(file); |
| 700 | } |
| 701 | |
| 702 | private: |
| 703 | FilePath path_; |
| 704 | int64 length_; |
| 705 | }; |
| 706 | |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 707 | class RelayFlush : public RelayWithStatusCallback { |
| 708 | public: |
| 709 | RelayFlush(base::PlatformFile file, |
| 710 | base::FileUtilProxy::StatusCallback* callback) |
| 711 | : RelayWithStatusCallback(callback), |
| 712 | file_(file) { |
| 713 | } |
| 714 | |
| 715 | protected: |
| 716 | virtual void RunWork() { |
| 717 | if (!base::FlushPlatformFile(file_)) |
| 718 | set_error_code(base::PLATFORM_FILE_ERROR_FAILED); |
| 719 | } |
| 720 | |
| 721 | private: |
| 722 | base::PlatformFile file_; |
| 723 | }; |
| 724 | |
dumi@chromium.org | 6dedd6d | 2010-08-25 05:26:23 +0900 | [diff] [blame] | 725 | bool Start(const tracked_objects::Location& from_here, |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 726 | scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 727 | scoped_refptr<MessageLoopRelay> relay) { |
dumi@chromium.org | 6dedd6d | 2010-08-25 05:26:23 +0900 | [diff] [blame] | 728 | return relay->Start(message_loop_proxy, from_here); |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | } // namespace |
| 732 | |
| 733 | namespace base { |
| 734 | |
| 735 | // static |
dumi@chromium.org | 6dedd6d | 2010-08-25 05:26:23 +0900 | [diff] [blame] | 736 | bool FileUtilProxy::CreateOrOpen( |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 737 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 738 | const FilePath& file_path, int file_flags, |
jhawkins@chromium.org | 3a6573d | 2011-10-18 03:40:30 +0900 | [diff] [blame] | 739 | const CreateOrOpenCallback& callback) { |
dumi@chromium.org | 6dedd6d | 2010-08-25 05:26:23 +0900 | [diff] [blame] | 740 | return Start(FROM_HERE, message_loop_proxy, new RelayCreateOrOpen( |
kinuko@chromium.org | 850eb6d | 2010-10-15 09:37:34 +0900 | [diff] [blame] | 741 | message_loop_proxy, file_path, file_flags, callback)); |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | // static |
dumi@chromium.org | 6dedd6d | 2010-08-25 05:26:23 +0900 | [diff] [blame] | 745 | bool FileUtilProxy::CreateTemporary( |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 746 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
noelutz@google.com | f56dab2 | 2011-06-14 05:29:50 +0900 | [diff] [blame] | 747 | int additional_file_flags, |
jhawkins@chromium.org | d90c4f5 | 2011-10-18 04:29:29 +0900 | [diff] [blame] | 748 | const CreateTemporaryCallback& callback) { |
dumi@chromium.org | 6dedd6d | 2010-08-25 05:26:23 +0900 | [diff] [blame] | 749 | return Start(FROM_HERE, message_loop_proxy, |
noelutz@google.com | f56dab2 | 2011-06-14 05:29:50 +0900 | [diff] [blame] | 750 | new RelayCreateTemporary(message_loop_proxy, |
| 751 | additional_file_flags, |
| 752 | callback)); |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | // static |
dumi@chromium.org | 6dedd6d | 2010-08-25 05:26:23 +0900 | [diff] [blame] | 756 | bool FileUtilProxy::Close(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 757 | base::PlatformFile file_handle, |
| 758 | StatusCallback* callback) { |
dumi@chromium.org | 6dedd6d | 2010-08-25 05:26:23 +0900 | [diff] [blame] | 759 | return Start(FROM_HERE, message_loop_proxy, |
| 760 | new RelayClose(file_handle, callback)); |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | // static |
kinuko@chromium.org | 850eb6d | 2010-10-15 09:37:34 +0900 | [diff] [blame] | 764 | bool FileUtilProxy::EnsureFileExists( |
| 765 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 766 | const FilePath& file_path, |
jhawkins@chromium.org | 70bffc1 | 2011-10-18 04:39:59 +0900 | [diff] [blame^] | 767 | const EnsureFileExistsCallback& callback) { |
kinuko@chromium.org | 850eb6d | 2010-10-15 09:37:34 +0900 | [diff] [blame] | 768 | return Start(FROM_HERE, message_loop_proxy, new RelayEnsureFileExists( |
| 769 | message_loop_proxy, file_path, callback)); |
| 770 | } |
| 771 | |
erg@google.com | 37c078e | 2011-01-11 09:50:59 +0900 | [diff] [blame] | 772 | // Retrieves the information about a file. It is invalid to pass NULL for the |
| 773 | // callback. |
| 774 | bool FileUtilProxy::GetFileInfo( |
| 775 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 776 | const FilePath& file_path, |
| 777 | GetFileInfoCallback* callback) { |
| 778 | return Start(FROM_HERE, message_loop_proxy, new RelayGetFileInfo( |
| 779 | file_path, callback)); |
| 780 | } |
| 781 | |
kinuko@chromium.org | 850eb6d | 2010-10-15 09:37:34 +0900 | [diff] [blame] | 782 | // static |
erg@google.com | 37c078e | 2011-01-11 09:50:59 +0900 | [diff] [blame] | 783 | bool FileUtilProxy::GetFileInfoFromPlatformFile( |
| 784 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 785 | PlatformFile file, |
| 786 | GetFileInfoCallback* callback) { |
dumi@chromium.org | 6dedd6d | 2010-08-25 05:26:23 +0900 | [diff] [blame] | 787 | return Start(FROM_HERE, message_loop_proxy, |
erg@google.com | 37c078e | 2011-01-11 09:50:59 +0900 | [diff] [blame] | 788 | new RelayGetFileInfoFromPlatformFile(file, callback)); |
| 789 | } |
| 790 | |
| 791 | // static |
| 792 | bool FileUtilProxy::ReadDirectory( |
| 793 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 794 | const FilePath& file_path, |
| 795 | ReadDirectoryCallback* callback) { |
| 796 | return Start(FROM_HERE, message_loop_proxy, new RelayReadDirectory( |
| 797 | file_path, callback)); |
| 798 | } |
| 799 | |
| 800 | // static |
| 801 | bool FileUtilProxy::CreateDirectory( |
| 802 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 803 | const FilePath& file_path, |
| 804 | bool exclusive, |
| 805 | bool recursive, |
| 806 | StatusCallback* callback) { |
| 807 | return Start(FROM_HERE, message_loop_proxy, new RelayCreateDirectory( |
| 808 | file_path, exclusive, recursive, callback)); |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | // static |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 812 | bool FileUtilProxy::Copy(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 813 | const FilePath& src_file_path, |
| 814 | const FilePath& dest_file_path, |
| 815 | StatusCallback* callback) { |
| 816 | return Start(FROM_HERE, message_loop_proxy, |
| 817 | new RelayCopy(src_file_path, dest_file_path, callback)); |
| 818 | } |
| 819 | |
| 820 | // static |
| 821 | bool FileUtilProxy::Move(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 822 | const FilePath& src_file_path, |
| 823 | const FilePath& dest_file_path, |
| 824 | StatusCallback* callback) { |
| 825 | return Start(FROM_HERE, message_loop_proxy, |
| 826 | new RelayMove(src_file_path, dest_file_path, callback)); |
| 827 | } |
| 828 | |
| 829 | // static |
erg@google.com | 37c078e | 2011-01-11 09:50:59 +0900 | [diff] [blame] | 830 | bool FileUtilProxy::Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 831 | const FilePath& file_path, |
| 832 | bool recursive, |
| 833 | StatusCallback* callback) { |
| 834 | return Start(FROM_HERE, message_loop_proxy, |
| 835 | new RelayDelete(file_path, recursive, callback)); |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | // static |
dumi@chromium.org | 6dedd6d | 2010-08-25 05:26:23 +0900 | [diff] [blame] | 839 | bool FileUtilProxy::RecursiveDelete( |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 840 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 841 | const FilePath& file_path, |
| 842 | StatusCallback* callback) { |
dumi@chromium.org | 6dedd6d | 2010-08-25 05:26:23 +0900 | [diff] [blame] | 843 | return Start(FROM_HERE, message_loop_proxy, |
| 844 | new RelayDelete(file_path, true, callback)); |
dumi@chromium.org | c980e40 | 2010-08-21 07:42:50 +0900 | [diff] [blame] | 845 | } |
| 846 | |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 847 | // static |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 848 | bool FileUtilProxy::Read( |
| 849 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 850 | PlatformFile file, |
| 851 | int64 offset, |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 852 | int bytes_to_read, |
darin@chromium.org | 44a9973 | 2011-02-04 09:39:34 +0900 | [diff] [blame] | 853 | ReadCallback* callback) { |
kinuko@chromium.org | 6e168d3 | 2011-08-19 19:14:27 +0900 | [diff] [blame] | 854 | if (bytes_to_read < 0) { |
| 855 | delete callback; |
sanga@chromium.org | f5cff13 | 2011-08-18 01:16:27 +0900 | [diff] [blame] | 856 | return false; |
kinuko@chromium.org | 6e168d3 | 2011-08-19 19:14:27 +0900 | [diff] [blame] | 857 | } |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 858 | return Start(FROM_HERE, message_loop_proxy, |
darin@chromium.org | 44a9973 | 2011-02-04 09:39:34 +0900 | [diff] [blame] | 859 | new RelayRead(file, offset, bytes_to_read, callback)); |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | // static |
| 863 | bool FileUtilProxy::Write( |
| 864 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 865 | PlatformFile file, |
| 866 | int64 offset, |
| 867 | const char* buffer, |
| 868 | int bytes_to_write, |
darin@chromium.org | 44a9973 | 2011-02-04 09:39:34 +0900 | [diff] [blame] | 869 | WriteCallback* callback) { |
kinuko@chromium.org | 6e168d3 | 2011-08-19 19:14:27 +0900 | [diff] [blame] | 870 | if (bytes_to_write <= 0) { |
| 871 | delete callback; |
sanga@chromium.org | 21d251f | 2011-08-18 01:45:48 +0900 | [diff] [blame] | 872 | return false; |
kinuko@chromium.org | 6e168d3 | 2011-08-19 19:14:27 +0900 | [diff] [blame] | 873 | } |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 874 | return Start(FROM_HERE, message_loop_proxy, |
| 875 | new RelayWrite(file, offset, buffer, bytes_to_write, callback)); |
| 876 | } |
| 877 | |
| 878 | // static |
| 879 | bool FileUtilProxy::Touch( |
| 880 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 881 | PlatformFile file, |
| 882 | const base::Time& last_access_time, |
| 883 | const base::Time& last_modified_time, |
| 884 | StatusCallback* callback) { |
| 885 | return Start(FROM_HERE, message_loop_proxy, |
| 886 | new RelayTouch(file, last_access_time, last_modified_time, |
| 887 | callback)); |
| 888 | } |
| 889 | |
| 890 | // static |
dumi@chromium.org | 12c2e2b | 2010-09-24 10:09:32 +0900 | [diff] [blame] | 891 | bool FileUtilProxy::Touch( |
| 892 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 893 | const FilePath& file_path, |
| 894 | const base::Time& last_access_time, |
| 895 | const base::Time& last_modified_time, |
| 896 | StatusCallback* callback) { |
| 897 | return Start(FROM_HERE, message_loop_proxy, |
| 898 | new RelayTouchFilePath(file_path, last_access_time, |
| 899 | last_modified_time, callback)); |
| 900 | } |
| 901 | |
| 902 | // static |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 903 | bool FileUtilProxy::Truncate( |
| 904 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 905 | PlatformFile file, |
ericu@google.com | 6a65222 | 2010-10-05 11:26:47 +0900 | [diff] [blame] | 906 | int64 length, |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 907 | StatusCallback* callback) { |
| 908 | return Start(FROM_HERE, message_loop_proxy, |
ericu@google.com | 6a65222 | 2010-10-05 11:26:47 +0900 | [diff] [blame] | 909 | new RelayTruncatePlatformFile(file, length, callback)); |
| 910 | } |
| 911 | |
| 912 | // static |
| 913 | bool FileUtilProxy::Truncate( |
| 914 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 915 | const FilePath& path, |
| 916 | int64 length, |
| 917 | StatusCallback* callback) { |
| 918 | return Start(FROM_HERE, message_loop_proxy, |
| 919 | new RelayTruncate(path, length, callback)); |
dumi@chromium.org | 2391598 | 2010-09-10 12:01:14 +0900 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | // static |
| 923 | bool FileUtilProxy::Flush( |
| 924 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 925 | PlatformFile file, |
| 926 | StatusCallback* callback) { |
| 927 | return Start(FROM_HERE, message_loop_proxy, new RelayFlush(file, callback)); |
| 928 | } |
| 929 | |
kkanetkar@chromium.org | 3a3bdea | 2010-09-02 12:43:36 +0900 | [diff] [blame] | 930 | } // namespace base |