Jump to page: 1 2
Thread overview
[Issue 16487] Add function to obtain the available disk space
Sep 12, 2016
Sobirari Muhomori
Sep 12, 2016
Johan Engelen
Sep 13, 2016
Johan Engelen
Sep 13, 2016
b2.temp@gmx.com
Sep 13, 2016
Johan Engelen
Sep 13, 2016
b2.temp@gmx.com
Sep 13, 2016
Jack Stouffer
Sep 13, 2016
b2.temp@gmx.com
Dec 27, 2016
greenify
Dec 27, 2016
Johan Engelen
Oct 09, 2017
b2.temp@gmx.com
Jun 18, 2019
Dlang Bot
Jun 28, 2019
Dlang Bot
September 12, 2016
https://issues.dlang.org/show_bug.cgi?id=16487

--- Comment #1 from Sobirari Muhomori <dfj1esp02@sneakemail.com> ---
NTFS supports symbolic links too, so GetDiskFreeSpaceExW should receive the folder path because it can reside on a different partition than the disk it's accessed through.

--
September 12, 2016
https://issues.dlang.org/show_bug.cgi?id=16487

--- Comment #2 from Johan Engelen <jbc.engelen@gmail.com> ---
Indeed, I misread GetDiskFreeSpaceEx's documentation. Thanks.

--
September 13, 2016
https://issues.dlang.org/show_bug.cgi?id=16487

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

--
September 13, 2016
https://issues.dlang.org/show_bug.cgi?id=16487

--- Comment #4 from b2.temp@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)
September 13, 2016
https://issues.dlang.org/show_bug.cgi?id=16487

--- Comment #5 from Johan Engelen <jbc.engelen@gmail.com> ---
(In reply to b2.temp from comment #4)
> 2. returns -1 on failure. ulong.max: No way !!!

The return type is _unsigned_.
Throwing an exception is also a possibility.

--
September 13, 2016
https://issues.dlang.org/show_bug.cgi?id=16487

b2.temp@gmx.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |b2.temp@gmx.com

--- Comment #6 from b2.temp@gmx.com ---
(In reply to Johan Engelen from comment #5)
> (In reply to b2.temp from comment #4)
> > 2. returns -1 on failure. ulong.max: No way !!!
> 
> The return type is _unsigned_.

Oops, sorry , i must be blind ^^

Anyway...Wouldn't 0 be a possible return type in case of error ? It would allow:

if (auto space = getAvailableDiskSpace(root))
{
}
else
{
    throw new Exception("either the disk is full or inaccessible");
}

> Throwing an exception is also a possibility.

No, seriously, delegate this option to the user.

--
September 13, 2016
https://issues.dlang.org/show_bug.cgi?id=16487

Jack Stouffer <jack@jackstouffer.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jack@jackstouffer.com

--- Comment #7 from Jack Stouffer <jack@jackstouffer.com> ---
(In reply to b2.temp from comment #6)
> Anyway...Wouldn't 0 be a possible return type in case of error

No. What if there's no more space in the drive? Would you return something other than zero?

--
September 13, 2016
https://issues.dlang.org/show_bug.cgi?id=16487

b2.temp@gmx.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|b2.temp@gmx.com             |

--- Comment #8 from b2.temp@gmx.com ---
In both cases I 'd return 0.

        statvfs_t stats;
        int err = statvfs(path.toStringz(), &stats);
        return err ?  0 : stats.f_bavail * stats.f_frsize;

> "either the disk is full or inaccessible"

Because in both cases you can do nothing. By the way HDD are never full. Only removable media can be full (bootable USB key, DVD/CD rom, etc).

--
December 27, 2016
https://issues.dlang.org/show_bug.cgi?id=16487

greenify <greeenify@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
                 CC|                            |greeenify@gmail.com
           Assignee|nobody@puremagic.com        |jbc.engelen@gmail.com

--- Comment #9 from greenify <greeenify@gmail.com> ---
@Johan: As you have already prepared a nice function & no one disagrees that it wouldn't be useful - I assigned this to you. Looking forward to your PR ;-)

--
December 27, 2016
https://issues.dlang.org/show_bug.cgi?id=16487

Johan Engelen <jbc.engelen@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |NEW
           Assignee|jbc.engelen@gmail.com       |nobody@puremagic.com

--- Comment #10 from Johan Engelen <jbc.engelen@gmail.com> ---
Unassigning myself. I'm not going to work on this any time soon.

--
« First   ‹ Prev
1 2