Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

vole
Site
Clone

Read only, no credentials:

https://rodent.software/vole.git

Write over SSH with a registered key:

git clone ssh://git@rodent.software/vole.git

Self-hosted forge that lives in your Git repo.

Blame · internal/storage/stat_unix.go

Line attribution on main · 46 line(s)

215f8aeb
Keenan Nemetz
2d ago
1 //go:build unix
2
3 package storage
4
5 import (
6 "io/fs"
7 "syscall"
8 )
9
10 // fileKey identifies a file by the device and inode the filesystem gave it, which
11 // is how a hard link is measured once per inode rather than once per name.
12 func fileKeyFor(_ string, info fs.FileInfo) (fileKey, bool) {
13 st, ok := info.Sys().(*syscall.Stat_t)
14 if !ok {
15 return fileKey{}, false
16 }
17 return fileKey{dev: uint64(st.Dev), ino: st.Ino}, true
18 }
19
20 // allocatedBytes is the space the file actually costs. Blocks is in 512-byte
21 // units, regardless of the filesystem's block size, so a sparse file does not
22 // claim space it does not use.
23 func allocatedBytes(_ string, info fs.FileInfo) int64 {
24 if st, ok := info.Sys().(*syscall.Stat_t); ok && st.Blocks > 0 {
25 return st.Blocks * 512
26 }
27 return info.Size()
28 }
29
30 // diskUsage is the capacity of the filesystem holding root. Available is what an
31 // unprivileged process can still write, which is the number an operator cares
32 // about; Used is the whole filesystem's usage, not just Vole's.
33 func diskUsage(root string) (Disk, error) {
34 var st syscall.Statfs_t
35 if err := syscall.Statfs(root, &st); err != nil {
36 return Disk{}, err
37 }
38 // Bsize is int64 on Linux and uint32 on Darwin, so it is converted here
39 // rather than used directly; this file is built for both when a release
40 // produces a macOS binary.
41 blockSize := int64(st.Bsize)
42 total := int64(st.Blocks) * blockSize
43 free := int64(st.Bfree) * blockSize
44 available := int64(st.Bavail) * blockSize
45 return Disk{Total: total, Used: total - free, Available: available}, nil
46 }