From 29478e617a3fff6ad2028e16bff8622fcf357854 Mon Sep 17 00:00:00 2001 From: Nikita <45687815+N1kSt4r@users.noreply.github.com> Date: Mon, 4 Sep 2023 18:16:40 +0300 Subject: [PATCH] Fix wide paths for Windows This commit allows to open wide paths, for example, cyrilic --- util/file.cc | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/util/file.cc b/util/file.cc index ea122ef8..8a4cc204 100644 --- a/util/file.cc +++ b/util/file.cc @@ -28,6 +28,7 @@ #elif defined(_WIN32) || defined(_WIN64) #include #include +#include #else #include #endif @@ -71,7 +72,13 @@ bool OutputFileIsStdout(StringPiece path) { int OpenReadOrThrow(const char *name) { int ret; #if defined(_WIN32) || defined(_WIN64) - UTIL_THROW_IF(-1 == (ret = _open(name, _O_BINARY | _O_RDONLY)), ErrnoException, "while opening " << name); + ret = _open(name, _O_BINARY | _O_RDONLY); + if (ret == -1) { + std::wstring_convert, wchar_t> convert; + std::wstring wideName = convert.from_bytes(name); + ret = _wopen(wideName.data(), _O_BINARY | _O_RDONLY); + } + UTIL_THROW_IF(-1 == ret, ErrnoException, "while opening " << name); #else UTIL_THROW_IF(-1 == (ret = open(name, O_RDONLY)), ErrnoException, "while opening " << name); #endif @@ -81,7 +88,13 @@ int OpenReadOrThrow(const char *name) { int CreateOrThrow(const char *name) { int ret; #if defined(_WIN32) || defined(_WIN64) - UTIL_THROW_IF(-1 == (ret = _open(name, _O_CREAT | _O_TRUNC | _O_RDWR | _O_BINARY, _S_IREAD | _S_IWRITE)), ErrnoException, "while creating " << name); + ret = _open(name, _O_CREAT | _O_TRUNC | _O_RDWR | _O_BINARY, _S_IREAD | _S_IWRITE); + if (ret == -1) { + std::wstring_convert, wchar_t> convert; + std::wstring wideName = convert.from_bytes(name); + ret = _wopen(wideName.data(), _O_CREAT | _O_TRUNC | _O_RDWR | _O_BINARY, _S_IREAD | _S_IWRITE); + } + UTIL_THROW_IF(-1 == ret, ErrnoException, "while creating " << name); #else UTIL_THROW_IF(-1 == (ret = open(name, O_CREAT | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)), ErrnoException, "while creating " << name); #endif