Reputation: 643
Is there a way to check the number of available/used/total inodes on a filesystem in Go?
I want something just like what df -i
returns, and don't want to call df
if possible.
Example of df
:
# On macOs 10.15 (-i not needed here)
df /
Filesystem 512-blocks Used Available Capacity iused ifree %iused Mounted on
/dev/disk1s2 236568496 22038704 44026328 34% 488339 1182354141 0% /
# On Ubuntu 18.04
df -i /
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/vda1 2621440 219719 2401721 9% /
Upvotes: 2
Views: 1718
Reputation: 10271
You can use syscall.Statfs. Its arguments are a pathname and a pointer to a Statfs_t struct. It fills in the struct with statistics for the filesystem that contains the file or directory specified by the pathname. Typically you'd use .
or /
or the pathname of a mount point.
Here's a Go program that takes a pathname as its argument and displays inode information.
package main
import (
"fmt"
"os"
"syscall"
)
func main() {
var statfs syscall.Statfs_t
path := os.Args[1]
if err := syscall.Statfs(path, &statfs); err != nil {
fmt.Fprintf(os.Stderr, "Cannot stat %s: %v\n", path, err)
os.Exit(1)
}
fmt.Printf("Inodes: total %d, free %d\n", statfs.Files, statfs.Ffree)
}
Upvotes: 2
Reputation: 2146
On the systems that you mentioned, macOs and Ubuntu, you can use
func Fstatfs(fd int, buf *Statfs_t) (err error)
.
The input argument *unix.Statfs_t
would then be updated assuming the unix.Fstatfs
call doesn't error.
unix.Statfs_t.Files
and unix.Statfs_t.Ffree
are total number of inodes and number of free inodes respectively for the filesystem that corresponds to the fd
, the first arugment of the unix.Fstatfs
call.
See also: manual of statfs system call
Upvotes: 2