Chromium Code Reviews| Index: base/file_util_win.cc |
| =================================================================== |
| --- base/file_util_win.cc (revision 69169) |
| +++ base/file_util_win.cc (working copy) |
| @@ -132,41 +132,47 @@ |
| 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. |
|
michaeln
2010/12/17 02:32:03
Should the read-only attribute testing/fixing also
Kavita Kanetkar
2010/12/23 03:14:28
Done.
|
| + DWORD attributes = ::GetFileAttributes(current.value().c_str()); |
| + if (attributes != INVALID_FILE_ATTRIBUTES && |
| + attributes & FILE_ATTRIBUTE_READONLY) { |
| + ::SetFileAttributes(current.value().c_str(), |
| + attributes &~ FILE_ATTRIBUTE_READONLY); |
| + } |
| + 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) { |
| @@ -183,12 +189,6 @@ |
| bool Move(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; |
| - } |
| if (MoveFileEx(from_path.value().c_str(), to_path.value().c_str(), |
| MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING) != 0) |
| return true; |
| @@ -225,72 +225,89 @@ |
| 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) { |
| +bool CopyDirectory(const FilePath& from_path, const FilePath& to_path, |
| + bool recursive) { |
| base::ThreadRestrictions::AssertIOAllowed(); |
| + FilePath real_to_path = to_path; |
| + if (PathExists(real_to_path)) { |
| + if (!real_to_path.StartsWithExtendedPathPrefix() && |
| + !AbsolutePath(&real_to_path)) |
|
michaeln
2010/12/17 02:32:03
Maybe AbsolutePath should test for the extended pa
Kavita Kanetkar
2010/12/23 03:14:28
As discussed, leaving this as-is.
On 2010/12/17 0
|
| + 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; |
|
michaeln
2010/12/17 02:32:03
I think these up front tests could be simplified..
Kavita Kanetkar
2010/12/23 03:14:28
The helper is only for strict parent/child. It won
|
| - // 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) { |
| + bool success = true; |
| + FileEnumerator::FILE_TYPE traverse_type = |
| + static_cast<FileEnumerator::FILE_TYPE>(FileEnumerator::FILES); |
|
michaeln
2010/12/17 02:32:03
Are these static_casts needed?
Kavita Kanetkar
2010/12/23 03:14:28
Yes
On 2010/12/17 02:32:03, michaeln wrote:
|
| + if (recursive) |
| + traverse_type = static_cast<FileEnumerator::FILE_TYPE>( |
| + traverse_type | FileEnumerator::DIRECTORIES); |
| + FileEnumerator traversal(from_path, recursive, traverse_type); |
|
michaeln
2010/12/17 02:32:03
It would be nice to instance this enumerator close
Kavita Kanetkar
2010/12/23 03:14:28
Done.
|
| + FilePath current = from_path; |
|
michaeln
2010/12/17 02:32:03
Why is 'real_from_path' not used here?
|
| + if (!PathExists(from_path)) { |
|
michaeln
2010/12/17 02:32:03
Would be nice to move this 'are the inputs valid'
Kavita Kanetkar
2010/12/23 03:14:28
Done.
|
| + LOG(ERROR) << "CopyDirectory() couldn't get info on source directory: " |
| + << from_path.value(); |
| 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()); |
| + 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(); |
| + } |
| - 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; |
| + // This matches previous shell implementation that assumed that |
| + // non-recursive calls will always have a directory for from_path. |
| + DCHECK(recursive || DirectoryExists(from_path)); |
| - return (SHFileOperation(&file_operation) == 0); |
| -} |
| + 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()]); |
| -bool CopyDirectory(const FilePath& from_path, const FilePath& to_path, |
| - bool recursive) { |
| - base::ThreadRestrictions::AssertIOAllowed(); |
| + // Strip the leading '\' (if any). |
| + if (!suffix.empty()) { |
|
michaeln
2010/12/17 02:32:03
Would 'relative_path' be a better name? Is this ev
Kavita Kanetkar
2010/12/23 03:14:28
Done.
|
| + DCHECK_EQ('\\', suffix[0]); |
| + suffix.erase(0, 1); |
| + } |
| + const FilePath target_path = to_path.Append(suffix); |
| - if (recursive) |
| - return ShellCopy(from_path, to_path, true); |
| - |
| - // 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)) { |
|
michaeln
2010/12/17 02:32:03
Could this system call be avoided by using the fil
Kavita Kanetkar
2010/12/23 03:14:28
Done.
|
| + 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 +325,6 @@ |
| return false; |
| } |
| - |
| bool PathExists(const FilePath& path) { |
| base::ThreadRestrictions::AssertIOAllowed(); |
| return (GetFileAttributes(path.value().c_str()) != INVALID_FILE_ATTRIBUTES); |