[Issue 16487] Add function to obtain the available disk space
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Tue Sep 13 01:56:14 PDT 2016
https://issues.dlang.org/show_bug.cgi?id=16487
--- Comment #4 from b2.temp at gmx.com ---
(In reply to Johan Engelen from comment #3)
> Version that actually compiles on Windows too:
> ```
> // Returns ulong.max when the available disk space could not be determined.
> ulong getAvailableDiskSpace(string path)
> {
> import std.string: toStringz;
> version (Windows)
> {
> import std.path;
> import core.sys.windows.winbase;
> import core.sys.windows.winnt;
> import std.internal.cstring;
>
> ULARGE_INTEGER freeBytesAvailable;
> path ~= dirSeparator;
> auto success = GetDiskFreeSpaceExW(path.tempCStringW(),
> &freeBytesAvailable, null, null);
> return success ? freeBytesAvailable.QuadPart : ulong.max;
> }
> else
> {
> import core.sys.posix.sys.statvfs;
>
> statvfs_t stats;
> int err = statvfs(path.toStringz(), &stats);
> return !err ? stats.f_bavail * stats.f_frsize : ulong.max;
> }
> }
> ```
what I would say if it'd be a PR:
1. change declaration to
ulong getAvailableDiskSpace(const(char)[] path)
because const(char)[] acts as a wildcard and takes all char[] variants.
2. returns -1 on failure. ulong.max: No way !!!
3. linux version: don't need to init the target so
statvfs_t stats = void;
Otherwise let's get the merge ;)
--
More information about the Digitalmars-d-bugs
mailing list