-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfilesystem.go
191 lines (164 loc) · 4.62 KB
/
filesystem.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
package watcher
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"runtime"
"github.com/fsnotify/fsnotify"
cp "github.com/otiai10/copy"
"go.artefactual.dev/tools/bucket"
"gocloud.dev/blob"
_ "gocloud.dev/blob/fileblob"
"github.com/artefactual-sdps/enduro/internal/filenotify"
"github.com/artefactual-sdps/enduro/internal/fsutil"
)
// filesystemWatcher implements a Watcher for watching paths in a local filesystem.
type filesystemWatcher struct {
ctx context.Context
cfg *FilesystemConfig
fw filenotify.FileWatcher
ch chan *fsnotify.Event
path string
regex *regexp.Regexp
*commonWatcherImpl
}
var _ Watcher = (*filesystemWatcher)(nil)
func NewFilesystemWatcher(ctx context.Context, config *FilesystemConfig) (*filesystemWatcher, error) {
config.setDefaults()
stat, err := os.Stat(config.Path)
if err != nil {
return nil, fmt.Errorf("error looking up stat info: %w", err)
}
if !stat.IsDir() {
return nil, errors.New("given path is not a directory")
}
abspath, err := filepath.Abs(config.Path)
if err != nil {
return nil, fmt.Errorf("error generating absolute path of %s: %v", config.Path, err)
}
var regex *regexp.Regexp
if config.Ignore != "" {
if regex, err = regexp.Compile(config.Ignore); err != nil {
return nil, fmt.Errorf("error compiling regular expression (ignore): %v", err)
}
}
if config.CompletedDir != "" && config.RetentionPeriod != nil {
return nil, errors.New("cannot use completedDir and retentionPeriod simultaneously")
}
fw, err := fileWatcher(config)
if err != nil {
return nil, err
}
w := &filesystemWatcher{
ctx: ctx,
cfg: config,
fw: fw,
ch: make(chan *fsnotify.Event, 100),
path: abspath,
regex: regex,
commonWatcherImpl: &commonWatcherImpl{
name: config.Name,
retentionPeriod: config.RetentionPeriod,
completedDir: config.CompletedDir,
stripTopLevelDir: config.StripTopLevelDir,
},
}
go w.loop()
if err := fw.Add(abspath); err != nil {
return nil, fmt.Errorf("error configuring filesystem watcher: %w", err)
}
return w, nil
}
func fileWatcher(cfg *FilesystemConfig) (filenotify.FileWatcher, error) {
var (
fsw filenotify.FileWatcher
err error
)
// The inotify API isn't always available, fall back to polling.
if cfg.Inotify && runtime.GOOS != "windows" {
fsw, err = filenotify.New(filenotify.Config{PollInterval: cfg.PollInterval})
} else {
fsw, err = filenotify.NewPollingWatcher(
filenotify.Config{PollInterval: cfg.PollInterval},
)
}
if err != nil {
return nil, fmt.Errorf("error creating filesystem watcher: %w", err)
}
return fsw, nil
}
func (w *filesystemWatcher) loop() {
for {
select {
case event, ok := <-w.fw.Events():
if !ok {
continue
}
if event.Op != fsnotify.Create && event.Op != fsnotify.Rename {
continue
}
if path, err := filepath.Abs(event.Name); err != nil || path == w.path {
continue
}
if w.regex != nil && w.regex.MatchString(filepath.Base(event.Name)) {
continue
}
w.ch <- &event
case _, ok := <-w.fw.Errors():
if !ok {
continue
}
case <-w.ctx.Done():
_ = w.fw.Close()
close(w.ch)
return
}
}
}
func (w *filesystemWatcher) Watch(ctx context.Context) (*BlobEvent, Cleanup, error) {
fsevent, ok := <-w.ch
if !ok {
return nil, noopCleanup, ErrWatchTimeout
}
info, err := os.Stat(fsevent.Name)
if err != nil {
return nil, noopCleanup, fmt.Errorf("error in file stat check: %s", err)
}
rel, err := filepath.Rel(w.path, fsevent.Name)
if err != nil {
return nil, noopCleanup, fmt.Errorf("error generating relative path of fsvent.Name %s - %w", fsevent.Name, err)
}
return NewBlobEvent(w, rel, info.IsDir()), noopCleanup, nil
}
func (w *filesystemWatcher) Path() string {
return w.path
}
func (w *filesystemWatcher) OpenBucket(ctx context.Context) (*blob.Bucket, error) {
return bucket.NewWithConfig(ctx, &bucket.Config{
URL: fmt.Sprintf("file://%s", w.path),
})
}
func (w *filesystemWatcher) RemoveAll(key string) error {
return os.RemoveAll(filepath.Join(w.path, key))
}
func (w *filesystemWatcher) Dispose(key string) error {
if w.completedDir == "" {
return nil
}
src := filepath.Join(w.path, key)
dst := filepath.Join(w.completedDir, key)
return fsutil.Move(src, dst)
}
// Download recursively copies the contents of key to dest. Key may be the name
// of a directory or file.
func (w *filesystemWatcher) Download(ctx context.Context, dest, key string) error {
src := filepath.Clean(filepath.Join(w.path, key))
dest = filepath.Clean(filepath.Join(dest, key))
if err := cp.Copy(src, dest); err != nil {
return fmt.Errorf("filesystem watcher: download: %v", err)
}
return nil
}