Skip to content

Commit

Permalink
Fix long path support in fs::statfs on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Nekotekina committed Oct 27, 2019
1 parent 839e088 commit 46d692d
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions Utilities/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,11 +501,27 @@ bool fs::statfs(const std::string& path, fs::device_stat& info)
ULARGE_INTEGER total_size;
ULARGE_INTEGER total_free;

// Get disk letter from path (TODO)
std::wstring disk(L"C:");
disk[0] = path[0];
// Convert path and return it back to the "short" format
const bool unc = path.size() > 2 && (path[0] == '\\' || path[0] == '/') && path[1] == path[0];

if (!GetDiskFreeSpaceExW(disk.c_str(), &avail_free, &total_size, &total_free))
std::wstring str = to_wchar(path).get() + (unc ? 6 : 4);

if (unc)
{
str[0] = '\\';
str[1] = '\\';
}

// Keep cutting path from right until it's short enough
while (str.size() > 256)
{
if (std::size_t x = str.find_last_of('\\') + 1)
str.resize(x - 1);
else
break;
}

if (!GetDiskFreeSpaceExW(str.c_str(), &avail_free, &total_size, &total_free))
{
g_tls_error = to_error(GetLastError());
return false;
Expand Down

0 comments on commit 46d692d

Please sign in to comment.