| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 | 8 |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "webkit/fileapi/file_system_context.h" | 10 #include "webkit/fileapi/file_system_context.h" |
| 11 #include "webkit/fileapi/file_system_operation_context.h" | 11 #include "webkit/fileapi/file_system_operation_context.h" |
| 12 | 12 |
| 13 namespace fileapi { | 13 namespace fileapi { |
| 14 | 14 |
| 15 FileSystemFileUtil::FileSystemFileUtil() { | 15 FileSystemFileUtil::FileSystemFileUtil() { |
| 16 } | 16 } |
| 17 | 17 |
| 18 FileSystemFileUtil::FileSystemFileUtil(FileSystemFileUtil* underlying_file_util) | 18 FileSystemFileUtil::FileSystemFileUtil(FileSystemFileUtil* underlying_file_util) |
| 19 : underlying_file_util_(underlying_file_util) { | 19 : underlying_file_util_(underlying_file_util) { |
| 20 } | 20 } |
| 21 | 21 |
| 22 FileSystemFileUtil::~FileSystemFileUtil() { | 22 FileSystemFileUtil::~FileSystemFileUtil() { |
| 23 } | 23 } |
| 24 | 24 |
| 25 PlatformFileError FileSystemFileUtil::Delete( | |
| 26 FileSystemOperationContext* context, | |
| 27 const FileSystemPath& path, | |
| 28 bool recursive) { | |
| 29 if (DirectoryExists(context, path)) { | |
| 30 if (!recursive) | |
| 31 return DeleteSingleDirectory(context, path); | |
| 32 else | |
| 33 return DeleteDirectoryRecursive(context, path); | |
| 34 } else { | |
| 35 return DeleteFile(context, path); | |
| 36 } | |
| 37 } | |
| 38 | |
| 39 PlatformFileError FileSystemFileUtil::CreateOrOpen( | 25 PlatformFileError FileSystemFileUtil::CreateOrOpen( |
| 40 FileSystemOperationContext* context, | 26 FileSystemOperationContext* context, |
| 41 const FileSystemPath& path, int file_flags, | 27 const FileSystemPath& path, int file_flags, |
| 42 PlatformFile* file_handle, bool* created) { | 28 PlatformFile* file_handle, bool* created) { |
| 43 if (underlying_file_util_.get()) { | 29 if (underlying_file_util_.get()) { |
| 44 return underlying_file_util_->CreateOrOpen( | 30 return underlying_file_util_->CreateOrOpen( |
| 45 context, path, file_flags, file_handle, created); | 31 context, path, file_flags, file_handle, created); |
| 46 } | 32 } |
| 47 NOTREACHED() << "Subclasses must provide implementation if they have no" | 33 NOTREACHED() << "Subclasses must provide implementation if they have no" |
| 48 << "underlying_file_util"; | 34 << "underlying_file_util"; |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 FileSystemOperationContext* context, | 225 FileSystemOperationContext* context, |
| 240 const FileSystemPath& path) { | 226 const FileSystemPath& path) { |
| 241 if (underlying_file_util_.get()) { | 227 if (underlying_file_util_.get()) { |
| 242 return underlying_file_util_->DeleteSingleDirectory(context, path); | 228 return underlying_file_util_->DeleteSingleDirectory(context, path); |
| 243 } | 229 } |
| 244 NOTREACHED() << "Subclasses must provide implementation if they have no" | 230 NOTREACHED() << "Subclasses must provide implementation if they have no" |
| 245 << "underlying_file_util"; | 231 << "underlying_file_util"; |
| 246 return base::PLATFORM_FILE_ERROR_FAILED; | 232 return base::PLATFORM_FILE_ERROR_FAILED; |
| 247 } | 233 } |
| 248 | 234 |
| 249 PlatformFileError FileSystemFileUtil::DeleteDirectoryRecursive( | |
| 250 FileSystemOperationContext* context, | |
| 251 const FileSystemPath& path) { | |
| 252 scoped_ptr<AbstractFileEnumerator> file_enum( | |
| 253 CreateFileEnumerator(context, path)); | |
| 254 FilePath file_path_each; | |
| 255 std::stack<FilePath> directories; | |
| 256 while (!(file_path_each = file_enum->Next()).empty()) { | |
| 257 if (file_enum->IsDirectory()) { | |
| 258 directories.push(file_path_each); | |
| 259 } else { | |
| 260 // DeleteFile here is the virtual overridden member function. | |
| 261 PlatformFileError error = DeleteFile( | |
| 262 context, path.WithInternalPath(file_path_each)); | |
| 263 if (error == base::PLATFORM_FILE_ERROR_NOT_FOUND) | |
| 264 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 265 else if (error != base::PLATFORM_FILE_OK) | |
| 266 return error; | |
| 267 } | |
| 268 } | |
| 269 | |
| 270 while (!directories.empty()) { | |
| 271 PlatformFileError error = DeleteSingleDirectory( | |
| 272 context, path.WithInternalPath(directories.top())); | |
| 273 if (error == base::PLATFORM_FILE_ERROR_NOT_FOUND) | |
| 274 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 275 else if (error != base::PLATFORM_FILE_OK) | |
| 276 return error; | |
| 277 directories.pop(); | |
| 278 } | |
| 279 return DeleteSingleDirectory(context, path); | |
| 280 } | |
| 281 | |
| 282 } // namespace fileapi | 235 } // namespace fileapi |
| OLD | NEW |