Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(469)

Side by Side Diff: webkit/fileapi/cross_file_util_helper.cc

Issue 9616033: Merge CrossFileUtilHelper to FileUtilHelper as Copy() and Move(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: test fix Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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
5 #include "webkit/fileapi/cross_file_util_helper.h"
6
7 #include "webkit/fileapi/file_system_file_util.h"
8 #include "webkit/fileapi/file_system_operation_context.h"
9 #include "webkit/fileapi/file_system_path.h"
10 #include "webkit/fileapi/file_util_helper.h"
11
12 using base::PlatformFileError;
13
14 namespace fileapi {
15
16 CrossFileUtilHelper::CrossFileUtilHelper(
17 FileSystemOperationContext* context,
18 FileSystemFileUtil* src_util,
19 FileSystemFileUtil* dest_util,
20 const FileSystemPath& src_path,
21 const FileSystemPath& dest_path,
22 Operation operation)
23 : context_(context),
24 src_util_(src_util),
25 dest_util_(dest_util),
26 src_root_path_(src_path),
27 dest_root_path_(dest_path),
28 operation_(operation) {}
29
30 CrossFileUtilHelper::~CrossFileUtilHelper() {}
31
32 base::PlatformFileError CrossFileUtilHelper::DoWork() {
33 base::PlatformFileError error =
34 PerformErrorCheckAndPreparationForMoveAndCopy();
35 if (error != base::PLATFORM_FILE_OK)
36 return error;
37 if (src_util_->DirectoryExists(context_, src_root_path_)) {
38 return CopyOrMoveDirectory(src_root_path_, dest_root_path_);
39 }
40 return CopyOrMoveFile(src_root_path_, dest_root_path_);
41 }
42
43 PlatformFileError
44 CrossFileUtilHelper::PerformErrorCheckAndPreparationForMoveAndCopy() {
45 // Exits earlier if the source path does not exist.
46 if (!src_util_->PathExists(context_, src_root_path_))
47 return base::PLATFORM_FILE_ERROR_NOT_FOUND;
48
49 bool same_file_system =
50 (src_root_path_.origin() == dest_root_path_.origin()) &&
51 (src_root_path_.type() == dest_root_path_.type());
52
53 // The parent of the |dest_root_path_| does not exist.
54 if (!ParentExists(dest_root_path_, dest_util_))
55 return base::PLATFORM_FILE_ERROR_NOT_FOUND;
56
57 // It is an error to try to copy/move an entry into its child.
58 if (same_file_system && src_root_path_.IsParent(dest_root_path_))
59 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION;
60
61 // Now it is ok to return if the |dest_root_path_| does not exist.
62 if (!dest_util_->PathExists(context_, dest_root_path_))
63 return base::PLATFORM_FILE_OK;
64
65 // |src_root_path_| exists and is a directory.
66 // |dest_root_path_| exists and is a file.
67 bool src_is_directory = src_util_->DirectoryExists(context_, src_root_path_);
68 bool dest_is_directory =
69 dest_util_->DirectoryExists(context_, dest_root_path_);
70
71 // Either one of |src_root_path_| or |dest_root_path_| is directory,
72 // while the other is not.
73 if (src_is_directory != dest_is_directory)
74 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION;
75
76 // It is an error to copy/move an entry into the same path.
77 if (same_file_system && (src_root_path_.internal_path() ==
78 dest_root_path_.internal_path()))
79 return base::PLATFORM_FILE_ERROR_EXISTS;
80
81 if (dest_is_directory) {
82 // It is an error to copy/move an entry to a non-empty directory.
83 // Otherwise the copy/move attempt must overwrite the destination, but
84 // the file_util's Copy or Move method doesn't perform overwrite
85 // on all platforms, so we delete the destination directory here.
86 if (base::PLATFORM_FILE_OK !=
87 dest_util_->DeleteSingleDirectory(context_, dest_root_path_)) {
88 if (!dest_util_->IsDirectoryEmpty(context_, dest_root_path_))
89 return base::PLATFORM_FILE_ERROR_NOT_EMPTY;
90 return base::PLATFORM_FILE_ERROR_FAILED;
91 }
92 }
93 return base::PLATFORM_FILE_OK;
94 }
95
96 bool CrossFileUtilHelper::ParentExists(
97 const FileSystemPath& path, FileSystemFileUtil* file_util) {
98 // If path is in the root, path.DirName() will be ".",
99 // since we use paths with no leading '/'.
100 FilePath parent = path.internal_path().DirName();
101 if (parent == FilePath(FILE_PATH_LITERAL(".")))
102 return true;
103 return file_util->DirectoryExists(
104 context_, path.WithInternalPath(parent));
105 }
106
107 PlatformFileError CrossFileUtilHelper::CopyOrMoveDirectory(
108 const FileSystemPath& src_path,
109 const FileSystemPath& dest_path) {
110 // At this point we must have gone through
111 // PerformErrorCheckAndPreparationForMoveAndCopy so this must be true.
112 DCHECK(!((src_path.origin() == dest_path.origin()) &&
113 (src_path.type() == dest_path.type())) ||
114 !src_path.IsParent(dest_path));
115
116 PlatformFileError error = dest_util_->CreateDirectory(
117 context_, dest_path, false, false);
118 if (error != base::PLATFORM_FILE_OK)
119 return error;
120
121 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> file_enum(
122 src_util_->CreateFileEnumerator(context_, src_path));
123 FilePath src_file_path_each;
124 while (!(src_file_path_each = file_enum->Next()).empty()) {
125 FilePath dest_file_path_each(dest_path.internal_path());
126 src_path.internal_path().AppendRelativePath(
127 src_file_path_each, &dest_file_path_each);
128
129 if (file_enum->IsDirectory()) {
130 PlatformFileError error = dest_util_->CreateDirectory(
131 context_,
132 dest_path.WithInternalPath(dest_file_path_each),
133 true /* exclusive */, false /* recursive */);
134 if (error != base::PLATFORM_FILE_OK)
135 return error;
136 } else {
137 PlatformFileError error = CopyOrMoveFile(
138 src_path.WithInternalPath(src_file_path_each),
139 dest_path.WithInternalPath(dest_file_path_each));
140 if (error != base::PLATFORM_FILE_OK)
141 return error;
142 }
143 }
144
145 if (operation_ == OPERATION_MOVE) {
146 PlatformFileError error =
147 FileUtilHelper::Delete(context_, src_util_,
148 src_path, true /* recursive */);
149 if (error != base::PLATFORM_FILE_OK)
150 return error;
151 }
152
153 return base::PLATFORM_FILE_OK;
154 }
155
156 PlatformFileError CrossFileUtilHelper::CopyOrMoveFile(
157 const FileSystemPath& src_path,
158 const FileSystemPath& dest_path) {
159 if ((src_path.origin() == dest_path.origin()) &&
160 (src_path.type() == dest_path.type())) {
161 DCHECK(src_util_ == dest_util_);
162 // Source and destination are in the same FileSystemFileUtil; now we can
163 // safely call FileSystemFileUtil method on src_util_ (== dest_util_).
164 return src_util_->CopyOrMoveFile(context_, src_path, dest_path,
165 operation_ == OPERATION_COPY);
166 }
167
168 // Resolve the src_path's underlying file path.
169 base::PlatformFileInfo file_info;
170 FilePath platform_file_path;
171 PlatformFileError error = src_util_->GetFileInfo(
172 context_, src_path, &file_info, &platform_file_path);
173 if (error != base::PLATFORM_FILE_OK)
174 return error;
175
176 // Call CopyInForeignFile() on the dest_util_ with the resolved source path
177 // to perform limited cross-FileSystemFileUtil copy/move.
178 error = dest_util_->CopyInForeignFile(
179 context_, src_path.WithInternalPath(platform_file_path), dest_path);
180
181 if (operation_ == OPERATION_COPY || error != base::PLATFORM_FILE_OK)
182 return error;
183 return src_util_->DeleteFile(context_, src_path);
184 }
185
186 } // namespace fileapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698