Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement querying of used space for APFS volumes on macOS #208

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 43 additions & 1 deletion mounts_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,31 @@
package main

import (
"syscall"
"unsafe"

"golang.org/x/sys/unix"
)

type attrlist struct {
bitmapcount uint16
reserved uint16
commonattr uint32
volattr uint32
dirattr uint32
fileattr uint32
forkattr uint32
}

type volAttrs struct {
length uint32
spaceUsed [8]byte
}

const ATTR_BIT_MAP_COUNT = 5
const ATTR_VOL_INFO = 0x80000000
const ATTR_VOL_SPACEUSED = 0x00800000

func (m *Mount) Stat() unix.Statfs_t {
return m.Metadata.(unix.Statfs_t)
}
Expand Down Expand Up @@ -71,6 +93,26 @@ func mounts() ([]Mount, []string, error) {
continue
}

used := (stat.Blocks - stat.Bfree) * uint64(stat.Bsize)

var mountPointPtr *byte
mountPointPtr, err = syscall.BytePtrFromString(mountPoint)
attrList := attrlist{
bitmapcount: ATTR_BIT_MAP_COUNT,
volattr: ATTR_VOL_INFO | ATTR_VOL_SPACEUSED,
}
var volAttrs volAttrs
volAttrsRes, _, _ := syscall.Syscall6(syscall.SYS_GETATTRLIST,
uintptr(unsafe.Pointer(mountPointPtr)),
uintptr(unsafe.Pointer(&attrList)),
uintptr(unsafe.Pointer(&volAttrs)),
unsafe.Sizeof(volAttrs),
unix.FSOPT_NOFOLLOW,
0)
if volAttrsRes == 0 {
used = *(*uint64)(unsafe.Pointer(&volAttrs.spaceUsed[0]))
}

d := Mount{
Device: device,
Mountpoint: mountPoint,
Expand All @@ -80,7 +122,7 @@ func mounts() ([]Mount, []string, error) {
Metadata: stat,
Total: stat.Blocks * uint64(stat.Bsize),
Free: stat.Bavail * uint64(stat.Bsize),
Used: (stat.Blocks - stat.Bfree) * uint64(stat.Bsize),
Used: used,
Inodes: stat.Files,
InodesFree: stat.Ffree,
InodesUsed: stat.Files - stat.Ffree,
Expand Down