-
Notifications
You must be signed in to change notification settings - Fork 12
/
wclean.go
72 lines (62 loc) · 1.68 KB
/
wclean.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
package main
import (
"io/ioutil"
"os"
"path/filepath"
"github.com/zyedidia/generic/mapset"
)
// Remove files in the data directory that are no longer relevant given the
// configuration file we're using.
func clean(podcastCount int, cleanc <-chan *cleaningWhitelist) (int, error) {
keepers := mapset.New[string]()
for i := 0; i < podcastCount; i++ {
wl := <-cleanc
for _, p := range wl.paths {
keepers.Put(p)
}
defer close(wl.cleanFinishedC)
}
var rmCount int
cleanSubdir := func(subd string) error {
dirContents, err := ioutil.ReadDir(subd)
if err != nil {
return err
}
for _, info := range dirContents {
path := filepath.Join(subd, info.Name())
if keepers.Has(path) {
continue
}
if err := os.Remove(path); err != nil {
return err
}
rmCount++
}
return nil
}
for _, subd := range []string{dataSubdirEpisodes, dataSubdirMetadata} {
if err := cleanSubdir(subd); err != nil {
return rmCount, err
}
}
return rmCount, nil
}
type cleaningWhitelist struct {
paths []string
cleanFinishedC chan struct{}
}
// ------------------------------------------------------------
// To the goroutine cleaning out the data directory, send a whitelist
// containing the paths of files on disk that remain relevant to this watcher,
// so that they will not be removed.
func (w *watcher) sendCleaningWhitelist(vids []ytVidInfo) {
wlist := cleaningWhitelist{cleanFinishedC: make(chan struct{})}
for _, vi := range vids {
wlist.paths = append(wlist.paths,
vi.episodePath(w.fileExtension()))
}
wlist.paths = append(wlist.paths, w.pod.artPath())
wlist.paths = append(wlist.paths, w.pod.feedPath())
w.cleanc <- &wlist
<-wlist.cleanFinishedC
}