Skip to content

Commit c350e8d

Browse files
committed
getAvailableDiskSpace now works on FreeBSD
1 parent abd3dc9 commit c350e8d

File tree

2 files changed

+52
-44
lines changed

2 files changed

+52
-44
lines changed

changelog/std-file-getAvailableDiskSpace.dd

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ $(REF getAvailableDiskSpace, std,file) receives as a parameter the path of a fil
44
directory in the file system, and returns the available disk space on the mounted filesystem.
55
If the given path is nonexistent, an exception is thrown.
66

7-
Due to inconsistent behaviour with the `statvfs` `Posix` function, FreeBSD does not support this functionality.
8-
97
---
108
import std.file;
119
ulong size = getAvailableDiskSpace(".");

std/file.d

Lines changed: 52 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5122,75 +5122,85 @@ string tempDir() @trusted
51225122
}
51235123

51245124
/**
5125-
* Returns the available space in a file system.
5126-
*
5127-
* On `Windows`, this function uses the result of the Windows API function
5128-
* $(LINK2 https://msdn.microsoft.com/en-us/library/windows/desktop/aa364937(v=vs.85).aspx, $(D GetDiskFreeSpaceExW)).
5129-
*
5130-
* On `POSIX` platforms, it uses the $(LINK2 http://man7.org/linux/man-pages/man3/statvfs.3.html,
5131-
* $(D statvfs)) function.
5132-
*
5133-
* Due to inconsistent behaviour with the `statvfs` `Posix` function,
5134-
* `FreeBSD` does not support `getAvailableDiskSpace`.
5135-
*
5136-
* Params:
5137-
* path = a valid path to any file in the file system
5138-
*
5139-
* Returns:
5140-
* ulong which contains the available space in bytes
5141-
*
5142-
* Throws:
5143-
* $(LREF FileException) in case statvfs (`POSIX`) or GetDiskFreeSpaceExW (`Windows`) fails.
5125+
Returns the available space in a file system.
5126+
5127+
On `Windows`, this function uses the result of the Windows API function
5128+
$(LINK2 https://msdn.microsoft.com/en-us/library/windows/desktop/aa364937(v=vs.85).aspx, $(D GetDiskFreeSpaceExW)).
5129+
5130+
On `POSIX` platforms, it uses the $(LINK2 http://man7.org/linux/man-pages/man3/statvfs.3.html,
5131+
$(D statvfs)) function. On `FreeBSD` however, it uses the $(LINK2 https://www.freebsd.org/cgi/man.cgi?query=statfs&sektion=2,
5132+
$(D statfs)) function.
5133+
5134+
Params:
5135+
path = a valid path to any file in the file system
5136+
Returns:
5137+
Available space in bytes
5138+
5139+
Throws:
5140+
$(LREF FileException) in case of failure
51445141
*/
5145-
ulong getAvailableDiskSpace(string path) @trusted
5142+
ulong getAvailableDiskSpace(string path) @safe
51465143
{
51475144
import std.exception : enforce;
51485145
import std.conv : text;
51495146

5150-
version (FreeBSD)
5151-
{
5152-
static assert(0, "Unsupported platform");
5153-
}
5154-
else version (Windows)
5147+
version (Windows)
51555148
{
51565149
import core.sys.windows.winbase : GetDiskFreeSpaceExW, GetLastError;
51575150
import core.sys.windows.winnt : ULARGE_INTEGER;
51585151
import std.internal.cstring : tempCStringW;
51595152

51605153
ULARGE_INTEGER freeBytesAvailable;
5161-
auto err = GetDiskFreeSpaceExW(path.tempCStringW(), &freeBytesAvailable, null, null);
5154+
auto err = () @trusted {
5155+
return GetDiskFreeSpaceExW(path.tempCStringW(), &freeBytesAvailable, null, null);
5156+
} ();
51625157
enforce(err != 0,
51635158
new FileException(text("Cannot get available disk space: ", GetLastError())));
51645159

51655160
return freeBytesAvailable.QuadPart;
51665161
}
51675162
else version (Posix)
51685163
{
5169-
import core.sys.posix.sys.statvfs : statvfs, statvfs_t;
51705164
import std.string : toStringz;
51715165

5172-
statvfs_t stats = void;
5173-
auto err = statvfs(path.toStringz(), &stats);
5174-
enforce(err == 0,
5175-
new FileException(text("Cannot get available disk space: ", err)));
5166+
version (FreeBSD)
5167+
{
5168+
import core.sys.posix.sys.statvfs : statfs, statfs_t;
51765169

5177-
return stats.f_bavail * stats.f_frsize;
5170+
statfs_t stats = void;
5171+
auto err = () @trusted {
5172+
return statfs(path.toStringz(), &stats);
5173+
} ();
5174+
enforce(err == 0,
5175+
new FileException(text("Cannot get available disk space: ", err)));
5176+
5177+
return stats.f_bavail * stats.f_bsize;
5178+
}
5179+
else
5180+
{
5181+
import core.sys.posix.sys.statvfs : statvfs, statvfs_t;
5182+
5183+
statvfs_t stats = void;
5184+
auto err = () @trusted {
5185+
return statvfs(path.toStringz(), &stats);
5186+
} ();
5187+
enforce(err == 0,
5188+
new FileException(text("Cannot get available disk space: ", err)));
5189+
5190+
return stats.f_bavail * stats.f_frsize;
5191+
}
51785192
}
51795193
else static assert(0, "Unsupported platform");
51805194
}
51815195

5182-
version (FreeBSD) {}
5183-
else
5196+
///
5197+
@safe unittest
51845198
{
5185-
///
5186-
@safe unittest
5187-
{
5188-
import std.exception : assertThrown;
5199+
import std.exception : assertThrown;
51895200

5190-
auto space = getAvailableDiskSpace(".");
5191-
assert(space > 0);
5201+
auto space = getAvailableDiskSpace(".");
5202+
assert(space > 0);
51925203

5193-
assertThrown(getAvailableDiskSpace("ThisFileDoesNotExist123123"));
5194-
}
5204+
assertThrown!FileException(getAvailableDiskSpace("ThisFileDoesNotExist123123"));
51955205
}
51965206

0 commit comments

Comments
 (0)