Skip to content

Commit

Permalink
fs: Return logicalused for ZFS filesystems
Browse files Browse the repository at this point in the history
  • Loading branch information
iBug committed Jun 26, 2024
1 parent 6f9eb24 commit 2cce3fa
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions pkg/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bufio"
"bytes"
"fmt"
"os"
"os/exec"
"path"
"strconv"
Expand Down Expand Up @@ -48,19 +49,38 @@ func (f *defaultFs) GetSize(d string) int64 {

type zfs struct{}

func getMountSource(d string) (string, error) {
if !dirExists(d) {
return "", os.ErrNotExist
}
var buf bytes.Buffer
cmd := exec.Command("df", "--output=source", d)
cmd.Stdout = &buf
if err := cmd.Run(); err != nil {
return "", err
}
scanner := bufio.NewScanner(&buf)
scanner.Scan()
scanner.Scan()
return strings.TrimSpace(scanner.Text()), nil
}

func (f *zfs) GetSize(d string) int64 {
if !dirExists(d) {
return -1
}
src, err := getMountSource(d)
if err != nil {
return -1
}
var buf bytes.Buffer
cmd := exec.Command("df", "-B1", "--output=used", d)
cmd := exec.Command("zfs", "get", "-H", "-p", "-o", "value", "logicalused", src)
cmd.Stdout = &buf
if err := cmd.Run(); err != nil {
return -1
}
scanner := bufio.NewScanner(&buf)
scanner.Scan()
scanner.Scan()
bs, err := strconv.ParseInt(scanner.Text(), 10, 64)
if err != nil {
return -1
Expand Down

0 comments on commit 2cce3fa

Please sign in to comment.