Index: base/file_util_win.cc |
=================================================================== |
--- base/file_util_win.cc (revision 69169) |
+++ base/file_util_win.cc (working copy) |
@@ -129,44 +129,50 @@ |
return file_count; |
} |
+// Safe to pass extended-path to this method. |
bool Delete(const FilePath& path, bool recursive) { |
base::ThreadRestrictions::AssertIOAllowed(); |
- if (path.value().length() >= MAX_PATH) |
- return false; |
+ base::PlatformFileInfo file_info; |
+ if (!GetFileInfo(path, &file_info)) |
+ 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. |
- base::PlatformFileInfo file_info; |
- if (GetFileInfo(path, &file_info) && file_info.is_directory) |
- return RemoveDirectory(path.value().c_str()) != 0; |
+ if (!file_info.is_directory) |
+ return (DeleteFile(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; |
+ if (!recursive) |
+ return RemoveDirectory(path.value().c_str()) != 0; |
+ |
+ // Matches posix version of iterating directories. |
+ bool success = true; |
+ std::stack<FilePath::StringType> directories; |
+ directories.push(path.value()); |
+ FileEnumerator traversal(path, true, static_cast<FileEnumerator::FILE_TYPE>( |
+ FileEnumerator::FILES | FileEnumerator::DIRECTORIES)); |
+ |
+ for (FilePath current = traversal.Next(); success && !current.empty(); |
+ current = traversal.Next()) { |
+ FileEnumerator::FindInfo info; |
+ traversal.GetFindInfo(&info); |
+ if (traversal.IsDirectory(info)) { |
+ directories.push(current.value()); |
+ } else { |
+ // This is needed because DeleteFile can fail for read-only files. |
+ uint32 attributes = ::GetFileAttributes(current.value().c_str()); |
+ if (attributes != INVALID_FILE_ATTRIBUTES && |
+ attributes & FILE_ATTRIBUTE_READONLY) |
+ ::SetFileAttributes(current.value().c_str(), |
+ attributes &~ FILE_ATTRIBUTE_NORMAL); |
+ success = (DeleteFile(current.value().c_str()) != 0); |
+ } |
} |
- // 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. |
- wchar_t double_terminated_path[MAX_PATH + 1] = {0}; |
-#pragma warning(suppress:4996) // don't complain about wcscpy deprecation |
- wcscpy(double_terminated_path, path.value().c_str()); |
- |
- SHFILEOPSTRUCT file_operation = {0}; |
- file_operation.wFunc = FO_DELETE; |
- file_operation.pFrom = double_terminated_path; |
- file_operation.fFlags = FOF_NOERRORUI | FOF_SILENT | FOF_NOCONFIRMATION; |
- if (!recursive) |
- file_operation.fFlags |= FOF_NORECURSION | FOF_FILESONLY; |
- int err = SHFileOperation(&file_operation); |
- // 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); |
+ while (success && !directories.empty()) { |
+ FilePath dir = FilePath(directories.top()); |
+ directories.pop(); |
+ success = (RemoveDirectory(dir.value().c_str()) != 0); |
+ } |
+ return success; |
} |
bool DeleteAfterReboot(const FilePath& path) { |
@@ -225,72 +231,91 @@ |
bool CopyFile(const FilePath& from_path, const FilePath& to_path) { |
base::ThreadRestrictions::AssertIOAllowed(); |
- // NOTE: I suspect we could support longer paths, but that would involve |
- // analyzing all our usage of files. |
- if (from_path.value().length() >= MAX_PATH || |
- to_path.value().length() >= MAX_PATH) { |
- return false; |
- } |
return (::CopyFile(from_path.value().c_str(), to_path.value().c_str(), |
false) != 0); |
} |
-bool ShellCopy(const FilePath& from_path, const FilePath& to_path, |
- bool recursive) { |
+// Safe to pass extended-path to this method. |
+bool CopyDirectory(const FilePath& from_path, const FilePath& to_path, |
+ bool recursive) { |
base::ThreadRestrictions::AssertIOAllowed(); |
- |
- // NOTE: I suspect we could support longer paths, but that would involve |
- // analyzing all our usage of files. |
- if (from_path.value().length() >= MAX_PATH || |
- to_path.value().length() >= MAX_PATH) { |
+ FilePath real_to_path = to_path; |
+ if (PathExists(real_to_path)) { |
+ if (!real_to_path.StartsWithExtendedPathPrefix() && |
+ !AbsolutePath(&real_to_path)) |
+ return false; |
+ } else { |
+ real_to_path = real_to_path.DirName(); |
+ if (!real_to_path.StartsWithExtendedPathPrefix() && |
+ !AbsolutePath(&real_to_path)) |
+ return false; |
+ } |
+ FilePath real_from_path = from_path; |
+ if (!real_from_path.StartsWithExtendedPathPrefix() && |
+ !AbsolutePath(&real_from_path)) |
return false; |
- } |
+ if (real_to_path.value().size() >= real_from_path.value().size() && |
+ real_to_path.value().compare(0, real_from_path.value().size(), |
+ real_from_path.value()) == 0) |
+ return false; |
- // 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. |
- wchar_t double_terminated_path_from[MAX_PATH + 1] = {0}; |
- wchar_t double_terminated_path_to[MAX_PATH + 1] = {0}; |
-#pragma warning(suppress:4996) // don't complain about wcscpy deprecation |
- wcscpy(double_terminated_path_from, from_path.value().c_str()); |
-#pragma warning(suppress:4996) // don't complain about wcscpy deprecation |
- wcscpy(double_terminated_path_to, to_path.value().c_str()); |
+ bool success = true; |
+ FileEnumerator::FILE_TYPE traverse_type = |
+ static_cast<FileEnumerator::FILE_TYPE>(FileEnumerator::FILES); |
+ if (recursive) |
+ traverse_type = static_cast<FileEnumerator::FILE_TYPE>( |
+ traverse_type | FileEnumerator::DIRECTORIES); |
+ FileEnumerator traversal(from_path, recursive, traverse_type); |
- SHFILEOPSTRUCT file_operation = {0}; |
- file_operation.wFunc = FO_COPY; |
- file_operation.pFrom = double_terminated_path_from; |
- file_operation.pTo = double_terminated_path_to; |
- file_operation.fFlags = FOF_NOERRORUI | FOF_SILENT | FOF_NOCONFIRMATION | |
- FOF_NOCONFIRMMKDIR; |
- if (!recursive) |
- file_operation.fFlags |= FOF_NORECURSION | FOF_FILESONLY; |
+ // |to_path| may not exist yet, start the loop with |to_path|. |
+ FilePath current = from_path; |
+ if (!PathExists(from_path)) { |
+ LOG(ERROR) << "CopyDirectory() couldn't get info on source directory: " |
+ << from_path.value(); |
+ success = false; |
+ } |
- return (SHFileOperation(&file_operation) == 0); |
-} |
+ FilePath from_path_base = from_path; |
+ if (recursive && DirectoryExists(to_path)) { |
+ // If the destination already exists and is a directory, then the |
+ // top level of source needs to be copied. |
+ from_path_base = from_path.DirName(); |
+ } |
-bool CopyDirectory(const FilePath& from_path, const FilePath& to_path, |
- bool recursive) { |
- base::ThreadRestrictions::AssertIOAllowed(); |
+ // This matches previous shell implementation that assumed that |
+ // non-recursive calls will always have a directory for from_path. |
+ DCHECK(recursive || DirectoryExists(from_path)); |
- if (recursive) |
- return ShellCopy(from_path, to_path, true); |
+ while (success && !current.empty()) { |
+ // current is the source path, including from_path, so paste |
+ // the suffix after from_path onto to_path to create the target_path. |
+ FilePath::StringType suffix( |
+ ¤t.value().c_str()[from_path_base.value().size()]); |
+ // Strip the leading '\' (if any). |
+ if (!suffix.empty()) { |
+ DCHECK_EQ('\\', suffix[0]); |
+ suffix.erase(0, 1); |
+ } |
+ const FilePath target_path = to_path.Append(suffix); |
- // The following code assumes that from path is a directory. |
- DCHECK(DirectoryExists(from_path)); |
- |
- // Instead of creating a new directory, we copy the old one to include the |
- // security information of the folder as part of the copy. |
- if (!PathExists(to_path)) { |
- // Except that Vista fails to do that, and instead do a recursive copy if |
- // the target directory doesn't exist. |
- if (base::win::GetVersion() >= base::win::VERSION_VISTA) |
- CreateDirectory(to_path); |
- else |
- ShellCopy(from_path, to_path, false); |
+ if (DirectoryExists(current)) { |
+ if (::CreateDirectory(target_path.value().c_str(), NULL) == 0) { |
+ if (!DirectoryExists(target_path)) { |
+ LOG(ERROR) << "CopyDirectory() couldn't create directory: " |
+ << target_path.value(); |
+ success = false; |
+ } |
+ } |
+ } else { |
+ if (!CopyFile(current, target_path)) { |
+ LOG(ERROR) << "CopyDirectory() couldn't create file: " |
+ << target_path.value(); |
+ success = false; |
+ } |
+ } |
+ current = traversal.Next(); |
} |
- |
- FilePath directory = from_path.Append(L"*.*"); |
- return ShellCopy(directory, to_path, false); |
+ return success; |
} |
bool CopyAndDeleteDirectory(const FilePath& from_path, |
@@ -308,7 +333,6 @@ |
return false; |
} |
- |
bool PathExists(const FilePath& path) { |
base::ThreadRestrictions::AssertIOAllowed(); |
return (GetFileAttributes(path.value().c_str()) != INVALID_FILE_ATTRIBUTES); |