Bazel client, Windows: CreateDirectoryW path limit Fix the path limit for non-UNC-prefixed paths when using CreateDirectoryW. According to MSDN [1], this is only 248 chars, as opposed to the usual 260 (MAX_PATH). See https://github.com/bazelbuild/bazel/issues/2107 [1] https://msdn.microsoft.com/en-us/library/windows/desktop/aa363855(v=vs.85).aspx -- PiperOrigin-RevId: 149627964 MOS_MIGRATED_REVID=149627964
diff --git a/src/main/cpp/util/file_platform.h b/src/main/cpp/util/file_platform.h index 7733e7a..50b75de 100644 --- a/src/main/cpp/util/file_platform.h +++ b/src/main/cpp/util/file_platform.h
@@ -205,7 +205,8 @@ #if defined(COMPILER_MSVC) || defined(__CYGWIN__) // Like `AsWindowsPath` but the result is absolute and has UNC prefix if needed. -bool AsWindowsPathWithUncPrefix(const std::string &path, std::wstring *wpath); +bool AsWindowsPathWithUncPrefix(const std::string &path, std::wstring *wpath, + size_t max_path = 260 /* MAX_PATH */); // Same as `AsWindowsPath`, but returns a lowercase 8dot3 style shortened path. // Result will never have a UNC prefix, nor a trailing "/" or "\".
diff --git a/src/main/cpp/util/file_windows.cc b/src/main/cpp/util/file_windows.cc index f763f9e..5159917 100644 --- a/src/main/cpp/util/file_windows.cc +++ b/src/main/cpp/util/file_windows.cc
@@ -101,8 +101,8 @@ return CharTraits<char_type>::IsAlpha(ch[0]) && ch[1] == ':'; } -static void AddUncPrefixMaybe(wstring* path) { - if (path->size() >= MAX_PATH && !HasUncPrefix(path->c_str())) { +static void AddUncPrefixMaybe(wstring* path, size_t max_path = MAX_PATH) { + if (path->size() >= max_path && !HasUncPrefix(path->c_str())) { *path = wstring(L"\\\\?\\") + *path; } } @@ -483,7 +483,8 @@ return true; } -bool AsWindowsPathWithUncPrefix(const string& path, wstring* wpath) { +bool AsWindowsPathWithUncPrefix(const string& path, wstring* wpath, + size_t max_path) { if (IsDevNull(path)) { wpath->assign(L"NUL"); return true; @@ -497,7 +498,7 @@ if (!IsAbsolute(path)) { wpath->assign(wstring(GetCwdW().get()) + L"\\" + *wpath); } - AddUncPrefixMaybe(wpath); + AddUncPrefixMaybe(wpath, max_path); return true; } @@ -1120,7 +1121,9 @@ return false; } wstring wpath; - if (!AsWindowsPathWithUncPrefix(path, &wpath)) { + // According to MSDN, CreateDirectory's limit without the UNC prefix is + // 248 characters (so it could fit another filename before reaching MAX_PATH). + if (!AsWindowsPathWithUncPrefix(path, &wpath, 248)) { return false; } return MakeDirectoriesW(wpath);