| 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/file_util.h" | 5 #include "base/file_util.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 #include <psapi.h> | 8 #include <psapi.h> |
| 9 #include <shellapi.h> | 9 #include <shellapi.h> |
| 10 #include <shlobj.h> | 10 #include <shlobj.h> |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 | 84 |
| 85 bool DeleteFile(const FilePath& path, bool recursive) { | 85 bool DeleteFile(const FilePath& path, bool recursive) { |
| 86 ThreadRestrictions::AssertIOAllowed(); | 86 ThreadRestrictions::AssertIOAllowed(); |
| 87 | 87 |
| 88 if (path.value().length() >= MAX_PATH) | 88 if (path.value().length() >= MAX_PATH) |
| 89 return false; | 89 return false; |
| 90 | 90 |
| 91 if (!recursive) { | 91 if (!recursive) { |
| 92 // If not recursing, then first check to see if |path| is a directory. | 92 // If not recursing, then first check to see if |path| is a directory. |
| 93 // If it is, then remove it with RemoveDirectory. | 93 // If it is, then remove it with RemoveDirectory. |
| 94 PlatformFileInfo file_info; | 94 File::Info file_info; |
| 95 if (GetFileInfo(path, &file_info) && file_info.is_directory) | 95 if (GetFileInfo(path, &file_info) && file_info.is_directory) |
| 96 return RemoveDirectory(path.value().c_str()) != 0; | 96 return RemoveDirectory(path.value().c_str()) != 0; |
| 97 | 97 |
| 98 // Otherwise, it's a file, wildcard or non-existant. Try DeleteFile first | 98 // Otherwise, it's a file, wildcard or non-existant. Try DeleteFile first |
| 99 // because it should be faster. If DeleteFile fails, then we fall through | 99 // because it should be faster. If DeleteFile fails, then we fall through |
| 100 // to SHFileOperation, which will do the right thing. | 100 // to SHFileOperation, which will do the right thing. |
| 101 if (::DeleteFile(path.value().c_str()) != 0) | 101 if (::DeleteFile(path.value().c_str()) != 0) |
| 102 return true; | 102 return true; |
| 103 } | 103 } |
| 104 | 104 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 if (path.value().length() >= MAX_PATH) | 136 if (path.value().length() >= MAX_PATH) |
| 137 return false; | 137 return false; |
| 138 | 138 |
| 139 return MoveFileEx(path.value().c_str(), NULL, | 139 return MoveFileEx(path.value().c_str(), NULL, |
| 140 MOVEFILE_DELAY_UNTIL_REBOOT | | 140 MOVEFILE_DELAY_UNTIL_REBOOT | |
| 141 MOVEFILE_REPLACE_EXISTING) != FALSE; | 141 MOVEFILE_REPLACE_EXISTING) != FALSE; |
| 142 } | 142 } |
| 143 | 143 |
| 144 bool ReplaceFile(const FilePath& from_path, | 144 bool ReplaceFile(const FilePath& from_path, |
| 145 const FilePath& to_path, | 145 const FilePath& to_path, |
| 146 PlatformFileError* error) { | 146 File::Error* error) { |
| 147 ThreadRestrictions::AssertIOAllowed(); | 147 ThreadRestrictions::AssertIOAllowed(); |
| 148 // Try a simple move first. It will only succeed when |to_path| doesn't | 148 // Try a simple move first. It will only succeed when |to_path| doesn't |
| 149 // already exist. | 149 // already exist. |
| 150 if (::MoveFile(from_path.value().c_str(), to_path.value().c_str())) | 150 if (::MoveFile(from_path.value().c_str(), to_path.value().c_str())) |
| 151 return true; | 151 return true; |
| 152 // Try the full-blown replace if the move fails, as ReplaceFile will only | 152 // Try the full-blown replace if the move fails, as ReplaceFile will only |
| 153 // succeed when |to_path| does exist. When writing to a network share, we may | 153 // succeed when |to_path| does exist. When writing to a network share, we may |
| 154 // not be able to change the ACLs. Ignore ACL errors then | 154 // not be able to change the ACLs. Ignore ACL errors then |
| 155 // (REPLACEFILE_IGNORE_MERGE_ERRORS). | 155 // (REPLACEFILE_IGNORE_MERGE_ERRORS). |
| 156 if (::ReplaceFile(to_path.value().c_str(), from_path.value().c_str(), NULL, | 156 if (::ReplaceFile(to_path.value().c_str(), from_path.value().c_str(), NULL, |
| 157 REPLACEFILE_IGNORE_MERGE_ERRORS, NULL, NULL)) { | 157 REPLACEFILE_IGNORE_MERGE_ERRORS, NULL, NULL)) { |
| 158 return true; | 158 return true; |
| 159 } | 159 } |
| 160 if (error) | 160 if (error) |
| 161 *error = LastErrorToPlatformFileError(GetLastError()); | 161 *error = File::OSErrorToFileError(GetLastError()); |
| 162 return false; | 162 return false; |
| 163 } | 163 } |
| 164 | 164 |
| 165 bool CopyDirectory(const FilePath& from_path, const FilePath& to_path, | 165 bool CopyDirectory(const FilePath& from_path, const FilePath& to_path, |
| 166 bool recursive) { | 166 bool recursive) { |
| 167 ThreadRestrictions::AssertIOAllowed(); | 167 ThreadRestrictions::AssertIOAllowed(); |
| 168 | 168 |
| 169 if (recursive) | 169 if (recursive) |
| 170 return ShellCopy(from_path, to_path, true); | 170 return ShellCopy(from_path, to_path, true); |
| 171 | 171 |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 ThreadRestrictions::AssertIOAllowed(); | 321 ThreadRestrictions::AssertIOAllowed(); |
| 322 | 322 |
| 323 FilePath system_temp_dir; | 323 FilePath system_temp_dir; |
| 324 if (!GetTempDir(&system_temp_dir)) | 324 if (!GetTempDir(&system_temp_dir)) |
| 325 return false; | 325 return false; |
| 326 | 326 |
| 327 return CreateTemporaryDirInDir(system_temp_dir, prefix, new_temp_path); | 327 return CreateTemporaryDirInDir(system_temp_dir, prefix, new_temp_path); |
| 328 } | 328 } |
| 329 | 329 |
| 330 bool CreateDirectoryAndGetError(const FilePath& full_path, | 330 bool CreateDirectoryAndGetError(const FilePath& full_path, |
| 331 PlatformFileError* error) { | 331 File::Error* error) { |
| 332 ThreadRestrictions::AssertIOAllowed(); | 332 ThreadRestrictions::AssertIOAllowed(); |
| 333 | 333 |
| 334 // If the path exists, we've succeeded if it's a directory, failed otherwise. | 334 // If the path exists, we've succeeded if it's a directory, failed otherwise. |
| 335 const wchar_t* full_path_str = full_path.value().c_str(); | 335 const wchar_t* full_path_str = full_path.value().c_str(); |
| 336 DWORD fileattr = ::GetFileAttributes(full_path_str); | 336 DWORD fileattr = ::GetFileAttributes(full_path_str); |
| 337 if (fileattr != INVALID_FILE_ATTRIBUTES) { | 337 if (fileattr != INVALID_FILE_ATTRIBUTES) { |
| 338 if ((fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0) { | 338 if ((fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0) { |
| 339 DVLOG(1) << "CreateDirectory(" << full_path_str << "), " | 339 DVLOG(1) << "CreateDirectory(" << full_path_str << "), " |
| 340 << "directory already exists."; | 340 << "directory already exists."; |
| 341 return true; | 341 return true; |
| 342 } | 342 } |
| 343 DLOG(WARNING) << "CreateDirectory(" << full_path_str << "), " | 343 DLOG(WARNING) << "CreateDirectory(" << full_path_str << "), " |
| 344 << "conflicts with existing file."; | 344 << "conflicts with existing file."; |
| 345 if (error) { | 345 if (error) { |
| 346 *error = PLATFORM_FILE_ERROR_NOT_A_DIRECTORY; | 346 *error = File::FILE_ERROR_NOT_A_DIRECTORY; |
| 347 } | 347 } |
| 348 return false; | 348 return false; |
| 349 } | 349 } |
| 350 | 350 |
| 351 // Invariant: Path does not exist as file or directory. | 351 // Invariant: Path does not exist as file or directory. |
| 352 | 352 |
| 353 // Attempt to create the parent recursively. This will immediately return | 353 // Attempt to create the parent recursively. This will immediately return |
| 354 // true if it already exists, otherwise will create all required parent | 354 // true if it already exists, otherwise will create all required parent |
| 355 // directories starting with the highest-level missing parent. | 355 // directories starting with the highest-level missing parent. |
| 356 FilePath parent_path(full_path.DirName()); | 356 FilePath parent_path(full_path.DirName()); |
| 357 if (parent_path.value() == full_path.value()) { | 357 if (parent_path.value() == full_path.value()) { |
| 358 if (error) { | 358 if (error) { |
| 359 *error = PLATFORM_FILE_ERROR_NOT_FOUND; | 359 *error = File::FILE_ERROR_NOT_FOUND; |
| 360 } | 360 } |
| 361 return false; | 361 return false; |
| 362 } | 362 } |
| 363 if (!CreateDirectoryAndGetError(parent_path, error)) { | 363 if (!CreateDirectoryAndGetError(parent_path, error)) { |
| 364 DLOG(WARNING) << "Failed to create one of the parent directories."; | 364 DLOG(WARNING) << "Failed to create one of the parent directories."; |
| 365 if (error) { | 365 if (error) { |
| 366 DCHECK(*error != PLATFORM_FILE_OK); | 366 DCHECK(*error != File::FILE_OK); |
| 367 } | 367 } |
| 368 return false; | 368 return false; |
| 369 } | 369 } |
| 370 | 370 |
| 371 if (!::CreateDirectory(full_path_str, NULL)) { | 371 if (!::CreateDirectory(full_path_str, NULL)) { |
| 372 DWORD error_code = ::GetLastError(); | 372 DWORD error_code = ::GetLastError(); |
| 373 if (error_code == ERROR_ALREADY_EXISTS && DirectoryExists(full_path)) { | 373 if (error_code == ERROR_ALREADY_EXISTS && DirectoryExists(full_path)) { |
| 374 // This error code ERROR_ALREADY_EXISTS doesn't indicate whether we | 374 // This error code ERROR_ALREADY_EXISTS doesn't indicate whether we |
| 375 // were racing with someone creating the same directory, or a file | 375 // were racing with someone creating the same directory, or a file |
| 376 // with the same path. If DirectoryExists() returns true, we lost the | 376 // with the same path. If DirectoryExists() returns true, we lost the |
| 377 // race to create the same directory. | 377 // race to create the same directory. |
| 378 return true; | 378 return true; |
| 379 } else { | 379 } else { |
| 380 if (error) | 380 if (error) |
| 381 *error = LastErrorToPlatformFileError(error_code); | 381 *error = File::OSErrorToFileError(error_code); |
| 382 DLOG(WARNING) << "Failed to create directory " << full_path_str | 382 DLOG(WARNING) << "Failed to create directory " << full_path_str |
| 383 << ", last error is " << error_code << "."; | 383 << ", last error is " << error_code << "."; |
| 384 return false; | 384 return false; |
| 385 } | 385 } |
| 386 } else { | 386 } else { |
| 387 return true; | 387 return true; |
| 388 } | 388 } |
| 389 } | 389 } |
| 390 | 390 |
| 391 bool NormalizeFilePath(const FilePath& path, FilePath* real_path) { | 391 bool NormalizeFilePath(const FilePath& path, FilePath* real_path) { |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 498 ::UnmapViewOfFile(file_view); | 498 ::UnmapViewOfFile(file_view); |
| 499 return success; | 499 return success; |
| 500 } | 500 } |
| 501 | 501 |
| 502 // TODO(rkc): Work out if we want to handle NTFS junctions here or not, handle | 502 // TODO(rkc): Work out if we want to handle NTFS junctions here or not, handle |
| 503 // them if we do decide to. | 503 // them if we do decide to. |
| 504 bool IsLink(const FilePath& file_path) { | 504 bool IsLink(const FilePath& file_path) { |
| 505 return false; | 505 return false; |
| 506 } | 506 } |
| 507 | 507 |
| 508 bool GetFileInfo(const FilePath& file_path, PlatformFileInfo* results) { | 508 bool GetFileInfo(const FilePath& file_path, File::Info* results) { |
| 509 ThreadRestrictions::AssertIOAllowed(); | 509 ThreadRestrictions::AssertIOAllowed(); |
| 510 | 510 |
| 511 WIN32_FILE_ATTRIBUTE_DATA attr; | 511 WIN32_FILE_ATTRIBUTE_DATA attr; |
| 512 if (!GetFileAttributesEx(file_path.value().c_str(), | 512 if (!GetFileAttributesEx(file_path.value().c_str(), |
| 513 GetFileExInfoStandard, &attr)) { | 513 GetFileExInfoStandard, &attr)) { |
| 514 return false; | 514 return false; |
| 515 } | 515 } |
| 516 | 516 |
| 517 ULARGE_INTEGER size; | 517 ULARGE_INTEGER size; |
| 518 size.HighPart = attr.nFileSizeHigh; | 518 size.HighPart = attr.nFileSizeHigh; |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 742 // Like Move, this function is not transactional, so we just | 742 // Like Move, this function is not transactional, so we just |
| 743 // leave the copied bits behind if deleting from_path fails. | 743 // leave the copied bits behind if deleting from_path fails. |
| 744 // If to_path exists previously then we have already overwritten | 744 // If to_path exists previously then we have already overwritten |
| 745 // it by now, we don't get better off by deleting the new bits. | 745 // it by now, we don't get better off by deleting the new bits. |
| 746 } | 746 } |
| 747 return false; | 747 return false; |
| 748 } | 748 } |
| 749 | 749 |
| 750 } // namespace internal | 750 } // namespace internal |
| 751 } // namespace base | 751 } // namespace base |
| OLD | NEW |