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

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

Issue 6604020: Introduce FileSystemFileUtil and -Proxy to decorate base::file_util in webkit/fileapi. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Made file_system_operation_context_ a simple variable (replaced from not scoped_ptr.) Created 9 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
« no previous file with comments | « webkit/fileapi/file_system_file_util.h ('k') | webkit/fileapi/file_system_file_util_proxy.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2011 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/file_system_file_util.h"
6
7 #include "base/file_util_proxy.h"
8
9 // This also removes the destination directory if it's non-empty and all other
10 // checks are passed (so that the copy/move correctly overwrites the
11 // destination).
12 // TODO(ericu, dmikurube): This method won't work with obfuscation and quota
13 // since all (file_util::) operations should consider obfuscation and quota.
14 // We will need to virtualize all these calls. We should do that by making this
15 // method a non-virtual member of FileSystemFileUtil, but changing all of its
16 // file_util calls to be FileSystemFileUtil calls. That way when we override
17 // them for obfuscation or quota, it'll just work.
18 static base::PlatformFileError PerformCommonCheckAndPreparationForMoveAndCopy(
19 const FilePath& src_file_path,
20 const FilePath& dest_file_path) {
21 // Exits earlier if the source path does not exist.
22 if (!file_util::PathExists(src_file_path))
23 return base::PLATFORM_FILE_ERROR_NOT_FOUND;
24
25 // The parent of the |dest_file_path| does not exist.
26 if (!file_util::DirectoryExists(dest_file_path.DirName()))
27 return base::PLATFORM_FILE_ERROR_NOT_FOUND;
28
29 // It is an error to try to copy/move an entry into its child.
30 if (src_file_path.IsParent(dest_file_path))
31 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION;
32
33 // Now it is ok to return if the |dest_file_path| does not exist.
34 if (!file_util::PathExists(dest_file_path))
35 return base::PLATFORM_FILE_OK;
36
37 // |src_file_path| exists and is a directory.
38 // |dest_file_path| exists and is a file.
39 bool src_is_directory = file_util::DirectoryExists(src_file_path);
40 bool dest_is_directory = file_util::DirectoryExists(dest_file_path);
41 if (src_is_directory && !dest_is_directory)
42 return base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY;
43
44 // |src_file_path| exists and is a file.
45 // |dest_file_path| exists and is a directory.
46 if (!src_is_directory && dest_is_directory)
47 return base::PLATFORM_FILE_ERROR_NOT_A_FILE;
48
49 // It is an error to copy/move an entry into the same path.
50 if (src_file_path.value() == dest_file_path.value())
51 return base::PLATFORM_FILE_ERROR_EXISTS;
52
53 if (dest_is_directory) {
54 // It is an error to copy/move an entry to a non-empty directory.
55 // Otherwise the copy/move attempt must overwrite the destination, but
56 // the file_util's Copy or Move method doesn't perform overwrite
57 // on all platforms, so we delete the destination directory here.
58 // TODO(kinuko): may be better to change the file_util::{Copy,Move}.
59 if (!file_util::Delete(dest_file_path, false /* recursive */)) {
60 if (!file_util::IsDirectoryEmpty(dest_file_path))
61 return base::PLATFORM_FILE_ERROR_NOT_EMPTY;
62 return base::PLATFORM_FILE_ERROR_FAILED;
63 }
64 }
65 return base::PLATFORM_FILE_OK;
66 }
67
68 namespace fileapi {
69
70 FileSystemFileUtil* FileSystemFileUtil::GetInstance() {
71 return Singleton<FileSystemFileUtil>::get();
72 }
73
74 PlatformFileError FileSystemFileUtil::CreateOrOpen(
75 FileSystemOperationContext* unused,
76 const FilePath& file_path, int file_flags,
77 PlatformFile* file_handle, bool* created) {
78 if (!file_util::DirectoryExists(file_path.DirName())) {
79 // If its parent does not exist, should return NOT_FOUND error.
80 return base::PLATFORM_FILE_ERROR_NOT_FOUND;
81 }
82 PlatformFileError error_code = base::PLATFORM_FILE_OK;
83 *file_handle = base::CreatePlatformFile(file_path, file_flags,
84 created, &error_code);
85 return error_code;
86 }
87
88 PlatformFileError FileSystemFileUtil::Close(
89 FileSystemOperationContext* unused,
90 PlatformFile file_handle) {
91 if (!base::ClosePlatformFile(file_handle))
92 return base::PLATFORM_FILE_ERROR_FAILED;
93 return base::PLATFORM_FILE_OK;
94 }
95
96 PlatformFileError FileSystemFileUtil::EnsureFileExists(
97 FileSystemOperationContext* unused,
98 const FilePath& file_path,
99 bool* created) {
100 if (!file_util::DirectoryExists(file_path.DirName()))
101 // If its parent does not exist, should return NOT_FOUND error.
102 return base::PLATFORM_FILE_ERROR_NOT_FOUND;
103 PlatformFileError error_code = base::PLATFORM_FILE_OK;
104 // Tries to create the |file_path| exclusively. This should fail
105 // with base::PLATFORM_FILE_ERROR_EXISTS if the path already exists.
106 PlatformFile handle = base::CreatePlatformFile(
107 file_path,
108 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_READ,
109 created, &error_code);
110 if (error_code == base::PLATFORM_FILE_ERROR_EXISTS) {
111 // Make sure created_ is false.
112 *created = false;
113 error_code = base::PLATFORM_FILE_OK;
114 }
115 if (handle != base::kInvalidPlatformFileValue)
116 base::ClosePlatformFile(handle);
117 return error_code;
118 }
119
120 PlatformFileError FileSystemFileUtil::GetFileInfo(
121 FileSystemOperationContext* unused,
122 const FilePath& file_path,
123 base::PlatformFileInfo* file_info) {
124 if (!file_util::PathExists(file_path))
125 return base::PLATFORM_FILE_ERROR_NOT_FOUND;
126 if (!file_util::GetFileInfo(file_path, file_info))
127 return base::PLATFORM_FILE_ERROR_FAILED;
128 return base::PLATFORM_FILE_OK;
129 }
130
131 PlatformFileError FileSystemFileUtil::ReadDirectory(
132 FileSystemOperationContext* unused,
133 const FilePath& file_path,
134 std::vector<base::FileUtilProxy::Entry>* entries) {
135 // TODO(kkanetkar): Implement directory read in multiple chunks.
136 if (!file_util::DirectoryExists(file_path))
137 return base::PLATFORM_FILE_ERROR_NOT_FOUND;
138
139 file_util::FileEnumerator file_enum(
140 file_path, false, static_cast<file_util::FileEnumerator::FILE_TYPE>(
141 file_util::FileEnumerator::FILES |
142 file_util::FileEnumerator::DIRECTORIES));
143 FilePath current;
144 while (!(current = file_enum.Next()).empty()) {
145 base::FileUtilProxy::Entry entry;
146 file_util::FileEnumerator::FindInfo info;
147 file_enum.GetFindInfo(&info);
148 entry.is_directory = file_enum.IsDirectory(info);
149 // This will just give the entry's name instead of entire path
150 // if we use current.value().
151 entry.name = file_util::FileEnumerator::GetFilename(info).value();
152 entries->push_back(entry);
153 }
154 return base::PLATFORM_FILE_OK;
155 }
156
157 PlatformFileError FileSystemFileUtil::CreateDirectory(
158 FileSystemOperationContext* unused,
159 const FilePath& file_path,
160 bool exclusive) {
161 bool path_exists = file_util::PathExists(file_path);
162 if (!file_util::PathExists(file_path.DirName()))
163 return base::PLATFORM_FILE_ERROR_NOT_FOUND;
164 // If parent dir of file doesn't exist.
165 if (exclusive && path_exists)
166 return base::PLATFORM_FILE_ERROR_EXISTS;
167 // If file exists at the path.
168 if (path_exists && !file_util::DirectoryExists(file_path))
169 return base::PLATFORM_FILE_ERROR_EXISTS;
170 if (!file_util::CreateDirectory(file_path))
171 return base::PLATFORM_FILE_ERROR_FAILED;
172 return base::PLATFORM_FILE_OK;
173 }
174
175 PlatformFileError FileSystemFileUtil::Copy(
176 FileSystemOperationContext* unused,
177 const FilePath& src_file_path,
178 const FilePath& dest_file_path) {
179 PlatformFileError error_code;
180 error_code =
181 PerformCommonCheckAndPreparationForMoveAndCopy(
182 src_file_path, dest_file_path);
183 if (error_code != base::PLATFORM_FILE_OK)
184 return error_code;
185 if (!file_util::CopyDirectory(src_file_path, dest_file_path,
186 true /* recursive */))
187 return base::PLATFORM_FILE_ERROR_FAILED;
188 return base::PLATFORM_FILE_OK;
189 }
190
191 PlatformFileError FileSystemFileUtil::Move(
192 FileSystemOperationContext* unused,
193 const FilePath& src_file_path,
194 const FilePath& dest_file_path) {
195 PlatformFileError error_code;
196 error_code =
197 PerformCommonCheckAndPreparationForMoveAndCopy(
198 src_file_path, dest_file_path);
199 if (error_code != base::PLATFORM_FILE_OK)
200 return error_code;
201 if (!file_util::Move(src_file_path, dest_file_path))
202 return base::PLATFORM_FILE_ERROR_FAILED;
203 return base::PLATFORM_FILE_OK;
204 }
205
206 PlatformFileError FileSystemFileUtil::Delete(
207 FileSystemOperationContext* unused,
208 const FilePath& file_path,
209 bool recursive) {
210 if (!file_util::PathExists(file_path)) {
211 return base::PLATFORM_FILE_ERROR_NOT_FOUND;
212 }
213 if (!file_util::Delete(file_path, recursive)) {
214 if (!recursive && !file_util::IsDirectoryEmpty(file_path)) {
215 return base::PLATFORM_FILE_ERROR_NOT_EMPTY;
216 }
217 return base::PLATFORM_FILE_ERROR_FAILED;
218 }
219 return base::PLATFORM_FILE_OK;
220 }
221
222 PlatformFileError FileSystemFileUtil::Touch(
223 FileSystemOperationContext* unused,
224 const FilePath& file_path,
225 const base::Time& last_access_time,
226 const base::Time& last_modified_time) {
227 if (!file_util::TouchFile(
228 file_path, last_access_time, last_modified_time))
229 return base::PLATFORM_FILE_ERROR_FAILED;
230 return base::PLATFORM_FILE_OK;
231 }
232
233 PlatformFileError FileSystemFileUtil::Truncate(
234 FileSystemOperationContext* unused,
235 const FilePath& file_path,
236 int64 length) {
237 PlatformFileError error_code(base::PLATFORM_FILE_ERROR_FAILED);
238 PlatformFile file =
239 base::CreatePlatformFile(
240 file_path,
241 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
242 NULL,
243 &error_code);
244 if (error_code != base::PLATFORM_FILE_OK) {
245 return error_code;
246 }
247 if (!base::TruncatePlatformFile(file, length))
248 error_code = base::PLATFORM_FILE_ERROR_FAILED;
249 base::ClosePlatformFile(file);
250 return error_code;
251 }
252
253 } // namespace fileapi
OLDNEW
« no previous file with comments | « webkit/fileapi/file_system_file_util.h ('k') | webkit/fileapi/file_system_file_util_proxy.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698