| Index: base/file_util_win.cc
|
| ===================================================================
|
| --- base/file_util_win.cc (revision 69859)
|
| +++ base/file_util_win.cc (working copy)
|
| @@ -33,6 +33,18 @@
|
| const DWORD kFileShareAll =
|
| FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
|
|
|
| +bool RemoveReadOnlyAttribute(const FilePath& path) {
|
| + // This helper for DeleteFile which can fail for read-only files.
|
| + DWORD attributes = ::GetFileAttributes(path.value().c_str());
|
| + if (attributes == INVALID_FILE_ATTRIBUTES)
|
| + return false;
|
| + if (attributes & FILE_ATTRIBUTE_READONLY) {
|
| + ::SetFileAttributes(path.value().c_str(),
|
| + attributes &~ FILE_ATTRIBUTE_READONLY);
|
| + }
|
| + return true;
|
| +}
|
| +
|
| // Helper for NormalizeFilePath(), defined below.
|
| bool DevicePathToDriveLetterPath(const FilePath& device_path,
|
| FilePath* drive_letter_path) {
|
| @@ -132,49 +144,44 @@
|
| 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;
|
| -
|
| - // 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 (!file_info.is_directory) {
|
| + if (!RemoveReadOnlyAttribute(path))
|
| + return false;
|
| + return (DeleteFile(path.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);
|
| + return RemoveDirectory(path.value().c_str()) != 0;
|
|
|
| - // Since we're passing flags to the operation telling it to be silent,
|
| - // it's possible for the operation to be aborted/cancelled without err
|
| - // being set (although MSDN doesn't give any scenarios for how this can
|
| - // happen). See MSDN for SHFileOperation and SHFILEOPTSTRUCT.
|
| - if (file_operation.fAnyOperationsAborted)
|
| - return false;
|
| + // 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));
|
|
|
| - // 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);
|
| + 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 {
|
| + if (!RemoveReadOnlyAttribute(current))
|
| + return false;
|
| + success = (DeleteFile(current.value().c_str()) != 0);
|
| + }
|
| + }
|
| + 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) {
|
| @@ -191,12 +198,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;
|
| @@ -233,72 +234,88 @@
|
| 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 (!AbsolutePath(&real_to_path))
|
| + return false;
|
| + } else {
|
| + real_to_path = real_to_path.DirName();
|
| + if (!AbsolutePath(&real_to_path))
|
| + return false;
|
| + }
|
| + FilePath real_from_path = from_path;
|
| + if (!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;
|
|
|
| - // 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) {
|
| + if (!PathExists(from_path)) {
|
| + 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);
|
| -}
|
| -
|
| -bool CopyDirectory(const FilePath& from_path, const FilePath& to_path,
|
| - bool recursive) {
|
| - base::ThreadRestrictions::AssertIOAllowed();
|
| -
|
| + bool success = true;
|
| + FileEnumerator::FILE_TYPE traverse_type =
|
| + static_cast<FileEnumerator::FILE_TYPE>(FileEnumerator::FILES);
|
| if (recursive)
|
| - return ShellCopy(from_path, to_path, true);
|
| + traverse_type = static_cast<FileEnumerator::FILE_TYPE>(
|
| + traverse_type | FileEnumerator::DIRECTORIES);
|
| + FileEnumerator traversal(from_path, recursive, traverse_type);
|
| + FilePath current = from_path;
|
| + 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 relative_path_suffix(
|
| + ¤t.value().c_str()[from_path_base.value().size()]);
|
|
|
| - // The following code assumes that from path is a directory.
|
| - DCHECK(DirectoryExists(from_path));
|
| + // Strip the leading '\' (if any).
|
| + if (!relative_path_suffix.empty()) {
|
| + DCHECK_EQ('\\', relative_path_suffix[0]);
|
| + relative_path_suffix.erase(0, 1);
|
| + }
|
| + const FilePath target_path = to_path.Append(relative_path_suffix);
|
|
|
| - // 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);
|
| + FileEnumerator::FindInfo info;
|
| + traversal.GetFindInfo(&info);
|
| + if (traversal.IsDirectory(info)) {
|
| + 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,
|
| @@ -316,7 +333,6 @@
|
| return false;
|
| }
|
|
|
| -
|
| bool PathExists(const FilePath& path) {
|
| base::ThreadRestrictions::AssertIOAllowed();
|
| return (GetFileAttributes(path.value().c_str()) != INVALID_FILE_ATTRIBUTES);
|
|
|