Pascal · lesson 28 of 30 · Tools for the NASAsk the system for free space
fpStatFS asks the system about the volume of a folder: how many blocks it has and how many are free.
28-free-space.pas Download 28-free-space.pas
program FreeSpace;
{$mode objfpc}{$H+}
uses
SysUtils, BaseUnix, Unix;
var
Info: TStatFS;
Folder: string;
Total, Free: Double;
begin
Folder := ParamStr(1);
if Folder = '' then
Folder := '.';
if fpStatFS(PChar(Folder), @Info) <> 0 then
begin
WriteLn(Folder, ': ', SysErrorMessage(fpGetErrno));
Halt(1);
end;
Total := Double(Info.blocks) * Info.frsize / 1024 / 1024 / 1024;
Free := Double(Info.bavail) * Info.frsize / 1024 / 1024 / 1024;
WriteLn(Free:0:1, ' GiB of ', Total:0:1, ' GiB are free');
end.
Terminal
$ ./28-free-space .
1035.3 GiB of 1327.1 GiB are free
$ ./28-free-space /no/such/folder; echo "exit code $?"
/no/such/folder: No such file or directory
exit code 1The numbers or the date differ on your NAS.
Exercise
Take a limit in percent and set the exit code.