Skip to content

Solaris port: zfs list without -p #16

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

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion helpers/jobinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ func (s *SnapshotInfo) Equal(t *SnapshotInfo) bool {
if s == nil || t == nil {
return s == t
}
return strings.Compare(s.Name, t.Name) == 0 && s.CreationTime.Equal(t.CreationTime)
return strings.Compare(s.Name, t.Name) == 0 &&
s.CreationTime.Truncate(time.Minute).Equal(t.CreationTime.Truncate(time.Minute))
}

// TotalBytesWritten will sum up the size of all underlying Volumes to give a total
Expand Down
39 changes: 29 additions & 10 deletions helpers/zfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"bytes"
"context"
"fmt"
"bufio"
"os/exec"
"strconv"
"strings"
Expand Down Expand Up @@ -52,7 +53,7 @@ func GetCreationDate(ctx context.Context, target string) (time.Time, error) {
// GetSnapshots will retrieve all snapshots for the given target
func GetSnapshots(ctx context.Context, target string) ([]SnapshotInfo, error) {
errB := new(bytes.Buffer)
cmd := exec.CommandContext(ctx, ZFSPath, "list", "-H", "-d", "1", "-p", "-t", "snapshot", "-r", "-o", "name,creation", "-S", "creation", target)
cmd := exec.CommandContext(ctx, ZFSPath, "list", "-H", "-d", "1", "-t", "snapshot", "-r", "-o", "name,creation", "-S", "creation", target)
AppLogger.Debugf("Getting ZFS Snapshots with command \"%s\"", strings.Join(cmd.Args, " "))
cmd.Stderr = errB
rpipe, err := cmd.StdoutPipe()
Expand All @@ -64,19 +65,37 @@ func GetSnapshots(ctx context.Context, target string) ([]SnapshotInfo, error) {
return nil, fmt.Errorf("%s (%v)", strings.TrimSpace(errB.String()), err)
}
var snapshots []SnapshotInfo
for {
scanner := bufio.NewScanner(rpipe)
for scanner.Scan() {
snapInfo := SnapshotInfo{}
var creation int64
n, nerr := fmt.Fscanln(rpipe, &snapInfo.Name, &creation)
if n == 0 || nerr != nil {
break
}
snapInfo.CreationTime = time.Unix(creation, 0)
var creation string
var nerr error

line := scanner.Text()
s := strings.SplitN(line, "\t", 2)
if len(s) != 2 {
AppLogger.Debugf("Failed to parse ZFS list output \"%s\"", line)
break
}
snapInfo.Name = s[0]
creation = s[1]

// XXX this will not work across DST changes
loc := time.Now().Local().Location()
snapInfo.CreationTime, nerr = time.ParseInLocation("Mon Jan 2 15:04 2006", creation, loc)
if nerr != nil {
AppLogger.Debugf("Failed to parse time \"%s\" from \"%s\"", creation, snapInfo.Name)
break
}
AppLogger.Debugf("Found ZFS snapshot \"%s\" from %s", snapInfo.Name, snapInfo.CreationTime.String())
snapInfo.Name = snapInfo.Name[strings.Index(snapInfo.Name, "@")+1:]
snapshots = append(snapshots, snapInfo)
}
err = cmd.Wait()
if err != nil {
err = scanner.Err()
if err == nil {
err = cmd.Wait()
}
if err != nil {
return nil, err
}

Expand Down