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

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

Issue 2545283002: A robust base::DeleteFile.
Patch Set: test fixes Created 3 years, 11 months 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') | no next file with comments »
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.
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
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.
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 (note that
91 // non-existant items count as successes in this path).
92 int delete_on_close_success_count;
93
94 // The number of items in |delete_on_close_success_count| that were moved into
95 // the temp directory. Ideally, this equals |delete_on_close_success_count|,
96 // meaning that the operation was 100% successful.
97 int move_to_temp_count;
98
99 // The number of times opening an item to be deleted failed, resulting in a
100 // fallback to plain ::DeleteFile that succeded.
101 int delete_file_success_count;
102
103 // The number of times each of the above strategies failed to delete an item.
104 int fail_count;
105
106 // The total number of retries before giving up (0 if success on the first
107 // attempt). If the overall operation succeeds and this is not zero, the
108 // retries were a very good thing. If the overall operation fails and the two
109 // success counts above are non-zero, then perhaps more retries or some delays
110 // would have helped. If the overall operation fails and the two success
111 // counts above are zero, then it's likely that nothing would have helped.
112 int total_retry_count;
113
114 // The maximum recursion depth.
115 int max_depth;
116 };
117
118 // The same as DeleteFile, but with metrics about its performance.
119 BASE_EXPORT bool DeleteFileWithMetrics(const FilePath& path,
120 bool recursive,
121 DeleteFileMetrics* metrics);
122
73 // Schedules to delete the given path, whether it's a file or a directory, until 123 // Schedules to delete the given path, whether it's a file or a directory, until
74 // the operating system is restarted. 124 // the operating system is restarted.
75 // Note: 125 // Note:
76 // 1) The file/directory to be deleted should exist in a temp folder. 126 // 1) The file/directory to be deleted should exist in a temp folder.
77 // 2) The directory to be deleted must be empty. 127 // 2) The directory to be deleted must be empty.
78 BASE_EXPORT bool DeleteFileAfterReboot(const FilePath& path); 128 BASE_EXPORT bool DeleteFileAfterReboot(const FilePath& path);
79 #endif 129 #endif
80 130
81 // Moves the given path, whether it's a file or a directory. 131 // 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 132 // 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. 498 // This function simulates Move(), but unlike Move() it works across volumes.
449 // This function is not transactional. 499 // This function is not transactional.
450 BASE_EXPORT bool CopyAndDeleteDirectory(const FilePath& from_path, 500 BASE_EXPORT bool CopyAndDeleteDirectory(const FilePath& from_path,
451 const FilePath& to_path); 501 const FilePath& to_path);
452 #endif // defined(OS_WIN) 502 #endif // defined(OS_WIN)
453 503
454 } // namespace internal 504 } // namespace internal
455 } // namespace base 505 } // namespace base
456 506
457 #endif // BASE_FILES_FILE_UTIL_H_ 507 #endif // BASE_FILES_FILE_UTIL_H_
OLDNEW
« no previous file with comments | « no previous file | base/files/file_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698