c0defellas / enzo

core utilities
11 stars 2 forks source link

missing df #7

Open i4ki opened 7 years ago

charlessachet commented 7 years ago

@tiago4orion

So... from the partition name we can found out where it is mounted.

However I'll continue searching for a file maintained by kernel that can give us the disk usage.

i4ki commented 7 years ago

Nice.

You'll need to use the paths in /proc/mounts to call the syscall statfs(2). Take a look here: https://github.com/StalkR/goircbot/blob/master/lib/disk/space_unix.go#L11

[]'s i4k

charlessachet commented 7 years ago

Thanks!

scall.go

package main

import (
    "fmt"
    "os"
    "syscall"
)

func main() {
    path := os.Args[1]
    scall := syscall.Statfs_t{}
    err := syscall.Statfs(path, &scall)
    if err != nil {
        panic(fmt.Sprintf("Error[%s] doing syscall to fs for path[%s]", err, path))
    }
    total := scall.Bsize * int64(scall.Blocks)
    free := scall.Bsize * int64(scall.Bfree)
    used := total - free
    usedPercentage := (float64(used) * 100) / float64(total)
    fmt.Printf("Total[%v], Free[%v], Used[%v], Used[%v]", total, free, used, usedPercentage)
}

Running: go run scall.go /home

I just did a test comparing the output between the script and the df command:

The output has a considerable difference. What do you think?

[]'s

i4ki commented 7 years ago

Found this: http://stackoverflow.com/questions/21693673/differ-in-output-of-df-and-statfs