Created
November 19, 2019 06:26
-
-
Save sl5net/8c16fbcb2a96888966c20b4490ceb2ca to your computer and use it in GitHub Desktop.
error never happens till yet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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