 Chromium Code Reviews
 Chromium Code Reviews Issue 2545283002:
  A robust base::DeleteFile.
    
  
    Issue 2545283002:
  A robust base::DeleteFile. 
  | 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 // This file contains utility functions for dealing with the local | 5 // This file contains utility functions for dealing with the local | 
| 6 // filesystem. | 6 // filesystem. | 
| 7 | 7 | 
| 8 #ifndef BASE_FILES_FILE_UTIL_H_ | 8 #ifndef BASE_FILES_FILE_UTIL_H_ | 
| 9 #define BASE_FILES_FILE_UTIL_H_ | 9 #define BASE_FILES_FILE_UTIL_H_ | 
| 10 | 10 | 
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 48 // function can result in I/O so it can be slow. | 48 // function can result in I/O so it can be slow. | 
| 49 BASE_EXPORT FilePath MakeAbsoluteFilePath(const FilePath& input); | 49 BASE_EXPORT FilePath MakeAbsoluteFilePath(const FilePath& input); | 
| 50 | 50 | 
| 51 // Returns the total number of bytes used by all the files under |root_path|. | 51 // Returns the total number of bytes used by all the files under |root_path|. | 
| 52 // If the path does not exist the function returns 0. | 52 // If the path does not exist the function returns 0. | 
| 53 // | 53 // | 
| 54 // This function is implemented using the FileEnumerator class so it is not | 54 // This function is implemented using the FileEnumerator class so it is not | 
| 55 // particularly speedy in any platform. | 55 // particularly speedy in any platform. | 
| 56 BASE_EXPORT int64_t ComputeDirectorySize(const FilePath& root_path); | 56 BASE_EXPORT int64_t ComputeDirectorySize(const FilePath& root_path); | 
| 57 | 57 | 
| 58 // Deletes the given path, whether it's a file or a directory. | 58 // Deletes the given path. If |path| names a directory and |recursive| is false, | 
| 59 // If it's a directory, it's perfectly happy to delete all of the | 59 // the directory is only deleted if it is empty or is a symbolic link. If |path| | 
| 60 // directory's contents. Passing true to recursive deletes | 60 // names a directory and |recursive| is true, this function will recursively | 
| 61 // subdirectories and their contents as well. | 61 // delete as much as possible within |path|, ideally ending in success when | 
| 62 // Returns true if successful, false otherwise. It is considered successful | 62 // |path| is deleted. In case of error, an unspecified amount of |path|'s | 
| 63 // to attempt to delete a file that does not exist. | 63 // original contents may already have been deleted. On Windows, as much as | 
| 64 // possible will always be deleted. Returns true if |path| was deleted or did | |
| 
Sigurður Ásgeirsson
2016/12/08 14:36:46
maybe some words that this is constrained to a sin
 
grt (UTC plus 2)
2016/12/08 20:35:16
Done.
 | |
