-
-
Notifications
You must be signed in to change notification settings - Fork 747
Fix Issue 16487 - Add function to obtain the available disk space #5776
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| Added the `std.file.getAvailableDiskSpace` functionality. | ||
|
|
||
| $(REF getAvailableDiskSpace, std,file) receives as a parameter the path of a file or | ||
| directory in the file system, and returns the available disk space on the mounted filesystem. | ||
| If the given path is nonexistent, an exception is thrown. | ||
|
|
||
| --- | ||
| import std.file; | ||
| ulong size = getAvailableDiskSpace("."); | ||
| assert(size > 0); | ||
| --- | ||
|
|
||
| --- | ||
| import std.file; | ||
| assertThrown(getAvailableDiskSpace("NonExistentFile")); | ||
| --- | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,6 +62,7 @@ $(TR $(TD Other) $(TD | |
| $(LREF FileException) | ||
| $(LREF PreserveAttributes) | ||
| $(LREF SpanMode) | ||
| $(LREF getAvailableDiskSpace) | ||
| )) | ||
| ) | ||
|
|
||
|
|
@@ -5232,3 +5233,74 @@ string tempDir() @trusted | |
| myFile.write("hello"); | ||
| assert(myFile.readText == "hello"); | ||
| } | ||
|
|
||
| /** | ||
| Returns the available disk space based on a given path. | ||
| On Windows, `path` must be a directory; on Posix systems, it can be a file or directory. | ||
|
|
||
| Params: | ||
| path = on Windows, it must be a directory; on Posix it can be a file or directory | ||
| Returns: | ||
| Available space in bytes | ||
|
|
||
| Throws: | ||
| $(LREF FileException) in case of failure | ||
| */ | ||
| ulong getAvailableDiskSpace(scope const(char)[] path) @safe | ||
| { | ||
| version (Windows) | ||
| { | ||
| import core.sys.windows.winbase : GetDiskFreeSpaceExW; | ||
| import core.sys.windows.winnt : ULARGE_INTEGER; | ||
| import std.internal.cstring : tempCStringW; | ||
|
|
||
| ULARGE_INTEGER freeBytesAvailable; | ||
| auto err = () @trusted { | ||
| return GetDiskFreeSpaceExW(path.tempCStringW(), &freeBytesAvailable, null, null); | ||
| } (); | ||
| cenforce(err != 0, "Cannot get available disk space"); | ||
|
|
||
| return freeBytesAvailable.QuadPart; | ||
| } | ||
| else version (Posix) | ||
| { | ||
| import std.internal.cstring : tempCString; | ||
|
|
||
| version (FreeBSD) | ||
| { | ||
| import core.sys.freebsd.sys.mount : statfs, statfs_t; | ||
|
|
||
| statfs_t stats; | ||
| auto err = () @trusted { | ||
| return statfs(path.tempCString(), &stats); | ||
| } (); | ||
| cenforce(err == 0, "Cannot get available disk space"); | ||
|
|
||
| return stats.f_bavail * stats.f_bsize; | ||
| } | ||
| else | ||
| { | ||
| import core.sys.posix.sys.statvfs : statvfs, statvfs_t; | ||
|
|
||
| statvfs_t stats; | ||
| auto err = () @trusted { | ||
| return statvfs(path.tempCString(), &stats); | ||
| } (); | ||
| cenforce(err == 0, "Cannot get available disk space"); | ||
|
|
||
| return stats.f_bavail * stats.f_frsize; | ||
| } | ||
| } | ||
thewilsonator marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| else static assert(0, "Unsupported platform"); | ||
| } | ||
|
|
||
| /// | ||
| @safe unittest | ||
thewilsonator marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| import std.exception : assertThrown; | ||
|
|
||
| auto space = getAvailableDiskSpace("."); | ||
| assert(space > 0); | ||
|
|
||
| assertThrown!FileException(getAvailableDiskSpace("ThisFileDoesNotExist123123")); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can do more tests here. Simple create a directory with Example:
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That'd be a bit excessive - we'd be testing the OS primitives.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on what you have running on your system, the size could vary a lot.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should test that the function works for |
||
Uh oh!
There was an error while loading. Please reload this page.