@@ -60,6 +60,7 @@ $(TR $(TD Other) $(TD
60
60
$(LREF FileException)
61
61
$(LREF PreserveAttributes)
62
62
$(LREF SpanMode)
63
+ $(LREF getAvailableDiskSpace)
63
64
))
64
65
)
65
66
@@ -5119,3 +5120,74 @@ string tempDir() @trusted
5119
5120
myFile.write(" hello" );
5120
5121
assert (myFile.readText == " hello" );
5121
5122
}
5123
+
5124
+ /**
5125
+ Returns the available disk space based on a given path.
5126
+ On Windows, `path` must be a directory; on Posix systems, it can be a file or directory.
5127
+
5128
+ Params:
5129
+ path = on Windows, it must be a directory; on Posix it can be a file or directory
5130
+ Returns:
5131
+ Available space in bytes
5132
+
5133
+ Throws:
5134
+ $(LREF FileException) in case of failure
5135
+ */
5136
+ ulong getAvailableDiskSpace (string path) @safe
5137
+ {
5138
+ version (Windows )
5139
+ {
5140
+ import core.sys.windows.winbase : GetDiskFreeSpaceExW;
5141
+ import core.sys.windows.winnt : ULARGE_INTEGER ;
5142
+ import std.internal.cstring : tempCStringW;
5143
+
5144
+ ULARGE_INTEGER freeBytesAvailable;
5145
+ auto err = () @trusted {
5146
+ return GetDiskFreeSpaceExW (path.tempCStringW(), &freeBytesAvailable, null , null );
5147
+ } ();
5148
+ cenforce(err != 0 , " Cannot get available disk space" );
5149
+
5150
+ return freeBytesAvailable.QuadPart;
5151
+ }
5152
+ else version (Posix )
5153
+ {
5154
+ import std.string : toStringz;
5155
+
5156
+ version (FreeBSD )
5157
+ {
5158
+ import core.sys.posix.sys.statvfs : statfs, statfs_t;
5159
+
5160
+ statfs_t stats;
5161
+ auto err = () @trusted {
5162
+ return statfs (path.toStringz(), &stats);
5163
+ } ();
5164
+ cenforce(err == 0 , " Cannot get available disk space" );
5165
+
5166
+ return stats.f_bavail * stats.f_bsize;
5167
+ }
5168
+ else
5169
+ {
5170
+ import core.sys.posix.sys.statvfs : statvfs, statvfs_t;
5171
+
5172
+ statvfs_t stats;
5173
+ auto err = () @trusted {
5174
+ return statvfs (path.toStringz(), &stats);
5175
+ } ();
5176
+ cenforce(err == 0 , " Cannot get available disk space" );
5177
+
5178
+ return stats.f_bavail * stats.f_frsize;
5179
+ }
5180
+ }
5181
+ else static assert (0 , " Unsupported platform" );
5182
+ }
5183
+
5184
+ // /
5185
+ @safe unittest
5186
+ {
5187
+ import std.exception : assertThrown;
5188
+
5189
+ auto space = getAvailableDiskSpace(" ." );
5190
+ assert (space > 0 );
5191
+
5192
+ assertThrown! FileException (getAvailableDiskSpace(" ThisFileDoesNotExist123123" ));
5193
+ }
0 commit comments