Go · lesson 25 of 30 · Tools for the NASAsk the system for free space
syscall.Statfs asks the system about the volume of a folder: how many blocks it has and how many are free.
25-free-space.go Download 25-free-space.go
package main
import (
"fmt"
"os"
"syscall"
)
func main() {
folder := "."
if len(os.Args) > 1 {
folder = os.Args[1]
}
var info syscall.Statfs_t
if err := syscall.Statfs(folder, &info); err != nil {
fmt.Printf("%s: %v\n", folder, err)
os.Exit(1)
}
block := float64(info.Frsize)
total := float64(info.Blocks) * block / (1 << 30)
free := float64(info.Bavail) * block / (1 << 30)
fmt.Printf("%.1f GiB of %.1f GiB are free\n", free, total)
}
Terminal
$ ./25-free-space .
1034.0 GiB of 1327.1 GiB are free
$ ./25-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
Add -limit and set the exit code.
TurboShell does not contain Go. These programs are made in Bucket & Code with its Go runtime; the finished file runs on the NAS without Go.