| 6 |
| 7 | #include <io.h> |
| 8 | #include <stdint.h> |
| 9 |
| 10 | #include "base/logging.h" |
| 11 | #include "base/metrics/histogram_functions.h" |
| 12 | #include "base/strings/string_util.h" |
| 13 | #include "base/threading/scoped_blocking_call.h" |
| 14 |
| 15 | #include <windows.h> |
| 16 |
| 17 | namespace base { |
| 18 |
| 19 | // Make sure our Whence mappings match the system headers. |
| 20 | static_assert(File::FROM_BEGIN == FILE_BEGIN && |
| 21 | File::FROM_CURRENT == FILE_CURRENT && |
| 22 | File::FROM_END == FILE_END, |
| 23 | "whence mapping must match the system headers"); |
| 24 |
| 25 | bool File::IsValid() const { |
| 26 | return file_.IsValid(); |
| 27 | } |
| 28 |
| 29 | PlatformFile File::GetPlatformFile() const { |
| 30 | return file_.Get(); |
| 31 | } |
| 32 |
| 33 | PlatformFile File::TakePlatformFile() { |
| 34 | return file_.Take(); |
| 35 | } |
| 36 |
| 37 | void File::Close() { |
| 38 | if (!file_.IsValid()) |
| 39 | return; |
| 40 |
| 41 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 42 | SCOPED_FILE_TRACE("Close"); |
| 43 | file_.Close(); |
| 44 | } |
| 45 |
| 46 | int64_t File::Seek(Whence whence, int64_t offset) { |
| 47 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 48 | DCHECK(IsValid()); |
| 49 |
| 50 | SCOPED_FILE_TRACE_WITH_SIZE("Seek", offset); |
| 51 |
| 52 | LARGE_INTEGER distance, res; |
| 53 | distance.QuadPart = offset; |
| 54 | DWORD move_method = static_cast<DWORD>(whence); |
| 55 | if (!SetFilePointerEx(file_.Get(), distance, &res, move_method)) |
| 56 | return -1; |
| 57 | return res.QuadPart; |
| 58 | } |
| 59 |
| 60 | int File::Read(int64_t offset, char* data, int size) { |
| 61 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 62 | DCHECK(IsValid()); |
| 63 | DCHECK(!async_); |
| 64 | if (size < 0) |
| 65 | return -1; |
| 66 |
| 67 | SCOPED_FILE_TRACE_WITH_SIZE("Read", size); |
| 68 |
| 69 | LARGE_INTEGER offset_li; |
| 70 | offset_li.QuadPart = offset; |
| 71 |
| 72 | OVERLAPPED overlapped = {}; |
| 73 | overlapped.Offset = offset_li.LowPart; |
| 74 | overlapped.OffsetHigh = offset_li.HighPart; |
| 75 |
| 76 | DWORD bytes_read; |
| 77 | if (::ReadFile(file_.Get(), data, size, &bytes_read, &overlapped)) |
| 78 | return bytes_read; |
| 79 | if (ERROR_HANDLE_EOF == GetLastError()) |
| 80 | return 0; |
| 81 |
| 82 | return -1; |
| 83 | } |
| 84 |
| 85 | int File::ReadAtCurrentPos(char* data, int size) { |
| 86 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 87 | DCHECK(IsValid()); |
| 88 | DCHECK(!async_); |
| 89 | if (size < 0) |
| 90 | return -1; |
| 91 |
| 92 | SCOPED_FILE_TRACE_WITH_SIZE("ReadAtCurrentPos", size); |
| 93 |
| 94 | DWORD bytes_read; |
| 95 | if (::ReadFile(file_.Get(), data, size, &bytes_read, NULL)) |
| 96 | return bytes_read; |
| 97 | if (ERROR_HANDLE_EOF == GetLastError()) |
| 98 | return 0; |
| 99 |
| 100 | return -1; |
| 101 | } |
| 102 |
| 103 | int File::ReadNoBestEffort(int64_t offset, char* data, int size) { |
| 104 | // TODO(dbeam): trace this separately? |
| 105 | return Read(offset, data, size); |
| 106 | } |
| 107 |
| 108 | int File::ReadAtCurrentPosNoBestEffort(char* data, int size) { |
| 109 | // TODO(dbeam): trace this separately? |
| 110 | return ReadAtCurrentPos(data, size); |
| 111 | } |
| 112 |
| 113 | int File::Write(int64_t offset, const char* data, int size) { |
| 114 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 115 | DCHECK(IsValid()); |
| 116 | DCHECK(!async_); |
| 117 |
| 118 | SCOPED_FILE_TRACE_WITH_SIZE("Write", size); |
| 119 |
| 120 | LARGE_INTEGER offset_li; |
| 121 | offset_li.QuadPart = offset; |
| 122 |
| 123 | OVERLAPPED overlapped = {}; |
| 124 | overlapped.Offset = offset_li.LowPart; |
| 125 | overlapped.OffsetHigh = offset_li.HighPart; |
| 126 |
| 127 | DWORD bytes_written; |
| 128 | if (::WriteFile(file_.Get(), data, size, &bytes_written, &overlapped)) |
| 129 | return bytes_written; |
| 130 |
| 131 | return -1; |
| 132 | } |
| 133 |
| 134 | int File::WriteAtCurrentPos(const char* data, int size) { |
| 135 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 136 | DCHECK(IsValid()); |
| 137 | DCHECK(!async_); |
| 138 | if (size < 0) |
| 139 | return -1; |
| 140 |
| 141 | SCOPED_FILE_TRACE_WITH_SIZE("WriteAtCurrentPos", size); |
| 142 |
| 143 | DWORD bytes_written; |
| 144 | if (::WriteFile(file_.Get(), data, size, &bytes_written, NULL)) |
| 145 | return bytes_written; |
| 146 |
| 147 | return -1; |
| 148 | } |
| 149 |
| 150 | int File::WriteAtCurrentPosNoBestEffort(const char* data, int size) { |
| 151 | return WriteAtCurrentPos(data, size); |
| 152 | } |
| 153 |
| 154 | int64_t File::GetLength() { |
| 155 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 156 | DCHECK(IsValid()); |
| 157 |
| 158 | SCOPED_FILE_TRACE("GetLength"); |
| 159 |
| 160 | LARGE_INTEGER size; |
| 161 | if (!::GetFileSizeEx(file_.Get(), &size)) |
| 162 | return -1; |
| 163 |
| 164 | return static_cast<int64_t>(size.QuadPart); |
| 165 | } |
| 166 |
| 167 | bool File::SetLength(int64_t length) { |
| 168 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 169 | DCHECK(IsValid()); |
| 170 |
| 171 | SCOPED_FILE_TRACE_WITH_SIZE("SetLength", length); |
| 172 |
| 173 | // Get the current file pointer. |
| 174 | LARGE_INTEGER file_pointer; |
| 175 | LARGE_INTEGER zero; |
| 176 | zero.QuadPart = 0; |
| 177 | if (!::SetFilePointerEx(file_.Get(), zero, &file_pointer, FILE_CURRENT)) |
| 178 | return false; |
| 179 |
| 180 | LARGE_INTEGER length_li; |
| 181 | length_li.QuadPart = length; |
| 182 | // If length > file size, SetFilePointerEx() should extend the file |
| 183 | // with zeroes on all Windows standard file systems (NTFS, FATxx). |
| 184 | if (!::SetFilePointerEx(file_.Get(), length_li, NULL, FILE_BEGIN)) |
| 185 | return false; |
| 186 |
| 187 | // Set the new file length and move the file pointer to its old position. |
| 188 | // This is consistent with ftruncate()'s behavior, even when the file |
| 189 | // pointer points to a location beyond the end of the file. |
| 190 | // TODO(rvargas): Emulating ftruncate details seem suspicious and it is not |
| 191 | // promised by the interface (nor was promised by PlatformFile). See if this |
| 192 | // implementation detail can be removed. |
| 193 | return ((::SetEndOfFile(file_.Get()) != FALSE) && |
| 194 | (::SetFilePointerEx(file_.Get(), file_pointer, NULL, FILE_BEGIN) != |
| 195 | FALSE)); |
| 196 | } |
| 197 |
| 198 | bool File::SetTimes(Time last_access_time, Time last_modified_time) { |
| 199 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 200 | DCHECK(IsValid()); |
| 201 |
| 202 | SCOPED_FILE_TRACE("SetTimes"); |
| 203 |
| 204 | FILETIME last_access_filetime = last_access_time.ToFileTime(); |
| 205 | FILETIME last_modified_filetime = last_modified_time.ToFileTime(); |
| 206 | return (::SetFileTime(file_.Get(), NULL, &last_access_filetime, |
| 207 | &last_modified_filetime) != FALSE); |
| 208 | } |
| 209 |
| 210 | bool File::GetInfo(Info* info) { |
| 211 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 212 | DCHECK(IsValid()); |
| 213 |
| 214 | SCOPED_FILE_TRACE("GetInfo"); |
| 215 |
| 216 | BY_HANDLE_FILE_INFORMATION file_info; |
| 217 | if (!GetFileInformationByHandle(file_.Get(), &file_info)) |
| 218 | return false; |
| 219 |
| 220 | LARGE_INTEGER size; |
| 221 | size.HighPart = file_info.nFileSizeHigh; |
| 222 | size.LowPart = file_info.nFileSizeLow; |
| 223 | info->size = size.QuadPart; |
| 224 | info->is_directory = |
| 225 | (file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; |
| 226 | info->is_symbolic_link = false; // Windows doesn't have symbolic links. |
| 227 | info->last_modified = Time::FromFileTime(file_info.ftLastWriteTime); |
| 228 | info->last_accessed = Time::FromFileTime(file_info.ftLastAccessTime); |
| 229 | info->creation_time = Time::FromFileTime(file_info.ftCreationTime); |
| 230 | return true; |
| 231 | } |
| 232 |
| 233 | namespace { |
| 234 |
| 235 | DWORD LockFileFlagsForMode(File::LockMode mode) { |
| 236 | DWORD flags = LOCKFILE_FAIL_IMMEDIATELY; |
| 237 | switch (mode) { |
| 238 | case File::LockMode::kShared: |
| 239 | return flags; |
| 240 | case File::LockMode::kExclusive: |
| 241 | return flags | LOCKFILE_EXCLUSIVE_LOCK; |
| 242 | } |
| 243 | NOTREACHED(); |
| 244 | } |
| 245 |
| 246 | } // namespace |
| 247 |
| 248 | File::Error File::Lock(File::LockMode mode) { |
| 249 | DCHECK(IsValid()); |
| 250 |
| 251 | SCOPED_FILE_TRACE("Lock"); |
| 252 |
| 253 | OVERLAPPED overlapped = {}; |
| 254 | BOOL result = |
| 255 | LockFileEx(file_.Get(), LockFileFlagsForMode(mode), /*dwReserved=*/0, |
| 256 | /*nNumberOfBytesToLockLow=*/MAXDWORD, |
| 257 | /*nNumberOfBytesToLockHigh=*/MAXDWORD, &overlapped); |
| 258 | if (!result) |
| 259 | return GetLastFileError(); |
| 260 | return FILE_OK; |
| 261 | } |
| 262 |
| 263 | File::Error File::Unlock() { |
| 264 | DCHECK(IsValid()); |
| 265 |
| 266 | SCOPED_FILE_TRACE("Unlock"); |
| 267 |
| 268 | OVERLAPPED overlapped = {}; |
| 269 | BOOL result = |
| 270 | UnlockFileEx(file_.Get(), /*dwReserved=*/0, |
| 271 | /*nNumberOfBytesToLockLow=*/MAXDWORD, |
| 272 | /*nNumberOfBytesToLockHigh=*/MAXDWORD, &overlapped); |
| 273 | if (!result) |
| 274 | return GetLastFileError(); |
| 275 | return FILE_OK; |
| 276 | } |
| 277 |
| 278 | File File::Duplicate() const { |
| 279 | if (!IsValid()) |
| 280 | return File(); |
| 281 |
| 282 | SCOPED_FILE_TRACE("Duplicate"); |
| 283 |
| 284 | HANDLE other_handle = nullptr; |
| 285 |
| 286 | if (!::DuplicateHandle(GetCurrentProcess(), // hSourceProcessHandle |
| 287 | GetPlatformFile(), |
| 288 | GetCurrentProcess(), // hTargetProcessHandle |
| 289 | &other_handle, |
| 290 | 0, // dwDesiredAccess ignored due to SAME_ACCESS |
| 291 | FALSE, // !bInheritHandle |
| 292 | DUPLICATE_SAME_ACCESS)) { |
| 293 | return File(GetLastFileError()); |
| 294 | } |
| 295 |
| 296 | return File(ScopedPlatformFile(other_handle), async()); |
| 297 | } |
| 298 |
| 299 | bool File::DeleteOnClose(bool delete_on_close) { |
| 300 | FILE_DISPOSITION_INFO disposition = {delete_on_close}; |
| 301 | return ::SetFileInformationByHandle(GetPlatformFile(), FileDispositionInfo, |
| 302 | &disposition, sizeof(disposition)) != 0; |
| 303 | } |
| 304 |
| 305 | // Static. |
| 306 | File::Error File::OSErrorToFileError(DWORD last_error) { |
| 307 | switch (last_error) { |
| 308 | case ERROR_SHARING_VIOLATION: |
| 309 | return FILE_ERROR_IN_USE; |
| 310 | case ERROR_ALREADY_EXISTS: |
| 311 | case ERROR_FILE_EXISTS: |
| 312 | return FILE_ERROR_EXISTS; |
| 313 | case ERROR_FILE_NOT_FOUND: |
| 314 | case ERROR_PATH_NOT_FOUND: |
| 315 | return FILE_ERROR_NOT_FOUND; |
| 316 | case ERROR_ACCESS_DENIED: |
| 317 | return FILE_ERROR_ACCESS_DENIED; |
| 318 | case ERROR_TOO_MANY_OPEN_FILES: |
| 319 | return FILE_ERROR_TOO_MANY_OPENED; |
| 320 | case ERROR_OUTOFMEMORY: |
| 321 | case ERROR_NOT_ENOUGH_MEMORY: |
| 322 | return FILE_ERROR_NO_MEMORY; |
| 323 | case ERROR_HANDLE_DISK_FULL: |
| 324 | case ERROR_DISK_FULL: |
| 325 | case ERROR_DISK_RESOURCES_EXHAUSTED: |
| 326 | return FILE_ERROR_NO_SPACE; |
| 327 | case ERROR_USER_MAPPED_FILE: |
| 328 | return FILE_ERROR_INVALID_OPERATION; |
| 329 | case ERROR_NOT_READY: |
| 330 | case ERROR_SECTOR_NOT_FOUND: |
| 331 | case ERROR_DEV_NOT_EXIST: |
| 332 | case ERROR_IO_DEVICE: |
| 333 | case ERROR_FILE_CORRUPT: |
| 334 | case ERROR_DISK_CORRUPT: |
| 335 | return FILE_ERROR_IO; |
| 336 | default: |
| 337 | UmaHistogramSparse("PlatformFile.UnknownErrors.Windows", last_error); |
| 338 | // This function should only be called for errors. |
| 339 | DCHECK_NE(static_cast<DWORD>(ERROR_SUCCESS), last_error); |
| 340 | return FILE_ERROR_FAILED; |
| 341 | } |
| 342 | } |
| 343 |
| 344 | void File::DoInitialize(const FilePath& path, uint32_t flags) { |
| 345 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 346 | DCHECK(!IsValid()); |
| 347 |
| 348 | DWORD disposition = 0; |
| 349 |
| 350 | if (flags & FLAG_OPEN) |
| 351 | disposition = OPEN_EXISTING; |
| 352 |
| 353 | if (flags & FLAG_CREATE) { |
| 354 | DCHECK(!disposition); |
| 355 | disposition = CREATE_NEW; |
| 356 | } |
| 357 |
| 358 | if (flags & FLAG_OPEN_ALWAYS) { |
| 359 | DCHECK(!disposition); |
| 360 | disposition = OPEN_ALWAYS; |
| 361 | } |
| 362 |
| 363 | if (flags & FLAG_CREATE_ALWAYS) { |
| 364 | DCHECK(!disposition); |
| 365 | DCHECK(flags & FLAG_WRITE); |
| 366 | disposition = CREATE_ALWAYS; |
| 367 | } |
| 368 |
| 369 | if (flags & FLAG_OPEN_TRUNCATED) { |
| 370 | DCHECK(!disposition); |
| 371 | DCHECK(flags & FLAG_WRITE); |
| 372 | disposition = TRUNCATE_EXISTING; |
| 373 | } |
| 374 |
| 375 | if (!disposition) { |
| 376 | ::SetLastError(ERROR_INVALID_PARAMETER); |
| 377 | error_details_ = FILE_ERROR_FAILED; |
| 378 | NOTREACHED(); |
| 379 | return; |
| 380 | } |
| 381 |
| 382 | DWORD access = 0; |
| 383 | if (flags & FLAG_WRITE) |
| 384 | access = GENERIC_WRITE; |
| 385 | if (flags & FLAG_APPEND) { |
| 386 | DCHECK(!access); |
| 387 | access = FILE_APPEND_DATA; |
| 388 | } |
| 389 | if (flags & FLAG_READ) |
| 390 | access |= GENERIC_READ; |
| 391 | if (flags & FLAG_WRITE_ATTRIBUTES) |
| 392 | access |= FILE_WRITE_ATTRIBUTES; |
| 393 | if (flags & FLAG_EXECUTE) |
| 394 | access |= GENERIC_EXECUTE; |
| 395 | if (flags & FLAG_CAN_DELETE_ON_CLOSE) |
| 396 | access |= DELETE; |
| 397 |
| 398 | DWORD sharing = (flags & FLAG_EXCLUSIVE_READ) ? 0 : FILE_SHARE_READ; |
| 399 | if (!(flags & FLAG_EXCLUSIVE_WRITE)) |
| 400 | sharing |= FILE_SHARE_WRITE; |
| 401 | if (flags & FLAG_SHARE_DELETE) |
| 402 | sharing |= FILE_SHARE_DELETE; |
| 403 |
| 404 | DWORD create_flags = 0; |
| 405 | if (flags & FLAG_ASYNC) |
| 406 | create_flags |= FILE_FLAG_OVERLAPPED; |
| 407 | if (flags & FLAG_TEMPORARY) |
| 408 | create_flags |= FILE_ATTRIBUTE_TEMPORARY; |
| 409 | if (flags & FLAG_HIDDEN) |
| 410 | create_flags |= FILE_ATTRIBUTE_HIDDEN; |
| 411 | if (flags & FLAG_DELETE_ON_CLOSE) |
| 412 | create_flags |= FILE_FLAG_DELETE_ON_CLOSE; |
| 413 | if (flags & FLAG_BACKUP_SEMANTICS) |
| 414 | create_flags |= FILE_FLAG_BACKUP_SEMANTICS; |
| 415 | if (flags & FLAG_SEQUENTIAL_SCAN) |
| 416 | create_flags |= FILE_FLAG_SEQUENTIAL_SCAN; |
| 417 |
| 418 | file_.Set(CreateFile(path.value().c_str(), access, sharing, NULL, disposition, |
| 419 | create_flags, NULL)); |
| 420 |
| 421 | if (file_.IsValid()) { |
| 422 | error_details_ = FILE_OK; |
| 423 | async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC); |
| 424 |
| 425 | if (flags & (FLAG_OPEN_ALWAYS)) |
| 426 | created_ = (ERROR_ALREADY_EXISTS != GetLastError()); |
| 427 | else if (flags & (FLAG_CREATE_ALWAYS | FLAG_CREATE)) |
| 428 | created_ = true; |
| 429 | } else { |
| 430 | error_details_ = GetLastFileError(); |
| 431 | } |
| 432 | } |
| 433 |
| 434 | bool File::Flush() { |
| 435 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 436 | DCHECK(IsValid()); |
| 437 | SCOPED_FILE_TRACE("Flush"); |
| 438 |
| 439 | // On Windows 8 and above, FlushFileBuffers is guaranteed to flush the storage |
| 440 | // device's internal buffers (if they exist) before returning. |
| 441 | // https://blogs.msdn.microsoft.com/oldnewthing/20170510-00/?p=95505 |
| 442 | return ::FlushFileBuffers(file_.Get()) != FALSE; |
| 443 | } |
| 444 |
| 445 | void File::SetPlatformFile(PlatformFile file) { |
| 446 | file_.Set(file); |
| 447 | } |
| 448 |
| 449 | // static |
| 450 | File::Error File::GetLastFileError() { |
| 451 | return File::OSErrorToFileError(GetLastError()); |
| 452 | } |
| 453 |
| 454 | } // namespace base |
| 455 |