[email protected] | 71b7da7 | 2012-01-17 22:44:31 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 4 | |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 5 | #include "base/files/file_util.h" |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 6 | |
| 7 | #include <windows.h> |
[email protected] | 4d35828 | 2014-06-10 11:19:50 | [diff] [blame] | 8 | #include <io.h> |
[email protected] | 6f5f432 | 2010-06-09 22:56:48 | [diff] [blame] | 9 | #include <psapi.h> |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 10 | #include <shellapi.h> |
| 11 | #include <shlobj.h> |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 12 | #include <stddef.h> |
avi | ab0f6cce | 2015-11-25 21:01:57 | [diff] [blame] | 13 | #include <stdint.h> |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 14 | #include <time.h> |
tfarina | 060df7e | 2015-12-16 05:15:32 | [diff] [blame] | 15 | #include <winsock2.h> |
[email protected] | f1ddaa4 | 2011-07-19 05:03:03 | [diff] [blame] | 16 | |
[email protected] | 5948030 | 2013-02-21 03:24:08 | [diff] [blame] | 17 | #include <algorithm> |
[email protected] | f1ddaa4 | 2011-07-19 05:03:03 | [diff] [blame] | 18 | #include <limits> |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 19 | #include <string> |
| 20 | |
[email protected] | 0d378d2b | 2014-02-05 19:55:52 | [diff] [blame] | 21 | #include "base/files/file_enumerator.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 22 | #include "base/files/file_path.h" |
chengx | 170f1cf | 2017-04-06 22:46:57 | [diff] [blame] | 23 | #include "base/guid.h" |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 24 | #include "base/logging.h" |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 25 | #include "base/macros.h" |
Greg Thompson | 51ebae1 | 2018-05-08 17:43:16 | [diff] [blame] | 26 | #include "base/metrics/histogram_functions.h" |
[email protected] | dd4b5126 | 2013-07-25 21:38:23 | [diff] [blame] | 27 | #include "base/process/process_handle.h" |
[email protected] | cd86bca | 2013-06-17 21:06:33 | [diff] [blame] | 28 | #include "base/rand_util.h" |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 29 | #include "base/strings/string_number_conversions.h" |
Greg Thompson | 51ebae1 | 2018-05-08 17:43:16 | [diff] [blame] | 30 | #include "base/strings/string_piece.h" |
[email protected] | 251cd6e5 | 2013-06-11 13:36:37 | [diff] [blame] | 31 | #include "base/strings/string_util.h" |
[email protected] | a4ea1f1 | 2013-06-07 18:37:07 | [diff] [blame] | 32 | #include "base/strings/utf_string_conversions.h" |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 33 | #include "base/threading/scoped_blocking_call.h" |
[email protected] | 99084f6 | 2013-06-28 00:49:07 | [diff] [blame] | 34 | #include "base/time/time.h" |
[email protected] | 58b7c5a6 | 2011-08-15 13:09:27 | [diff] [blame] | 35 | #include "base/win/scoped_handle.h" |
[email protected] | 935aa54 | 2010-10-15 01:59:15 | [diff] [blame] | 36 | #include "base/win/windows_version.h" |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 37 | |
[email protected] | 1547693 | 2013-04-12 05:17:15 | [diff] [blame] | 38 | namespace base { |
| 39 | |
[email protected] | 6f5f432 | 2010-06-09 22:56:48 | [diff] [blame] | 40 | namespace { |
| 41 | |
[email protected] | 53af568e | 2010-08-04 17:10:17 | [diff] [blame] | 42 | const DWORD kFileShareAll = |
| 43 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; |
| 44 | |
Greg Thompson | 51ebae1 | 2018-05-08 17:43:16 | [diff] [blame] | 45 | // Records a sample in a histogram named |
| 46 | // "Windows.PostOperationState.|operation|" indicating the state of |path| |
| 47 | // following the named operation. If |operation_succeeded| is true, the |
| 48 | // "operation succeeded" sample is recorded. Otherwise, the state of |path| is |
| 49 | // queried and the most meaningful sample is recorded. |
| 50 | void RecordPostOperationState(const FilePath& path, |
| 51 | StringPiece operation, |
| 52 | bool operation_succeeded) { |
| 53 | // The state of a filesystem item after an operation. |
| 54 | // These values are persisted to logs. Entries should not be renumbered and |
| 55 | // numeric values should never be reused. |
| 56 | enum class PostOperationState { |
| 57 | kOperationSucceeded = 0, |
| 58 | kFileNotFoundAfterFailure = 1, |
| 59 | kPathNotFoundAfterFailure = 2, |
| 60 | kAccessDeniedAfterFailure = 3, |
| 61 | kNoAttributesAfterFailure = 4, |
| 62 | kEmptyDirectoryAfterFailure = 5, |
| 63 | kNonEmptyDirectoryAfterFailure = 6, |
| 64 | kNotDirectoryAfterFailure = 7, |
| 65 | kCount |
| 66 | } metric = PostOperationState::kOperationSucceeded; |
| 67 | |
| 68 | if (!operation_succeeded) { |
| 69 | const DWORD attributes = ::GetFileAttributes(path.value().c_str()); |
| 70 | if (attributes == INVALID_FILE_ATTRIBUTES) { |
| 71 | // On failure to delete, one might expect the file/directory to still be |
| 72 | // in place. Slice a failure to get its attributes into a few common error |
| 73 | // buckets. |
| 74 | const DWORD error_code = ::GetLastError(); |
| 75 | if (error_code == ERROR_FILE_NOT_FOUND) |
| 76 | metric = PostOperationState::kFileNotFoundAfterFailure; |
| 77 | else if (error_code == ERROR_PATH_NOT_FOUND) |
| 78 | metric = PostOperationState::kPathNotFoundAfterFailure; |
| 79 | else if (error_code == ERROR_ACCESS_DENIED) |
| 80 | metric = PostOperationState::kAccessDeniedAfterFailure; |
| 81 | else |
| 82 | metric = PostOperationState::kNoAttributesAfterFailure; |
| 83 | } else if (attributes & FILE_ATTRIBUTE_DIRECTORY) { |
| 84 | if (IsDirectoryEmpty(path)) |
| 85 | metric = PostOperationState::kEmptyDirectoryAfterFailure; |
| 86 | else |
| 87 | metric = PostOperationState::kNonEmptyDirectoryAfterFailure; |
| 88 | } else { |
| 89 | metric = PostOperationState::kNotDirectoryAfterFailure; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | std::string histogram_name = "Windows.PostOperationState."; |
| 94 | operation.AppendToString(&histogram_name); |
| 95 | UmaHistogramEnumeration(histogram_name, metric, PostOperationState::kCount); |
| 96 | } |
| 97 | |
| 98 | // Records the sample |error| in a histogram named |
| 99 | // "Windows.FilesystemError.|operation|". |
| 100 | void RecordFilesystemError(StringPiece operation, DWORD error) { |
| 101 | std::string histogram_name = "Windows.FilesystemError."; |
| 102 | operation.AppendToString(&histogram_name); |
| 103 | UmaHistogramSparse(histogram_name, error); |
| 104 | } |
| 105 | |
Greg Thompson | 9278098 | 2018-05-24 18:20:39 | [diff] [blame] | 106 | // Returns the Win32 last error code or ERROR_SUCCESS if the last error code is |
| 107 | // ERROR_FILE_NOT_FOUND or ERROR_PATH_NOT_FOUND. This is useful in cases where |
| 108 | // the absence of a file or path is a success condition (e.g., when attempting |
| 109 | // to delete an item in the filesystem). |
| 110 | DWORD ReturnLastErrorOrSuccessOnNotFound() { |
| 111 | const DWORD error_code = ::GetLastError(); |
| 112 | return (error_code == ERROR_FILE_NOT_FOUND || |
| 113 | error_code == ERROR_PATH_NOT_FOUND) |
| 114 | ? ERROR_SUCCESS |
| 115 | : error_code; |
| 116 | } |
| 117 | |
jschuh | a1d3baae | 2015-10-23 00:48:28 | [diff] [blame] | 118 | // Deletes all files and directories in a path. |
Greg Thompson | 51ebae1 | 2018-05-08 17:43:16 | [diff] [blame] | 119 | // Returns ERROR_SUCCESS on success or the Windows error code corresponding to |
Greg Thompson | 9278098 | 2018-05-24 18:20:39 | [diff] [blame] | 120 | // the first error encountered. ERROR_FILE_NOT_FOUND and ERROR_PATH_NOT_FOUND |
| 121 | // are considered success conditions, and are therefore never returned. |
Greg Thompson | 51ebae1 | 2018-05-08 17:43:16 | [diff] [blame] | 122 | DWORD DeleteFileRecursive(const FilePath& path, |
| 123 | const FilePath::StringType& pattern, |
| 124 | bool recursive) { |
jschuh | a1d3baae | 2015-10-23 00:48:28 | [diff] [blame] | 125 | FileEnumerator traversal(path, false, |
| 126 | FileEnumerator::FILES | FileEnumerator::DIRECTORIES, |
| 127 | pattern); |
Greg Thompson | 51ebae1 | 2018-05-08 17:43:16 | [diff] [blame] | 128 | DWORD result = ERROR_SUCCESS; |
jschuh | a1d3baae | 2015-10-23 00:48:28 | [diff] [blame] | 129 | for (FilePath current = traversal.Next(); !current.empty(); |
| 130 | current = traversal.Next()) { |
| 131 | // Try to clear the read-only bit if we find it. |
| 132 | FileEnumerator::FileInfo info = traversal.GetInfo(); |
| 133 | if ((info.find_data().dwFileAttributes & FILE_ATTRIBUTE_READONLY) && |
| 134 | (recursive || !info.IsDirectory())) { |
Greg Thompson | 51ebae1 | 2018-05-08 17:43:16 | [diff] [blame] | 135 | ::SetFileAttributes( |
jschuh | a1d3baae | 2015-10-23 00:48:28 | [diff] [blame] | 136 | current.value().c_str(), |
| 137 | info.find_data().dwFileAttributes & ~FILE_ATTRIBUTE_READONLY); |
| 138 | } |
| 139 | |
Greg Thompson | 51ebae1 | 2018-05-08 17:43:16 | [diff] [blame] | 140 | DWORD this_result = ERROR_SUCCESS; |
jschuh | a1d3baae | 2015-10-23 00:48:28 | [diff] [blame] | 141 | if (info.IsDirectory()) { |
Greg Thompson | 51ebae1 | 2018-05-08 17:43:16 | [diff] [blame] | 142 | if (recursive) { |
| 143 | this_result = DeleteFileRecursive(current, pattern, true); |
Greg Thompson | 9278098 | 2018-05-24 18:20:39 | [diff] [blame] | 144 | DCHECK_NE(static_cast<LONG>(this_result), ERROR_FILE_NOT_FOUND); |
| 145 | DCHECK_NE(static_cast<LONG>(this_result), ERROR_PATH_NOT_FOUND); |
Greg Thompson | 51ebae1 | 2018-05-08 17:43:16 | [diff] [blame] | 146 | if (this_result == ERROR_SUCCESS && |
| 147 | !::RemoveDirectory(current.value().c_str())) { |
Greg Thompson | 9278098 | 2018-05-24 18:20:39 | [diff] [blame] | 148 | this_result = ReturnLastErrorOrSuccessOnNotFound(); |
Greg Thompson | 51ebae1 | 2018-05-08 17:43:16 | [diff] [blame] | 149 | } |
| 150 | } |
jschuh | a1d3baae | 2015-10-23 00:48:28 | [diff] [blame] | 151 | } else if (!::DeleteFile(current.value().c_str())) { |
Greg Thompson | 9278098 | 2018-05-24 18:20:39 | [diff] [blame] | 152 | this_result = ReturnLastErrorOrSuccessOnNotFound(); |
jschuh | a1d3baae | 2015-10-23 00:48:28 | [diff] [blame] | 153 | } |
Greg Thompson | 51ebae1 | 2018-05-08 17:43:16 | [diff] [blame] | 154 | if (result == ERROR_SUCCESS) |
| 155 | result = this_result; |
jschuh | a1d3baae | 2015-10-23 00:48:28 | [diff] [blame] | 156 | } |
Greg Thompson | 51ebae1 | 2018-05-08 17:43:16 | [diff] [blame] | 157 | return result; |
jschuh | a1d3baae | 2015-10-23 00:48:28 | [diff] [blame] | 158 | } |
| 159 | |
grt | d0bc44f | 2017-02-13 17:52:17 | [diff] [blame] | 160 | // Appends |mode_char| to |mode| before the optional character set encoding; see |
| 161 | // https://msdn.microsoft.com/library/yeby3zcb.aspx for details. |
| 162 | void AppendModeCharacter(base::char16 mode_char, base::string16* mode) { |
| 163 | size_t comma_pos = mode->find(L','); |
| 164 | mode->insert(comma_pos == base::string16::npos ? mode->length() : comma_pos, |
| 165 | 1, mode_char); |
| 166 | } |
| 167 | |
Eric Caruso | 128f0dd | 2017-12-16 01:32:49 | [diff] [blame] | 168 | bool DoCopyFile(const FilePath& from_path, |
| 169 | const FilePath& to_path, |
| 170 | bool fail_if_exists) { |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 171 | ScopedBlockingCall scoped_blocking_call(BlockingType::MAY_BLOCK); |
Eric Caruso | 128f0dd | 2017-12-16 01:32:49 | [diff] [blame] | 172 | if (from_path.ReferencesParent() || to_path.ReferencesParent()) |
[email protected] | ad56b50 | 2010-12-21 15:47:54 | [diff] [blame] | 173 | return false; |
| 174 | |
Eric Caruso | 128f0dd | 2017-12-16 01:32:49 | [diff] [blame] | 175 | // NOTE: I suspect we could support longer paths, but that would involve |
| 176 | // analyzing all our usage of files. |
| 177 | if (from_path.value().length() >= MAX_PATH || |
| 178 | to_path.value().length() >= MAX_PATH) { |
jschuh | a1d3baae | 2015-10-23 00:48:28 | [diff] [blame] | 179 | return false; |
| 180 | } |
jschuh | a1d3baae | 2015-10-23 00:48:28 | [diff] [blame] | 181 | |
Eric Caruso | 128f0dd | 2017-12-16 01:32:49 | [diff] [blame] | 182 | // Unlike the posix implementation that copies the file manually and discards |
| 183 | // the ACL bits, CopyFile() copies the complete SECURITY_DESCRIPTOR and access |
| 184 | // bits, which is usually not what we want. We can't do much about the |
| 185 | // SECURITY_DESCRIPTOR but at least remove the read only bit. |
| 186 | const wchar_t* dest = to_path.value().c_str(); |
| 187 | if (!::CopyFile(from_path.value().c_str(), dest, fail_if_exists)) { |
| 188 | // Copy failed. |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 189 | return false; |
Eric Caruso | 128f0dd | 2017-12-16 01:32:49 | [diff] [blame] | 190 | } |
| 191 | DWORD attrs = GetFileAttributes(dest); |
| 192 | if (attrs == INVALID_FILE_ATTRIBUTES) { |
| 193 | return false; |
| 194 | } |
| 195 | if (attrs & FILE_ATTRIBUTE_READONLY) { |
| 196 | SetFileAttributes(dest, attrs & ~FILE_ATTRIBUTE_READONLY); |
| 197 | } |
| 198 | return true; |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 199 | } |
| 200 | |
Eric Caruso | 128f0dd | 2017-12-16 01:32:49 | [diff] [blame] | 201 | bool DoCopyDirectory(const FilePath& from_path, |
| 202 | const FilePath& to_path, |
| 203 | bool recursive, |
| 204 | bool fail_if_exists) { |
[email protected] | 0d378d2b | 2014-02-05 19:55:52 | [diff] [blame] | 205 | // NOTE(maruel): Previous version of this function used to call |
| 206 | // SHFileOperation(). This used to copy the file attributes and extended |
| 207 | // attributes, OLE structured storage, NTFS file system alternate data |
| 208 | // streams, SECURITY_DESCRIPTOR. In practice, this is not what we want, we |
| 209 | // want the containing directory to propagate its SECURITY_DESCRIPTOR. |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 210 | ScopedBlockingCall scoped_blocking_call(BlockingType::MAY_BLOCK); |
[email protected] | 45446a5 | 2010-11-04 17:41:00 | [diff] [blame] | 211 | |
[email protected] | 0d378d2b | 2014-02-05 19:55:52 | [diff] [blame] | 212 | // NOTE: I suspect we could support longer paths, but that would involve |
| 213 | // analyzing all our usage of files. |
| 214 | if (from_path.value().length() >= MAX_PATH || |
| 215 | to_path.value().length() >= MAX_PATH) { |
| 216 | return false; |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 217 | } |
| 218 | |
[email protected] | 0d378d2b | 2014-02-05 19:55:52 | [diff] [blame] | 219 | // This function does not properly handle destinations within the source. |
| 220 | FilePath real_to_path = to_path; |
| 221 | if (PathExists(real_to_path)) { |
| 222 | real_to_path = MakeAbsoluteFilePath(real_to_path); |
| 223 | if (real_to_path.empty()) |
| 224 | return false; |
| 225 | } else { |
| 226 | real_to_path = MakeAbsoluteFilePath(real_to_path.DirName()); |
| 227 | if (real_to_path.empty()) |
| 228 | return false; |
| 229 | } |
| 230 | FilePath real_from_path = MakeAbsoluteFilePath(from_path); |
| 231 | if (real_from_path.empty()) |
| 232 | return false; |
chengx | 5f1289d | 2016-12-16 03:38:32 | [diff] [blame] | 233 | if (real_to_path == real_from_path || real_from_path.IsParent(real_to_path)) |
[email protected] | 0d378d2b | 2014-02-05 19:55:52 | [diff] [blame] | 234 | return false; |
[email protected] | 0d378d2b | 2014-02-05 19:55:52 | [diff] [blame] | 235 | |
| 236 | int traverse_type = FileEnumerator::FILES; |
| 237 | if (recursive) |
| 238 | traverse_type |= FileEnumerator::DIRECTORIES; |
| 239 | FileEnumerator traversal(from_path, recursive, traverse_type); |
| 240 | |
| 241 | if (!PathExists(from_path)) { |
| 242 | DLOG(ERROR) << "CopyDirectory() couldn't stat source directory: " |
| 243 | << from_path.value().c_str(); |
| 244 | return false; |
| 245 | } |
| 246 | // TODO(maruel): This is not necessary anymore. |
| 247 | DCHECK(recursive || DirectoryExists(from_path)); |
| 248 | |
| 249 | FilePath current = from_path; |
| 250 | bool from_is_dir = DirectoryExists(from_path); |
| 251 | bool success = true; |
| 252 | FilePath from_path_base = from_path; |
| 253 | if (recursive && DirectoryExists(to_path)) { |
| 254 | // If the destination already exists and is a directory, then the |
| 255 | // top level of source needs to be copied. |
| 256 | from_path_base = from_path.DirName(); |
| 257 | } |
| 258 | |
| 259 | while (success && !current.empty()) { |
| 260 | // current is the source path, including from_path, so append |
| 261 | // the suffix after from_path to to_path to create the target_path. |
| 262 | FilePath target_path(to_path); |
| 263 | if (from_path_base != current) { |
| 264 | if (!from_path_base.AppendRelativePath(current, &target_path)) { |
| 265 | success = false; |
| 266 | break; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | if (from_is_dir) { |
| 271 | if (!DirectoryExists(target_path) && |
| 272 | !::CreateDirectory(target_path.value().c_str(), NULL)) { |
| 273 | DLOG(ERROR) << "CopyDirectory() couldn't create directory: " |
| 274 | << target_path.value().c_str(); |
| 275 | success = false; |
| 276 | } |
Eric Caruso | 128f0dd | 2017-12-16 01:32:49 | [diff] [blame] | 277 | } else if (!DoCopyFile(current, target_path, fail_if_exists)) { |
[email protected] | 0d378d2b | 2014-02-05 19:55:52 | [diff] [blame] | 278 | DLOG(ERROR) << "CopyDirectory() couldn't create file: " |
| 279 | << target_path.value().c_str(); |
| 280 | success = false; |
| 281 | } |
| 282 | |
| 283 | current = traversal.Next(); |
| 284 | if (!current.empty()) |
| 285 | from_is_dir = traversal.GetInfo().IsDirectory(); |
| 286 | } |
| 287 | |
| 288 | return success; |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 289 | } |
| 290 | |
Greg Thompson | 51ebae1 | 2018-05-08 17:43:16 | [diff] [blame] | 291 | // Returns ERROR_SUCCESS on success, or a Windows error code on failure. |
| 292 | DWORD DoDeleteFile(const FilePath& path, bool recursive) { |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 293 | ScopedBlockingCall scoped_blocking_call(BlockingType::MAY_BLOCK); |
Greg Thompson | 51ebae1 | 2018-05-08 17:43:16 | [diff] [blame] | 294 | |
| 295 | if (path.empty()) |
| 296 | return ERROR_SUCCESS; |
| 297 | |
| 298 | if (path.value().length() >= MAX_PATH) |
| 299 | return ERROR_BAD_PATHNAME; |
| 300 | |
| 301 | // Handle any path with wildcards. |
| 302 | if (path.BaseName().value().find_first_of(L"*?") != |
| 303 | FilePath::StringType::npos) { |
Greg Thompson | 9278098 | 2018-05-24 18:20:39 | [diff] [blame] | 304 | const DWORD error_code = |
| 305 | DeleteFileRecursive(path.DirName(), path.BaseName().value(), recursive); |
| 306 | DCHECK_NE(static_cast<LONG>(error_code), ERROR_FILE_NOT_FOUND); |
| 307 | DCHECK_NE(static_cast<LONG>(error_code), ERROR_PATH_NOT_FOUND); |
| 308 | return error_code; |
Greg Thompson | 51ebae1 | 2018-05-08 17:43:16 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | // Report success if the file or path does not exist. |
| 312 | const DWORD attr = ::GetFileAttributes(path.value().c_str()); |
Greg Thompson | 9278098 | 2018-05-24 18:20:39 | [diff] [blame] | 313 | if (attr == INVALID_FILE_ATTRIBUTES) |
| 314 | return ReturnLastErrorOrSuccessOnNotFound(); |
Greg Thompson | 51ebae1 | 2018-05-08 17:43:16 | [diff] [blame] | 315 | |
| 316 | // Clear the read-only bit if it is set. |
| 317 | if ((attr & FILE_ATTRIBUTE_READONLY) && |
| 318 | !::SetFileAttributes(path.value().c_str(), |
| 319 | attr & ~FILE_ATTRIBUTE_READONLY)) { |
Greg Thompson | 9278098 | 2018-05-24 18:20:39 | [diff] [blame] | 320 | // It's possible for |path| to be gone now under a race with other deleters. |
| 321 | return ReturnLastErrorOrSuccessOnNotFound(); |
Greg Thompson | 51ebae1 | 2018-05-08 17:43:16 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | // Perform a simple delete on anything that isn't a directory. |
| 325 | if (!(attr & FILE_ATTRIBUTE_DIRECTORY)) { |
Greg Thompson | 9278098 | 2018-05-24 18:20:39 | [diff] [blame] | 326 | return ::DeleteFile(path.value().c_str()) |
| 327 | ? ERROR_SUCCESS |
| 328 | : ReturnLastErrorOrSuccessOnNotFound(); |
Greg Thompson | 51ebae1 | 2018-05-08 17:43:16 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | if (recursive) { |
| 332 | const DWORD error_code = DeleteFileRecursive(path, L"*", true); |
Greg Thompson | 9278098 | 2018-05-24 18:20:39 | [diff] [blame] | 333 | DCHECK_NE(static_cast<LONG>(error_code), ERROR_FILE_NOT_FOUND); |
| 334 | DCHECK_NE(static_cast<LONG>(error_code), ERROR_PATH_NOT_FOUND); |
Greg Thompson | 51ebae1 | 2018-05-08 17:43:16 | [diff] [blame] | 335 | if (error_code != ERROR_SUCCESS) |
| 336 | return error_code; |
| 337 | } |
Greg Thompson | 9278098 | 2018-05-24 18:20:39 | [diff] [blame] | 338 | return ::RemoveDirectory(path.value().c_str()) |
| 339 | ? ERROR_SUCCESS |
| 340 | : ReturnLastErrorOrSuccessOnNotFound(); |
Greg Thompson | 51ebae1 | 2018-05-08 17:43:16 | [diff] [blame] | 341 | } |
| 342 | |
Eric Caruso | 128f0dd | 2017-12-16 01:32:49 | [diff] [blame] | 343 | } // namespace |
| 344 | |
| 345 | FilePath MakeAbsoluteFilePath(const FilePath& input) { |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 346 | ScopedBlockingCall scoped_blocking_call(BlockingType::MAY_BLOCK); |
Eric Caruso | 128f0dd | 2017-12-16 01:32:49 | [diff] [blame] | 347 | wchar_t file_path[MAX_PATH]; |
| 348 | if (!_wfullpath(file_path, input.value().c_str(), MAX_PATH)) |
| 349 | return FilePath(); |
| 350 | return FilePath(file_path); |
| 351 | } |
| 352 | |
| 353 | bool DeleteFile(const FilePath& path, bool recursive) { |
Greg Thompson | 51ebae1 | 2018-05-08 17:43:16 | [diff] [blame] | 354 | static constexpr char kRecursive[] = "DeleteFile.Recursive"; |
| 355 | static constexpr char kNonRecursive[] = "DeleteFile.NonRecursive"; |
| 356 | const StringPiece operation(recursive ? kRecursive : kNonRecursive); |
| 357 | |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 358 | ScopedBlockingCall scoped_blocking_call(BlockingType::MAY_BLOCK); |
Eric Caruso | 128f0dd | 2017-12-16 01:32:49 | [diff] [blame] | 359 | |
Greg Thompson | 51ebae1 | 2018-05-08 17:43:16 | [diff] [blame] | 360 | // Metrics for delete failures tracked in https://crbug.com/599084. Delete may |
| 361 | // fail for a number of reasons. Log some metrics relating to failures in the |
| 362 | // current code so that any improvements or regressions resulting from |
| 363 | // subsequent code changes can be detected. |
| 364 | const DWORD error = DoDeleteFile(path, recursive); |
| 365 | RecordPostOperationState(path, operation, error == ERROR_SUCCESS); |
| 366 | if (error == ERROR_SUCCESS) |
Eric Caruso | 128f0dd | 2017-12-16 01:32:49 | [diff] [blame] | 367 | return true; |
| 368 | |
Greg Thompson | 51ebae1 | 2018-05-08 17:43:16 | [diff] [blame] | 369 | RecordFilesystemError(operation, error); |
Eric Caruso | 128f0dd | 2017-12-16 01:32:49 | [diff] [blame] | 370 | return false; |
| 371 | } |
| 372 | |
| 373 | bool DeleteFileAfterReboot(const FilePath& path) { |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 374 | ScopedBlockingCall scoped_blocking_call(BlockingType::MAY_BLOCK); |
Eric Caruso | 128f0dd | 2017-12-16 01:32:49 | [diff] [blame] | 375 | |
| 376 | if (path.value().length() >= MAX_PATH) |
| 377 | return false; |
| 378 | |
S. Ganesh | c18d593 | 2018-11-05 03:45:31 | [diff] [blame] | 379 | return ::MoveFileEx(path.value().c_str(), nullptr, |
| 380 | MOVEFILE_DELAY_UNTIL_REBOOT); |
Eric Caruso | 128f0dd | 2017-12-16 01:32:49 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | bool ReplaceFile(const FilePath& from_path, |
| 384 | const FilePath& to_path, |
| 385 | File::Error* error) { |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 386 | ScopedBlockingCall scoped_blocking_call(BlockingType::MAY_BLOCK); |
Eric Caruso | 128f0dd | 2017-12-16 01:32:49 | [diff] [blame] | 387 | // Try a simple move first. It will only succeed when |to_path| doesn't |
| 388 | // already exist. |
| 389 | if (::MoveFile(from_path.value().c_str(), to_path.value().c_str())) |
| 390 | return true; |
| 391 | File::Error move_error = File::OSErrorToFileError(GetLastError()); |
| 392 | |
| 393 | // Try the full-blown replace if the move fails, as ReplaceFile will only |
| 394 | // succeed when |to_path| does exist. When writing to a network share, we may |
| 395 | // not be able to change the ACLs. Ignore ACL errors then |
| 396 | // (REPLACEFILE_IGNORE_MERGE_ERRORS). |
| 397 | if (::ReplaceFile(to_path.value().c_str(), from_path.value().c_str(), NULL, |
| 398 | REPLACEFILE_IGNORE_MERGE_ERRORS, NULL, NULL)) { |
| 399 | return true; |
| 400 | } |
| 401 | // In the case of FILE_ERROR_NOT_FOUND from ReplaceFile, it is likely that |
| 402 | // |to_path| does not exist. In this case, the more relevant error comes |
| 403 | // from the call to MoveFile. |
| 404 | if (error) { |
| 405 | File::Error replace_error = File::OSErrorToFileError(GetLastError()); |
| 406 | *error = replace_error == File::FILE_ERROR_NOT_FOUND ? move_error |
| 407 | : replace_error; |
| 408 | } |
| 409 | return false; |
| 410 | } |
| 411 | |
| 412 | bool CopyDirectory(const FilePath& from_path, |
| 413 | const FilePath& to_path, |
| 414 | bool recursive) { |
| 415 | return DoCopyDirectory(from_path, to_path, recursive, false); |
| 416 | } |
| 417 | |
| 418 | bool CopyDirectoryExcl(const FilePath& from_path, |
| 419 | const FilePath& to_path, |
| 420 | bool recursive) { |
| 421 | return DoCopyDirectory(from_path, to_path, recursive, true); |
| 422 | } |
| 423 | |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 424 | bool PathExists(const FilePath& path) { |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 425 | ScopedBlockingCall scoped_blocking_call(BlockingType::MAY_BLOCK); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 426 | return (GetFileAttributes(path.value().c_str()) != INVALID_FILE_ATTRIBUTES); |
| 427 | } |
| 428 | |
[email protected] | 7e1fde6a | 2008-12-23 20:20:10 | [diff] [blame] | 429 | bool PathIsWritable(const FilePath& path) { |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 430 | ScopedBlockingCall scoped_blocking_call(BlockingType::MAY_BLOCK); |
brucedawson | d350943 | 2015-09-18 18:28:13 | [diff] [blame] | 431 | HANDLE dir = |
[email protected] | 53af568e | 2010-08-04 17:10:17 | [diff] [blame] | 432 | CreateFile(path.value().c_str(), FILE_ADD_FILE, kFileShareAll, |
brucedawson | d350943 | 2015-09-18 18:28:13 | [diff] [blame] | 433 | NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 434 | |
brucedawson | d350943 | 2015-09-18 18:28:13 | [diff] [blame] | 435 | if (dir == INVALID_HANDLE_VALUE) |
| 436 | return false; |
| 437 | |
| 438 | CloseHandle(dir); |
| 439 | return true; |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 440 | } |
| 441 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 442 | bool DirectoryExists(const FilePath& path) { |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 443 | ScopedBlockingCall scoped_blocking_call(BlockingType::MAY_BLOCK); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 444 | DWORD fileattr = GetFileAttributes(path.value().c_str()); |
[email protected] | 806b9c6 | 2008-09-11 16:09:11 | [diff] [blame] | 445 | if (fileattr != INVALID_FILE_ATTRIBUTES) |
| 446 | return (fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0; |
| 447 | return false; |
| 448 | } |
| 449 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 450 | bool GetTempDir(FilePath* path) { |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 451 | wchar_t temp_path[MAX_PATH + 1]; |
| 452 | DWORD path_len = ::GetTempPath(MAX_PATH, temp_path); |
| 453 | if (path_len >= MAX_PATH || path_len <= 0) |
| 454 | return false; |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 455 | // TODO(evanm): the old behavior of this function was to always strip the |
| 456 | // trailing slash. We duplicate this here, but it shouldn't be necessary |
| 457 | // when everyone is using the appropriate FilePath APIs. |
[email protected] | 7d95aae | 2009-10-09 07:33:39 | [diff] [blame] | 458 | *path = FilePath(temp_path).StripTrailingSeparators(); |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 459 | return true; |
| 460 | } |
| 461 | |
[email protected] | ffaee18e | 2014-02-19 20:34:23 | [diff] [blame] | 462 | FilePath GetHomeDir() { |
| 463 | char16 result[MAX_PATH]; |
| 464 | if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PROFILE, NULL, SHGFP_TYPE_CURRENT, |
| 465 | result)) && |
| 466 | result[0]) { |
| 467 | return FilePath(result); |
| 468 | } |
| 469 | |
| 470 | // Fall back to the temporary directory on failure. |
| 471 | FilePath temp; |
| 472 | if (GetTempDir(&temp)) |
| 473 | return temp; |
| 474 | |
| 475 | // Last resort. |
| 476 | return FilePath(L"C:\\"); |
| 477 | } |
| 478 | |
[email protected] | 33edeab | 2009-08-18 16:07:55 | [diff] [blame] | 479 | bool CreateTemporaryFile(FilePath* path) { |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 480 | ScopedBlockingCall scoped_blocking_call(BlockingType::MAY_BLOCK); |
[email protected] | 45446a5 | 2010-11-04 17:41:00 | [diff] [blame] | 481 | |
[email protected] | 33edeab | 2009-08-18 16:07:55 | [diff] [blame] | 482 | FilePath temp_file; |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 483 | |
[email protected] | 33edeab | 2009-08-18 16:07:55 | [diff] [blame] | 484 | if (!GetTempDir(path)) |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 485 | return false; |
| 486 | |
[email protected] | 33edeab | 2009-08-18 16:07:55 | [diff] [blame] | 487 | if (CreateTemporaryFileInDir(*path, &temp_file)) { |
| 488 | *path = temp_file; |
[email protected] | 392264c | 2008-11-11 00:01:38 | [diff] [blame] | 489 | return true; |
| 490 | } |
| 491 | |
[email protected] | bd05da2 | 2009-01-27 19:05:54 | [diff] [blame] | 492 | return false; |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 493 | } |
| 494 | |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 495 | // On POSIX we have semantics to create and open a temporary file |
| 496 | // atomically. |
| 497 | // TODO(jrg): is there equivalent call to use on Windows instead of |
| 498 | // going 2-step? |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 499 | FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path) { |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 500 | ScopedBlockingCall scoped_blocking_call(BlockingType::MAY_BLOCK); |
[email protected] | 33edeab | 2009-08-18 16:07:55 | [diff] [blame] | 501 | if (!CreateTemporaryFileInDir(dir, path)) { |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 502 | return NULL; |
| 503 | } |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 504 | // Open file in binary mode, to avoid problems with fwrite. On Windows |
| 505 | // it replaces \n's with \r\n's, which may surprise you. |
| 506 | // Reference: http://msdn.microsoft.com/en-us/library/h9t88zwz(VS.71).aspx |
[email protected] | 7600d0b | 2013-12-08 21:43:30 | [diff] [blame] | 507 | return OpenFile(*path, "wb+"); |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 508 | } |
| 509 | |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 510 | bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) { |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 511 | ScopedBlockingCall scoped_blocking_call(BlockingType::MAY_BLOCK); |
[email protected] | 45446a5 | 2010-11-04 17:41:00 | [diff] [blame] | 512 | |
chengx | 170f1cf | 2017-04-06 22:46:57 | [diff] [blame] | 513 | // Use GUID instead of ::GetTempFileName() to generate unique file names. |
| 514 | // "Due to the algorithm used to generate file names, GetTempFileName can |
| 515 | // perform poorly when creating a large number of files with the same prefix. |
| 516 | // In such cases, it is recommended that you construct unique file names based |
| 517 | // on GUIDs." |
Greg Thompson | 51ebae1 | 2018-05-08 17:43:16 | [diff] [blame] | 518 | // https://msdn.microsoft.com/library/windows/desktop/aa364991.aspx |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 519 | |
chengx | 170f1cf | 2017-04-06 22:46:57 | [diff] [blame] | 520 | FilePath temp_name; |
| 521 | bool create_file_success = false; |
| 522 | |
| 523 | // Although it is nearly impossible to get a duplicate name with GUID, we |
| 524 | // still use a loop here in case it happens. |
| 525 | for (int i = 0; i < 100; ++i) { |
| 526 | temp_name = dir.Append(ASCIIToUTF16(base::GenerateGUID()) + L".tmp"); |
| 527 | File file(temp_name, |
| 528 | File::FLAG_CREATE | File::FLAG_READ | File::FLAG_WRITE); |
| 529 | if (file.IsValid()) { |
| 530 | file.Close(); |
| 531 | create_file_success = true; |
| 532 | break; |
| 533 | } |
| 534 | } |
| 535 | |
chengx | 170f1cf | 2017-04-06 22:46:57 | [diff] [blame] | 536 | if (!create_file_success) { |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 537 | DPLOG(WARNING) << "Failed to get temporary file name in " |
| 538 | << UTF16ToUTF8(dir.value()); |
[email protected] | 04a6bd3b9 | 2010-01-11 20:23:41 | [diff] [blame] | 539 | return false; |
| 540 | } |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 541 | |
[email protected] | e1af0d5 | 2012-11-12 16:28:07 | [diff] [blame] | 542 | wchar_t long_temp_name[MAX_PATH + 1]; |
chengx | 170f1cf | 2017-04-06 22:46:57 | [diff] [blame] | 543 | DWORD long_name_len = |
| 544 | GetLongPathName(temp_name.value().c_str(), long_temp_name, MAX_PATH); |
[email protected] | e1af0d5 | 2012-11-12 16:28:07 | [diff] [blame] | 545 | if (long_name_len > MAX_PATH || long_name_len == 0) { |
| 546 | // GetLongPathName() failed, but we still have a temporary file. |
chengx | 170f1cf | 2017-04-06 22:46:57 | [diff] [blame] | 547 | *temp_file = std::move(temp_name); |
[email protected] | e1af0d5 | 2012-11-12 16:28:07 | [diff] [blame] | 548 | return true; |
[email protected] | 04a6bd3b9 | 2010-01-11 20:23:41 | [diff] [blame] | 549 | } |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 550 | |
[email protected] | e1af0d5 | 2012-11-12 16:28:07 | [diff] [blame] | 551 | FilePath::StringType long_temp_name_str; |
| 552 | long_temp_name_str.assign(long_temp_name, long_name_len); |
chengx | 170f1cf | 2017-04-06 22:46:57 | [diff] [blame] | 553 | *temp_file = FilePath(std::move(long_temp_name_str)); |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 554 | return true; |
| 555 | } |
| 556 | |
[email protected] | b0b3abd9 | 2010-04-30 17:00:09 | [diff] [blame] | 557 | bool CreateTemporaryDirInDir(const FilePath& base_dir, |
| 558 | const FilePath::StringType& prefix, |
[email protected] | 046062e8 | 2010-06-30 07:19:11 | [diff] [blame] | 559 | FilePath* new_dir) { |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 560 | ScopedBlockingCall scoped_blocking_call(BlockingType::MAY_BLOCK); |
[email protected] | 45446a5 | 2010-11-04 17:41:00 | [diff] [blame] | 561 | |
[email protected] | 7e1fde6a | 2008-12-23 20:20:10 | [diff] [blame] | 562 | FilePath path_to_create; |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 563 | |
[email protected] | b026e35d | 2010-10-19 02:31:03 | [diff] [blame] | 564 | for (int count = 0; count < 50; ++count) { |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 565 | // Try create a new temporary directory with random generated name. If |
| 566 | // the one exists, keep trying another path name until we reach some limit. |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 567 | string16 new_dir_name; |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 568 | new_dir_name.assign(prefix); |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 569 | new_dir_name.append(IntToString16(GetCurrentProcId())); |
[email protected] | d6c6113 | 2012-05-01 23:57:51 | [diff] [blame] | 570 | new_dir_name.push_back('_'); |
avi | ab0f6cce | 2015-11-25 21:01:57 | [diff] [blame] | 571 | new_dir_name.append( |
| 572 | IntToString16(RandInt(0, std::numeric_limits<int16_t>::max()))); |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 573 | |
[email protected] | bdddb67 | 2011-02-18 14:54:50 | [diff] [blame] | 574 | path_to_create = base_dir.Append(new_dir_name); |
[email protected] | b026e35d | 2010-10-19 02:31:03 | [diff] [blame] | 575 | if (::CreateDirectory(path_to_create.value().c_str(), NULL)) { |
| 576 | *new_dir = path_to_create; |
| 577 | return true; |
| 578 | } |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 579 | } |
| 580 | |
[email protected] | b026e35d | 2010-10-19 02:31:03 | [diff] [blame] | 581 | return false; |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 582 | } |
| 583 | |
[email protected] | b0b3abd9 | 2010-04-30 17:00:09 | [diff] [blame] | 584 | bool CreateNewTempDirectory(const FilePath::StringType& prefix, |
| 585 | FilePath* new_temp_path) { |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 586 | ScopedBlockingCall scoped_blocking_call(BlockingType::MAY_BLOCK); |
[email protected] | 45446a5 | 2010-11-04 17:41:00 | [diff] [blame] | 587 | |
[email protected] | b0b3abd9 | 2010-04-30 17:00:09 | [diff] [blame] | 588 | FilePath system_temp_dir; |
| 589 | if (!GetTempDir(&system_temp_dir)) |
| 590 | return false; |
| 591 | |
[email protected] | bd781d89 | 2010-07-30 15:33:28 | [diff] [blame] | 592 | return CreateTemporaryDirInDir(system_temp_dir, prefix, new_temp_path); |
[email protected] | b0b3abd9 | 2010-04-30 17:00:09 | [diff] [blame] | 593 | } |
| 594 | |
[email protected] | cfd23d2 | 2013-06-11 03:50:25 | [diff] [blame] | 595 | bool CreateDirectoryAndGetError(const FilePath& full_path, |
[email protected] | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 596 | File::Error* error) { |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 597 | ScopedBlockingCall scoped_blocking_call(BlockingType::MAY_BLOCK); |
[email protected] | 45446a5 | 2010-11-04 17:41:00 | [diff] [blame] | 598 | |
[email protected] | 40676ab | 2009-11-27 14:54:41 | [diff] [blame] | 599 | // If the path exists, we've succeeded if it's a directory, failed otherwise. |
Greg Thompson | a995c72 | 2018-08-16 21:50:01 | [diff] [blame] | 600 | const wchar_t* const full_path_str = full_path.value().c_str(); |
| 601 | const DWORD fileattr = ::GetFileAttributes(full_path_str); |
[email protected] | 075be5d | 2010-09-24 15:39:39 | [diff] [blame] | 602 | if (fileattr != INVALID_FILE_ATTRIBUTES) { |
[email protected] | 40676ab | 2009-11-27 14:54:41 | [diff] [blame] | 603 | if ((fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0) { |
[email protected] | b026e35d | 2010-10-19 02:31:03 | [diff] [blame] | 604 | DVLOG(1) << "CreateDirectory(" << full_path_str << "), " |
| 605 | << "directory already exists."; |
[email protected] | 40676ab | 2009-11-27 14:54:41 | [diff] [blame] | 606 | return true; |
[email protected] | 40676ab | 2009-11-27 14:54:41 | [diff] [blame] | 607 | } |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 608 | DLOG(WARNING) << "CreateDirectory(" << full_path_str << "), " |
| 609 | << "conflicts with existing file."; |
Greg Thompson | a995c72 | 2018-08-16 21:50:01 | [diff] [blame] | 610 | if (error) |
[email protected] | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 611 | *error = File::FILE_ERROR_NOT_A_DIRECTORY; |
Greg Thompson | a995c72 | 2018-08-16 21:50:01 | [diff] [blame] | 612 | ::SetLastError(ERROR_FILE_EXISTS); |
[email protected] | b026e35d | 2010-10-19 02:31:03 | [diff] [blame] | 613 | return false; |
[email protected] | 40676ab | 2009-11-27 14:54:41 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | // Invariant: Path does not exist as file or directory. |
| 617 | |
| 618 | // Attempt to create the parent recursively. This will immediately return |
| 619 | // true if it already exists, otherwise will create all required parent |
| 620 | // directories starting with the highest-level missing parent. |
[email protected] | 89b9ae09 | 2009-12-17 20:42:40 | [diff] [blame] | 621 | FilePath parent_path(full_path.DirName()); |
| 622 | if (parent_path.value() == full_path.value()) { |
Greg Thompson | a995c72 | 2018-08-16 21:50:01 | [diff] [blame] | 623 | if (error) |
[email protected] | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 624 | *error = File::FILE_ERROR_NOT_FOUND; |
Greg Thompson | a995c72 | 2018-08-16 21:50:01 | [diff] [blame] | 625 | ::SetLastError(ERROR_FILE_NOT_FOUND); |
[email protected] | 89b9ae09 | 2009-12-17 20:42:40 | [diff] [blame] | 626 | return false; |
| 627 | } |
[email protected] | cfd23d2 | 2013-06-11 03:50:25 | [diff] [blame] | 628 | if (!CreateDirectoryAndGetError(parent_path, error)) { |
[email protected] | 075be5d | 2010-09-24 15:39:39 | [diff] [blame] | 629 | DLOG(WARNING) << "Failed to create one of the parent directories."; |
Greg Thompson | a995c72 | 2018-08-16 21:50:01 | [diff] [blame] | 630 | DCHECK(!error || *error != File::FILE_OK); |
[email protected] | 40676ab | 2009-11-27 14:54:41 | [diff] [blame] | 631 | return false; |
| 632 | } |
| 633 | |
Greg Thompson | a995c72 | 2018-08-16 21:50:01 | [diff] [blame] | 634 | if (::CreateDirectory(full_path_str, NULL)) |
| 635 | return true; |
| 636 | |
| 637 | const DWORD error_code = ::GetLastError(); |
| 638 | if (error_code == ERROR_ALREADY_EXISTS && DirectoryExists(full_path)) { |
| 639 | // This error code ERROR_ALREADY_EXISTS doesn't indicate whether we were |
| 640 | // racing with someone creating the same directory, or a file with the same |
| 641 | // path. If DirectoryExists() returns true, we lost the race to create the |
| 642 | // same directory. |
[email protected] | 8156962 | 2008-09-09 22:35:16 | [diff] [blame] | 643 | return true; |
[email protected] | 40676ab | 2009-11-27 14:54:41 | [diff] [blame] | 644 | } |
Greg Thompson | a995c72 | 2018-08-16 21:50:01 | [diff] [blame] | 645 | if (error) |
| 646 | *error = File::OSErrorToFileError(error_code); |
| 647 | ::SetLastError(error_code); |
| 648 | DPLOG(WARNING) << "Failed to create directory " << full_path_str; |
| 649 | return false; |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 650 | } |
| 651 | |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 652 | bool NormalizeFilePath(const FilePath& path, FilePath* real_path) { |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 653 | ScopedBlockingCall scoped_blocking_call(BlockingType::MAY_BLOCK); |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 654 | FilePath mapped_file; |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 655 | if (!NormalizeToNativeFilePath(path, &mapped_file)) |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 656 | return false; |
| 657 | // NormalizeToNativeFilePath() will return a path that starts with |
| 658 | // "\Device\Harddisk...". Helper DevicePathToDriveLetterPath() |
| 659 | // will find a drive letter which maps to the path's device, so |
| 660 | // that we return a path starting with a drive letter. |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 661 | return DevicePathToDriveLetterPath(mapped_file, real_path); |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 662 | } |
| 663 | |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 664 | bool DevicePathToDriveLetterPath(const FilePath& nt_device_path, |
| 665 | FilePath* out_drive_letter_path) { |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 666 | ScopedBlockingCall scoped_blocking_call(BlockingType::MAY_BLOCK); |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 667 | |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 668 | // Get the mapping of drive letters to device paths. |
| 669 | const int kDriveMappingSize = 1024; |
| 670 | wchar_t drive_mapping[kDriveMappingSize] = {'\0'}; |
| 671 | if (!::GetLogicalDriveStrings(kDriveMappingSize - 1, drive_mapping)) { |
| 672 | DLOG(ERROR) << "Failed to get drive mapping."; |
| 673 | return false; |
| 674 | } |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 675 | |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 676 | // The drive mapping is a sequence of null terminated strings. |
| 677 | // The last string is empty. |
| 678 | wchar_t* drive_map_ptr = drive_mapping; |
| 679 | wchar_t device_path_as_string[MAX_PATH]; |
| 680 | wchar_t drive[] = L" :"; |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 681 | |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 682 | // For each string in the drive mapping, get the junction that links |
| 683 | // to it. If that junction is a prefix of |device_path|, then we |
| 684 | // know that |drive| is the real path prefix. |
| 685 | while (*drive_map_ptr) { |
| 686 | drive[0] = drive_map_ptr[0]; // Copy the drive letter. |
| 687 | |
| 688 | if (QueryDosDevice(drive, device_path_as_string, MAX_PATH)) { |
| 689 | FilePath device_path(device_path_as_string); |
| 690 | if (device_path == nt_device_path || |
| 691 | device_path.IsParent(nt_device_path)) { |
| 692 | *out_drive_letter_path = FilePath(drive + |
| 693 | nt_device_path.value().substr(wcslen(device_path_as_string))); |
| 694 | return true; |
| 695 | } |
| 696 | } |
| 697 | // Move to the next drive letter string, which starts one |
| 698 | // increment after the '\0' that terminates the current string. |
thestig | 8badc79 | 2014-12-04 22:14:22 | [diff] [blame] | 699 | while (*drive_map_ptr++) {} |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | // No drive matched. The path does not start with a device junction |
| 703 | // that is mounted as a drive letter. This means there is no drive |
| 704 | // letter path to the volume that holds |device_path|, so fail. |
| 705 | return false; |
| 706 | } |
| 707 | |
| 708 | bool NormalizeToNativeFilePath(const FilePath& path, FilePath* nt_path) { |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 709 | ScopedBlockingCall scoped_blocking_call(BlockingType::MAY_BLOCK); |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 710 | // In Vista, GetFinalPathNameByHandle() would give us the real path |
| 711 | // from a file handle. If we ever deprecate XP, consider changing the |
| 712 | // code below to a call to GetFinalPathNameByHandle(). The method this |
| 713 | // function uses is explained in the following msdn article: |
| 714 | // http://msdn.microsoft.com/en-us/library/aa366789(VS.85).aspx |
thestig | 8badc79 | 2014-12-04 22:14:22 | [diff] [blame] | 715 | win::ScopedHandle file_handle( |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 716 | ::CreateFile(path.value().c_str(), |
| 717 | GENERIC_READ, |
| 718 | kFileShareAll, |
| 719 | NULL, |
| 720 | OPEN_EXISTING, |
| 721 | FILE_ATTRIBUTE_NORMAL, |
| 722 | NULL)); |
rvargas | ecaef8a | 2014-09-23 22:03:11 | [diff] [blame] | 723 | if (!file_handle.IsValid()) |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 724 | return false; |
| 725 | |
| 726 | // Create a file mapping object. Can't easily use MemoryMappedFile, because |
| 727 | // we only map the first byte, and need direct access to the handle. You can |
| 728 | // not map an empty file, this call fails in that case. |
thestig | 8badc79 | 2014-12-04 22:14:22 | [diff] [blame] | 729 | win::ScopedHandle file_map_handle( |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 730 | ::CreateFileMapping(file_handle.Get(), |
| 731 | NULL, |
| 732 | PAGE_READONLY, |
| 733 | 0, |
| 734 | 1, // Just one byte. No need to look at the data. |
| 735 | NULL)); |
rvargas | ecaef8a | 2014-09-23 22:03:11 | [diff] [blame] | 736 | if (!file_map_handle.IsValid()) |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 737 | return false; |
| 738 | |
| 739 | // Use a view of the file to get the path to the file. |
| 740 | void* file_view = MapViewOfFile(file_map_handle.Get(), |
| 741 | FILE_MAP_READ, 0, 0, 1); |
| 742 | if (!file_view) |
| 743 | return false; |
| 744 | |
| 745 | // The expansion of |path| into a full path may make it longer. |
| 746 | // GetMappedFileName() will fail if the result is longer than MAX_PATH. |
| 747 | // Pad a bit to be safe. If kMaxPathLength is ever changed to be less |
| 748 | // than MAX_PATH, it would be nessisary to test that GetMappedFileName() |
| 749 | // not return kMaxPathLength. This would mean that only part of the |
| 750 | // path fit in |mapped_file_path|. |
| 751 | const int kMaxPathLength = MAX_PATH + 10; |
| 752 | wchar_t mapped_file_path[kMaxPathLength]; |
| 753 | bool success = false; |
| 754 | HANDLE cp = GetCurrentProcess(); |
| 755 | if (::GetMappedFileNameW(cp, file_view, mapped_file_path, kMaxPathLength)) { |
| 756 | *nt_path = FilePath(mapped_file_path); |
| 757 | success = true; |
| 758 | } |
| 759 | ::UnmapViewOfFile(file_view); |
| 760 | return success; |
| 761 | } |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 762 | |
[email protected] | cd78ad5 | 2011-05-31 23:10:06 | [diff] [blame] | 763 | // TODO(rkc): Work out if we want to handle NTFS junctions here or not, handle |
| 764 | // them if we do decide to. |
| 765 | bool IsLink(const FilePath& file_path) { |
| 766 | return false; |
| 767 | } |
| 768 | |
[email protected] | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 769 | bool GetFileInfo(const FilePath& file_path, File::Info* results) { |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 770 | ScopedBlockingCall scoped_blocking_call(BlockingType::MAY_BLOCK); |
[email protected] | 45446a5 | 2010-11-04 17:41:00 | [diff] [blame] | 771 | |
[email protected] | f5e3da4d | 2008-09-26 01:04:08 | [diff] [blame] | 772 | WIN32_FILE_ATTRIBUTE_DATA attr; |
[email protected] | 83d8625 | 2010-05-07 18:58:45 | [diff] [blame] | 773 | if (!GetFileAttributesEx(file_path.value().c_str(), |
[email protected] | 759c4f3 | 2008-11-04 21:15:00 | [diff] [blame] | 774 | GetFileExInfoStandard, &attr)) { |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 775 | return false; |
[email protected] | 759c4f3 | 2008-11-04 21:15:00 | [diff] [blame] | 776 | } |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 777 | |
[email protected] | f5e3da4d | 2008-09-26 01:04:08 | [diff] [blame] | 778 | ULARGE_INTEGER size; |
| 779 | size.HighPart = attr.nFileSizeHigh; |
| 780 | size.LowPart = attr.nFileSizeLow; |
| 781 | results->size = size.QuadPart; |
| 782 | |
| 783 | results->is_directory = |
| 784 | (attr.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 785 | results->last_modified = Time::FromFileTime(attr.ftLastWriteTime); |
| 786 | results->last_accessed = Time::FromFileTime(attr.ftLastAccessTime); |
| 787 | results->creation_time = Time::FromFileTime(attr.ftCreationTime); |
[email protected] | 5ef32389 | 2009-07-24 16:13:53 | [diff] [blame] | 788 | |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 789 | return true; |
| 790 | } |
| 791 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 792 | FILE* OpenFile(const FilePath& filename, const char* mode) { |
grt | d0bc44f | 2017-02-13 17:52:17 | [diff] [blame] | 793 | // 'N' is unconditionally added below, so be sure there is not one already |
| 794 | // present before a comma in |mode|. |
| 795 | DCHECK( |
| 796 | strchr(mode, 'N') == nullptr || |
| 797 | (strchr(mode, ',') != nullptr && strchr(mode, 'N') > strchr(mode, ','))); |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 798 | ScopedBlockingCall scoped_blocking_call(BlockingType::MAY_BLOCK); |
thestig | 8badc79 | 2014-12-04 22:14:22 | [diff] [blame] | 799 | string16 w_mode = ASCIIToUTF16(mode); |
grt | d0bc44f | 2017-02-13 17:52:17 | [diff] [blame] | 800 | AppendModeCharacter(L'N', &w_mode); |
[email protected] | e3e9b5e | 2010-03-05 03:26:16 | [diff] [blame] | 801 | return _wfsopen(filename.value().c_str(), w_mode.c_str(), _SH_DENYNO); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 802 | } |
| 803 | |
[email protected] | 4d35828 | 2014-06-10 11:19:50 | [diff] [blame] | 804 | FILE* FileToFILE(File file, const char* mode) { |
| 805 | if (!file.IsValid()) |
| 806 | return NULL; |
| 807 | int fd = |
| 808 | _open_osfhandle(reinterpret_cast<intptr_t>(file.GetPlatformFile()), 0); |
| 809 | if (fd < 0) |
| 810 | return NULL; |
| 811 | file.TakePlatformFile(); |
| 812 | FILE* stream = _fdopen(fd, mode); |
| 813 | if (!stream) |
| 814 | _close(fd); |
| 815 | return stream; |
| 816 | } |
| 817 | |
[email protected] | d8b565d | 2014-04-18 10:34:15 | [diff] [blame] | 818 | int ReadFile(const FilePath& filename, char* data, int max_size) { |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 819 | ScopedBlockingCall scoped_blocking_call(BlockingType::MAY_BLOCK); |
thestig | 8badc79 | 2014-12-04 22:14:22 | [diff] [blame] | 820 | win::ScopedHandle file(CreateFile(filename.value().c_str(), |
| 821 | GENERIC_READ, |
| 822 | FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 823 | NULL, |
| 824 | OPEN_EXISTING, |
| 825 | FILE_FLAG_SEQUENTIAL_SCAN, |
| 826 | NULL)); |
rvargas | ecaef8a | 2014-09-23 22:03:11 | [diff] [blame] | 827 | if (!file.IsValid()) |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 828 | return -1; |
| 829 | |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 830 | DWORD read; |
rvargas | ecaef8a | 2014-09-23 22:03:11 | [diff] [blame] | 831 | if (::ReadFile(file.Get(), data, max_size, &read, NULL)) |
[email protected] | f55bd486 | 2010-05-27 15:38:07 | [diff] [blame] | 832 | return read; |
[email protected] | d8b565d | 2014-04-18 10:34:15 | [diff] [blame] | 833 | |
[email protected] | f55bd486 | 2010-05-27 15:38:07 | [diff] [blame] | 834 | return -1; |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 835 | } |
| 836 | |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 837 | int WriteFile(const FilePath& filename, const char* data, int size) { |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 838 | ScopedBlockingCall scoped_blocking_call(BlockingType::MAY_BLOCK); |
Takuto Ikuta | 1a1cb7b | 2017-05-29 08:18:53 | [diff] [blame] | 839 | win::ScopedHandle file(CreateFile(filename.value().c_str(), GENERIC_WRITE, 0, |
| 840 | NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, |
thestig | 8badc79 | 2014-12-04 22:14:22 | [diff] [blame] | 841 | NULL)); |
rvargas | ecaef8a | 2014-09-23 22:03:11 | [diff] [blame] | 842 | if (!file.IsValid()) { |
[email protected] | ad8cfa9 | 2014-05-21 20:06:23 | [diff] [blame] | 843 | DPLOG(WARNING) << "CreateFile failed for path " |
| 844 | << UTF16ToUTF8(filename.value()); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 845 | return -1; |
| 846 | } |
| 847 | |
| 848 | DWORD written; |
rvargas | ecaef8a | 2014-09-23 22:03:11 | [diff] [blame] | 849 | BOOL result = ::WriteFile(file.Get(), data, size, &written, NULL); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 850 | if (result && static_cast<int>(written) == size) |
| 851 | return written; |
| 852 | |
| 853 | if (!result) { |
| 854 | // WriteFile failed. |
[email protected] | ad8cfa9 | 2014-05-21 20:06:23 | [diff] [blame] | 855 | DPLOG(WARNING) << "writing file " << UTF16ToUTF8(filename.value()) |
| 856 | << " failed"; |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 857 | } else { |
| 858 | // Didn't write all the bytes. |
| 859 | DLOG(WARNING) << "wrote" << written << " bytes to " |
| 860 | << UTF16ToUTF8(filename.value()) << " expected " << size; |
| 861 | } |
| 862 | return -1; |
| 863 | } |
| 864 | |
chirantan | 75ea2fd | 2014-10-07 23:15:30 | [diff] [blame] | 865 | bool AppendToFile(const FilePath& filename, const char* data, int size) { |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 866 | ScopedBlockingCall scoped_blocking_call(BlockingType::MAY_BLOCK); |
thestig | 8badc79 | 2014-12-04 22:14:22 | [diff] [blame] | 867 | win::ScopedHandle file(CreateFile(filename.value().c_str(), |
| 868 | FILE_APPEND_DATA, |
| 869 | 0, |
| 870 | NULL, |
| 871 | OPEN_EXISTING, |
| 872 | 0, |
| 873 | NULL)); |
rvargas | ecaef8a | 2014-09-23 22:03:11 | [diff] [blame] | 874 | if (!file.IsValid()) { |
chirantan | 75ea2fd | 2014-10-07 23:15:30 | [diff] [blame] | 875 | VPLOG(1) << "CreateFile failed for path " << UTF16ToUTF8(filename.value()); |
| 876 | return false; |
[email protected] | 891128f | 2012-04-29 12:57:10 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | DWORD written; |
rvargas | ecaef8a | 2014-09-23 22:03:11 | [diff] [blame] | 880 | BOOL result = ::WriteFile(file.Get(), data, size, &written, NULL); |
[email protected] | 891128f | 2012-04-29 12:57:10 | [diff] [blame] | 881 | if (result && static_cast<int>(written) == size) |
chirantan | 75ea2fd | 2014-10-07 23:15:30 | [diff] [blame] | 882 | return true; |
[email protected] | 891128f | 2012-04-29 12:57:10 | [diff] [blame] | 883 | |
| 884 | if (!result) { |
| 885 | // WriteFile failed. |
chirantan | 75ea2fd | 2014-10-07 23:15:30 | [diff] [blame] | 886 | VPLOG(1) << "Writing file " << UTF16ToUTF8(filename.value()) << " failed"; |
[email protected] | 891128f | 2012-04-29 12:57:10 | [diff] [blame] | 887 | } else { |
| 888 | // Didn't write all the bytes. |
chirantan | 75ea2fd | 2014-10-07 23:15:30 | [diff] [blame] | 889 | VPLOG(1) << "Only wrote " << written << " out of " << size << " byte(s) to " |
| 890 | << UTF16ToUTF8(filename.value()); |
[email protected] | 891128f | 2012-04-29 12:57:10 | [diff] [blame] | 891 | } |
chirantan | 75ea2fd | 2014-10-07 23:15:30 | [diff] [blame] | 892 | return false; |
[email protected] | 891128f | 2012-04-29 12:57:10 | [diff] [blame] | 893 | } |
| 894 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 895 | bool GetCurrentDirectory(FilePath* dir) { |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 896 | ScopedBlockingCall scoped_blocking_call(BlockingType::MAY_BLOCK); |
[email protected] | 45446a5 | 2010-11-04 17:41:00 | [diff] [blame] | 897 | |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 898 | wchar_t system_buffer[MAX_PATH]; |
| 899 | system_buffer[0] = 0; |
| 900 | DWORD len = ::GetCurrentDirectory(MAX_PATH, system_buffer); |
| 901 | if (len == 0 || len > MAX_PATH) |
| 902 | return false; |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 903 | // TODO(evanm): the old behavior of this function was to always strip the |
| 904 | // trailing slash. We duplicate this here, but it shouldn't be necessary |
| 905 | // when everyone is using the appropriate FilePath APIs. |
| 906 | std::wstring dir_str(system_buffer); |
[email protected] | 2c8088a4 | 2009-10-15 05:00:25 | [diff] [blame] | 907 | *dir = FilePath(dir_str).StripTrailingSeparators(); |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 908 | return true; |
| 909 | } |
| 910 | |
[email protected] | a9cd2a65 | 2008-11-17 21:01:19 | [diff] [blame] | 911 | bool SetCurrentDirectory(const FilePath& directory) { |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 912 | ScopedBlockingCall scoped_blocking_call(BlockingType::MAY_BLOCK); |
Thiago Farina | d892b4a | 2017-05-18 22:07:49 | [diff] [blame] | 913 | return ::SetCurrentDirectory(directory.value().c_str()) != 0; |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 914 | } |
| 915 | |
[email protected] | 5948030 | 2013-02-21 03:24:08 | [diff] [blame] | 916 | int GetMaximumPathComponentLength(const FilePath& path) { |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 917 | ScopedBlockingCall scoped_blocking_call(BlockingType::MAY_BLOCK); |
[email protected] | 5948030 | 2013-02-21 03:24:08 | [diff] [blame] | 918 | |
| 919 | wchar_t volume_path[MAX_PATH]; |
| 920 | if (!GetVolumePathNameW(path.NormalizePathSeparators().value().c_str(), |
| 921 | volume_path, |
| 922 | arraysize(volume_path))) { |
| 923 | return -1; |
| 924 | } |
| 925 | |
| 926 | DWORD max_length = 0; |
| 927 | if (!GetVolumeInformationW(volume_path, NULL, 0, NULL, &max_length, NULL, |
| 928 | NULL, 0)) { |
| 929 | return -1; |
| 930 | } |
| 931 | |
| 932 | // Length of |path| with path separator appended. |
| 933 | size_t prefix = path.StripTrailingSeparators().value().size() + 1; |
| 934 | // The whole path string must be shorter than MAX_PATH. That is, it must be |
| 935 | // prefix + component_length < MAX_PATH (or equivalently, <= MAX_PATH - 1). |
| 936 | int whole_path_limit = std::max(0, MAX_PATH - 1 - static_cast<int>(prefix)); |
| 937 | return std::min(whole_path_limit, static_cast<int>(max_length)); |
| 938 | } |
| 939 | |
cmumford | 620ace8 | 2014-11-17 18:58:20 | [diff] [blame] | 940 | bool CopyFile(const FilePath& from_path, const FilePath& to_path) { |
Eric Caruso | 128f0dd | 2017-12-16 01:32:49 | [diff] [blame] | 941 | return DoCopyFile(from_path, to_path, false); |
cmumford | 620ace8 | 2014-11-17 18:58:20 | [diff] [blame] | 942 | } |
| 943 | |
tfarina | 060df7e | 2015-12-16 05:15:32 | [diff] [blame] | 944 | bool SetNonBlocking(int fd) { |
| 945 | unsigned long nonblocking = 1; |
| 946 | if (ioctlsocket(fd, FIONBIO, &nonblocking) == 0) |
| 947 | return true; |
| 948 | return false; |
| 949 | } |
| 950 | |
[email protected] | a26f4ae | 2014-03-13 17:26:21 | [diff] [blame] | 951 | // ----------------------------------------------------------------------------- |
[email protected] | f0ff2ad | 2013-07-09 17:42:26 | [diff] [blame] | 952 | |
[email protected] | f0ff2ad | 2013-07-09 17:42:26 | [diff] [blame] | 953 | namespace internal { |
| 954 | |
| 955 | bool MoveUnsafe(const FilePath& from_path, const FilePath& to_path) { |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 956 | ScopedBlockingCall scoped_blocking_call(BlockingType::MAY_BLOCK); |
[email protected] | f0ff2ad | 2013-07-09 17:42:26 | [diff] [blame] | 957 | |
| 958 | // NOTE: I suspect we could support longer paths, but that would involve |
| 959 | // analyzing all our usage of files. |
| 960 | if (from_path.value().length() >= MAX_PATH || |
| 961 | to_path.value().length() >= MAX_PATH) { |
| 962 | return false; |
| 963 | } |
| 964 | if (MoveFileEx(from_path.value().c_str(), to_path.value().c_str(), |
| 965 | MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING) != 0) |
| 966 | return true; |
| 967 | |
| 968 | // Keep the last error value from MoveFileEx around in case the below |
| 969 | // fails. |
| 970 | bool ret = false; |
| 971 | DWORD last_error = ::GetLastError(); |
| 972 | |
[email protected] | dcd1661 | 2013-07-15 20:18:09 | [diff] [blame] | 973 | if (DirectoryExists(from_path)) { |
[email protected] | f0ff2ad | 2013-07-09 17:42:26 | [diff] [blame] | 974 | // MoveFileEx fails if moving directory across volumes. We will simulate |
| 975 | // the move by using Copy and Delete. Ideally we could check whether |
| 976 | // from_path and to_path are indeed in different volumes. |
| 977 | ret = internal::CopyAndDeleteDirectory(from_path, to_path); |
| 978 | } |
| 979 | |
| 980 | if (!ret) { |
| 981 | // Leave a clue about what went wrong so that it can be (at least) picked |
| 982 | // up by a PLOG entry. |
| 983 | ::SetLastError(last_error); |
| 984 | } |
| 985 | |
| 986 | return ret; |
| 987 | } |
| 988 | |
[email protected] | f0ff2ad | 2013-07-09 17:42:26 | [diff] [blame] | 989 | bool CopyAndDeleteDirectory(const FilePath& from_path, |
| 990 | const FilePath& to_path) { |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 991 | ScopedBlockingCall scoped_blocking_call(BlockingType::MAY_BLOCK); |
[email protected] | f0ff2ad | 2013-07-09 17:42:26 | [diff] [blame] | 992 | if (CopyDirectory(from_path, to_path, true)) { |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 993 | if (DeleteFile(from_path, true)) |
[email protected] | f0ff2ad | 2013-07-09 17:42:26 | [diff] [blame] | 994 | return true; |
| 995 | |
| 996 | // Like Move, this function is not transactional, so we just |
| 997 | // leave the copied bits behind if deleting from_path fails. |
| 998 | // If to_path exists previously then we have already overwritten |
| 999 | // it by now, we don't get better off by deleting the new bits. |
| 1000 | } |
| 1001 | return false; |
| 1002 | } |
| 1003 | |
| 1004 | } // namespace internal |
| 1005 | } // namespace base |