@@ -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
5354func 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