aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/cpp/util/file_windows.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/cpp/util/file_windows.cc')
-rw-r--r--src/main/cpp/util/file_windows.cc27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/main/cpp/util/file_windows.cc b/src/main/cpp/util/file_windows.cc
index 75555bba89..3fa01a445b 100644
--- a/src/main/cpp/util/file_windows.cc
+++ b/src/main/cpp/util/file_windows.cc
@@ -308,6 +308,8 @@ bool AsWindowsPath(const string& path, wstring* result) {
static bool AsWindowsPathWithUncPrefix(const string& path, wstring* wpath) {
if (!AsWindowsPath(path, wpath)) {
+ PrintError("AsWindowsPathWithUncPrefix(%s): AsWindowsPath failed, err=%d\n",
+ path.c_str(), GetLastError());
return false;
}
if (!IsAbsolute(path)) {
@@ -317,6 +319,31 @@ static bool AsWindowsPathWithUncPrefix(const string& path, wstring* wpath) {
return true;
}
+bool AsShortWindowsPath(const string& path, string* result) {
+ result->clear();
+ wstring wpath;
+ if (!AsWindowsPathWithUncPrefix(path, &wpath)) {
+ return false;
+ }
+ DWORD size = ::GetShortPathNameW(wpath.c_str(), nullptr, 0);
+ if (size == 0) {
+ return false;
+ }
+
+ unique_ptr<WCHAR[]> wshort(new WCHAR[size]); // size includes null-terminator
+ if (size - 1 != ::GetShortPathNameW(wpath.c_str(), wshort.get(), size)) {
+ pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
+ "AsShortWindowsPath(%s): GetShortPathNameW(%S) failed, err=%d",
+ path.c_str(), wpath.c_str(), GetLastError());
+ }
+ // GetShortPathNameW may preserve the UNC prefix in the result, so strip it.
+ WCHAR* result_ptr = wshort.get() + (HasUncPrefix(wshort.get()) ? 4 : 0);
+
+ result->assign(WstringToCstring(result_ptr).get());
+ ToLower(result);
+ return true;
+}
+
bool ReadFile(const string& filename, string* content, int max_size) {
if (filename.empty()) {
return false;