Skip to content

Commit

Permalink
Don't use POSIX APIs in Windows builds (#212)
Browse files Browse the repository at this point in the history
The POSIX compatibility layer provided by the Microsoft C Runtime Library
treats its string arguments as if they were passed to the A-family of WinAPI
functions, which means that they undergo conversion to the ACP before being
passed to the actual WinAPI call. Passing UTF-8 encoded strings could have
never really worked.
  • Loading branch information
lighterowl authored Feb 27, 2020
1 parent e9d438b commit 1bead15
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
29 changes: 17 additions & 12 deletions libmediation/fs/directory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include <sstream>

#include "file.h"

#ifdef _WIN32
#include <windows.h>
#else
Expand Down Expand Up @@ -45,30 +47,33 @@ string extractFileDir(const string& fileName)

bool fileExists(const string& fileName)
{
bool fileExists = false;
#ifdef _WIN32
struct _stat64 buf;
fileExists = _stat64(fileName.c_str(), &buf) == 0;
File f;
return f.open(fileName.c_str(), File::ofRead | File::ofOpenExisting);
#else
struct stat64 buf;
fileExists = stat64(fileName.c_str(), &buf) == 0;
return stat64(fileName.c_str(), &buf) == 0;
#endif

return fileExists;
}

uint64_t getFileSize(const std::string& fileName)
{
bool res = false;
#ifdef _WIN32
struct _stat64 fileStat;
res = _stat64(fileName.c_str(), &fileStat) == 0;
File f;
if (f.open(fileName.c_str(), File::ofRead | File::ofOpenExisting))
{
uint64_t rv;
return f.size(&rv) ? rv : 0;
}
else
{
return 0;
}
#else
struct stat64 fileStat;
res = stat64(fileName.c_str(), &fileStat) == 0;
auto res = stat64(fileName.c_str(), &fileStat) == 0;
return res ? static_cast<uint64_t>(fileStat.st_size) : 0;
#endif

return res ? (uint64_t)fileStat.st_size : 0;
}

bool createDir(const std::string& dirName, bool createParentDirs)
Expand Down
5 changes: 4 additions & 1 deletion libmediation/fs/osdep/file_win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ bool File::open(const char* fName, unsigned int oflag, unsigned int systemDepend
systemDependentFlags = FILE_FLAG_RANDOM_ACCESS;
}

createDir(extractFileDir(fName), true);
if ((oflag & File::ofOpenExisting) == 0)
{
createDir(extractFileDir(fName), true);
}
m_impl = CreateFile(toWide(fName).data(), dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition,
systemDependentFlags, NULL);
if (m_impl == INVALID_HANDLE_VALUE)
Expand Down

0 comments on commit 1bead15

Please sign in to comment.