-
Notifications
You must be signed in to change notification settings - Fork 29
/
diff.go
68 lines (57 loc) · 1.19 KB
/
diff.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package types
import (
"bytes"
"encoding/base64"
"encoding/gob"
"fmt"
)
type FileChange uint
const (
FileChangeUnknown FileChange = iota
FileChangeAdded
FileChangeModified
FileChangeRemoved
FileChangeRenamed
)
func (c FileChange) String() string {
switch c {
case FileChangeAdded:
return "+"
case FileChangeModified:
return "M"
case FileChangeRemoved:
return "-"
case FileChangeRenamed:
return "R"
default:
return "unknown"
}
}
type ZFSFileDiff struct {
Change FileChange `json:"change"`
Filename string `json:"filename"`
}
func EncodeZFSFileDiff(files []ZFSFileDiff) (string, error) {
buf := bytes.Buffer{}
enc := gob.NewEncoder(&buf)
err := enc.Encode(&files)
if err != nil {
return "", nil
}
return base64.StdEncoding.EncodeToString(buf.Bytes()), nil
}
func DecodeZFSFileDiff(data string) ([]ZFSFileDiff, error) {
decoded, err := base64.StdEncoding.DecodeString(data)
if err != nil {
return nil, fmt.Errorf("failed base64 decode step: %s", err)
}
var files []ZFSFileDiff
err = gob.NewDecoder(bytes.NewBuffer(decoded)).Decode(&files)
return files, err
}
type RPCDiffRequest struct {
FilesystemID string
}
type RPCDiffResponse struct {
Files []ZFSFileDiff
}