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

Unified Diff: base/file_util_win.cc

Issue 1763008: Windows: Make file_util::Delete("c:\\foo_dir", false) work correctly. Add more unit tests for Delete (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: re-upload patch set 4 Created 10 years, 7 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/file_util_unittest.cc ('k') | chrome/installer/util/copy_tree_work_item_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/file_util_win.cc
===================================================================
--- base/file_util_win.cc (revision 46599)
+++ base/file_util_win.cc (working copy)
@@ -73,12 +73,20 @@
if (path.value().length() >= MAX_PATH)
return false;
- // If we're not recursing use DeleteFile; it should be faster. DeleteFile
- // fails if passed a directory though, which is why we fall through on
- // failure to the SHFileOperation.
- if (!recursive && DeleteFile(path.value().c_str()) != 0)
- return true;
+ if (!recursive) {
+ // If not recursing, then first check to see if |path| is a directory.
+ // If it is, then remove it with RemoveDirectory.
+ FileInfo file_info;
+ if (GetFileInfo(path, &file_info) && file_info.is_directory)
+ return RemoveDirectory(path.value().c_str()) != 0;
+ // Otherwise, it's a file, wildcard or non-existant. Try DeleteFile first
+ // because it should be faster. If DeleteFile fails, then we fall through
+ // to SHFileOperation, which will do the right thing.
+ if (DeleteFile(path.value().c_str()) != 0)
+ return true;
+ }
+
// SHFILEOPSTRUCT wants the path to be terminated with two NULLs,
// so we have to use wcscpy because wcscpy_s writes non-NULLs
// into the rest of the buffer.
@@ -93,9 +101,10 @@
if (!recursive)
file_operation.fFlags |= FOF_NORECURSION | FOF_FILESONLY;
int err = SHFileOperation(&file_operation);
- // Some versions of Windows return ERROR_FILE_NOT_FOUND when
- // deleting an empty directory.
- return (err == 0 || err == ERROR_FILE_NOT_FOUND);
+ // Some versions of Windows return ERROR_FILE_NOT_FOUND (0x2) when deleting
+ // an empty directory and some return 0x402 when they should be returning
+ // ERROR_FILE_NOT_FOUND. MSDN says Vista and up won't return 0x402.
+ return (err == 0 || err == ERROR_FILE_NOT_FOUND || err == 0x402);
}
bool DeleteAfterReboot(const FilePath& path) {
@@ -600,7 +609,7 @@
bool GetFileInfo(const FilePath& file_path, FileInfo* results) {
WIN32_FILE_ATTRIBUTE_DATA attr;
- if (!GetFileAttributesEx(file_path.ToWStringHack().c_str(),
+ if (!GetFileAttributesEx(file_path.value().c_str(),
GetFileExInfoStandard, &attr)) {
return false;
}
« no previous file with comments | « base/file_util_unittest.cc ('k') | chrome/installer/util/copy_tree_work_item_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698