[Issue 16487] Add function to obtain the available disk space

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Tue Sep 13 01:11:49 PDT 2016


https://issues.dlang.org/show_bug.cgi?id=16487

--- Comment #3 from Johan Engelen <jbc.engelen at gmail.com> ---
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;
    }
}
```

--


More information about the Digitalmars-d-bugs mailing list