diff options
-rw-r--r-- | base/file_util_proxy.cc | 84 | ||||
-rw-r--r-- | base/file_util_proxy.h | 26 | ||||
-rw-r--r-- | base/platform_file_posix.cc | 3 | ||||
-rw-r--r-- | base/platform_file_win.cc | 4 | ||||
-rw-r--r-- | webkit/fileapi/file_system_operation.cc | 33 | ||||
-rw-r--r-- | webkit/fileapi/file_system_operation.h | 12 |
6 files changed, 103 insertions, 59 deletions
diff --git a/base/file_util_proxy.cc b/base/file_util_proxy.cc index ab7903e..fc96e8f 100644 --- a/base/file_util_proxy.cc +++ b/base/file_util_proxy.cc @@ -14,7 +14,8 @@ namespace { // Performs common checks for move and copy. // This also removes the destination directory if it's non-empty and all other -// checks are passed (so that the copy/move correctly overwrites the destination). +// checks are passed (so that the copy/move correctly overwrites the +// destination). static base::PlatformFileError PerformCommonCheckAndPreparationForMoveAndCopy( const FilePath& src_file_path, const FilePath& dest_file_path) { @@ -119,14 +120,12 @@ class RelayCreateOrOpen : public MessageLoopRelay { scoped_refptr<base::MessageLoopProxy> message_loop_proxy, const FilePath& file_path, int file_flags, - bool return_no_handle, base::FileUtilProxy::CreateOrOpenCallback* callback) : message_loop_proxy_(message_loop_proxy), file_path_(file_path), file_flags_(file_flags), callback_(callback), file_handle_(base::kInvalidPlatformFileValue), - return_no_handle_(return_no_handle), created_(false) { DCHECK(callback); } @@ -146,13 +145,6 @@ class RelayCreateOrOpen : public MessageLoopRelay { base::PlatformFileError error_code = base::PLATFORM_FILE_OK; file_handle_ = base::CreatePlatformFile(file_path_, file_flags_, &created_, &error_code); - // If the return_no_handle is true the caller is not interested - // in the file_handle_. Close it right now. - if (return_no_handle_ && file_handle_ != base::kInvalidPlatformFileValue) { - // We don't check the return value here. - base::ClosePlatformFile(file_handle_); - file_handle_ = base::kInvalidPlatformFileValue; - } set_error_code(error_code); } @@ -168,7 +160,6 @@ class RelayCreateOrOpen : public MessageLoopRelay { int file_flags_; base::FileUtilProxy::CreateOrOpenCallback* callback_; base::PlatformFile file_handle_; - bool return_no_handle_; bool created_; }; @@ -259,6 +250,55 @@ class RelayClose : public RelayWithStatusCallback { base::PlatformFile file_handle_; }; +class RelayEnsureFileExists : public MessageLoopRelay { + public: + RelayEnsureFileExists( + scoped_refptr<base::MessageLoopProxy> message_loop_proxy, + const FilePath& file_path, + base::FileUtilProxy::EnsureFileExistsCallback* callback) + : message_loop_proxy_(message_loop_proxy), + file_path_(file_path), + callback_(callback), + created_(false) { + DCHECK(callback); + } + + protected: + virtual void RunWork() { + if (!file_util::DirectoryExists(file_path_.DirName())) { + // If its parent does not exist, should return NOT_FOUND error. + set_error_code(base::PLATFORM_FILE_ERROR_NOT_FOUND); + return; + } + base::PlatformFileError error_code = base::PLATFORM_FILE_OK; + // Tries to create the |file_path_| exclusively. This should fail + // with PLATFORM_FILE_ERROR_EXISTS if the path already exists. + base::PlatformFile handle = base::CreatePlatformFile( + file_path_, + base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_READ, + &created_, &error_code); + if (error_code == base::PLATFORM_FILE_ERROR_EXISTS) { + // Make sure created_ is false. + created_ = false; + error_code = base::PLATFORM_FILE_OK; + } + if (handle != base::kInvalidPlatformFileValue) + base::ClosePlatformFile(handle); + set_error_code(error_code); + } + + virtual void RunCallback() { + callback_->Run(error_code(), created_); + delete callback_; + } + + private: + scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; + FilePath file_path_; + base::FileUtilProxy::EnsureFileExistsCallback* callback_; + bool created_; +}; + class RelayDelete : public RelayWithStatusCallback { public: RelayDelete(const FilePath& file_path, @@ -695,18 +735,7 @@ bool FileUtilProxy::CreateOrOpen( const FilePath& file_path, int file_flags, CreateOrOpenCallback* callback) { return Start(FROM_HERE, message_loop_proxy, new RelayCreateOrOpen( - message_loop_proxy, file_path, file_flags, false /* return_no_handle */, - callback)); -} - -// static -bool FileUtilProxy::Create( - scoped_refptr<MessageLoopProxy> message_loop_proxy, - const FilePath& file_path, int file_flags, - CreateOrOpenCallback* callback) { - return Start(FROM_HERE, message_loop_proxy, new RelayCreateOrOpen( - message_loop_proxy, file_path, file_flags, true /* return_no_handle */, - callback)); + message_loop_proxy, file_path, file_flags, callback)); } // static @@ -737,6 +766,15 @@ bool FileUtilProxy::Close(scoped_refptr<MessageLoopProxy> message_loop_proxy, } // static +bool FileUtilProxy::EnsureFileExists( + scoped_refptr<MessageLoopProxy> message_loop_proxy, + const FilePath& file_path, + EnsureFileExistsCallback* callback) { + return Start(FROM_HERE, message_loop_proxy, new RelayEnsureFileExists( + message_loop_proxy, file_path, callback)); +} + +// static bool FileUtilProxy::Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy, const FilePath& file_path, bool recursive, diff --git a/base/file_util_proxy.h b/base/file_util_proxy.h index 11fc988..09c2377 100644 --- a/base/file_util_proxy.h +++ b/base/file_util_proxy.h @@ -40,6 +40,9 @@ class FileUtilProxy { // Creates or opens a file with the given flags. It is invalid to pass NULL // for the callback. + // If PLATFORM_FILE_CREATE is set in |file_flags| it always tries to create + // a new file at the given |file_path| and calls back with + // PLATFORM_FILE_ERROR_FILE_EXISTS if the |file_path| already exists. typedef Callback3<base::PlatformFileError /* error code */, base::PassPlatformFile, bool /* created */>::Type CreateOrOpenCallback; @@ -48,13 +51,6 @@ class FileUtilProxy { int file_flags, CreateOrOpenCallback* callback); - // Creates a file with the given flags. This one is a variation of - // CreateOrOpen but it doesn't return a file handle. - static bool Create(scoped_refptr<MessageLoopProxy> message_loop_proxy, - const FilePath& file_path, - int file_flags, - CreateOrOpenCallback* callback); - // Creates a temporary file for writing. The path and an open file handle // are returned. It is invalid to pass NULL for the callback. typedef Callback3<base::PlatformFileError /* error code */, @@ -69,6 +65,22 @@ class FileUtilProxy { base::PlatformFile, StatusCallback* callback); + // Ensures that the given |file_path| exist. This creates a empty new file + // at |file_path| if the |file_path| does not exist. + // If a new file han not existed and is created at the |file_path|, + // |created| of the callback argument is set true and |error code| + // is set PLATFORM_FILE_OK. + // If the file already exists, |created| is set false and |error code| + // is set PLATFORM_FILE_OK. + // If the file hasn't existed but it couldn't be created for some other + // reasons, |created| is set false and |error code| indicates the error. + typedef Callback2<base::PlatformFileError /* error code */, + bool /* created */>::Type EnsureFileExistsCallback; + static bool EnsureFileExists( + scoped_refptr<MessageLoopProxy> message_loop_proxy, + const FilePath& file_path, + EnsureFileExistsCallback* callback); + // Retrieves the information about a file. It is invalid to pass NULL for the // callback. typedef Callback2<base::PlatformFileError /* error code */, diff --git a/base/platform_file_posix.cc b/base/platform_file_posix.cc index 4b744fe..4d2ac46 100644 --- a/base/platform_file_posix.cc +++ b/base/platform_file_posix.cc @@ -84,7 +84,8 @@ PlatformFile CreatePlatformFile(const FilePath& name, int flags, } } - if (created && (descriptor > 0) && (flags & PLATFORM_FILE_CREATE_ALWAYS)) + if (created && (descriptor > 0) && + (flags & (PLATFORM_FILE_CREATE_ALWAYS | PLATFORM_FILE_CREATE))) *created = true; if ((descriptor > 0) && (flags & PLATFORM_FILE_DELETE_ON_CLOSE)) { diff --git a/base/platform_file_win.cc b/base/platform_file_win.cc index 63dfef9..3aa02e8 100644 --- a/base/platform_file_win.cc +++ b/base/platform_file_win.cc @@ -68,9 +68,9 @@ PlatformFile CreatePlatformFile(const FilePath& name, disposition, create_flags, NULL); if (created && (INVALID_HANDLE_VALUE != file)) { - if (flags & PLATFORM_FILE_OPEN_ALWAYS) + if (flags & (PLATFORM_FILE_OPEN_ALWAYS)) *created = (ERROR_ALREADY_EXISTS != GetLastError()); - else if (flags & PLATFORM_FILE_CREATE_ALWAYS) + else if (flags & (PLATFORM_FILE_CREATE_ALWAYS | PLATFORM_FILE_CREATE)) *created = true; } diff --git a/webkit/fileapi/file_system_operation.cc b/webkit/fileapi/file_system_operation.cc index 6a88b6f..0e2e823 100644 --- a/webkit/fileapi/file_system_operation.cc +++ b/webkit/fileapi/file_system_operation.cc @@ -36,11 +36,10 @@ void FileSystemOperation::CreateFile(const FilePath& path, pending_operation_ = kOperationCreateFile; #endif - base::FileUtilProxy::Create( - proxy_, path, base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_READ, - callback_factory_.NewCallback( - exclusive ? &FileSystemOperation::DidCreateFileExclusive - : &FileSystemOperation::DidCreateFileNonExclusive)); + base::FileUtilProxy::EnsureFileExists( + proxy_, path, callback_factory_.NewCallback( + exclusive ? &FileSystemOperation::DidEnsureFileExistsExclusive + : &FileSystemOperation::DidEnsureFileExistsNonExclusive)); } void FileSystemOperation::CreateDirectory(const FilePath& path, @@ -219,23 +218,17 @@ void FileSystemOperation::Cancel(FileSystemOperation* cancel_operation) { } } -void FileSystemOperation::DidCreateFileExclusive( - base::PlatformFileError rv, - base::PassPlatformFile file, - bool created) { - DidFinishFileOperation(rv); +void FileSystemOperation::DidEnsureFileExistsExclusive( + base::PlatformFileError rv, bool created) { + if (rv == base::PLATFORM_FILE_OK && !created) + dispatcher_->DidFail(base::PLATFORM_FILE_ERROR_EXISTS); + else + DidFinishFileOperation(rv); } -void FileSystemOperation::DidCreateFileNonExclusive( - base::PlatformFileError rv, - base::PassPlatformFile file, - bool created) { - // Suppress the already exists error and report success. - if (rv == base::PLATFORM_FILE_OK || - rv == base::PLATFORM_FILE_ERROR_EXISTS) - dispatcher_->DidSucceed(); - else - dispatcher_->DidFail(rv); +void FileSystemOperation::DidEnsureFileExistsNonExclusive( + base::PlatformFileError rv, bool /* created */) { + DidFinishFileOperation(rv); } void FileSystemOperation::DidFinishFileOperation( diff --git a/webkit/fileapi/file_system_operation.h b/webkit/fileapi/file_system_operation.h index bfa4141..6b4599a 100644 --- a/webkit/fileapi/file_system_operation.h +++ b/webkit/fileapi/file_system_operation.h @@ -82,13 +82,13 @@ class FileSystemOperation { scoped_refptr<base::MessageLoopProxy> proxy_; private: - // Callbacks for above methods. - void DidCreateFileExclusive( - base::PlatformFileError rv, base::PassPlatformFile file, bool created); + // Callback for CreateFile for |exclusive|=true cases. + void DidEnsureFileExistsExclusive(base::PlatformFileError rv, + bool created); - // Returns success even if the file already existed. - void DidCreateFileNonExclusive( - base::PlatformFileError rv, base::PassPlatformFile file, bool created); + // Callback for CreateFile for |exclusive|=false cases. + void DidEnsureFileExistsNonExclusive(base::PlatformFileError rv, + bool created); // Generic callback that translates platform errors to WebKit error codes. void DidFinishFileOperation(base::PlatformFileError rv); |