@@ -5122,75 +5122,85 @@ string tempDir() @trusted
5122
5122
}
5123
5123
5124
5124
/**
5125
- * Returns the available space in a file system.
5126
- *
5127
- * On `Windows`, this function uses the result of the Windows API function
5128
- * $(LINK2 https://msdn.microsoft.com/en-us/library/windows/desktop/aa364937(v=vs.85).aspx, $(D GetDiskFreeSpaceExW)).
5129
- *
5130
- * On `POSIX` platforms, it uses the $(LINK2 http://man7.org/linux/man-pages/man3/statvfs.3.html,
5131
- * $(D statvfs)) function.
5132
- *
5133
- * Due to inconsistent behaviour with the `statvfs` `Posix` function,
5134
- * `FreeBSD` does not support `getAvailableDiskSpace`.
5135
- *
5136
- * Params:
5137
- * path = a valid path to any file in the file system
5138
- *
5139
- * Returns:
5140
- * ulong which contains the available space in bytes
5141
- *
5142
- * Throws:
5143
- * $(LREF FileException) in case statvfs (`POSIX`) or GetDiskFreeSpaceExW (`Windows`) fails.
5125
+ Returns the available space in a file system.
5126
+
5127
+ On `Windows`, this function uses the result of the Windows API function
5128
+ $(LINK2 https://msdn.microsoft.com/en-us/library/windows/desktop/aa364937(v=vs.85).aspx, $(D GetDiskFreeSpaceExW)).
5129
+
5130
+ On `POSIX` platforms, it uses the $(LINK2 http://man7.org/linux/man-pages/man3/statvfs.3.html,
5131
+ $(D statvfs)) function. On `FreeBSD` however, it uses the $(LINK2 https://www.freebsd.org/cgi/man.cgi?query=statfs&sektion=2,
5132
+ $(D statfs)) function.
5133
+
5134
+ Params:
5135
+ path = a valid path to any file in the file system
5136
+ Returns:
5137
+ Available space in bytes
5138
+
5139
+ Throws:
5140
+ $(LREF FileException) in case of failure
5144
5141
*/
5145
- ulong getAvailableDiskSpace (string path) @trusted
5142
+ ulong getAvailableDiskSpace (string path) @safe
5146
5143
{
5147
5144
import std.exception : enforce;
5148
5145
import std.conv : text;
5149
5146
5150
- version (FreeBSD )
5151
- {
5152
- static assert (0 , " Unsupported platform" );
5153
- }
5154
- else version (Windows )
5147
+ version (Windows )
5155
5148
{
5156
5149
import core.sys.windows.winbase : GetDiskFreeSpaceExW, GetLastError;
5157
5150
import core.sys.windows.winnt : ULARGE_INTEGER ;
5158
5151
import std.internal.cstring : tempCStringW;
5159
5152
5160
5153
ULARGE_INTEGER freeBytesAvailable;
5161
- auto err = GetDiskFreeSpaceExW(path.tempCStringW(), &freeBytesAvailable, null , null );
5154
+ auto err = () @trusted {
5155
+ return GetDiskFreeSpaceExW (path.tempCStringW(), &freeBytesAvailable, null , null );
5156
+ } ();
5162
5157
enforce(err != 0 ,
5163
5158
new FileException (text(" Cannot get available disk space: " , GetLastError())));
5164
5159
5165
5160
return freeBytesAvailable.QuadPart;
5166
5161
}
5167
5162
else version (Posix )
5168
5163
{
5169
- import core.sys.posix.sys.statvfs : statvfs, statvfs_t;
5170
5164
import std.string : toStringz;
5171
5165
5172
- statvfs_t stats = void ;
5173
- auto err = statvfs(path.toStringz(), &stats);
5174
- enforce(err == 0 ,
5175
- new FileException (text(" Cannot get available disk space: " , err)));
5166
+ version (FreeBSD )
5167
+ {
5168
+ import core.sys.posix.sys.statvfs : statfs, statfs_t;
5176
5169
5177
- return stats.f_bavail * stats.f_frsize;
5170
+ statfs_t stats = void ;
5171
+ auto err = () @trusted {
5172
+ return statfs (path.toStringz(), &stats);
5173
+ } ();
5174
+ enforce(err == 0 ,
5175
+ new FileException (text(" Cannot get available disk space: " , err)));
5176
+
5177
+ return stats.f_bavail * stats.f_bsize;
5178
+ }
5179
+ else
5180
+ {
5181
+ import core.sys.posix.sys.statvfs : statvfs, statvfs_t;
5182
+
5183
+ statvfs_t stats = void ;
5184
+ auto err = () @trusted {
5185
+ return statvfs (path.toStringz(), &stats);
5186
+ } ();
5187
+ enforce(err == 0 ,
5188
+ new FileException (text(" Cannot get available disk space: " , err)));
5189
+
5190
+ return stats.f_bavail * stats.f_frsize;
5191
+ }
5178
5192
}
5179
5193
else static assert (0 , " Unsupported platform" );
5180
5194
}
5181
5195
5182
- version ( FreeBSD ) {}
5183
- else
5196
+ // /
5197
+ @safe unittest
5184
5198
{
5185
- // /
5186
- @safe unittest
5187
- {
5188
- import std.exception : assertThrown;
5199
+ import std.exception : assertThrown;
5189
5200
5190
- auto space = getAvailableDiskSpace(" ." );
5191
- assert (space > 0 );
5201
+ auto space = getAvailableDiskSpace(" ." );
5202
+ assert (space > 0 );
5192
5203
5193
- assertThrown(getAvailableDiskSpace(" ThisFileDoesNotExist123123" ));
5194
- }
5204
+ assertThrown! FileException (getAvailableDiskSpace(" ThisFileDoesNotExist123123" ));
5195
5205
}
5196
5206
0 commit comments