Skip to content

Commit

Permalink
Add support for FreeBSD
Browse files Browse the repository at this point in the history
  • Loading branch information
muesli committed Sep 24, 2020
1 parent f73b053 commit c8cc3e9
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 1 deletion.
1 change: 1 addition & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ builds:
ldflags: -s -w -X main.Version={{ .Version }} -X main.CommitSHA={{ .Commit }}
goos:
- linux
- freebsd
goarch:
- amd64
- arm64
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![Go ReportCard](http://goreportcard.com/badge/muesli/duf)](http://goreportcard.com/report/muesli/duf)
[![GoDoc](https://godoc.org/github.com/golang/gddo?status.svg)](https://pkg.go.dev/github.com/muesli/duf)

Disk Usage/Free Utility (currently Linux & macOS-only, support for BSDs soon)
Disk Usage/Free Utility (Linux, FreeBSD & macOS)

![duf](/duf.png)

Expand Down
22 changes: 22 additions & 0 deletions filesystems_freebsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// +build freebsd

package main

func isLocalFs(m Mount) bool {
//FIXME: implement
return false
}

func isFuseFs(m Mount) bool {
//FIXME: implement
return false
}

func isNetworkFs(m Mount) bool {
//FIXME: implement
return false
}

func isSpecialFs(m Mount) bool {
return m.Fstype == "devfs"
}
124 changes: 124 additions & 0 deletions mounts_freebsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// +build freebsd

package main

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

func mounts() ([]Mount, []string, error) {
var ret []Mount
var warnings []string

count, err := unix.Getfsstat(nil, unix.MNT_WAIT)
if err != nil {
return nil, nil, err
}
fs := make([]unix.Statfs_t, count)
if _, err = unix.Getfsstat(fs, unix.MNT_WAIT); err != nil {
return nil, nil, err
}

for _, stat := range fs {
opts := "rw"
if stat.Flags&unix.MNT_RDONLY != 0 {
opts = "ro"
}
if stat.Flags&unix.MNT_SYNCHRONOUS != 0 {
opts += ",sync"
}
if stat.Flags&unix.MNT_NOEXEC != 0 {
opts += ",noexec"
}
if stat.Flags&unix.MNT_NOSUID != 0 {
opts += ",nosuid"
}
if stat.Flags&unix.MNT_UNION != 0 {
opts += ",union"
}
if stat.Flags&unix.MNT_ASYNC != 0 {
opts += ",async"
}
if stat.Flags&unix.MNT_SUIDDIR != 0 {
opts += ",suiddir"
}
if stat.Flags&unix.MNT_SOFTDEP != 0 {
opts += ",softdep"
}
if stat.Flags&unix.MNT_NOSYMFOLLOW != 0 {
opts += ",nosymfollow"
}
if stat.Flags&unix.MNT_GJOURNAL != 0 {
opts += ",gjournal"
}
if stat.Flags&unix.MNT_MULTILABEL != 0 {
opts += ",multilabel"
}
if stat.Flags&unix.MNT_ACLS != 0 {
opts += ",acls"
}
if stat.Flags&unix.MNT_NOATIME != 0 {
opts += ",noatime"
}
if stat.Flags&unix.MNT_NOCLUSTERR != 0 {
opts += ",noclusterr"
}
if stat.Flags&unix.MNT_NOCLUSTERW != 0 {
opts += ",noclusterw"
}
if stat.Flags&unix.MNT_NFS4ACLS != 0 {
opts += ",nfsv4acls"
}

device := byteToString(stat.Mntfromname[:])
mountPoint := byteToString(stat.Mntonname[:])
fsType := byteToString(stat.Fstypename[:])

if len(device) == 0 {
continue
}

d := Mount{
Device: device,
Mountpoint: mountPoint,
Fstype: fsType,
Type: fsType,
Opts: opts,
Stat: stat,
Total: (uint64(stat.Blocks) * uint64(stat.Bsize)),
Free: (uint64(stat.Bavail) * uint64(stat.Bsize)),
Used: (uint64(stat.Blocks) - uint64(stat.Bfree)) * uint64(stat.Bsize),
Inodes: stat.Files,
InodesFree: uint64(stat.Ffree),
InodesUsed: stat.Files - uint64(stat.Ffree),
}
d.DeviceType = deviceType(d)

ret = append(ret, d)
}

return ret, warnings, nil
}

func byteToString(orig []byte) string {
n := -1
l := -1
for i, b := range orig {
// skip left side null
if l == -1 && b == 0 {
continue
}
if l == -1 {
l = i
}

if b == 0 {
break
}
n = i + 1
}
if n == -1 {
return string(orig)
}
return string(orig[l:n])
}

0 comments on commit c8cc3e9

Please sign in to comment.