| 401 | } |
| 402 | |
| 403 | Status NewRandomAccessFile(const std::string& filename, |
| 404 | RandomAccessFile** result) override { |
| 405 | *result = nullptr; |
| 406 | DWORD desired_access = GENERIC_READ; |
| 407 | DWORD share_mode = FILE_SHARE_READ; |
| 408 | auto wFilename = toUtf16(filename); |
| 409 | ScopedHandle handle = |
| 410 | ::CreateFileW(wFilename.c_str(), desired_access, share_mode, |
| 411 | /*lpSecurityAttributes=*/nullptr, OPEN_EXISTING, |
| 412 | FILE_ATTRIBUTE_READONLY, |
| 413 | /*hTemplateFile=*/nullptr); |
| 414 | if (!handle.is_valid()) { |
| 415 | return WindowsError(filename, ::GetLastError()); |
| 416 | } |
| 417 | if (!mmap_limiter_.Acquire()) { |
| 418 | *result = new WindowsRandomAccessFile(filename, std::move(handle)); |
| 419 | return Status::OK(); |
| 420 | } |
| 421 | |
| 422 | LARGE_INTEGER file_size; |
| 423 | Status status; |
| 424 | if (!::GetFileSizeEx(handle.get(), &file_size)) { |
| 425 | mmap_limiter_.Release(); |
| 426 | return WindowsError(filename, ::GetLastError()); |
| 427 | } |
| 428 | |
| 429 | ScopedHandle mapping = |
| 430 | ::CreateFileMappingW(handle.get(), |
| 431 | /*security attributes=*/nullptr, PAGE_READONLY, |
| 432 | /*dwMaximumSizeHigh=*/0, |
| 433 | /*dwMaximumSizeLow=*/0, |
| 434 | /*lpName=*/nullptr); |
| 435 | if (mapping.is_valid()) { |
| 436 | void* mmap_base = ::MapViewOfFile(mapping.get(), FILE_MAP_READ, |
| 437 | /*dwFileOffsetHigh=*/0, |
| 438 | /*dwFileOffsetLow=*/0, |
| 439 | /*dwNumberOfBytesToMap=*/0); |
| 440 | if (mmap_base) { |
| 441 | *result = new WindowsMmapReadableFile( |
| 442 | filename, reinterpret_cast<char*>(mmap_base), |
| 443 | static_cast<size_t>(file_size.QuadPart), &mmap_limiter_); |
| 444 | return Status::OK(); |
| 445 | } |
| 446 | } |
| 447 | mmap_limiter_.Release(); |
| 448 | return WindowsError(filename, ::GetLastError()); |
| 449 | } |
| 450 | |
| 451 | Status NewWritableFile(const std::string& filename, |
| 452 | WritableFile** result) override { |