diff options
author | kinuko@chromium.org <kinuko@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-10-05 05:33:39 +0000 |
---|---|---|
committer | kinuko@chromium.org <kinuko@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-10-05 05:33:39 +0000 |
commit | d292409206bdfee695b72eae60279a15acb0acd8 (patch) | |
tree | 1991c7c6a67eb83973d8ae5cc1ec4c40ba025271 /base | |
parent | faec631688b3e0c87032d95a47cc945613431f45 (diff) | |
download | chromium_src-d292409206bdfee695b72eae60279a15acb0acd8.zip chromium_src-d292409206bdfee695b72eae60279a15acb0acd8.tar.gz chromium_src-d292409206bdfee695b72eae60279a15acb0acd8.tar.bz2 |
Support removeRecursively and new copy/move behaviors added to the spec recently.
http://lists.w3.org/Archives/Public/public-webapps/2010JulSep/1101.html
> For a move/copy of a file on top of existing file, or a directory on
> top of an existing empty directory, you get an automatic overwrite.
> A move/copy of a file on top of an existing directory, or of a
> directory on top of an existing file, will always fail.
> A move/copy of a file or directory on top of an existing non-empty
> directory will always fail.
BUG=32277
TEST=FileSystemOperationTest.*
Review URL: http://codereview.chromium.org/3567012
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@61480 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/file_util_proxy.cc | 125 | ||||
-rw-r--r-- | base/file_util_proxy.h | 5 | ||||
-rw-r--r-- | base/platform_file.h | 4 |
3 files changed, 75 insertions, 59 deletions
diff --git a/base/file_util_proxy.cc b/base/file_util_proxy.cc index d71f374..6d16043 100644 --- a/base/file_util_proxy.cc +++ b/base/file_util_proxy.cc @@ -10,6 +10,63 @@ // that all of the base:: prefixes would be unnecessary. namespace { +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). +static base::PlatformFileError PerformCommonCheckAndPreparationForMoveAndCopy( + const FilePath& src_file_path, + const FilePath& dest_file_path) { + // Exits earlier if the source path does not exist. + if (!file_util::PathExists(src_file_path)) + return base::PLATFORM_FILE_ERROR_NOT_FOUND; + + // The parent of the |dest_file_path| does not exist. + if (!file_util::DirectoryExists(dest_file_path.DirName())) + return base::PLATFORM_FILE_ERROR_NOT_FOUND; + + // It is an error to try to copy/move an entry into its child. + if (file_util::ContainsPath(src_file_path, dest_file_path)) + return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; + + // Now it is ok to return if the |dest_file_path| does not exist. + if (!file_util::PathExists(dest_file_path)) + return base::PLATFORM_FILE_OK; + + // |src_file_path| exists and is a directory. + // |dest_file_path| exists and is a file. + bool src_is_directory = file_util::DirectoryExists(src_file_path); + bool dest_is_directory = file_util::DirectoryExists(dest_file_path); + if (src_is_directory && !dest_is_directory) + return base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY; + + // |src_file_path| exists and is a file. + // |dest_file_path| exists and is a directory. + if (!src_is_directory && dest_is_directory) + return base::PLATFORM_FILE_ERROR_NOT_A_FILE; + + // It is an error to copy/move an entry into the same path. + if (src_file_path.value() == dest_file_path.value()) + return base::PLATFORM_FILE_ERROR_EXISTS; + + if (dest_is_directory) { + // It is an error to copy/move an entry to a non-empty directory. + // Otherwise the copy/move attempt must overwrite the destination, but + // the file_util's Copy or Move method doesn't perform overwrite + // on all platforms, so we delete the destination directory here. + // TODO(kinuko): may be better to change the file_util::{Copy,Move}. + if (!file_util::Delete(dest_file_path, false /* recursive */)) { + if (!file_util::IsDirectoryEmpty(dest_file_path)) + return base::PLATFORM_FILE_ERROR_NOT_EMPTY; + return base::PLATFORM_FILE_ERROR_FAILED; + } + } + return base::PLATFORM_FILE_OK; +} + +} // anonymous namespace + class MessageLoopRelay : public base::RefCountedThreadSafe<MessageLoopRelay> { public: @@ -205,7 +262,7 @@ class RelayDelete : public RelayWithStatusCallback { } if (!file_util::Delete(file_path_, recursive_)) { if (!recursive_ && !file_util::IsDirectoryEmpty(file_path_)) { - set_error_code(base::PLATFORM_FILE_ERROR_INVALID_OPERATION); + set_error_code(base::PLATFORM_FILE_ERROR_NOT_EMPTY); return; } set_error_code(base::PLATFORM_FILE_ERROR_FAILED); @@ -229,36 +286,13 @@ class RelayCopy : public RelayWithStatusCallback { protected: virtual void RunWork() { - bool dest_path_exists = file_util::PathExists(dest_file_path_); - if (!dest_path_exists && - !file_util::DirectoryExists(dest_file_path_.DirName())) { - set_error_code(base::PLATFORM_FILE_ERROR_NOT_FOUND); + set_error_code(PerformCommonCheckAndPreparationForMoveAndCopy( + src_file_path_, dest_file_path_)); + if (error_code() != base::PLATFORM_FILE_OK) return; - } - // |src_file_path| exists and is a directory. - // |dest_file_path| exists and is a file. - if (file_util::DirectoryExists(src_file_path_) && - dest_path_exists && !file_util::DirectoryExists(dest_file_path_)) { - set_error_code(base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY); - return; - } - if (file_util::ContainsPath(src_file_path_, dest_file_path_)) { - set_error_code(base::PLATFORM_FILE_ERROR_FAILED); - return; - } if (!file_util::CopyDirectory(src_file_path_, dest_file_path_, - true /* recursive */)) { - if (!file_util::PathExists(src_file_path_)) { - set_error_code(base::PLATFORM_FILE_ERROR_NOT_FOUND); - return; - } - if (src_file_path_.value() == dest_file_path_.value()) { - set_error_code(base::PLATFORM_FILE_ERROR_EXISTS); - return; - } - // Something else went wrong. + true /* recursive */)) set_error_code(base::PLATFORM_FILE_ERROR_FAILED); - } } private: @@ -278,36 +312,12 @@ class RelayMove : public RelayWithStatusCallback { protected: virtual void RunWork() { - bool dest_path_exists = file_util::PathExists(dest_file_path_); - if (!dest_path_exists && - !file_util::DirectoryExists(dest_file_path_.DirName())) { - set_error_code(base::PLATFORM_FILE_ERROR_NOT_FOUND); + set_error_code(PerformCommonCheckAndPreparationForMoveAndCopy( + src_file_path_, dest_file_path_)); + if (error_code() != base::PLATFORM_FILE_OK) return; - } - // |src_file_path| exists and is a directory. - // |dest_file_path| exists and is a file. - if (file_util::DirectoryExists(src_file_path_) && - dest_path_exists && - !file_util::DirectoryExists(dest_file_path_)) { - set_error_code(base::PLATFORM_FILE_ERROR_EXISTS); - return; - } - if (file_util::ContainsPath(src_file_path_, dest_file_path_)) { - set_error_code(base::PLATFORM_FILE_ERROR_INVALID_OPERATION); - return; - } - if (!file_util::Move(src_file_path_, dest_file_path_)) { - if (!file_util::PathExists(src_file_path_)) { - set_error_code(base::PLATFORM_FILE_ERROR_NOT_FOUND); - return; - } - if (src_file_path_.value() == dest_file_path_.value()) { - set_error_code(base::PLATFORM_FILE_ERROR_EXISTS); - return; - } - // Something else went wrong. + if (!file_util::Move(src_file_path_, dest_file_path_)) set_error_code(base::PLATFORM_FILE_ERROR_FAILED); - } } private: @@ -703,9 +713,10 @@ bool FileUtilProxy::Close(scoped_refptr<MessageLoopProxy> message_loop_proxy, // static bool FileUtilProxy::Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy, const FilePath& file_path, + bool recursive, StatusCallback* callback) { return Start(FROM_HERE, message_loop_proxy, - new RelayDelete(file_path, false, callback)); + new RelayDelete(file_path, recursive, callback)); } // static diff --git a/base/file_util_proxy.h b/base/file_util_proxy.h index 32b78630..e716ab7 100644 --- a/base/file_util_proxy.h +++ b/base/file_util_proxy.h @@ -89,6 +89,7 @@ class FileUtilProxy { // If destination file doesn't exist or destination's parent // doesn't exists. // If source dir exists but destination path is an existing file. + // If source file exists but destination path is an existing directory. // If source is a parent of destination. // If source doesn't exists. static bool Copy(scoped_refptr<MessageLoopProxy> message_loop_proxy, @@ -105,9 +106,11 @@ class FileUtilProxy { bool recursive, StatusCallback* callback); - // Deletes a file or empty directory. + // Deletes a file or a directory. + // It is an error to delete a non-empty directory with recursive=false. static bool Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy, const FilePath& file_path, + bool recursive, StatusCallback* callback); // Moves a file or a directory from src_file_path to dest_file_path. diff --git a/base/platform_file.h b/base/platform_file.h index 5024717..b350f8c 100644 --- a/base/platform_file.h +++ b/base/platform_file.h @@ -60,7 +60,9 @@ enum PlatformFileError { PLATFORM_FILE_ERROR_NOT_A_DIRECTORY = -9, PLATFORM_FILE_ERROR_INVALID_OPERATION = -10, PLATFORM_FILE_ERROR_SECURITY = -11, - PLATFORM_FILE_ERROR_ABORT = -12 + PLATFORM_FILE_ERROR_ABORT = -12, + PLATFORM_FILE_ERROR_NOT_A_FILE = -13, + PLATFORM_FILE_ERROR_NOT_EMPTY = -14, }; // Used to hold information about a given file. |