Chromium Code Reviews| Index: webkit/fileapi/file_util_delete_helper.cc |
| diff --git a/webkit/fileapi/file_util_delete_helper.cc b/webkit/fileapi/file_util_delete_helper.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0750146ff871a0f42ebe73018801bbb88ca702e6 |
| --- /dev/null |
| +++ b/webkit/fileapi/file_util_delete_helper.cc |
| @@ -0,0 +1,68 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "webkit/fileapi/file_util_delete_helper.h" |
| + |
| +#include "webkit/fileapi/file_system_file_util.h" |
| +#include "webkit/fileapi/file_system_operation_context.h" |
| +#include "webkit/fileapi/file_system_path.h" |
| + |
| +using base::PlatformFileError; |
| + |
| +namespace fileapi { |
| + |
| +FileUtilDeleteHelper::FileUtilDeleteHelper( |
| + FileSystemOperationContext* context, |
| + FileSystemFileUtil* file_util) |
| + : context_(context), |
| + file_util_(file_util) { |
| +} |
| + |
| +FileUtilDeleteHelper::~FileUtilDeleteHelper() { |
| +} |
| + |
| +base::PlatformFileError FileUtilDeleteHelper::DoWork(const FileSystemPath& path, |
| + bool recursive) { |
| + if (file_util_->DirectoryExists(context_, path)) { |
| + if (!recursive) |
| + return file_util_->DeleteSingleDirectory(context_, path); |
| + else |
| + return DeleteDirectoryRecursive(path); |
| + } else { |
| + return file_util_->DeleteFile(context_, path); |
| + } |
| +} |
| + |
| +PlatformFileError FileUtilDeleteHelper::DeleteDirectoryRecursive( |
| + const FileSystemPath& path) { |
| + scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> file_enum( |
| + file_util_->CreateFileEnumerator(context_, path)); |
| + FilePath file_path_each; |
| + std::stack<FilePath> directories; |
| + while (!(file_path_each = file_enum->Next()).empty()) { |
| + if (file_enum->IsDirectory()) { |
| + directories.push(file_path_each); |
| + } else { |
| + PlatformFileError error = file_util_->DeleteFile( |
| + context_, path.WithInternalPath(file_path_each)); |
| + if (error == base::PLATFORM_FILE_ERROR_NOT_FOUND) |
| + return base::PLATFORM_FILE_ERROR_FAILED; |
|
kinuko
2012/03/02 05:25:16
It makes me wonder we should just let it go here..
tzik
2012/03/02 19:09:09
Seems right. No test require it.
|
| + else if (error != base::PLATFORM_FILE_OK) |
| + return error; |
| + } |
| + } |
| + |
| + while (!directories.empty()) { |
| + PlatformFileError error = file_util_->DeleteSingleDirectory( |
| + context_, path.WithInternalPath(directories.top())); |
| + if (error == base::PLATFORM_FILE_ERROR_NOT_FOUND) |
| + return base::PLATFORM_FILE_ERROR_FAILED; |
| + else if (error != base::PLATFORM_FILE_OK) |
| + return error; |
| + directories.pop(); |
| + } |
| + return file_util_->DeleteSingleDirectory(context_, path); |
| +} |
| + |
| +} // namespace fileapi |