Skip to content

Commit 42b4e24

Browse files
committed
Solaris port: zfs list without -p
Solaris does not support the -p option to zfs list. Parse dates manually. parse time in current location Solaris zfs list only accurate to minute
1 parent 1284791 commit 42b4e24

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

helpers/jobinfo.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ func (s *SnapshotInfo) Equal(t *SnapshotInfo) bool {
9494
if s == nil || t == nil {
9595
return s == t
9696
}
97-
return strings.Compare(s.Name, t.Name) == 0 && s.CreationTime.Equal(t.CreationTime)
97+
return strings.Compare(s.Name, t.Name) == 0 &&
98+
s.CreationTime.Truncate(time.Minute).Equal(t.CreationTime.Truncate(time.Minute))
9899
}
99100

100101
// TotalBytesWritten will sum up the size of all underlying Volumes to give a total

helpers/zfs.go

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"bytes"
2525
"context"
2626
"fmt"
27+
"bufio"
2728
"os/exec"
2829
"strconv"
2930
"strings"
@@ -52,7 +53,7 @@ func GetCreationDate(ctx context.Context, target string) (time.Time, error) {
5253
// GetSnapshots will retrieve all snapshots for the given target
5354
func GetSnapshots(ctx context.Context, target string) ([]SnapshotInfo, error) {
5455
errB := new(bytes.Buffer)
55-
cmd := exec.CommandContext(ctx, ZFSPath, "list", "-H", "-d", "1", "-p", "-t", "snapshot", "-r", "-o", "name,creation", "-S", "creation", target)
56+
cmd := exec.CommandContext(ctx, ZFSPath, "list", "-H", "-d", "1", "-t", "snapshot", "-r", "-o", "name,creation", "-S", "creation", target)
5657
AppLogger.Debugf("Getting ZFS Snapshots with command \"%s\"", strings.Join(cmd.Args, " "))
5758
cmd.Stderr = errB
5859
rpipe, err := cmd.StdoutPipe()
@@ -64,19 +65,37 @@ func GetSnapshots(ctx context.Context, target string) ([]SnapshotInfo, error) {
6465
return nil, fmt.Errorf("%s (%v)", strings.TrimSpace(errB.String()), err)
6566
}
6667
var snapshots []SnapshotInfo
67-
for {
68+
scanner := bufio.NewScanner(rpipe)
69+
for scanner.Scan() {
6870
snapInfo := SnapshotInfo{}
69-
var creation int64
70-
n, nerr := fmt.Fscanln(rpipe, &snapInfo.Name, &creation)
71-
if n == 0 || nerr != nil {
72-
break
73-
}
74-
snapInfo.CreationTime = time.Unix(creation, 0)
71+
var creation string
72+
var nerr error
73+
74+
line := scanner.Text()
75+
s := strings.SplitN(line, "\t", 2)
76+
if len(s) != 2 {
77+
AppLogger.Debugf("Failed to parse ZFS list output \"%s\"", line)
78+
break
79+
}
80+
snapInfo.Name = s[0]
81+
creation = s[1]
82+
83+
// XXX this will not work across DST changes
84+
loc := time.Now().Local().Location()
85+
snapInfo.CreationTime, nerr = time.ParseInLocation("Mon Jan 2 15:04 2006", creation, loc)
86+
if nerr != nil {
87+
AppLogger.Debugf("Failed to parse time \"%s\" from \"%s\"", creation, snapInfo.Name)
88+
break
89+
}
90+
AppLogger.Debugf("Found ZFS snapshot \"%s\" from %s", snapInfo.Name, snapInfo.CreationTime.String())
7591
snapInfo.Name = snapInfo.Name[strings.Index(snapInfo.Name, "@")+1:]
7692
snapshots = append(snapshots, snapInfo)
7793
}
78-
err = cmd.Wait()
79-
if err != nil {
94+
err = scanner.Err()
95+
if err == nil {
96+
err = cmd.Wait()
97+
}
98+
if err != nil {
8099
return nil, err
81100
}
82101

0 commit comments

Comments
 (0)