Skip to content

Commit

Permalink
Don't always set GENERIC_READ when opening files on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminwinger committed May 29, 2023
1 parent 2478bc8 commit 171f404
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/common/file_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,16 @@ int64_t FileInfo::getFileSize() {

std::unique_ptr<FileInfo> FileUtils::openFile(const std::string& path, int flags) {
#if defined(_WIN32)
// Not providing GENERIC_READ seems to cause problems.
auto dwDesiredAccess = GENERIC_READ;
auto dwDesiredAccess = 0ul;
auto dwCreationDisposition = (flags & O_CREAT) ? OPEN_ALWAYS : OPEN_EXISTING;
auto dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
if (flags & (O_CREAT | O_WRONLY | O_RDWR)) {
dwDesiredAccess |= GENERIC_WRITE;
}
// O_RDONLY is 0 in practice, so flags & (O_RDONLY | O_RDWR) doesn't work.
if (!(flags & O_WRONLY)) {
dwDesiredAccess |= GENERIC_READ;
}

HANDLE handle = CreateFileA(path.c_str(), dwDesiredAccess, dwShareMode, nullptr,
dwCreationDisposition, FILE_ATTRIBUTE_NORMAL, nullptr);
Expand Down

0 comments on commit 171f404

Please sign in to comment.