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

Side by Side Diff: base/files/file_util.h

Issue 2545283002: A robust base::DeleteFile.
Patch Set: sync to position 437832 Created 4 years 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
« no previous file with comments | « no previous file | base/files/file_util_unittest.cc » ('j') | base/files/file_util_unittest.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
Nico 2017/01/10 18:31:55 should BUG= include 674135 too?
grt (UTC plus 2) 2017/01/12 15:07:03 May as well since a comment in that issue referenc
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
11 #include <stddef.h> 11 #include <stddef.h>
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
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,
Nico 2017/01/10 18:31:55 should BUG= include 674135 too?
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.
64 // 64 //
65 // In posix environment and if |path| is a symbolic link, this deletes only 65 // On Windows: as much as possible will always be deleted. Returns true if
66 // the symlink. (even if the symlink points to a non-existent file) 66 // |path| was deleted or did not exist. This function will not recurse into
67 // symlinks or mount points (a.k.a. junction points), but rather will delete the
68 // links/points themselves.
69 //
70 // In posix environment and if |path| is a symbolic link, this deletes only the
71 // symlink. (even if the symlink points to a non-existent file)
67 // 72 //
68 // WARNING: USING THIS WITH recursive==true IS EQUIVALENT 73 // WARNING: USING THIS WITH recursive==true IS EQUIVALENT
69 // TO "rm -rf", SO USE WITH CAUTION. 74 // TO "rm -rf", SO USE WITH CAUTION.
70 BASE_EXPORT bool DeleteFile(const FilePath& path, bool recursive); 75 BASE_EXPORT bool DeleteFile(const FilePath& path, bool recursive);
71 76
72 #if defined(OS_WIN) 77 #if defined(OS_WIN)
78 struct DeleteFileMetrics {
79 enum TemporaryDirectoryLocation {
80 UNDECIDED, // No attempt has yet been made to find a directory.
81 NONE, // Failed to find a writeable temporary directory.
82 TEMP, // Using %TEMP%.
83 VOLUME_TEMP, // Using X:\Temp or X:\Tmp.
84 PARENT, // Using the directory above the top-most item being deleted.
85 ITEM, // Using the directory containing the item being deleted.
86 };
87
88 TemporaryDirectoryLocation temporary_directory_location;
89
90 // The number of items deleted via the DELETE_ON_CLOSE path.
91 int delete_on_close_success_count;
92
93 // The number of items in |delete_on_close_success_count| that were moved into
94 // the temp directory. Ideally, this equals |delete_on_close_success_count|,
95 // meaning that the operation was 100% successful.
96 int move_to_temp_count;
97
98 // The number of times opening an item to be deleted failed, resulting in a
99 // fallback to plain ::DeleteFile that succeded.
100 int delete_file_success_count;
101
102 // The number of times each of the above strategies failed to delete an item.
103 int fail_count;
104
105 // The total number of retries before giving up (0 if success on the first
106 // attempt). If the overall operation succeeds and this is not zero, the
107 // retries were a very good thing. If the overall operation fails and the two
108 // success counts above are non-zero, then perhaps more retries or some delays
109 // would have helped. If the overall operation fails and the two success
110 // counts above are zero, then it's likely that nothing would have helped.
111 int total_retry_count;
112
113 // The maximum recursion depth.
114 int max_depth;
115 };
116
117 // The same as DeleteFile, but with metrics about its performance.
118 BASE_EXPORT bool DeleteFileWithMetrics(const FilePath& path,
119 bool recursive,
120 DeleteFileMetrics* metrics);
121
73 // Schedules to delete the given path, whether it's a file or a directory, until 122 // Schedules to delete the given path, whether it's a file or a directory, until
74 // the operating system is restarted. 123 // the operating system is restarted.
75 // Note: 124 // Note:
76 // 1) The file/directory to be deleted should exist in a temp folder. 125 // 1) The file/directory to be deleted should exist in a temp folder.
77 // 2) The directory to be deleted must be empty. 126 // 2) The directory to be deleted must be empty.
78 BASE_EXPORT bool DeleteFileAfterReboot(const FilePath& path); 127 BASE_EXPORT bool DeleteFileAfterReboot(const FilePath& path);
79 #endif 128 #endif
80 129
81 // Moves the given path, whether it's a file or a directory. 130 // 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 131 // 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
448 // This function simulates Move(), but unlike Move() it works across volumes. 497 // This function simulates Move(), but unlike Move() it works across volumes.
449 // This function is not transactional. 498 // This function is not transactional.
450 BASE_EXPORT bool CopyAndDeleteDirectory(const FilePath& from_path, 499 BASE_EXPORT bool CopyAndDeleteDirectory(const FilePath& from_path,
451 const FilePath& to_path); 500 const FilePath& to_path);
452 #endif // defined(OS_WIN) 501 #endif // defined(OS_WIN)
453 502
454 } // namespace internal 503 } // namespace internal
455 } // namespace base 504 } // namespace base
456 505
457 #endif // BASE_FILES_FILE_UTIL_H_ 506 #endif // BASE_FILES_FILE_UTIL_H_
OLDNEW
« no previous file with comments | « no previous file | base/files/file_util_unittest.cc » ('j') | base/files/file_util_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698