 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 #include "base/files/file_util.h" | 5 #include "base/files/file_util.h" | 
| 6 | 6 | 
| 7 #include <windows.h> | 7 #include <windows.h> | 
| 8 #include <io.h> | 8 #include <io.h> | 
| 9 #include <psapi.h> | 9 #include <psapi.h> | 
| 10 #include <shellapi.h> | 10 #include <shellapi.h> | 
| 11 #include <shlobj.h> | 11 #include <shlobj.h> | 
| 12 #include <stddef.h> | 12 #include <stddef.h> | 
| 13 #include <stdint.h> | 13 #include <stdint.h> | 
| 14 #include <string.h> | |
| 14 #include <time.h> | 15 #include <time.h> | 
| 16 #include <winioctl.h> | |
| 15 #include <winsock2.h> | 17 #include <winsock2.h> | 
| 16 | 18 | 
| 17 #include <algorithm> | 19 #include <algorithm> | 
| 18 #include <limits> | 20 #include <limits> | 
| 19 #include <string> | 21 #include <string> | 
| 20 | 22 | 
| 23 #include "base/base32.h" | |
| 21 #include "base/files/file_enumerator.h" | 24 #include "base/files/file_enumerator.h" | 
| 22 #include "base/files/file_path.h" | 25 #include "base/files/file_path.h" | 
| 23 #include "base/logging.h" | 26 #include "base/logging.h" | 
| 24 #include "base/macros.h" | 27 #include "base/macros.h" | 
| 25 #include "base/metrics/histogram.h" | 28 #include "base/metrics/histogram.h" | 
| 26 #include "base/process/process_handle.h" | 29 #include "base/process/process_handle.h" | 
| 27 #include "base/rand_util.h" | 30 #include "base/rand_util.h" | 
| 28 #include "base/strings/string_number_conversions.h" | 31 #include "base/strings/string_number_conversions.h" | 
| 29 #include "base/strings/string_util.h" | 32 #include "base/strings/string_util.h" | 
| 30 #include "base/strings/utf_string_conversions.h" | 33 #include "base/strings/utf_string_conversions.h" | 
| 31 #include "base/threading/thread_restrictions.h" | 34 #include "base/threading/thread_restrictions.h" | 
| 32 #include "base/time/time.h" | 35 #include "base/time/time.h" | 
| 33 #include "base/win/scoped_handle.h" | 36 #include "base/win/scoped_handle.h" | 
| 34 #include "base/win/windows_version.h" | 37 #include "base/win/windows_version.h" | 
| 35 | 38 | 
| 36 namespace base { | 39 namespace base { | 
| 37 | 40 | 
| 38 namespace { | 41 namespace { | 
| 39 | 42 | 
| 40 const DWORD kFileShareAll = | 43 const DWORD kFileShareAll = | 
| 41 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; | 44 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; | 
| 42 | 45 | 
| 43 // Deletes all files and directories in a path. | 46 // The workhorse of base::DeleteFile. This class implements a robust file and | 
| 44 // Returns false on the first failure it encounters. | 47 // directory deletion strategy to work with Windows' pecurlarity around file | 
| 45 bool DeleteFileRecursive(const FilePath& path, | 48 // deletion -- on Windows, one does not simply delete a file. The Win32 | 
| 46 const FilePath::StringType& pattern, | 49 // DeleteFile function is deceptively named (though properly documented) as it | 
| 47 bool recursive) { | 50 // marks a file for deletion at some unspecified time in the future when all | 
| 48 FileEnumerator traversal(path, false, | 51 // handles are closed. This here workhorse accomplishes two goals: recursive | 
| 49 FileEnumerator::FILES | FileEnumerator::DIRECTORIES, | 52 // deletes can succeed in the face of other software operating therein (so long | 
| 50 pattern); | 53 // as they open files with SHARE_DELETE) and it's much more likely that callers | 
| 51 for (FilePath current = traversal.Next(); !current.empty(); | 54 // can delete a file or directory and then immediately create another with the | 
| 52 current = traversal.Next()) { | 55 // same name. This class implementes DeleteFile as follows: | 
| 53 // Try to clear the read-only bit if we find it. | 56 // - Open the item to be deleted. | 
| 54 FileEnumerator::FileInfo info = traversal.GetInfo(); | 57 // - Rob the item of its read-only status, if applicable. | 
| 55 if ((info.find_data().dwFileAttributes & FILE_ATTRIBUTE_READONLY) && | 58 // - In the case of a directory that is not a mount point or a symlink, | 
| 56 (recursive || !info.IsDirectory())) { | 59 // recursively delete everything contained therein using the strategy | 
| 57 SetFileAttributes( | 60 // described here. | 
| 58 current.value().c_str(), | 61 // - Paint the item with delete-on-close to make sure it is deletable. | 
| 59 info.find_data().dwFileAttributes & ~FILE_ATTRIBUTE_READONLY); | 62 // - Hide the item. | 
| 60 } | 63 // - Clear the delete-on-close bit so that it can... | 
| 61 | 64 // - Move the item up one directory (with a random name). | 
| 62 if (info.IsDirectory()) { | 65 // - Paint the item with delete-on-close for good. | 
| 63 if (recursive && (!DeleteFileRecursive(current, pattern, true) || | 66 // | 
| 64 !RemoveDirectory(current.value().c_str()))) | 67 // Failure possibilities are: | 
| 65 return false; | 68 // - The item could not be opened. | 
| 66 } else if (!::DeleteFile(current.value().c_str())) { | 69 // - The item's attributes could not be read. | 
| 70 // - The item was read-only and this attribute could not be cleared. | |
| 71 // - The item could not be deleted for various legitimate reasons such as it | |
| 72 // being open without SHARE_DELETE access, or the current user not having | |
| 73 // permission to delete it. | |
| 74 // - The file was moved out to the parent directory, could not be deleted, and | |
| 75 // could not be moved back to its original location. This will DCHECK in a | |
| 76 // debug build, as it is highly undesirable and so far outside of expectation. | |
| 77 // | |
| 78 // There also exists a partial success case where the item could not be moved | |
| 79 // prior to deletion but was deleted in situ. This is essentially the Win32 | |
| 80 // DeleteFile behavior, also known as "better than nothing". | |
| 81 class TreeDeleter { | |
| 82 public: | |
| 83 // Deletes a single item (a file, directory, mount point, symlink, etc). | |
| 84 // Returns false if the item could not be deleted. | |
| 85 static bool DeleteItem(const FilePath& path); | |
| 86 | |
| 87 // Recursively deletes |path| and its contents if |path| is a directory and | |
| 88 // is not a mount point or a symlink. | |
| 89 static bool DeleteRecursive(const FilePath& path); | |
| 90 | |
| 91 private: | |
| 92 // Constructs an instance to delete |path| recursively or not. | |
| 93 explicit TreeDeleter(const FilePath& path); | |
| 94 | |
| 95 // Main entrypoint to delete |path|. |recurse|, when true, indicates that an | |
| 96 // instance created for recursion should recurse into directories. | |
| 97 bool Delete(const FilePath& path, bool recurse); | |
| 98 | |
| 99 // Deletes as much of the contents of |path| as possible, returning |true| | |
| 100 // only if everything within was deleted. | |
| 101 bool DeleteContents(const FilePath& path); | |
| 102 | |
| 103 // Deletes |to_delete|, using |basic_info| to adjust its attributes in the | |
| 104 // process. | |
| 105 bool DoDelete(const FilePath& path, | |
| 106 const win::ScopedHandle& to_delete, | |
| 107 FILE_BASIC_INFO* basic_info); | |
| 108 | |
| 109 // Moves |to_delete| out into the parent directory of the operation. | |
| 110 // |to_delete| is expected to already have its delete-on-close attribute set. | |
| 111 // This function will clear it, move the file, and re-establish | |
| 112 // delete-on-close. Returns false if the delete-on-close attribute is not set | |
| 113 // on exit. | |
| 114 bool MoveToParent(const FilePath& path, const win::ScopedHandle& to_delete); | |
| 115 | |
| 116 // Returns a buffer used to determine if a directory is a mount point or a | |
| 117 // symlink. | |
| 118 REPARSE_GUID_DATA_BUFFER* GetReparseDataBuffer(); | |
| 119 | |
| 120 // Returns |true| if |to_delete| (whose attributes are represented in | |
| 121 // |basic_info|) is a mount point (a.k.a. a junction point) or a symlink. | |
| 122 bool IsMountPointOrSymlink(const win::ScopedHandle& to_delete, | |
| 123 const FILE_BASIC_INFO& basic_info); | |
| 124 | |
| 125 // Renames the item |to_delete| to |target|. | |
| 126 bool MoveTo(const win::ScopedHandle& to_delete, const FilePath& target); | |
| 127 | |
| 128 // The parent directory into which items are to be moved immediately prior to | |
| 129 // deletion. | |
| 130 FilePath parent_directory_; | |
| 131 | |
| 132 // The size, in bytes, of the FILE_RENAME_INFO block. | |
| 133 size_t rename_info_buffer_size_ = 0; | |
| 134 | |
| 135 // Holds a FILE_RENAME_INFO when the instance will be used to move files out | |
| 136 // of their current directory prior to deletion. | |
| 137 std::unique_ptr<uint8_t> rename_info_buffer_; | |
| 138 | |
| 139 // A buffer of size MAXIMUM_REPARSE_DATA_BUFFER_SIZE used to determine if a | |
| 140 // directory with a reparse point is either a mount point or a symlink. Only | |
| 141 // used for recursive deletes. | |
| 142 std::unique_ptr<uint8_t> reparse_data_buffer_; | |
| 143 | |
| 144 DISALLOW_COPY_AND_ASSIGN(TreeDeleter); | |
| 145 }; | |
| 146 | |
| 147 // static | |
| 148 bool TreeDeleter::DeleteItem(const FilePath& path) { | |
| 149 return TreeDeleter(path).Delete(path, false /* !recurse */); | |
| 150 } | |
| 151 | |
| 152 // static | |
| 153 bool TreeDeleter::DeleteRecursive(const FilePath& path) { | |
| 154 return TreeDeleter(path).Delete(path, true /* recurse */); | |
| 155 } | |
| 156 | |
| 157 TreeDeleter::TreeDeleter(const FilePath& path) { | |
| 158 const FilePath directory = path.DirName(); | |
| 159 const FilePath parent_directory = directory.DirName(); | |
| 160 | |
| 161 // Prepare to move files up to the parent directory while deleting them if the | |
| 162 // path to be deleted is not at the root of a volume. | |
| 163 if (parent_directory != directory) | |
| 164 parent_directory_ = parent_directory; | |
| 165 } | |
| 166 | |
| 167 bool TreeDeleter::Delete(const FilePath& path, bool recurse) { | |
| 168 // Open the item to be deleted with permission to hide and delete it with full | |
| 169 // sharing. Note that backup semantics are required to open a directory. | |
| 170 // Operate on a link or a mount point (implemented via reparse points) itself | |
| 171 // rather than on its target. | |
| 172 win::ScopedHandle to_delete(::CreateFile( | |
| 173 path.value().c_str(), | |
| 174 DELETE | FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES, | |
| 175 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, | |
| 176 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, | |
| 177 nullptr)); | |
| 178 if (!to_delete.IsValid()) { | |
| 179 DWORD error = ::GetLastError(); | |
| 180 if (error == ERROR_FILE_NOT_FOUND || error == ERROR_PATH_NOT_FOUND) | |
| 181 return true; | |
| 182 // Try a straight-up delete as a fallback. | |
| 183 if (::DeleteFile(path.value().c_str())) | |
| 184 return true; | |
| 185 ::SetLastError(error); | |
| 186 PLOG(ERROR) << "Failed to open " << path.value() << " for deletion"; | |
| 187 return false; | |
| 188 } | |
| 189 | |
| 190 // Remove the item's read-only bit if it is set. This is required for | |
| 191 // deletion. | |
| 192 bool read_only_set = false; | |
| 193 FILE_BASIC_INFO basic_info = {}; | |
| 194 if (!::GetFileInformationByHandleEx(to_delete.Get(), FileBasicInfo, | |
| 195 &basic_info, sizeof(basic_info))) { | |
| 196 PLOG(ERROR) << "Failed to read attributes of " << path.value(); | |
| 197 return false; | |
| 198 } | |
| 199 if ((basic_info.FileAttributes & FILE_ATTRIBUTE_READONLY) != 0) { | |
| 200 basic_info.FileAttributes &= ~FILE_ATTRIBUTE_READONLY; | |
| 201 if (!::SetFileInformationByHandle(to_delete.Get(), FileBasicInfo, | |
| 202 &basic_info, sizeof(basic_info))) { | |
| 203 PLOG(ERROR) << "Failed to strip read-only attribute of " << path.value(); | |
| 67 return false; | 204 return false; | 
| 68 } | 205 } | 
| 69 } | 206 read_only_set = true; | 
| 207 } | |
| 208 | |
| 209 // Recurse into this item if desired, this item is a directory, and this item | |
| 210 // is neither a mount point nor a symlink. | |
| 211 if (recurse && (basic_info.FileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0 && | |
| 212 !IsMountPointOrSymlink(to_delete, basic_info) && !DeleteContents(path)) { | |
| 
grt (UTC plus 2)
2016/12/04 08:52:06
i wonder if this is overkill. a directory with a r
 
jschuh
2016/12/04 14:10:46
For IsMountPointOrSymlink()? Doesn't seem like ove
 
grt (UTC plus 2)
2016/12/04 21:31:48
I'm not too familiar with the other uses for repar
 
jschuh
2016/12/06 22:16:34
Ah, I see. I think it would probably be safe enoug
 
grt (UTC plus 2)
2016/12/07 08:34:39
Done.
 | |
| 213 return false; | |
| 214 } | |
| 215 | |
| 216 if (DoDelete(path, to_delete, &basic_info)) | |
| 217 return true; | |
| 218 | |
| 219 // Restore the item's read-only state on failure. | |
| 220 if (read_only_set) { | |
| 
Scott Hess - ex-Googler
2016/12/06 08:15:56
Maybe split this function into a helper, so that y
 
grt (UTC plus 2)
2016/12/07 08:34:39
Nice catch! Indeed, the block above belongs in DoD
 | |
| 221 basic_info.FileAttributes |= FILE_ATTRIBUTE_READONLY; | |
| 222 if (!::SetFileInformationByHandle(to_delete.Get(), FileBasicInfo, | |
| 223 &basic_info, sizeof(basic_info))) { | |
| 224 PLOG(ERROR) << "Failed to restore read-only attribute of " | |
| 225 << path.value(); | |
| 226 } | |
| 227 } | |
| 228 return false; | |
| 229 } | |
| 230 | |
| 231 bool TreeDeleter::DeleteContents(const FilePath& path) { | |
| 232 // Recurse through all subdirectories to clear them out. Ignore errors during | |
| 233 // this pass -- they will be reported if the directories themselves cannot be | |
| 234 // deleted below. | |
| 235 { | |
| 236 FileEnumerator enumer(path, false, FileEnumerator::DIRECTORIES); | |
| 237 for (FilePath item = enumer.Next(); !item.empty(); item = enumer.Next()) | |
| 238 Delete(item, true /* recurse */); | |
| 239 } | |
| 240 | |
| 241 // Now that the contents of all subdirectories have been cleared, repeatedly | |
| 242 // pass over the contents of this directory until there is nothing left that | |
| 243 // can be deleted. Bail out after five fruitless iterations. This is a | |
| 244 // mitigation for the failure mode where the item could not be moved out to | |
| 245 // the parent directory before deletion. | |
| 246 bool items_failed; | |
| 247 int retries = 0; | |
| 248 do { | |
| 249 items_failed = false; | |
| 250 FileEnumerator enumer( | |
| 251 path, false, (FileEnumerator::DIRECTORIES | FileEnumerator::FILES)); | |
| 252 for (FilePath item = enumer.Next(); !item.empty(); item = enumer.Next()) { | |
| 253 if (Delete(item, false /* !recurse */)) | |
| 254 retries = 0; // Reset the retry count on any success. | |
| 255 else | |
| 256 items_failed = true; | |
| 257 } | |
| 258 } while (items_failed && ++retries < 5); | |
| 
grt (UTC plus 2)
2016/12/07 08:34:39
wdyt of a short sleep here to give the system time
 | |
| 259 | |
| 260 // Report success if the last pass encountered no errors. | |
| 261 return !items_failed; | |
| 262 } | |
| 263 | |
| 264 bool TreeDeleter::DoDelete(const FilePath& path, | |
| 265 const win::ScopedHandle& to_delete, | |
| 266 FILE_BASIC_INFO* basic_info) { | |
| 267 // Check to see if it's possible to delete this item. A non-empty directory, | |
| 268 // for example, cannot be deleted. | |
| 269 FILE_DISPOSITION_INFO disposition = {TRUE}; // DeleteFile | |
| 270 if (!::SetFileInformationByHandle(to_delete.Get(), FileDispositionInfo, | |
| 271 &disposition, sizeof(disposition))) { | |
| 272 PLOG(ERROR) << "Unable to delete " << path.value(); | |
| 273 return false; | |
| 274 } | |
| 275 | |
| 276 // The item will now be deleted when all handles are closed. Hide it so that | |
| 277 // it appears to vanish right away and so that any other procs (e.g., | |
| 278 // explorer.exe) observing the parent directory have a greater liklihood of | |
| 279 // not reacting when a new file suddenly appears and disappears. | |
| 280 bool hidden_set = false; | |
| 281 if ((basic_info->FileAttributes & FILE_ATTRIBUTE_HIDDEN) == 0) { | |
| 282 basic_info->FileAttributes |= FILE_ATTRIBUTE_HIDDEN; | |
| 283 if (!::SetFileInformationByHandle(to_delete.Get(), FileBasicInfo, | |
| 284 basic_info, sizeof(*basic_info))) { | |
| 285 PLOG(WARNING) << "Failed to hide " << path.value(); | |
| 286 } | |
| 287 hidden_set = true; | |
| 288 } | |
| 289 | |
| 290 if (MoveToParent(path, to_delete)) | |
| 291 return true; | |
| 292 | |
| 293 // The item couldn't be deleted. Restore its visibility. | |
| 294 if (hidden_set) { | |
| 295 basic_info->FileAttributes &= ~FILE_ATTRIBUTE_HIDDEN; | |
| 296 if (!::SetFileInformationByHandle(to_delete.Get(), FileBasicInfo, | |
| 297 basic_info, sizeof(*basic_info))) { | |
| 298 PLOG(WARNING) << "Failed to unhide " << path.value(); | |
| 299 } | |
| 300 } | |
| 301 return false; | |
| 302 } | |
| 303 | |
| 304 bool TreeDeleter::MoveToParent(const FilePath& path, | |
| 
jschuh
2016/12/04 14:10:46
Is there a better place to move to than the parent
 
grt (UTC plus 2)
2016/12/04 21:35:46
I had similar misgivings about doing this, and I'm
 
Scott Hess - ex-Googler
2016/12/06 08:15:56
In most cases in our code, there probably is going
 
jschuh
2016/12/06 22:16:34
I just worry about accumulating a bunch of files i
 
grt (UTC plus 2)
2016/12/07 08:34:39
The next two things on my plate are:
- Survey curr
 
grt (UTC plus 2)
2016/12/07 15:03:10
I've added this. PTAL.
 | |
| 305 const win::ScopedHandle& to_delete) { | |
| 306 // Do the shuffle to move the item out to the parent directory so that a new | |
| 307 // item with the same name can be created immediately in this directory, or | |
| 308 // so that the containing directory can be deleted immediately. | |
| 309 | |
| 310 // There is nothing to do when deleting at the root of a volume. | |
| 311 if (parent_directory_.empty()) | |
| 312 return true; | |
| 313 | |
| 314 // Revoke deletion so that the item can be moved. | |
| 315 FILE_DISPOSITION_INFO disposition = {FALSE}; // DeleteFile | |
| 316 if (!::SetFileInformationByHandle(to_delete.Get(), FileDispositionInfo, | |
| 317 &disposition, sizeof(disposition))) { | |
| 318 PLOG(WARNING) << "Unable to revoke deletion of " << path.value() | |
| 319 << "; skipping move to parent"; | |
| 320 return true; | |
| 321 } | |
| 322 | |
| 323 // Move the file out to the parent directory. Failure to move is not a fatal | |
| 324 // failure, though it will lead to a race when deleting the containing | |
| 325 // directory. This is mitigated by retries in directory deletion. | |
| 326 bool file_moved = false; | |
| 327 int attempts = 0; | |
| 328 do { | |
| 329 char random[5]; | |
| 330 base::RandBytes(&random[0], sizeof(random)); | |
| 331 file_moved = MoveTo( | |
| 332 to_delete, | |
| 333 parent_directory_.AppendASCII( | |
| 334 base::Base32Encode(base::StringPiece(&random[0], sizeof(random))) | |
| 335 .append(".tmp"))); | |
| 
Scott Hess - ex-Googler
2016/12/06 08:15:56
IMHO adding .tmp is just putting lipstick on the p
 
jschuh
2016/12/06 22:16:34
I disagree. I think this is a good indicator if so
 
grt (UTC plus 2)
2016/12/07 08:34:38
This was my thinking exactly.
 | |
| 336 } while (!file_moved && ++attempts < 5); | |
| 337 | |
| 338 // Mark the file to be deleted on close once again. | |
| 339 disposition.DeleteFile = TRUE; | |
| 340 if (!::SetFileInformationByHandle(to_delete.Get(), FileDispositionInfo, | |
| 341 &disposition, sizeof(disposition))) { | |
| 342 // This is highly unexpected since the file was to be deleted moments ago. | |
| 343 PLOG(ERROR) << "Failed to mark file to be deleted on close"; | |
| 344 | |
| 345 if (file_moved) { | |
| 346 // Try to move the file back to where it was. If this fails, we're in the | |
| 347 // town of cats and leaving the file in the wrong place. Regardless, make | |
| 348 // a final attempt to mark the file for deletion. | |
| 349 bool file_moved_back = MoveTo(to_delete, path); | |
| 350 LONG error = ::GetLastError(); | |
| 351 if (::SetFileInformationByHandle(to_delete.Get(), FileDispositionInfo, | |
| 352 &disposition, sizeof(disposition))) { | |
| 353 // Whew! The file will eventually be deleted in situ. | |
| 354 return true; | |
| 355 } | |
| 356 if (!file_moved_back) { | |
| 357 ::SetLastError(error); | |
| 358 PLOG(DFATAL) << "Failed to move item back to original location"; | |
| 359 } | |
| 360 } | |
| 361 return false; | |
| 362 } | |
| 363 | |
| 70 return true; | 364 return true; | 
| 71 } | 365 } | 
| 366 | |
| 367 REPARSE_GUID_DATA_BUFFER* TreeDeleter::GetReparseDataBuffer() { | |
| 368 if (!reparse_data_buffer_) | |
| 369 reparse_data_buffer_.reset(new uint8_t[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]); | |
| 370 return reinterpret_cast<REPARSE_GUID_DATA_BUFFER*>( | |
| 371 reparse_data_buffer_.get()); | |
| 372 } | |
| 373 | |
| 374 bool TreeDeleter::IsMountPointOrSymlink(const win::ScopedHandle& to_delete, | |
| 375 const FILE_BASIC_INFO& basic_info) { | |
| 376 if ((basic_info.FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) == 0) | |
| 377 return false; | |
| 378 | |
| 379 REPARSE_GUID_DATA_BUFFER* buffer = GetReparseDataBuffer(); | |
| 380 DWORD bytes_used = 0; | |
| 381 if (!::DeviceIoControl(to_delete.Get(), FSCTL_GET_REPARSE_POINT, nullptr, 0, | |
| 382 buffer, MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &bytes_used, | |
| 383 nullptr) || | |
| 384 bytes_used < sizeof(DWORD)) { | |
| 385 PLOG(ERROR) << "Failed getting reparse data"; | |
| 386 return true; // Err on the safe side. | |
| 387 } | |
| 388 | |
| 389 return buffer->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT || | |
| 390 buffer->ReparseTag == IO_REPARSE_TAG_SYMLINK; | |
| 391 } | |
| 392 | |
| 393 bool TreeDeleter::MoveTo(const win::ScopedHandle& to_delete, | |
| 394 const FilePath& target) { | |
| 395 // Compute the proper size of the FILE_RENAME_INFO including the target path. | |
| 396 size_t path_bytes = (target.value().length() + 1) * sizeof(wchar_t); | |
| 397 size_t struct_size = offsetof(FILE_RENAME_INFO, FileName) + path_bytes; | |
| 398 | |
| 399 // Allocate a new buffer if needed. | |
| 400 if (rename_info_buffer_size_ < struct_size) { | |
| 401 rename_info_buffer_.reset(); | |
| 402 rename_info_buffer_.reset(new uint8_t[struct_size]); | |
| 403 rename_info_buffer_size_ = struct_size; | |
| 404 } | |
| 405 | |
| 406 // Formulate a FILE_RENAME_INFO struct in the buffer. | |
| 407 FILE_RENAME_INFO* file_rename_info = | |
| 408 reinterpret_cast<FILE_RENAME_INFO*>(rename_info_buffer_.get()); | |
| 409 file_rename_info->ReplaceIfExists = FALSE; | |
| 410 file_rename_info->RootDirectory = nullptr; | |
| 411 file_rename_info->FileNameLength = target.value().length(); | |
| 412 ::memcpy(&file_rename_info->FileName[0], target.value().c_str(), path_bytes); | |
| 413 | |
| 414 if (!::SetFileInformationByHandle(to_delete.Get(), FileRenameInfo, | |
| 415 file_rename_info, struct_size)) { | |
| 416 PLOG(ERROR) << "Failed to move item to " << target.value(); | |
| 417 return false; | |
| 418 } | |
| 419 return true; | |
| 420 } | |
| 72 | 421 | 
| 73 } // namespace | 422 } // namespace | 
| 74 | 423 | 
| 75 FilePath MakeAbsoluteFilePath(const FilePath& input) { | 424 FilePath MakeAbsoluteFilePath(const FilePath& input) { | 
| 76 ThreadRestrictions::AssertIOAllowed(); | 425 ThreadRestrictions::AssertIOAllowed(); | 
| 77 wchar_t file_path[MAX_PATH]; | 426 wchar_t file_path[MAX_PATH]; | 
| 78 if (!_wfullpath(file_path, input.value().c_str(), MAX_PATH)) | 427 if (!_wfullpath(file_path, input.value().c_str(), MAX_PATH)) | 
| 79 return FilePath(); | 428 return FilePath(); | 
| 80 return FilePath(file_path); | 429 return FilePath(file_path); | 
| 81 } | 430 } | 
| 82 | 431 | 
| 83 bool DeleteFile(const FilePath& path, bool recursive) { | 432 bool DeleteFile(const FilePath& path, bool recursive) { | 
| 84 ThreadRestrictions::AssertIOAllowed(); | 433 ThreadRestrictions::AssertIOAllowed(); | 
| 85 | 434 | 
| 435 // Wildcards are not supported. | |
| 436 DCHECK_EQ(path.BaseName().value().find_first_of(L"*?"), | |
| 437 FilePath::StringType::npos); | |
| 438 | |
| 86 if (path.empty()) | 439 if (path.empty()) | 
| 87 return true; | 440 return true; | 
| 88 | 441 | 
| 89 if (path.value().length() >= MAX_PATH) | 442 if (path.value().length() >= MAX_PATH) | 
| 90 return false; | 443 return false; | 
| 91 | 444 | 
| 92 // Handle any path with wildcards. | 445 return recursive ? TreeDeleter::DeleteRecursive(path) | 
| 93 if (path.BaseName().value().find_first_of(L"*?") != | 446 : TreeDeleter::DeleteItem(path); | 
| 94 FilePath::StringType::npos) { | |
| 95 return DeleteFileRecursive(path.DirName(), path.BaseName().value(), | |
| 96 recursive); | |
| 97 } | |
| 98 DWORD attr = GetFileAttributes(path.value().c_str()); | |
| 99 // We're done if we can't find the path. | |
| 100 if (attr == INVALID_FILE_ATTRIBUTES) | |
| 101 return true; | |
| 102 // We may need to clear the read-only bit. | |
| 103 if ((attr & FILE_ATTRIBUTE_READONLY) && | |
| 104 !SetFileAttributes(path.value().c_str(), | |
| 105 attr & ~FILE_ATTRIBUTE_READONLY)) { | |
| 106 return false; | |
| 107 } | |
| 108 // Directories are handled differently if they're recursive. | |
| 109 if (!(attr & FILE_ATTRIBUTE_DIRECTORY)) | |
| 110 return !!::DeleteFile(path.value().c_str()); | |
| 111 // Handle a simple, single file delete. | |
| 112 if (!recursive || DeleteFileRecursive(path, L"*", true)) | |
| 113 return !!RemoveDirectory(path.value().c_str()); | |
| 114 | |
| 115 return false; | |
| 116 } | 447 } | 
| 117 | 448 | 
| 118 bool DeleteFileAfterReboot(const FilePath& path) { | 449 bool DeleteFileAfterReboot(const FilePath& path) { | 
| 119 ThreadRestrictions::AssertIOAllowed(); | 450 ThreadRestrictions::AssertIOAllowed(); | 
| 120 | 451 | 
| 121 if (path.value().length() >= MAX_PATH) | 452 if (path.value().length() >= MAX_PATH) | 
| 122 return false; | 453 return false; | 
| 123 | 454 | 
| 124 return MoveFileEx(path.value().c_str(), NULL, | 455 return MoveFileEx(path.value().c_str(), NULL, | 
| 125 MOVEFILE_DELAY_UNTIL_REBOOT | | 456 MOVEFILE_DELAY_UNTIL_REBOOT | | 
| (...skipping 697 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 823 // Like Move, this function is not transactional, so we just | 1154 // Like Move, this function is not transactional, so we just | 
| 824 // leave the copied bits behind if deleting from_path fails. | 1155 // leave the copied bits behind if deleting from_path fails. | 
| 825 // If to_path exists previously then we have already overwritten | 1156 // If to_path exists previously then we have already overwritten | 
| 826 // it by now, we don't get better off by deleting the new bits. | 1157 // it by now, we don't get better off by deleting the new bits. | 
| 827 } | 1158 } | 
| 828 return false; | 1159 return false; | 
| 829 } | 1160 } | 
| 830 | 1161 | 
| 831 } // namespace internal | 1162 } // namespace internal | 
| 832 } // namespace base | 1163 } // namespace base | 
| OLD | NEW |