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 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 122 // File was created after or on comparison time | 122 // File was created after or on comparison time |
| 123 if ((result == 1) || (result == 0)) | 123 if ((result == 1) || (result == 0)) |
| 124 ++file_count; | 124 ++file_count; |
| 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 // Callers of Delete can call with extended path prefix and with path lengths | |
| 133 // greater than 260. | |
|
kinuko
2010/12/15 00:09:30
Can we put this comment (maybe in a simpler form,
Kavita Kanetkar
2010/12/16 00:48:55
Done.
| |
| 132 bool Delete(const FilePath& path, bool recursive) { | 134 bool Delete(const FilePath& path, bool recursive) { |
| 133 base::ThreadRestrictions::AssertIOAllowed(); | 135 base::ThreadRestrictions::AssertIOAllowed(); |
| 134 | 136 |
| 135 if (path.value().length() >= MAX_PATH) | 137 base::PlatformFileInfo file_info; |
| 136 return false; | 138 if (!GetFileInfo(path, &file_info)) |
| 139 return true; | |
| 137 | 140 |
| 138 if (!recursive) { | 141 if (!file_info.is_directory) |
| 139 // If not recursing, then first check to see if |path| is a directory. | 142 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 | 143 |
| 145 // Otherwise, it's a file, wildcard or non-existant. Try DeleteFile first | 144 if (!recursive) |
| 146 // because it should be faster. If DeleteFile fails, then we fall through | 145 return RemoveDirectory(path.value().c_str()) != 0; |
| 147 // to SHFileOperation, which will do the right thing. | 146 |
| 148 if (DeleteFile(path.value().c_str()) != 0) | 147 // Matches posix version of iterating directories. |
| 149 return true; | 148 bool success = true; |
| 149 std::stack<FilePath::StringType> directories; | |
| 150 directories.push(path.value()); | |
| 151 FileEnumerator traversal(path, true, static_cast<FileEnumerator::FILE_TYPE>( | |
| 152 FileEnumerator::FILES | FileEnumerator::DIRECTORIES)); | |
| 153 | |
| 154 for (FilePath current = traversal.Next(); success && !current.empty(); | |
| 155 current = traversal.Next()) { | |
| 156 FileEnumerator::FindInfo info; | |
| 157 traversal.GetFindInfo(&info); | |
| 158 if (traversal.IsDirectory(info)) { | |
| 159 directories.push(current.value()); | |
| 160 } else { | |
| 161 // This is needed because DeleteFile can fail for read-only files. | |
|
kinuko
2010/12/15 00:09:30
nit: indentation (in this block)
Kavita Kanetkar
2010/12/16 00:48:55
Done.
| |
| 162 uint32 attributes = ::GetFileAttributes(current.value().c_str()); | |
| 163 if (attributes != INVALID_FILE_ATTRIBUTES && | |
|
kinuko
2010/12/15 00:09:30
If it's INVALID_FILE_ATTRIBUTES the following Dele
Kavita Kanetkar
2010/12/16 00:48:55
I thought about it but I feel it's ok to let Delet
| |
| 164 attributes &~ FILE_ATTRIBUTE_READONLY) | |
|
kinuko
2010/12/15 00:09:30
um, you'd want to keep this '&'
| |
| 165 ::SetFileAttributes(current.value().c_str(), FILE_ATTRIBUTE_NORMAL); | |
|
kinuko
2010/12/15 00:09:30
and replace this FILE_ATTRIBUTE_NORMAL with (attri
Kavita Kanetkar
2010/12/16 00:48:55
Oops yes.
On 2010/12/15 00:09:30, kinuko wrote:
| |
| 166 success = (DeleteFile(current.value().c_str()) != 0); | |
| 167 } | |
| 150 } | 168 } |
| 151 | 169 |
| 152 // SHFILEOPSTRUCT wants the path to be terminated with two NULLs, | 170 while (success && !directories.empty()) { |
| 153 // so we have to use wcscpy because wcscpy_s writes non-NULLs | 171 FilePath dir = FilePath(directories.top()); |
| 154 // into the rest of the buffer. | 172 directories.pop(); |
| 155 wchar_t double_terminated_path[MAX_PATH + 1] = {0}; | 173 success = (RemoveDirectory(dir.value().c_str()) != 0); |
| 156 #pragma warning(suppress:4996) // don't complain about wcscpy deprecation | 174 } |
| 157 wcscpy(double_terminated_path, path.value().c_str()); | 175 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 } | 176 } |
| 171 | 177 |
| 172 bool DeleteAfterReboot(const FilePath& path) { | 178 bool DeleteAfterReboot(const FilePath& path) { |
| 173 base::ThreadRestrictions::AssertIOAllowed(); | 179 base::ThreadRestrictions::AssertIOAllowed(); |
| 174 | 180 |
| 175 if (path.value().length() >= MAX_PATH) | 181 if (path.value().length() >= MAX_PATH) |
| 176 return false; | 182 return false; |
| 177 | 183 |
| 178 return MoveFileEx(path.value().c_str(), NULL, | 184 return MoveFileEx(path.value().c_str(), NULL, |
| 179 MOVEFILE_DELAY_UNTIL_REBOOT | | 185 MOVEFILE_DELAY_UNTIL_REBOOT | |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 218 // When writing to a network share, we may not be able to change the ACLs. | 224 // When writing to a network share, we may not be able to change the ACLs. |
| 219 // Ignore ACL errors then (REPLACEFILE_IGNORE_MERGE_ERRORS). | 225 // Ignore ACL errors then (REPLACEFILE_IGNORE_MERGE_ERRORS). |
| 220 return ::ReplaceFile(to_path.value().c_str(), | 226 return ::ReplaceFile(to_path.value().c_str(), |
| 221 from_path.value().c_str(), NULL, | 227 from_path.value().c_str(), NULL, |
| 222 REPLACEFILE_IGNORE_MERGE_ERRORS, NULL, NULL) ? true : false; | 228 REPLACEFILE_IGNORE_MERGE_ERRORS, NULL, NULL) ? true : false; |
| 223 } | 229 } |
| 224 | 230 |
| 225 bool CopyFile(const FilePath& from_path, const FilePath& to_path) { | 231 bool CopyFile(const FilePath& from_path, const FilePath& to_path) { |
| 226 base::ThreadRestrictions::AssertIOAllowed(); | 232 base::ThreadRestrictions::AssertIOAllowed(); |
| 227 | 233 |
| 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(), | 234 return (::CopyFile(from_path.value().c_str(), to_path.value().c_str(), |
| 235 false) != 0); | 235 false) != 0); |
| 236 } | 236 } |
| 237 | 237 |
| 238 bool ShellCopy(const FilePath& from_path, const FilePath& to_path, | 238 // CopyDirectory works for windows extended path lengths that are greater than |
| 239 bool recursive) { | 239 // 260. Such paths are prefixed. |
| 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, | 240 bool CopyDirectory(const FilePath& from_path, const FilePath& to_path, |
| 272 bool recursive) { | 241 bool recursive) { |
| 273 base::ThreadRestrictions::AssertIOAllowed(); | 242 base::ThreadRestrictions::AssertIOAllowed(); |
| 243 FilePath real_to_path = to_path; | |
| 244 if (PathExists(real_to_path)) { | |
| 245 if (!real_to_path.StartsWithExtendedPathPrefix() && | |
|
kinuko
2010/12/15 00:09:30
nit: indentation
Kavita Kanetkar
2010/12/16 00:48:55
Done.
| |
| 246 !AbsolutePath(&real_to_path)) | |
| 247 return false; | |
| 248 } else { | |
| 249 real_to_path = real_to_path.DirName(); | |
| 250 if (!real_to_path.StartsWithExtendedPathPrefix() && | |
| 251 !AbsolutePath(&real_to_path)) | |
| 252 return false; | |
| 253 } | |
| 254 FilePath real_from_path = from_path; | |
| 255 if (!real_from_path.StartsWithExtendedPathPrefix() && | |
| 256 !AbsolutePath(&real_from_path)) | |
| 257 return false; | |
| 258 if (real_to_path.value().size() >= real_from_path.value().size() && | |
| 259 real_to_path.value().compare(0, real_from_path.value().size(), | |
| 260 real_from_path.value()) == 0) | |
| 261 return false; | |
| 274 | 262 |
| 263 bool success = true; | |
| 264 FileEnumerator::FILE_TYPE traverse_type = | |
| 265 static_cast<FileEnumerator::FILE_TYPE>(FileEnumerator::FILES); | |
| 275 if (recursive) | 266 if (recursive) |
| 276 return ShellCopy(from_path, to_path, true); | 267 traverse_type = static_cast<FileEnumerator::FILE_TYPE>( |
| 268 traverse_type | FileEnumerator::DIRECTORIES); | |
| 269 FileEnumerator traversal(from_path, recursive, traverse_type); | |
| 277 | 270 |
| 278 // The following code assumes that from path is a directory. | 271 // |to_path| may not exist yet, start the loop with |to_path|. |
| 279 DCHECK(DirectoryExists(from_path)); | 272 FilePath current = from_path; |
| 280 | 273 if (!PathExists(from_path)) { |
| 281 // Instead of creating a new directory, we copy the old one to include the | 274 LOG(ERROR) << "CopyDirectory() couldn't get info on source directory: " |
| 282 // security information of the folder as part of the copy. | 275 << from_path.value(); |
| 283 if (!PathExists(to_path)) { | 276 success = false; |
| 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 } | 277 } |
| 291 | 278 |
| 292 FilePath directory = from_path.Append(L"*.*"); | 279 FilePath from_path_base = from_path; |
| 293 return ShellCopy(directory, to_path, false); | 280 if (recursive && DirectoryExists(to_path)) { |
| 281 // If the destination already exists and is a directory, then the | |
| 282 // top level of source needs to be copied. | |
| 283 from_path_base = from_path.DirName(); | |
| 284 } | |
| 285 DCHECK(recursive || DirectoryExists(from_path)); | |
|
kinuko
2010/12/15 00:09:30
Now we may want some comment here (as we do in fil
Kavita Kanetkar
2010/12/16 00:48:55
Done.
| |
| 286 while (success && !current.empty()) { | |
| 287 // current is the source path, including from_path, so paste | |
| 288 // the suffix after from_path onto to_path to create the target_path. | |
| 289 FilePath::StringType suffix( | |
| 290 ¤t.value().c_str()[from_path_base.value().size()]); | |
| 291 // Strip the leading '\' (if any). | |
| 292 if (!suffix.empty()) { | |
| 293 DCHECK_EQ('\\', suffix[0]); | |
| 294 suffix.erase(0, 1); | |
| 295 } | |
| 296 const FilePath target_path = to_path.Append(suffix); | |
| 297 | |
| 298 if (DirectoryExists(current)) { | |
| 299 if (::CreateDirectory(target_path.value().c_str(), NULL) == 0) { | |
| 300 if (!DirectoryExists(target_path)) { | |
| 301 LOG(ERROR) << "CopyDirectory() couldn't create directory: " | |
| 302 << target_path.value(); | |
| 303 success = false; | |
| 304 } | |
| 305 } | |
| 306 } else { | |
| 307 if (!CopyFile(current, target_path)) { | |
| 308 LOG(ERROR) << "CopyDirectory() couldn't create file: " | |
| 309 << target_path.value(); | |
| 310 success = false; | |
| 311 } | |
| 312 } | |
| 313 current = traversal.Next(); | |
| 314 } | |
| 315 return success; | |
|
kinuko
2010/12/15 00:09:30
I really wonder if we could just have this one in
| |
| 294 } | 316 } |
| 295 | 317 |
| 296 bool CopyAndDeleteDirectory(const FilePath& from_path, | 318 bool CopyAndDeleteDirectory(const FilePath& from_path, |
| 297 const FilePath& to_path) { | 319 const FilePath& to_path) { |
| 298 base::ThreadRestrictions::AssertIOAllowed(); | 320 base::ThreadRestrictions::AssertIOAllowed(); |
| 299 if (CopyDirectory(from_path, to_path, true)) { | 321 if (CopyDirectory(from_path, to_path, true)) { |
| 300 if (Delete(from_path, true)) { | 322 if (Delete(from_path, true)) { |
| 301 return true; | 323 return true; |
| 302 } | 324 } |
| 303 // Like Move, this function is not transactional, so we just | 325 // Like Move, this function is not transactional, so we just |
| 304 // leave the copied bits behind if deleting from_path fails. | 326 // leave the copied bits behind if deleting from_path fails. |
| 305 // If to_path exists previously then we have already overwritten | 327 // 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. | 328 // it by now, we don't get better off by deleting the new bits. |
| 307 } | 329 } |
| 308 return false; | 330 return false; |
| 309 } | 331 } |
| 310 | 332 |
| 311 | |
| 312 bool PathExists(const FilePath& path) { | 333 bool PathExists(const FilePath& path) { |
| 313 base::ThreadRestrictions::AssertIOAllowed(); | 334 base::ThreadRestrictions::AssertIOAllowed(); |
| 314 return (GetFileAttributes(path.value().c_str()) != INVALID_FILE_ATTRIBUTES); | 335 return (GetFileAttributes(path.value().c_str()) != INVALID_FILE_ATTRIBUTES); |
| 315 } | 336 } |
| 316 | 337 |
| 317 bool PathIsWritable(const FilePath& path) { | 338 bool PathIsWritable(const FilePath& path) { |
| 318 base::ThreadRestrictions::AssertIOAllowed(); | 339 base::ThreadRestrictions::AssertIOAllowed(); |
| 319 HANDLE dir = | 340 HANDLE dir = |
| 320 CreateFile(path.value().c_str(), FILE_ADD_FILE, kFileShareAll, | 341 CreateFile(path.value().c_str(), FILE_ADD_FILE, kFileShareAll, |
| 321 NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); | 342 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); | 1146 uint8 unused = *(touch + offset); |
| 1126 offset += step_size; | 1147 offset += step_size; |
| 1127 } | 1148 } |
| 1128 FreeLibrary(dll_module); | 1149 FreeLibrary(dll_module); |
| 1129 } | 1150 } |
| 1130 | 1151 |
| 1131 return true; | 1152 return true; |
| 1132 } | 1153 } |
| 1133 | 1154 |
| 1134 } // namespace file_util | 1155 } // namespace file_util |
| OLD | NEW |