| 65 // not exist. | |
| 64 // | 66 // | 
| 65 // In posix environment and if |path| is a symbolic link, this deletes only | 67 // In posix environment and if |path| is a symbolic link, this deletes only the | 
| 66 // the symlink. (even if the symlink points to a non-existent file) | 68 // symlink. (even if the symlink points to a non-existent file) | 
| 67 // | 69 // | 
| 68 // WARNING: USING THIS WITH recursive==true IS EQUIVALENT | 70 // WARNING: USING THIS WITH recursive==true IS EQUIVALENT | 
| 69 // TO "rm -rf", SO USE WITH CAUTION. | 71 // TO "rm -rf", SO USE WITH CAUTION. | 
| 70 BASE_EXPORT bool DeleteFile(const FilePath& path, bool recursive); | 72 BASE_EXPORT bool DeleteFile(const FilePath& path, bool recursive); | 
| 71 | 73 | 
| 72 #if defined(OS_WIN) | 74 #if defined(OS_WIN) | 
| 75 struct DeleteFileMetrics { | |
| 
Sigurður Ásgeirsson
2016/12/08 14:36:46
nice and simple, I like it.
 
grt (UTC plus 2)
2016/12/08 20:35:16
Thanks.
 | |
| 76 enum TemporaryDirectoryLocation { | |
| 77 UNDECIDED, // No attempt has yet been made to find a directory. | |
| 78 NONE, // Failed to find a writeable temporary directory. | |
| 79 TEMP, // Using %TEMP%. | |
| 80 VOLUME_TEMP, // Using X:\Temp or X:\Tmp. | |
| 81 PARENT, // Using the directory above the top-most item being deleted. | |
| 82 ITEM, // Using the directory containing the item being deleted. | |
| 83 }; | |
| 84 | |
| 85 TemporaryDirectoryLocation temporary_directory_location; | |
| 86 | |
| 87 // The number of items deleted via the DELETE_ON_CLOSE path. | |
| 88 int delete_on_close_success_count; | |
| 89 | |
| 90 // The number of items in |delete_on_close_success_count| that were moved into | |
| 91 // the temp directory. Ideally, this equals |delete_on_close_success_count|, | |
| 92 // meaning that the operation was 100% successful. | |
| 93 int move_to_temp_count; | |
| 94 | |
| 95 // The number of times opening an item to be deleted failed, resulting in a | |
| 96 // fallback to plain ::DeleteFile that succeded. | |
| 97 int delete_file_success_count; | |
| 98 | |
| 99 // The number of times each of the above strategies failed to delete an item. | |
| 100 int fail_count; | |
| 101 | |
| 102 // The total number of retries before giving up (0 if success on the first | |
| 103 // attempt). If the overall operation succeeds and this is not zero, the | |
| 104 // retries were a very good thing. If the overall operation fails and the two | |
| 105 // success counts above are non-zero, then perhaps more retries or some delays | |
| 106 // would have helped. If the overall operation fails and the two success | |
| 107 // counts above are zero, then it's likely that nothing would have helped. | |
| 108 int total_retry_count; | |
| 109 | |
| 110 // The maximum recursion depth. | |
| 111 int max_depth; | |
| 112 }; | |
| 113 | |
| 114 // The same as DeleteFile, but with metrics about its performance. | |
| 115 BASE_EXPORT bool DeleteFileWithMetrics(const FilePath& path, | |
| 116 bool recursive, | |
| 117 DeleteFileMetrics* metrics); | |
| 118 | |
| 73 // Schedules to delete the given path, whether it's a file or a directory, until | 119 // Schedules to delete the given path, whether it's a file or a directory, until | 
| 74 // the operating system is restarted. | 120 // the operating system is restarted. | 
| 75 // Note: | 121 // Note: | 
| 76 // 1) The file/directory to be deleted should exist in a temp folder. | 122 // 1) The file/directory to be deleted should exist in a temp folder. | 
| 77 // 2) The directory to be deleted must be empty. | 123 // 2) The directory to be deleted must be empty. | 
| 78 BASE_EXPORT bool DeleteFileAfterReboot(const FilePath& path); | 124 BASE_EXPORT bool DeleteFileAfterReboot(const FilePath& path); | 
| 79 #endif | 125 #endif | 
| 80 | 126 | 
| 81 // Moves the given path, whether it's a file or a directory. | 127 // Moves the given path, whether it's a file or a directory. | 
| 82 // If a simple rename is not possible, such as in the case where the paths are | 128 // If a simple rename is not possible, such as in the case where the paths are | 
| (...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 448 // This function simulates Move(), but unlike Move() it works across volumes. | 494 // This function simulates Move(), but unlike Move() it works across volumes. | 
| 449 // This function is not transactional. | 495 // This function is not transactional. | 
| 450 BASE_EXPORT bool CopyAndDeleteDirectory(const FilePath& from_path, | 496 BASE_EXPORT bool CopyAndDeleteDirectory(const FilePath& from_path, | 
| 451 const FilePath& to_path); | 497 const FilePath& to_path); | 
| 452 #endif // defined(OS_WIN) | 498 #endif // defined(OS_WIN) | 
| 453 | 499 | 
| 454 } // namespace internal | 500 } // namespace internal | 
| 455 } // namespace base | 501 } // namespace base | 
| 456 | 502 | 
| 457 #endif // BASE_FILES_FILE_UTIL_H_ | 503 #endif // BASE_FILES_FILE_UTIL_H_ | 
| OLD | NEW |