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 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 85 return L""; | 85 return L""; |
| 86 | 86 |
| 87 std::wstring::size_type length = | 87 std::wstring::size_type length = |
| 88 file_ptr ? file_ptr - path_buffer : path.length(); | 88 file_ptr ? file_ptr - path_buffer : path.length(); |
| 89 std::wstring directory(path, 0, length); | 89 std::wstring directory(path, 0, length); |
| 90 return FilePath(directory).StripTrailingSeparators().value(); | 90 return FilePath(directory).StripTrailingSeparators().value(); |
| 91 } | 91 } |
| 92 | 92 |
| 93 bool AbsolutePath(FilePath* path) { | 93 bool AbsolutePath(FilePath* path) { |
| 94 base::ThreadRestrictions::AssertIOAllowed(); | 94 base::ThreadRestrictions::AssertIOAllowed(); |
| 95 wchar_t file_path_buf[MAX_PATH]; | 95 wchar_t file_path_buf[MAX_PATH]; |
|
kinuko
2010/12/10 23:03:43
If we use any of these long-path unsafe methods we
| |
| 96 if (!_wfullpath(file_path_buf, path->value().c_str(), MAX_PATH)) | 96 if (!_wfullpath(file_path_buf, path->value().c_str(), MAX_PATH)) |
| 97 return false; | 97 return false; |
| 98 *path = FilePath(file_path_buf); | 98 *path = FilePath(file_path_buf); |
| 99 return true; | 99 return true; |
| 100 } | 100 } |
| 101 | 101 |
| 102 int CountFilesCreatedAfter(const FilePath& path, | 102 int CountFilesCreatedAfter(const FilePath& path, |
| 103 const base::Time& comparison_time) { | 103 const base::Time& comparison_time) { |
| 104 base::ThreadRestrictions::AssertIOAllowed(); | 104 base::ThreadRestrictions::AssertIOAllowed(); |
| 105 | 105 |
| (...skipping 19 matching lines...) Expand all 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; | |
| 138 | |
| 139 // Otherwise, it's a file. Try DeleteFile first because it should be faster. | |
|
michaeln
2010/12/10 20:08:44
comment doesn't add any value and its somewhat sta
Kavita Kanetkar
2010/12/10 22:44:57
Done.
| |
| 140 if (!file_info.is_directory) | |
| 141 return (DeleteFile(path.value().c_str()) != 0); | |
| 137 | 142 |
| 138 if (!recursive) { | 143 if (!recursive) { |
| 139 // If not recursing, then first check to see if |path| is a directory. | 144 // If not recursing, then first check to see if |path| is a directory. |
| 140 // If it is, then remove it with RemoveDirectory. | 145 // If it is, then remove it with RemoveDirectory. |
|
michaeln
2010/12/10 20:08:44
comment doesn't add any value, stating the obvious
Kavita Kanetkar
2010/12/10 22:44:57
Done.
| |
| 141 base::PlatformFileInfo file_info; | 146 return RemoveDirectory(path.value().c_str()) != 0; |
|
michaeln
2010/12/10 20:08:44
indent is off by one and {}'s are needed for this
Kavita Kanetkar
2010/12/10 22:44:57
Done.
| |
| 142 if (GetFileInfo(path, &file_info) && file_info.is_directory) | |
| 143 return RemoveDirectory(path.value().c_str()) != 0; | |
| 144 | |
| 145 // Otherwise, it's a file, wildcard or non-existant. Try DeleteFile first | |
| 146 // because it should be faster. If DeleteFile fails, then we fall through | |
| 147 // to SHFileOperation, which will do the right thing. | |
| 148 if (DeleteFile(path.value().c_str()) != 0) | |
| 149 return true; | |
| 150 } | 147 } |
| 151 | 148 |
| 152 // SHFILEOPSTRUCT wants the path to be terminated with two NULLs, | 149 // Matches posix version of iterating directories. |
|
michaeln
2010/12/10 20:08:44
How does this loop recurse into the child director
Kavita Kanetkar
2010/12/10 22:44:57
FileEnumerator takes care of the full depth. I add
| |
| 153 // so we have to use wcscpy because wcscpy_s writes non-NULLs | 150 bool success = true; |
| 154 // into the rest of the buffer. | 151 std::stack<FilePath::StringType> directories; |
|
michaeln
2010/12/10 20:08:44
Would it make sense to have this be a stack<FilePa
Kavita Kanetkar
2010/12/10 22:44:57
I looked at posix. I liked stack<string> more than
| |
| 155 wchar_t double_terminated_path[MAX_PATH + 1] = {0}; | 152 directories.push(path.value()); |
| 156 #pragma warning(suppress:4996) // don't complain about wcscpy deprecation | 153 FileEnumerator traversal(path, true, static_cast<FileEnumerator::FILE_TYPE>( |
| 157 wcscpy(double_terminated_path, path.value().c_str()); | 154 FileEnumerator::FILES | FileEnumerator::DIRECTORIES)); |
| 158 | 155 |
| 159 SHFILEOPSTRUCT file_operation = {0}; | 156 for (FilePath current = traversal.Next(); success && !current.empty(); |
| 160 file_operation.wFunc = FO_DELETE; | 157 current = traversal.Next()) { |
| 161 file_operation.pFrom = double_terminated_path; | 158 FileEnumerator::FindInfo info; |
| 162 file_operation.fFlags = FOF_NOERRORUI | FOF_SILENT | FOF_NOCONFIRMATION; | 159 traversal.GetFindInfo(&info); |
| 163 if (!recursive) | 160 if (traversal.IsDirectory(info)) { |
| 164 file_operation.fFlags |= FOF_NORECURSION | FOF_FILESONLY; | 161 directories.push(current.value()); |
| 165 int err = SHFileOperation(&file_operation); | 162 } |
| 166 // Some versions of Windows return ERROR_FILE_NOT_FOUND (0x2) when deleting | 163 else { |
|
michaeln
2010/12/10 20:08:44
'else {' should be up on the previous line
Kavita Kanetkar
2010/12/10 22:44:57
Done.
| |
| 167 // an empty directory and some return 0x402 when they should be returning | 164 uint32 attributes = ::GetFileAttributes(current.value().c_str()); |
| 168 // ERROR_FILE_NOT_FOUND. MSDN says Vista and up won't return 0x402. | 165 if (attributes != INVALID_FILE_ATTRIBUTES && |
| 169 return (err == 0 || err == ERROR_FILE_NOT_FOUND || err == 0x402); | 166 attributes & FILE_ATTRIBUTE_READONLY) |
| 167 DCHECK(::SetFileAttributes(current.value().c_str(), | |
|
michaeln
2010/12/10 20:08:44
I don't think SetFileAttributes() call will execut
Kavita Kanetkar
2010/12/10 22:44:57
Done.
| |
| 168 FILE_ATTRIBUTE_NORMAL)); | |
|
kinuko
2010/12/10 23:03:43
Shouldn't we call this with (attribute &~ FILE_ATT
Kavita Kanetkar
2010/12/14 21:36:37
Actually since file was being deleted anyway, I di
| |
| 169 success = (DeleteFile(current.value().c_str()) != 0); | |
| 170 } | |
| 171 } | |
| 172 | |
| 173 while (success && !directories.empty()) { | |
| 174 FilePath dir = FilePath(directories.top()); | |
| 175 directories.pop(); | |
| 176 success = (RemoveDirectory(dir.value().c_str()) != 0); | |
| 177 } | |
| 178 | |
| 179 return success; | |
| 170 } | 180 } |
| 171 | 181 |
| 172 bool DeleteAfterReboot(const FilePath& path) { | 182 bool DeleteAfterReboot(const FilePath& path) { |
| 173 base::ThreadRestrictions::AssertIOAllowed(); | 183 base::ThreadRestrictions::AssertIOAllowed(); |
| 174 | 184 |
| 175 if (path.value().length() >= MAX_PATH) | 185 if (path.value().length() >= MAX_PATH) |
| 176 return false; | 186 return false; |
| 177 | 187 |
| 178 return MoveFileEx(path.value().c_str(), NULL, | 188 return MoveFileEx(path.value().c_str(), NULL, |
| 179 MOVEFILE_DELAY_UNTIL_REBOOT | | 189 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. | 228 // When writing to a network share, we may not be able to change the ACLs. |
| 219 // Ignore ACL errors then (REPLACEFILE_IGNORE_MERGE_ERRORS). | 229 // Ignore ACL errors then (REPLACEFILE_IGNORE_MERGE_ERRORS). |
| 220 return ::ReplaceFile(to_path.value().c_str(), | 230 return ::ReplaceFile(to_path.value().c_str(), |
| 221 from_path.value().c_str(), NULL, | 231 from_path.value().c_str(), NULL, |
| 222 REPLACEFILE_IGNORE_MERGE_ERRORS, NULL, NULL) ? true : false; | 232 REPLACEFILE_IGNORE_MERGE_ERRORS, NULL, NULL) ? true : false; |
| 223 } | 233 } |
| 224 | 234 |
| 225 bool CopyFile(const FilePath& from_path, const FilePath& to_path) { | 235 bool CopyFile(const FilePath& from_path, const FilePath& to_path) { |
| 226 base::ThreadRestrictions::AssertIOAllowed(); | 236 base::ThreadRestrictions::AssertIOAllowed(); |
| 227 | 237 |
| 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(), | 238 return (::CopyFile(from_path.value().c_str(), to_path.value().c_str(), |
| 235 false) != 0); | 239 false) != 0); |
| 236 } | 240 } |
| 237 | 241 |
| 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, | 242 bool CopyDirectory(const FilePath& from_path, const FilePath& to_path, |
| 272 bool recursive) { | 243 bool recursive) { |
| 273 base::ThreadRestrictions::AssertIOAllowed(); | 244 base::ThreadRestrictions::AssertIOAllowed(); |
| 274 | 245 |
| 246 FilePath real_to_path = to_path; | |
| 247 if (PathExists(real_to_path)) { | |
| 248 if (!AbsolutePath(&real_to_path)) | |
|
kinuko
2010/12/10 23:03:43
...and here we're calling AbsolutePath. It is not
| |
| 249 return false; | |
| 250 } else { | |
| 251 real_to_path = real_to_path.DirName(); | |
| 252 if (!AbsolutePath(&real_to_path)) | |
| 253 return false; | |
| 254 } | |
| 255 FilePath real_from_path = from_path; | |
| 256 if (!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; | |
| 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 | |
| 286 // The Windows version of this function assumes that non-recursive calls | |
| 287 // will always have a directory for from_path. | |
| 288 | |
| 289 DCHECK(recursive || DirectoryExists(from_path)); | |
| 290 while (success && !current.empty()) { | |
| 291 // current is the source path, including from_path, so paste | |
| 292 // the suffix after from_path onto to_path to create the target_path. | |
| 293 FilePath::StringType suffix( | |
|
kinuko
2010/12/10 23:03:43
nit: indent
| |
| 294 ¤t.value().c_str()[from_path_base.value().size()]); | |
| 295 // Strip the leading '\' (if any). | |
| 296 if (!suffix.empty()) { | |
| 297 DCHECK_EQ('\\', suffix[0]); | |
| 298 suffix.erase(0, 1); | |
| 299 } | |
| 300 const FilePath target_path = to_path.Append(suffix); | |
| 301 | |
| 302 if (DirectoryExists(current)) { | |
| 303 if (::CreateDirectory(target_path.value().c_str(), NULL) == 0) { | |
| 304 if (!DirectoryExists(target_path)) { | |
| 305 LOG(ERROR) << "CopyDirectory() couldn't create directory: " << | |
|
kinuko
2010/12/10 23:03:43
nit: would better put this << in the next line, in
| |
| 306 target_path.value(); | |
| 307 success = false; | |
| 308 } | |
| 309 } | |
|
kinuko
2010/12/10 23:03:43
indent (entire this block)
| |
| 310 } else { | |
| 311 if (!CopyFile(current, target_path)) { | |
| 312 LOG(ERROR) << "CopyDirectory() couldn't create file: " << | |
| 313 target_path.value(); | |
| 314 success = false; | |
| 315 } | |
| 316 } | |
| 317 current = traversal.Next(); | |
| 318 } | |
| 319 return success; | |
| 294 } | 320 } |
| 295 | 321 |
| 296 bool CopyAndDeleteDirectory(const FilePath& from_path, | 322 bool CopyAndDeleteDirectory(const FilePath& from_path, |
| 297 const FilePath& to_path) { | 323 const FilePath& to_path) { |
| 298 base::ThreadRestrictions::AssertIOAllowed(); | 324 base::ThreadRestrictions::AssertIOAllowed(); |
| 299 if (CopyDirectory(from_path, to_path, true)) { | 325 if (CopyDirectory(from_path, to_path, true)) { |
| 300 if (Delete(from_path, true)) { | 326 if (Delete(from_path, true)) { |
| 301 return true; | 327 return true; |
| 302 } | 328 } |
| 303 // Like Move, this function is not transactional, so we just | 329 // Like Move, this function is not transactional, so we just |
| 304 // leave the copied bits behind if deleting from_path fails. | 330 // leave the copied bits behind if deleting from_path fails. |
| 305 // If to_path exists previously then we have already overwritten | 331 // 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. | 332 // it by now, we don't get better off by deleting the new bits. |
| 307 } | 333 } |
| 308 return false; | 334 return false; |
| 309 } | 335 } |
| 310 | 336 |
| 311 | |
| 312 bool PathExists(const FilePath& path) { | 337 bool PathExists(const FilePath& path) { |
| 313 base::ThreadRestrictions::AssertIOAllowed(); | 338 base::ThreadRestrictions::AssertIOAllowed(); |
| 314 return (GetFileAttributes(path.value().c_str()) != INVALID_FILE_ATTRIBUTES); | 339 return (GetFileAttributes(path.value().c_str()) != INVALID_FILE_ATTRIBUTES); |
| 315 } | 340 } |
| 316 | 341 |
| 317 bool PathIsWritable(const FilePath& path) { | 342 bool PathIsWritable(const FilePath& path) { |
| 318 base::ThreadRestrictions::AssertIOAllowed(); | 343 base::ThreadRestrictions::AssertIOAllowed(); |
| 319 HANDLE dir = | 344 HANDLE dir = |
| 320 CreateFile(path.value().c_str(), FILE_ADD_FILE, kFileShareAll, | 345 CreateFile(path.value().c_str(), FILE_ADD_FILE, kFileShareAll, |
| 321 NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); | 346 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); | 1150 uint8 unused = *(touch + offset); |
| 1126 offset += step_size; | 1151 offset += step_size; |
| 1127 } | 1152 } |
| 1128 FreeLibrary(dll_module); | 1153 FreeLibrary(dll_module); |
| 1129 } | 1154 } |
| 1130 | 1155 |
| 1131 return true; | 1156 return true; |
| 1132 } | 1157 } |
| 1133 | 1158 |
| 1134 } // namespace file_util | 1159 } // namespace file_util |
| OLD | NEW |