forked from bucko909/btrfs-send-go
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
235 lines (190 loc) · 5.6 KB
/
main.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package main
import (
"fmt"
"os"
"path"
"path/filepath"
"sort"
"strings"
btrfsdiff "github.com/mbideau/btrfs-diff-go/pkg"
)
func usage(progname string) {
fmt.Printf(`
%[1]s - Analyse the differences between two related btrfs subvolumes.
USAGE
%[1]s [OPTIONS] PARENT CHILD
Analyse the difference between btrfs PARENT and CHILD.
%[1]s [OPTIONS] -f|--file STREAM
Analyse the differences from a STREAM file (output from 'btrfs send').
%[1]s [ -h | --help ]
Display help.
ARGUMENTS
PARENT
A btrfs subvolume that is the parent of the CHILD one.
CHILD
A btrfs subvolume that is the child of the PARENT one.
OPTIONS
-h | --help
Display help.
-i | --info
Be verbose.
-d | --debug
Be more verbose.
-f | --file STREAM
Use a STREAM file to get the btrfs operations.
This stream file must have been generated by the command
'btrfs send' (with or without the option --no-data).
-t[changed] | --with-times[=changed]
By defautl time modifications are ignored. With that option
they will be taken into account. They are labelled as 'times'
but if you also specify '=changed' they will be labelled
'changed'.
-p[changed] | --with-perms[=changed]
By defautl permission modifications are ignored. With that option
they will be taken into account. They are labelled as 'perms'
but if you also specify '=changed' they will be labelled
'changed'.
-o[changed] | --with-own[=changed]
By defautl ownership modifications are ignored. With that option
they will be taken into account. They are labelled as 'own'
but if you also specify '=changed' they will be labelled
'changed'.
-a[changed] | --with-attr[=changed]
By defautl attribute modifications are ignored. With that option
they will be taken into account. They are labelled as 'attr'
but if you also specify '=changed' they will be labelled
'changed'.
EXAMPLES
Get the differences between two snapshots.
$ %[1]s /backup/btrfs-sp/rootfs/2020-12-25_22h00m00.shutdown.safe \
/backup/btrfs-sp/rootfs/2019-12-25_21h00m00.shutdown.safe
AUTHORS
Originally written by: David Buckley
Extended, fixed, and maintained by: Michael Bideau
REPORTING BUGS
Report bugs to: <https://github.com/mbideau/btrfs-diff-go/issues>
COPYRIGHT
Copyright © 2020-2021 Michael Bideau.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Info: original license chosen by David Buckley was MIT, but it allows sublicensing, so I
chose to sublicense it to GPLv3+ to ensure code sharing
SEE ALSO
Home page: <https://github.com/mbideau/btrfs-diff-go>
`, progname)
}
func main() {
var optionHelp bool = false
var optionDebug bool = false
var optionInfo bool = false
var optionFile string = ""
var optionWithTimes bool = false
var optionTimesAsChanged bool = false
var optionWithPerms bool = false
var optionPermsAsChanged bool = false
var optionWithOwn bool = false
var optionOwnAsChanged bool = false
var optionWithAttr bool = false
var optionAttrAsChanged bool = false
var argSubvolParent string = ""
var argSubvolChild string = ""
skip := false
for index, arg := range os.Args[1:] {
// hacky way to compensate for lack of 'continue 2' instruction
if skip {
skip = false
continue
}
switch(arg) {
case "-h", "--help":
optionHelp = true
case "-i", "--info":
optionInfo = true
case "-d", "--debug":
optionDebug = true
case "-t", "--with-times":
optionWithTimes = true
case "-tchanged", "--with-times=changed":
optionTimesAsChanged = true
case "-p", "--with-perms":
optionWithPerms = true
case "-pchanged", "--with-perms=changed":
optionPermsAsChanged = true
case "-o", "--with-own":
optionWithOwn = true
case "-ochanged", "--with-own=changed":
optionOwnAsChanged = true
case "-a", "--with-attr":
optionWithAttr = true
case "-achanged", "--with-attr=changed":
optionAttrAsChanged = true
case "-f", "--file":
if len(os.Args) > index+2 {
optionFile = os.Args[index+2]
skip = true
}
default:
if len(argSubvolParent) == 0 {
argSubvolParent = arg
} else if len(argSubvolChild) == 0 {
argSubvolChild = arg
}
}
}
if len(os.Args) <= 2 || optionHelp {
usage(path.Base(os.Args[0]))
os.Exit(0)
}
if optionInfo {
btrfsdiff.SetInfo(true)
}
if optionDebug {
btrfsdiff.SetInfo(true)
btrfsdiff.SetDebug(true)
}
if optionWithTimes || optionTimesAsChanged {
btrfsdiff.ConsiderUtimeOp(optionTimesAsChanged)
}
if optionWithPerms || optionPermsAsChanged {
btrfsdiff.ConsiderChmodOp(optionPermsAsChanged)
}
if optionWithOwn || optionOwnAsChanged {
btrfsdiff.ConsiderChownOp(optionOwnAsChanged)
}
if optionWithAttr || optionAttrAsChanged {
btrfsdiff.ConsiderXattrOp(optionAttrAsChanged)
}
var changes []string
var err error
if len(optionFile) > 0 {
var streamfile string
streamfile, err = filepath.Abs(optionFile)
if err == nil {
changes, err = btrfsdiff.GetChangesFromStreamFile(streamfile)
}
} else {
var parent string
var child string
parent, err = filepath.Abs(argSubvolParent)
if err == nil {
child, err = filepath.Abs(argSubvolChild)
if err == nil {
changes, err = btrfsdiff.GetChangesFromTwoSubvolumes(child, parent)
}
}
}
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
// if there are changes/differences
if len(changes) > 0 {
// sort the list alphabetically (default)
sort.Strings(changes)
// print changes
fmt.Printf("%v\n", strings.Join(changes, "\n"))
// exit 1 if there are differences
os.Exit(1)
}
}