Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <propvarutil.h> | 8 #include <propvarutil.h> |
| 9 #include <psapi.h> | 9 #include <psapi.h> |
| 10 #include <shellapi.h> | 10 #include <shellapi.h> |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 125 } while (FindNextFile(find_handle, &find_file_data)); | 125 } while (FindNextFile(find_handle, &find_file_data)); |
| 126 FindClose(find_handle); | 126 FindClose(find_handle); |
| 127 } | 127 } |
| 128 | 128 |
| 129 return file_count; | 129 return file_count; |
| 130 } | 130 } |
| 131 | 131 |
| 132 bool Delete(const FilePath& path, bool recursive) { | 132 bool Delete(const FilePath& path, bool recursive) { |
| 133 base::ThreadRestrictions::AssertIOAllowed(); | 133 base::ThreadRestrictions::AssertIOAllowed(); |
| 134 | 134 |
| 135 if (path.value().length() >= MAX_PATH) | 135 base::PlatformFileInfo file_info; |
| 136 return false; | 136 if (!GetFileInfo(path, &file_info)) |
| 137 return true; | |
| 137 | 138 |
| 138 if (!recursive) { | 139 if (!file_info.is_directory) |
| 139 // If not recursing, then first check to see if |path| is a directory. | 140 return (DeleteFile(path.value().c_str()) != 0); |
| 140 // If it is, then remove it with RemoveDirectory. | |
| 141 base::PlatformFileInfo file_info; | |
| 142 if (GetFileInfo(path, &file_info) && file_info.is_directory) | |
| 143 return RemoveDirectory(path.value().c_str()) != 0; | |
| 144 | 141 |
| 145 // Otherwise, it's a file, wildcard or non-existant. Try DeleteFile first | 142 if (!recursive) |
| 146 // because it should be faster. If DeleteFile fails, then we fall through | 143 return RemoveDirectory(path.value().c_str()) != 0; |
| 147 // to SHFileOperation, which will do the right thing. | 144 |
| 148 if (DeleteFile(path.value().c_str()) != 0) | 145 // Matches posix version of iterating directories. |
| 149 return true; | 146 bool success = true; |
| 147 std::stack<FilePath::StringType> directories; | |
| 148 directories.push(path.value()); | |
| 149 FileEnumerator traversal(path, true, static_cast<FileEnumerator::FILE_TYPE>( | |
| 150 FileEnumerator::FILES | FileEnumerator::DIRECTORIES)); | |
| 151 | |
| 152 for (FilePath current = traversal.Next(); success && !current.empty(); | |
| 153 current = traversal.Next()) { | |
| 154 FileEnumerator::FindInfo info; | |
| 155 traversal.GetFindInfo(&info); | |
| 156 if (traversal.IsDirectory(info)) { | |
| 157 directories.push(current.value()); | |
| 158 } else { | |
| 159 // This is needed because DeleteFile can fail for read-only files. | |
|
Erik does not do reviews
2010/12/16 18:34:54
doesn't this mean that we need the same logic abov
Kavita Kanetkar
2010/12/17 00:59:32
DeleteFile resolves to
BOOL WINAPI DeleteFile(
| |
| 160 DWORD attributes = ::GetFileAttributes(current.value().c_str()); | |
| 161 if (attributes != INVALID_FILE_ATTRIBUTES && | |
| 162 attributes & FILE_ATTRIBUTE_READONLY) | |
|
Erik does not do reviews
2010/12/16 18:34:54
multiline conditional plus multiline body means yo
Kavita Kanetkar
2010/12/17 00:59:32
Done.
| |
| 163 ::SetFileAttributes(current.value().c_str(), | |
| 164 attributes &~ FILE_ATTRIBUTE_READONLY); | |
| 165 success = (DeleteFile(current.value().c_str()) != 0); | |
| 166 } | |
| 150 } | 167 } |
| 151 | 168 |
| 152 // SHFILEOPSTRUCT wants the path to be terminated with two NULLs, | 169 while (success && !directories.empty()) { |
| 153 // so we have to use wcscpy because wcscpy_s writes non-NULLs | 170 FilePath dir = FilePath(directories.top()); |
| 154 // into the rest of the buffer. | 171 directories.pop(); |
| 155 wchar_t double_terminated_path[MAX_PATH + 1] = {0}; | 172 success = (RemoveDirectory(dir.value().c_str()) != 0); |
| 156 #pragma warning(suppress:4996) // don't complain about wcscpy deprecation | 173 } |
| 157 wcscpy(double_terminated_path, path.value().c_str()); | 174 return success; |
| 158 | |
| 159 SHFILEOPSTRUCT file_operation = {0}; | |
| 160 file_operation.wFunc = FO_DELETE; | |
| 161 file_operation.pFrom = double_terminated_path; | |
| 162 file_operation.fFlags = FOF_NOERRORUI | FOF_SILENT | FOF_NOCONFIRMATION; | |
| 163 if (!recursive) | |
| 164 file_operation.fFlags |= FOF_NORECURSION | FOF_FILESONLY; | |
| 165 int err = SHFileOperation(&file_operation); | |
| 166 // Some versions of Windows return ERROR_FILE_NOT_FOUND (0x2) when deleting | |
| 167 // an empty directory and some return 0x402 when they should be returning | |
| 168 // ERROR_FILE_NOT_FOUND. MSDN says Vista and up won't return 0x402. | |
| 169 return (err == 0 || err == ERROR_FILE_NOT_FOUND || err == 0x402); | |
| 170 } | 175 } |
| 171 | 176 |
| 172 bool DeleteAfterReboot(const FilePath& path) { | 177 bool DeleteAfterReboot(const FilePath& path) { |
| 173 base::ThreadRestrictions::AssertIOAllowed(); | 178 base::ThreadRestrictions::AssertIOAllowed(); |
| 174 | 179 |
| 175 if (path.value().length() >= MAX_PATH) | 180 if (path.value().length() >= MAX_PATH) |
| 176 return false; | 181 return false; |
| 177 | 182 |
| 178 return MoveFileEx(path.value().c_str(), NULL, | 183 return MoveFileEx(path.value().c_str(), NULL, |
| 179 MOVEFILE_DELAY_UNTIL_REBOOT | | 184 MOVEFILE_DELAY_UNTIL_REBOOT | |
| 180 MOVEFILE_REPLACE_EXISTING) != FALSE; | 185 MOVEFILE_REPLACE_EXISTING) != FALSE; |
| 181 } | 186 } |
| 182 | 187 |
| 183 bool Move(const FilePath& from_path, const FilePath& to_path) { | 188 bool Move(const FilePath& from_path, const FilePath& to_path) { |
| 184 base::ThreadRestrictions::AssertIOAllowed(); | 189 base::ThreadRestrictions::AssertIOAllowed(); |
| 185 | 190 |
| 186 // NOTE: I suspect we could support longer paths, but that would involve | |
| 187 // analyzing all our usage of files. | |
| 188 if (from_path.value().length() >= MAX_PATH || | |
| 189 to_path.value().length() >= MAX_PATH) { | |
| 190 return false; | |
| 191 } | |
| 192 if (MoveFileEx(from_path.value().c_str(), to_path.value().c_str(), | 191 if (MoveFileEx(from_path.value().c_str(), to_path.value().c_str(), |
| 193 MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING) != 0) | 192 MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING) != 0) |
| 194 return true; | 193 return true; |
| 195 if (DirectoryExists(from_path)) { | 194 if (DirectoryExists(from_path)) { |
| 196 // MoveFileEx fails if moving directory across volumes. We will simulate | 195 // MoveFileEx fails if moving directory across volumes. We will simulate |
| 197 // the move by using Copy and Delete. Ideally we could check whether | 196 // the move by using Copy and Delete. Ideally we could check whether |
| 198 // from_path and to_path are indeed in different volumes. | 197 // from_path and to_path are indeed in different volumes. |
| 199 return CopyAndDeleteDirectory(from_path, to_path); | 198 return CopyAndDeleteDirectory(from_path, to_path); |
| 200 } | 199 } |
| 201 return false; | 200 return false; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 218 // When writing to a network share, we may not be able to change the ACLs. | 217 // When writing to a network share, we may not be able to change the ACLs. |
| 219 // Ignore ACL errors then (REPLACEFILE_IGNORE_MERGE_ERRORS). | 218 // Ignore ACL errors then (REPLACEFILE_IGNORE_MERGE_ERRORS). |
| 220 return ::ReplaceFile(to_path.value().c_str(), | 219 return ::ReplaceFile(to_path.value().c_str(), |
| 221 from_path.value().c_str(), NULL, | 220 from_path.value().c_str(), NULL, |
| 222 REPLACEFILE_IGNORE_MERGE_ERRORS, NULL, NULL) ? true : false; | 221 REPLACEFILE_IGNORE_MERGE_ERRORS, NULL, NULL) ? true : false; |
| 223 } | 222 } |
| 224 | 223 |
| 225 bool CopyFile(const FilePath& from_path, const FilePath& to_path) { | 224 bool CopyFile(const FilePath& from_path, const FilePath& to_path) { |
| 226 base::ThreadRestrictions::AssertIOAllowed(); | 225 base::ThreadRestrictions::AssertIOAllowed(); |
| 227 | 226 |
| 228 // NOTE: I suspect we could support longer paths, but that would involve | |
| 229 // analyzing all our usage of files. | |
| 230 if (from_path.value().length() >= MAX_PATH || | |
| 231 to_path.value().length() >= MAX_PATH) { | |
| 232 return false; | |
| 233 } | |
| 234 return (::CopyFile(from_path.value().c_str(), to_path.value().c_str(), | 227 return (::CopyFile(from_path.value().c_str(), to_path.value().c_str(), |
| 235 false) != 0); | 228 false) != 0); |
| 236 } | 229 } |
| 237 | 230 |
| 238 bool ShellCopy(const FilePath& from_path, const FilePath& to_path, | |
| 239 bool recursive) { | |
| 240 base::ThreadRestrictions::AssertIOAllowed(); | |
| 241 | |
| 242 // NOTE: I suspect we could support longer paths, but that would involve | |
| 243 // analyzing all our usage of files. | |
| 244 if (from_path.value().length() >= MAX_PATH || | |
| 245 to_path.value().length() >= MAX_PATH) { | |
| 246 return false; | |
| 247 } | |
| 248 | |
| 249 // SHFILEOPSTRUCT wants the path to be terminated with two NULLs, | |
| 250 // so we have to use wcscpy because wcscpy_s writes non-NULLs | |
| 251 // into the rest of the buffer. | |
| 252 wchar_t double_terminated_path_from[MAX_PATH + 1] = {0}; | |
| 253 wchar_t double_terminated_path_to[MAX_PATH + 1] = {0}; | |
| 254 #pragma warning(suppress:4996) // don't complain about wcscpy deprecation | |
| 255 wcscpy(double_terminated_path_from, from_path.value().c_str()); | |
| 256 #pragma warning(suppress:4996) // don't complain about wcscpy deprecation | |
| 257 wcscpy(double_terminated_path_to, to_path.value().c_str()); | |
| 258 | |
| 259 SHFILEOPSTRUCT file_operation = {0}; | |
| 260 file_operation.wFunc = FO_COPY; | |
| 261 file_operation.pFrom = double_terminated_path_from; | |
| 262 file_operation.pTo = double_terminated_path_to; | |
| 263 file_operation.fFlags = FOF_NOERRORUI | FOF_SILENT | FOF_NOCONFIRMATION | | |
| 264 FOF_NOCONFIRMMKDIR; | |
| 265 if (!recursive) | |
| 266 file_operation.fFlags |= FOF_NORECURSION | FOF_FILESONLY; | |
| 267 | |
| 268 return (SHFileOperation(&file_operation) == 0); | |
| 269 } | |
| 270 | |
| 271 bool CopyDirectory(const FilePath& from_path, const FilePath& to_path, | 231 bool CopyDirectory(const FilePath& from_path, const FilePath& to_path, |
| 272 bool recursive) { | 232 bool recursive) { |
| 273 base::ThreadRestrictions::AssertIOAllowed(); | 233 base::ThreadRestrictions::AssertIOAllowed(); |
| 234 FilePath real_to_path = to_path; | |
| 235 if (PathExists(real_to_path)) { | |
| 236 if (!real_to_path.StartsWithExtendedPathPrefix() && | |
| 237 !AbsolutePath(&real_to_path)) | |
|
Erik does not do reviews
2010/12/16 18:34:54
This seems to imply that AbsolutePath returns the
Kavita Kanetkar
2010/12/17 00:59:32
AbsolutePath currently creates:
wchar_t file_path_
| |
| 238 return false; | |
| 239 } else { | |
| 240 real_to_path = real_to_path.DirName(); | |
| 241 if (!real_to_path.StartsWithExtendedPathPrefix() && | |
| 242 !AbsolutePath(&real_to_path)) | |
| 243 return false; | |
| 244 } | |
| 245 FilePath real_from_path = from_path; | |
| 246 if (!real_from_path.StartsWithExtendedPathPrefix() && | |
| 247 !AbsolutePath(&real_from_path)) | |
| 248 return false; | |
| 249 if (real_to_path.value().size() >= real_from_path.value().size() && | |
|
Erik does not do reviews
2010/12/16 18:34:54
Is this just testing whether from is an ancestor o
Kavita Kanetkar
2010/12/17 00:59:32
I don't think it handles cases such as:
to_path =
| |
| 250 real_to_path.value().compare(0, real_from_path.value().size(), | |
| 251 real_from_path.value()) == 0) | |
| 252 return false; | |
| 274 | 253 |
| 254 bool success = true; | |
| 255 FileEnumerator::FILE_TYPE traverse_type = | |
| 256 static_cast<FileEnumerator::FILE_TYPE>(FileEnumerator::FILES); | |
| 275 if (recursive) | 257 if (recursive) |
| 276 return ShellCopy(from_path, to_path, true); | 258 traverse_type = static_cast<FileEnumerator::FILE_TYPE>( |
| 259 traverse_type | FileEnumerator::DIRECTORIES); | |
| 260 FileEnumerator traversal(from_path, recursive, traverse_type); | |
| 277 | 261 |
| 278 // The following code assumes that from path is a directory. | 262 // |to_path| may not exist yet, start the loop with |to_path|. |
|
Erik does not do reviews
2010/12/16 18:34:54
this comment doesn't seem right
Kavita Kanetkar
2010/12/17 00:59:32
Done.
| |
| 279 DCHECK(DirectoryExists(from_path)); | 263 FilePath current = from_path; |
| 280 | 264 if (!PathExists(from_path)) { |
| 281 // Instead of creating a new directory, we copy the old one to include the | 265 LOG(ERROR) << "CopyDirectory() couldn't get info on source directory: " |
| 282 // security information of the folder as part of the copy. | 266 << from_path.value(); |
| 283 if (!PathExists(to_path)) { | 267 success = false; |
|
Erik does not do reviews
2010/12/16 18:34:54
Why does the code continue from here? The next li
Kavita Kanetkar
2010/12/17 00:59:32
Done.
| |
| 284 // Except that Vista fails to do that, and instead do a recursive copy if | |
| 285 // the target directory doesn't exist. | |
| 286 if (base::win::GetVersion() >= base::win::VERSION_VISTA) | |
| 287 CreateDirectory(to_path); | |
| 288 else | |
| 289 ShellCopy(from_path, to_path, false); | |
| 290 } | 268 } |
| 291 | 269 |
| 292 FilePath directory = from_path.Append(L"*.*"); | 270 FilePath from_path_base = from_path; |
| 293 return ShellCopy(directory, to_path, false); | 271 if (recursive && DirectoryExists(to_path)) { |
| 272 // If the destination already exists and is a directory, then the | |
| 273 // top level of source needs to be copied. | |
| 274 from_path_base = from_path.DirName(); | |
| 275 } | |
| 276 | |
| 277 // This matches previous shell implementation that assumed that | |
|
Erik does not do reviews
2010/12/16 18:34:54
does the POSIX impl match this behavior as well?
Kavita Kanetkar
2010/12/17 00:59:32
Yes. Actually previous shell32 implementation matc
| |
| 278 // non-recursive calls will always have a directory for from_path. | |
| 279 DCHECK(recursive || DirectoryExists(from_path)); | |
| 280 | |
| 281 while (success && !current.empty()) { | |
| 282 // current is the source path, including from_path, so paste | |
| 283 // the suffix after from_path onto to_path to create the target_path. | |
| 284 FilePath::StringType suffix( | |
| 285 ¤t.value().c_str()[from_path_base.value().size()]); | |
| 286 // Strip the leading '\' (if any). | |
|
Erik does not do reviews
2010/12/16 18:34:54
nit: newline above //
Kavita Kanetkar
2010/12/17 00:59:32
Done.
| |
| 287 if (!suffix.empty()) { | |
| 288 DCHECK_EQ('\\', suffix[0]); | |
| 289 suffix.erase(0, 1); | |
| 290 } | |
| 291 const FilePath target_path = to_path.Append(suffix); | |
| 292 | |
| 293 if (DirectoryExists(current)) { | |
| 294 if (::CreateDirectory(target_path.value().c_str(), NULL) == 0) { | |
|
Erik does not do reviews
2010/12/16 18:34:54
doesn't this mean that if CreateDirectory fails, t
Kavita Kanetkar
2010/12/17 00:59:32
No. This part of the code checks if target directo
| |
| 295 if (!DirectoryExists(target_path)) { | |
| 296 LOG(ERROR) << "CopyDirectory() couldn't create directory: " | |
| 297 << target_path.value(); | |
| 298 success = false; | |
| 299 } | |
| 300 } | |
| 301 } else { | |
| 302 if (!CopyFile(current, target_path)) { | |
| 303 LOG(ERROR) << "CopyDirectory() couldn't create file: " | |
| 304 << target_path.value(); | |
| 305 success = false; | |
| 306 } | |
| 307 } | |
| 308 current = traversal.Next(); | |
| 309 } | |
| 310 return success; | |
| 294 } | 311 } |
| 295 | 312 |
| 296 bool CopyAndDeleteDirectory(const FilePath& from_path, | 313 bool CopyAndDeleteDirectory(const FilePath& from_path, |
| 297 const FilePath& to_path) { | 314 const FilePath& to_path) { |
| 298 base::ThreadRestrictions::AssertIOAllowed(); | 315 base::ThreadRestrictions::AssertIOAllowed(); |
| 299 if (CopyDirectory(from_path, to_path, true)) { | 316 if (CopyDirectory(from_path, to_path, true)) { |
| 300 if (Delete(from_path, true)) { | 317 if (Delete(from_path, true)) { |
| 301 return true; | 318 return true; |
| 302 } | 319 } |
| 303 // Like Move, this function is not transactional, so we just | 320 // Like Move, this function is not transactional, so we just |
| 304 // leave the copied bits behind if deleting from_path fails. | 321 // leave the copied bits behind if deleting from_path fails. |
| 305 // If to_path exists previously then we have already overwritten | 322 // If to_path exists previously then we have already overwritten |
| 306 // it by now, we don't get better off by deleting the new bits. | 323 // it by now, we don't get better off by deleting the new bits. |
| 307 } | 324 } |
| 308 return false; | 325 return false; |
| 309 } | 326 } |
| 310 | 327 |
| 311 | |
| 312 bool PathExists(const FilePath& path) { | 328 bool PathExists(const FilePath& path) { |
| 313 base::ThreadRestrictions::AssertIOAllowed(); | 329 base::ThreadRestrictions::AssertIOAllowed(); |
| 314 return (GetFileAttributes(path.value().c_str()) != INVALID_FILE_ATTRIBUTES); | 330 return (GetFileAttributes(path.value().c_str()) != INVALID_FILE_ATTRIBUTES); |
| 315 } | 331 } |
| 316 | 332 |
| 317 bool PathIsWritable(const FilePath& path) { | 333 bool PathIsWritable(const FilePath& path) { |
| 318 base::ThreadRestrictions::AssertIOAllowed(); | 334 base::ThreadRestrictions::AssertIOAllowed(); |
| 319 HANDLE dir = | 335 HANDLE dir = |
| 320 CreateFile(path.value().c_str(), FILE_ADD_FILE, kFileShareAll, | 336 CreateFile(path.value().c_str(), FILE_ADD_FILE, kFileShareAll, |
| 321 NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); | 337 NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| (...skipping 803 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1125 uint8 unused = *(touch + offset); | 1141 uint8 unused = *(touch + offset); |
| 1126 offset += step_size; | 1142 offset += step_size; |
| 1127 } | 1143 } |
| 1128 FreeLibrary(dll_module); | 1144 FreeLibrary(dll_module); |
| 1129 } | 1145 } |
| 1130 | 1146 |
| 1131 return true; | 1147 return true; |
| 1132 } | 1148 } |
| 1133 | 1149 |
| 1134 } // namespace file_util | 1150 } // namespace file_util |
| OLD | NEW |