OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "webkit/fileapi/file_system_file_util.h" | 5 #include "webkit/fileapi/file_system_file_util.h" |
6 | 6 |
7 #include <stack> | 7 #include <stack> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/file_util_proxy.h" | 10 #include "base/file_util_proxy.h" |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
129 // http://code.google.com/p/chromium-os/issues/detail?id=15948 | 129 // http://code.google.com/p/chromium-os/issues/detail?id=15948 |
130 // This currently just prevents a file from showing up at all | 130 // This currently just prevents a file from showing up at all |
131 // if it's a link, hence preventing arbitary 'read' exploits. | 131 // if it's a link, hence preventing arbitary 'read' exploits. |
132 if (!file_util::IsLink(file_path.Append(entry.name))) | 132 if (!file_util::IsLink(file_path.Append(entry.name))) |
133 entries->push_back(entry); | 133 entries->push_back(entry); |
134 } | 134 } |
135 return base::PLATFORM_FILE_OK; | 135 return base::PLATFORM_FILE_OK; |
136 } | 136 } |
137 | 137 |
138 PlatformFileError FileSystemFileUtil::CreateDirectory( | 138 PlatformFileError FileSystemFileUtil::CreateDirectory( |
139 FileSystemOperationContext* fs_context, | 139 FileSystemOperationContext* unused, |
140 const FilePath& file_path, | 140 const FilePath& file_path, |
141 bool exclusive, | 141 bool exclusive, |
142 bool recursive) { | 142 bool recursive) { |
143 if (fs_context->do_not_write_actually()) | |
144 return base::PLATFORM_FILE_OK; | |
145 | |
146 // If parent dir of file doesn't exist. | 143 // If parent dir of file doesn't exist. |
147 if (!recursive && !file_util::PathExists(file_path.DirName())) | 144 if (!recursive && !file_util::PathExists(file_path.DirName())) |
148 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 145 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
149 | 146 |
150 bool path_exists = file_util::PathExists(file_path); | 147 bool path_exists = file_util::PathExists(file_path); |
151 if (exclusive && path_exists) | 148 if (exclusive && path_exists) |
152 return base::PLATFORM_FILE_ERROR_EXISTS; | 149 return base::PLATFORM_FILE_ERROR_EXISTS; |
153 | 150 |
154 // If file exists at the path. | 151 // If file exists at the path. |
155 if (path_exists && !file_util::DirectoryExists(file_path)) | 152 if (path_exists && !file_util::DirectoryExists(file_path)) |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
246 PlatformFileError | 243 PlatformFileError |
247 FileSystemFileUtil::PerformCommonCheckAndPreparationForMoveAndCopy( | 244 FileSystemFileUtil::PerformCommonCheckAndPreparationForMoveAndCopy( |
248 FileSystemOperationContext* context, | 245 FileSystemOperationContext* context, |
249 const FilePath& src_file_path, | 246 const FilePath& src_file_path, |
250 const FilePath& dest_file_path) { | 247 const FilePath& dest_file_path) { |
251 bool same_file_system = | 248 bool same_file_system = |
252 (context->src_origin_url() == context->dest_origin_url()) && | 249 (context->src_origin_url() == context->dest_origin_url()) && |
253 (context->src_type() == context->dest_type()); | 250 (context->src_type() == context->dest_type()); |
254 FileSystemFileUtil* dest_util = context->dest_file_system_file_util(); | 251 FileSystemFileUtil* dest_util = context->dest_file_system_file_util(); |
255 DCHECK(dest_util); | 252 DCHECK(dest_util); |
256 if (same_file_system) | 253 FileSystemOperationContext local_dest_context( |
254 context->file_system_context(), dest_util); | |
255 FileSystemOperationContext* dest_context; | |
256 if (same_file_system) { | |
257 dest_context = context; | |
257 DCHECK(context->src_file_system_file_util() == | 258 DCHECK(context->src_file_system_file_util() == |
258 context->dest_file_system_file_util()); | 259 context->dest_file_system_file_util()); |
259 // All the single-path virtual FSFU methods expect the context information | 260 } else { |
260 // to be in the src_* variables, not the dest_* variables, so we have to | 261 // All the single-path virtual FSFU methods expect the context information |
261 // make a new context if we want to call them on the dest_file_path. | 262 // to be in the src_* variables, not the dest_* variables, so we have to |
262 scoped_ptr<FileSystemOperationContext> dest_context( | 263 // make a new context if we want to call them on the dest_file_path. |
263 context->CreateInheritedContextForDest()); | 264 dest_context = &local_dest_context; |
265 dest_context->set_src_type(context->dest_type()); | |
266 dest_context->set_src_origin_url(context->dest_origin_url()); | |
267 dest_context->set_src_virtual_path(context->dest_virtual_path()); | |
268 dest_context->set_allowed_bytes_growth(context->allowed_bytes_growth()); | |
269 } | |
Dai Mikurube (NOT FULLTIME)
2011/08/12 05:17:54
I guess passing a correct context is better even i
| |
264 | 270 |
265 // Exits earlier if the source path does not exist. | 271 // Exits earlier if the source path does not exist. |
266 if (!PathExists(context, src_file_path)) | 272 if (!PathExists(context, src_file_path)) |
267 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 273 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
268 | 274 |
269 // The parent of the |dest_file_path| does not exist. | 275 // The parent of the |dest_file_path| does not exist. |
270 if (!ParentExists(dest_context.get(), dest_util, dest_file_path)) | 276 if (!ParentExists(dest_context, dest_util, dest_file_path)) |
271 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 277 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
272 | 278 |
273 // It is an error to try to copy/move an entry into its child. | 279 // It is an error to try to copy/move an entry into its child. |
274 if (same_file_system && src_file_path.IsParent(dest_file_path)) | 280 if (same_file_system && src_file_path.IsParent(dest_file_path)) |
275 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | 281 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; |
276 | 282 |
277 // Now it is ok to return if the |dest_file_path| does not exist. | 283 // Now it is ok to return if the |dest_file_path| does not exist. |
278 if (!dest_util->PathExists(dest_context.get(), dest_file_path)) | 284 if (!dest_util->PathExists(dest_context, dest_file_path)) |
279 return base::PLATFORM_FILE_OK; | 285 return base::PLATFORM_FILE_OK; |
280 | 286 |
281 // |src_file_path| exists and is a directory. | 287 // |src_file_path| exists and is a directory. |
282 // |dest_file_path| exists and is a file. | 288 // |dest_file_path| exists and is a file. |
283 bool src_is_directory = DirectoryExists(context, src_file_path); | 289 bool src_is_directory = DirectoryExists(context, src_file_path); |
284 bool dest_is_directory = | 290 bool dest_is_directory = |
285 dest_util->DirectoryExists(dest_context.get(), dest_file_path); | 291 dest_util->DirectoryExists(dest_context, dest_file_path); |
286 if (src_is_directory && !dest_is_directory) | 292 if (src_is_directory && !dest_is_directory) |
287 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | 293 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; |
288 | 294 |
289 // |src_file_path| exists and is a file. | 295 // |src_file_path| exists and is a file. |
290 // |dest_file_path| exists and is a directory. | 296 // |dest_file_path| exists and is a directory. |
291 if (!src_is_directory && dest_is_directory) | 297 if (!src_is_directory && dest_is_directory) |
292 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | 298 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; |
293 | 299 |
294 // It is an error to copy/move an entry into the same path. | 300 // It is an error to copy/move an entry into the same path. |
295 if (same_file_system && (src_file_path.value() == dest_file_path.value())) | 301 if (same_file_system && (src_file_path.value() == dest_file_path.value())) |
296 return base::PLATFORM_FILE_ERROR_EXISTS; | 302 return base::PLATFORM_FILE_ERROR_EXISTS; |
297 | 303 |
298 if (dest_is_directory) { | 304 if (dest_is_directory) { |
299 // It is an error to copy/move an entry to a non-empty directory. | 305 // It is an error to copy/move an entry to a non-empty directory. |
300 // Otherwise the copy/move attempt must overwrite the destination, but | 306 // Otherwise the copy/move attempt must overwrite the destination, but |
301 // the file_util's Copy or Move method doesn't perform overwrite | 307 // the file_util's Copy or Move method doesn't perform overwrite |
302 // on all platforms, so we delete the destination directory here. | 308 // on all platforms, so we delete the destination directory here. |
303 // TODO(kinuko): may be better to change the file_util::{Copy,Move}. | 309 // TODO(kinuko): may be better to change the file_util::{Copy,Move}. |
304 PlatformFileError error = dest_util->Delete( | 310 if (base::PLATFORM_FILE_OK != |
305 dest_context.get(), dest_file_path, false /* recursive */); | 311 dest_util->Delete(dest_context, dest_file_path, |
306 context->ImportAllowedBytesGrowth(*dest_context); | 312 false /* recursive */)) { |
307 if (base::PLATFORM_FILE_OK != error) { | 313 if (!dest_util->IsDirectoryEmpty(dest_context, dest_file_path)) |
308 if (!dest_util->IsDirectoryEmpty(dest_context.get(), dest_file_path)) | |
309 return base::PLATFORM_FILE_ERROR_NOT_EMPTY; | 314 return base::PLATFORM_FILE_ERROR_NOT_EMPTY; |
310 return base::PLATFORM_FILE_ERROR_FAILED; | 315 return base::PLATFORM_FILE_ERROR_FAILED; |
311 } | 316 } |
312 } | 317 } |
313 return base::PLATFORM_FILE_OK; | 318 return base::PLATFORM_FILE_OK; |
314 } | 319 } |
315 | 320 |
316 PlatformFileError FileSystemFileUtil::CopyOrMoveFile( | 321 PlatformFileError FileSystemFileUtil::CopyOrMoveFile( |
317 FileSystemOperationContext* unused, | 322 FileSystemOperationContext* unused, |
318 const FilePath& src_file_path, | 323 const FilePath& src_file_path, |
(...skipping 16 matching lines...) Expand all Loading... | |
335 const FilePath& dest_file_path) { | 340 const FilePath& dest_file_path) { |
336 return CopyOrMoveFile(context, src_file_path, dest_file_path, true); | 341 return CopyOrMoveFile(context, src_file_path, dest_file_path, true); |
337 } | 342 } |
338 | 343 |
339 PlatformFileError FileSystemFileUtil::CopyOrMoveDirectory( | 344 PlatformFileError FileSystemFileUtil::CopyOrMoveDirectory( |
340 FileSystemOperationContext* context, | 345 FileSystemOperationContext* context, |
341 const FilePath& src_file_path, | 346 const FilePath& src_file_path, |
342 const FilePath& dest_file_path, | 347 const FilePath& dest_file_path, |
343 bool copy) { | 348 bool copy) { |
344 FileSystemFileUtil* dest_util = context->dest_file_system_file_util(); | 349 FileSystemFileUtil* dest_util = context->dest_file_system_file_util(); |
345 scoped_ptr<FileSystemOperationContext> dest_context( | 350 FileSystemOperationContext dest_context( |
346 context->CreateInheritedContextForDest()); | 351 context->file_system_context(), dest_util); |
352 // All the single-path virtual FSFU methods expect the context information to | |
353 // be in the src_* variables, not the dest_* variables, so we have to make a | |
354 // new context if we want to call them on the dest_file_path. | |
355 dest_context.set_src_type(context->dest_type()); | |
356 dest_context.set_src_origin_url(context->dest_origin_url()); | |
357 dest_context.set_src_virtual_path(context->dest_virtual_path()); | |
358 dest_context.set_allowed_bytes_growth(context->allowed_bytes_growth()); | |
347 | 359 |
348 // Re-check PerformCommonCheckAndPreparationForMoveAndCopy() by DCHECK. | 360 // Re-check PerformCommonCheckAndPreparationForMoveAndCopy() by DCHECK. |
349 DCHECK(DirectoryExists(context, src_file_path)); | 361 DCHECK(DirectoryExists(context, src_file_path)); |
350 DCHECK(ParentExists(dest_context.get(), dest_util, dest_file_path)); | 362 DCHECK(ParentExists(&dest_context, dest_util, dest_file_path)); |
351 DCHECK(!dest_util->PathExists(dest_context.get(), dest_file_path)); | 363 DCHECK(!dest_util->PathExists(&dest_context, dest_file_path)); |
352 if ((context->src_origin_url() == context->dest_origin_url()) && | 364 if ((context->src_origin_url() == context->dest_origin_url()) && |
353 (context->src_type() == context->dest_type())) | 365 (context->src_type() == context->dest_type())) |
354 DCHECK(!src_file_path.IsParent(dest_file_path)); | 366 DCHECK(!src_file_path.IsParent(dest_file_path)); |
355 | 367 |
356 if (!dest_util->DirectoryExists(dest_context.get(), dest_file_path)) { | 368 if (!dest_util->DirectoryExists(&dest_context, dest_file_path)) { |
357 PlatformFileError error = dest_util->CreateDirectory(dest_context.get(), | 369 PlatformFileError error = dest_util->CreateDirectory(&dest_context, |
358 dest_file_path, false, false); | 370 dest_file_path, false, false); |
359 context->ImportAllowedBytesGrowth(*dest_context); | |
360 if (error != base::PLATFORM_FILE_OK) | 371 if (error != base::PLATFORM_FILE_OK) |
361 return error; | 372 return error; |
362 } | 373 } |
363 | 374 |
364 scoped_ptr<AbstractFileEnumerator> file_enum( | 375 scoped_ptr<AbstractFileEnumerator> file_enum( |
365 CreateFileEnumerator(context, src_file_path)); | 376 CreateFileEnumerator(context, src_file_path)); |
366 FilePath src_file_path_each; | 377 FilePath src_file_path_each; |
367 while (!(src_file_path_each = file_enum->Next()).empty()) { | 378 while (!(src_file_path_each = file_enum->Next()).empty()) { |
368 FilePath dest_file_path_each(dest_file_path); | 379 FilePath dest_file_path_each(dest_file_path); |
369 src_file_path.AppendRelativePath(src_file_path_each, &dest_file_path_each); | 380 src_file_path.AppendRelativePath(src_file_path_each, &dest_file_path_each); |
370 | 381 |
371 if (file_enum->IsDirectory()) { | 382 if (file_enum->IsDirectory()) { |
372 scoped_ptr<FileSystemOperationContext> new_directory_context( | 383 PlatformFileError error = dest_util->CreateDirectory(&dest_context, |
373 dest_context->CreateInheritedContextWithNewVirtualPaths( | 384 dest_file_path_each, false, false); |
Dai Mikurube (NOT FULLTIME)
2011/08/12 05:17:54
ditto. The function names (CreateInherited..., Im
ericu
2011/08/15 23:47:10
I agree with you about putting CreateInheritedCont
Dai Mikurube (NOT FULLTIME)
2011/08/16 05:11:00
Ah, make sense. I agree with taking *_virtual_pat
| |
374 dest_file_path_each, FilePath())); | |
375 PlatformFileError error = dest_util->CreateDirectory( | |
376 new_directory_context.get(), dest_file_path_each, false, false); | |
377 context->ImportAllowedBytesGrowth(*new_directory_context); | |
378 if (error != base::PLATFORM_FILE_OK) | 385 if (error != base::PLATFORM_FILE_OK) |
379 return error; | 386 return error; |
380 } else { | 387 } else { |
381 scoped_ptr<FileSystemOperationContext> copy_context( | |
382 context->CreateInheritedContextWithNewVirtualPaths( | |
383 src_file_path_each, dest_file_path_each)); | |
384 PlatformFileError error = CopyOrMoveFileHelper( | 388 PlatformFileError error = CopyOrMoveFileHelper( |
385 copy_context.get(), src_file_path_each, dest_file_path_each, copy); | 389 context, src_file_path_each, dest_file_path_each, copy); |
386 context->ImportAllowedBytesGrowth(*copy_context); | |
387 if (error != base::PLATFORM_FILE_OK) | 390 if (error != base::PLATFORM_FILE_OK) |
388 return error; | 391 return error; |
389 } | 392 } |
390 } | 393 } |
391 | 394 |
392 if (!copy) { | 395 if (!copy) { |
393 PlatformFileError error = Delete(context, src_file_path, true); | 396 PlatformFileError error = Delete(context, src_file_path, true); |
394 if (error != base::PLATFORM_FILE_OK) | 397 if (error != base::PLATFORM_FILE_OK) |
395 return error; | 398 return error; |
396 } | 399 } |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
462 scoped_ptr<AbstractFileEnumerator> file_enum( | 465 scoped_ptr<AbstractFileEnumerator> file_enum( |
463 CreateFileEnumerator(context, file_path)); | 466 CreateFileEnumerator(context, file_path)); |
464 FilePath file_path_each; | 467 FilePath file_path_each; |
465 | 468 |
466 std::stack<FilePath> directories; | 469 std::stack<FilePath> directories; |
467 while (!(file_path_each = file_enum->Next()).empty()) { | 470 while (!(file_path_each = file_enum->Next()).empty()) { |
468 if (file_enum->IsDirectory()) { | 471 if (file_enum->IsDirectory()) { |
469 directories.push(file_path_each); | 472 directories.push(file_path_each); |
470 } else { | 473 } else { |
471 // DeleteFile here is the virtual overridden member function. | 474 // DeleteFile here is the virtual overridden member function. |
472 scoped_ptr<FileSystemOperationContext> inherited_context( | 475 PlatformFileError error = DeleteFile(context, file_path_each); |
473 context->CreateInheritedContextWithNewVirtualPaths( | |
474 file_path_each, FilePath())); | |
475 PlatformFileError error = | |
476 DeleteFile(inherited_context.get(), file_path_each); | |
477 context->ImportAllowedBytesGrowth(*inherited_context); | |
478 if (error == base::PLATFORM_FILE_ERROR_NOT_FOUND) | 476 if (error == base::PLATFORM_FILE_ERROR_NOT_FOUND) |
479 return base::PLATFORM_FILE_ERROR_FAILED; | 477 return base::PLATFORM_FILE_ERROR_FAILED; |
480 else if (error != base::PLATFORM_FILE_OK) | 478 else if (error != base::PLATFORM_FILE_OK) |
481 return error; | 479 return error; |
482 } | 480 } |
483 } | 481 } |
484 | 482 |
485 while (!directories.empty()) { | 483 while (!directories.empty()) { |
486 scoped_ptr<FileSystemOperationContext> inherited_context( | 484 PlatformFileError error = DeleteSingleDirectory(context, directories.top()); |
487 context->CreateInheritedContextWithNewVirtualPaths( | |
488 directories.top(), FilePath())); | |
489 PlatformFileError error = | |
490 DeleteSingleDirectory(inherited_context.get(), directories.top()); | |
491 context->ImportAllowedBytesGrowth(*inherited_context); | |
492 if (error == base::PLATFORM_FILE_ERROR_NOT_FOUND) | 485 if (error == base::PLATFORM_FILE_ERROR_NOT_FOUND) |
493 return base::PLATFORM_FILE_ERROR_FAILED; | 486 return base::PLATFORM_FILE_ERROR_FAILED; |
494 else if (error != base::PLATFORM_FILE_OK) | 487 else if (error != base::PLATFORM_FILE_OK) |
495 return error; | 488 return error; |
496 directories.pop(); | 489 directories.pop(); |
497 } | 490 } |
498 return DeleteSingleDirectory(context, file_path); | 491 return DeleteSingleDirectory(context, file_path); |
499 } | 492 } |
500 | 493 |
501 bool FileSystemFileUtil::PathExists( | 494 bool FileSystemFileUtil::PathExists( |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
548 FileSystemFileUtil::CreateFileEnumerator( | 541 FileSystemFileUtil::CreateFileEnumerator( |
549 FileSystemOperationContext* unused, | 542 FileSystemOperationContext* unused, |
550 const FilePath& root_path) { | 543 const FilePath& root_path) { |
551 return new FileSystemFileEnumerator( | 544 return new FileSystemFileEnumerator( |
552 root_path, true, static_cast<file_util::FileEnumerator::FILE_TYPE>( | 545 root_path, true, static_cast<file_util::FileEnumerator::FILE_TYPE>( |
553 file_util::FileEnumerator::FILES | | 546 file_util::FileEnumerator::FILES | |
554 file_util::FileEnumerator::DIRECTORIES)); | 547 file_util::FileEnumerator::DIRECTORIES)); |
555 } | 548 } |
556 | 549 |
557 } // namespace fileapi | 550 } // namespace fileapi |
OLD | NEW |