-
Notifications
You must be signed in to change notification settings - Fork 0
/
commandClean.go
205 lines (170 loc) · 4.51 KB
/
commandClean.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
package main
import (
"errors"
"flag"
"fmt"
"os"
"path"
"strings"
"sync"
"github.com/golang/glog"
"github.com/rokeller/bart/domain"
"github.com/rokeller/bart/inspection"
)
type CleanupLocation int
const (
CleanupLocationBackup CleanupLocation = iota
CleanupLocationLocal
)
type cmdCleanup struct {
cmdBase
location CleanupLocation
wg *sync.WaitGroup
queue chan deleteMessage
}
type deleteMessage interface{}
type deleteFromBackup struct {
domain.Entry
}
type deleteFromLocal struct {
relPath string
absolutePath string
}
// Finished implements Command.
func (c *cmdCleanup) Finished() <-chan bool {
return c.finished
}
// Run implements Command.
func (c *cmdCleanup) Run() {
defer c.signalFinished()
for i := 0; i < c.args.degreeOfParallelism; i++ {
c.wg.Add(1)
go func(id int) {
defer c.wg.Done()
c.handleCleanupQueue(id)
}(i)
}
switch c.location {
case CleanupLocationBackup:
c.cleanupBackup()
case CleanupLocationLocal:
c.cleanupLocal()
default:
glog.Fatalf("Unhandled cleanup location %d.", c.location)
}
}
// Stop implements Command.
func (c *cmdCleanup) Stop() {
close(c.queue)
c.wg.Wait()
c.stop()
}
func newCleanupCommand(args []string) Command {
locationStr := "backup"
cleanFlags := flag.NewFlagSet("cleanup", flag.ExitOnError)
cleanFlags.StringVar(&locationStr, "l", "backup",
"The location to clean up: 'backup' to remove files missing locally "+
"from the backup, 'local' to remove files missing in the backup "+
"from the local file system.")
commonArgs := addCommonArgs(cleanFlags)
cleanFlags.Parse(args)
var location CleanupLocation
switch strings.ToLower(locationStr) {
case "backup":
location = CleanupLocationBackup
case "local":
location = CleanupLocationLocal
default:
glog.Exit("The cleanup location must either be 'backup' or 'local'.")
}
return &cmdCleanup{
cmdBase: cmdBase{
args: *commonArgs,
archive: newArchive(*commonArgs),
finished: make(chan bool),
},
location: location,
wg: &sync.WaitGroup{},
queue: make(chan deleteMessage, commonArgs.degreeOfParallelism*2),
}
}
func (c *cmdCleanup) cleanupBackup() {
// Find files that are in the backup index, but cannot be found locally and
// queue their backup copy for deletion.
c.archive.FindLocallyMissing(func(entry domain.Entry) {
// The item is present in the backup, but not locally.
absLocalPath := path.Join(c.args.localRoot, entry.RelPath)
if glog.V(3) {
glog.Infof("Checking local file '%s' ...", absLocalPath)
}
_, err := os.Stat(absLocalPath)
if errors.Is(err, os.ErrNotExist) {
if glog.V(3) {
glog.Infof("Local file '%s' not found. Queue deletion of '%s' from backup",
absLocalPath, entry.RelPath)
}
c.queue <- deleteFromBackup{Entry: entry}
} else if nil != err {
glog.Errorf("Failed to check for local file '%s': %v",
entry.RelPath, err)
}
})
}
func (c *cmdCleanup) cleanupLocal() {
// Find local files that are not in the backup and queue them for deletion
// from the local file system.
v := NewDeletingVisitor(c.archive, c.args.localRoot, c.queue)
err := inspection.Discover(c.args.localRoot, v)
if nil != err {
glog.Errorf("Discovery failed: %v", err)
}
}
func (c *cmdCleanup) handleCleanupQueue(id int) {
numSuccessful, numFailed := 0, 0
for {
msg, isOpen := <-c.queue
if !isOpen {
break
}
switch m := msg.(type) {
case deleteFromBackup:
// Remove the entry from the backup, and from the backup index.
glog.V(1).Infof("[Cleanup-%d] Remove file '%s' from backup ...",
id, m.Entry.RelPath)
if c.args.whatIf {
numSuccessful++
fmt.Println(m.Entry.RelPath)
continue
}
if err := c.archive.Delete(m.Entry); nil != err {
numFailed++
glog.Errorf("[Cleanup-%d] Removal of file '%s' failed: %v",
id, m.Entry.RelPath, err)
} else {
numSuccessful++
fmt.Println(m.Entry.RelPath)
}
case deleteFromLocal:
// Remove the file from the local file system.
glog.V(1).Infof("[Cleanup-%d] Remove local file '%s' ...",
id, m.relPath)
if c.args.whatIf {
numSuccessful++
fmt.Println(m.relPath)
continue
}
if err := os.Remove(m.absolutePath); nil != err {
numFailed++
glog.Errorf("[Cleanup-%d] Removal of local file '%s' failed: %v",
id, m.relPath, err)
} else {
numSuccessful++
fmt.Println(m.relPath)
}
default:
glog.Warningf("Unsupported message type: %v", m)
}
}
glog.Infof("[Cleanup-%d] Finished. Successfully removed %d file(s), failed to remove %d file(s).",
id, numSuccessful, numFailed)
}