Skip to content

Instantly share code, notes, and snippets.

@sl5net
Created November 19, 2019 06:26
Show Gist options
  • Save sl5net/8c16fbcb2a96888966c20b4490ceb2ca to your computer and use it in GitHub Desktop.
Save sl5net/8c16fbcb2a96888966c20b4490ceb2ca to your computer and use it in GitHub Desktop.
error never happens till yet
//pacman -S go
//go get -u golang.org/x/sys/unix
package main
import (
"fmt"
syscall "golang.org/x/sys/unix"
)
type DiskStatus struct {
All uint64 `json:"all"`
Used uint64 `json:"used"`
Free uint64 `json:"free"`
Avail uint64 `json:"avail"`
}
// disk usage of path/disk
func DiskUsage(path string) (disk DiskStatus) {
fs := syscall.Statfs_t{}
err := syscall.Statfs(path, &fs)
if err != nil {
println("#### THIS LINE NEVER HAPPENS TILL YET")
return
}
disk.All = fs.Blocks * uint64(fs.Bsize)
disk.Avail = fs.Bavail * uint64(fs.Bsize)
disk.Free = fs.Bfree * uint64(fs.Bsize)
disk.Used = disk.All - disk.Free
return
}
const (
B = 1
KB = 1024 * B
MB = 1024 * KB
GB = 1024 * MB
)
func main() {
path := "/"
funcName("/mnt/ix4")
funcName("/mnt/ix4300d")
funcName(path)
//funcName("192.168.178.42/share")
}
func funcName(path string) {
disk := DiskUsage(path)
fmt.Println("")
fmt.Println(path, ":")
fmt.Printf("All: %.2f GB\n", float64(disk.All)/float64(GB))
fmt.Printf("Avail: %.2f GB\n", float64(disk.Avail)/float64(GB))
fmt.Printf("Used: %.2f GB\n", float64(disk.Used)/float64(GB))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment