MCPcopy Create free account
hub / github.com/AutoHotkey/AutoHotkey / DriveSpace

Function DriveSpace

source/script2.cpp:6751–6780  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6749
6750
6751void DriveSpace(ResultToken &aResultToken, LPTSTR aPath, bool aGetFreeSpace)
6752// Because of NTFS's ability to mount volumes into a directory, a path might not necessarily
6753// have the same amount of free space as its root drive. However, I'm not sure if this
6754// method here actually takes that into account.
6755{
6756 ASSERT(aPath && *aPath);
6757
6758 TCHAR buf[MAX_PATH]; // MAX_PATH vs T_MAX_PATH because testing shows it doesn't support long paths even with \\?\.
6759 tcslcpy(buf, aPath, _countof(buf));
6760 size_t length = _tcslen(buf);
6761 if (buf[length - 1] != '\\' // Trailing backslash is absent,
6762 && length + 1 < _countof(buf)) // and there's room to fix it.
6763 {
6764 buf[length++] = '\\';
6765 buf[length] = '\0';
6766 }
6767 //else it should still work unless this is a UNC path.
6768
6769 // MSDN: "The GetDiskFreeSpaceEx function returns correct values for all volumes, including those
6770 // that are greater than 2 gigabytes."
6771 __int64 free_space;
6772 ULARGE_INTEGER total, free, used;
6773 if (!GetDiskFreeSpaceEx(buf, &free, &total, &used))
6774 _f_throw_win32();
6775 // Casting this way allows sizes of up to 2,097,152 gigabytes:
6776 free_space = (__int64)((unsigned __int64)(aGetFreeSpace ? free.QuadPart : total.QuadPart)
6777 / (1024*1024));
6778
6779 _f_return(free_space);
6780}
6781
6782
6783

Callers 1

BIF_DECLFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected