-
Notifications
You must be signed in to change notification settings - Fork 0
/
usher.go
307 lines (266 loc) · 8.25 KB
/
usher.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package usher
import (
"errors"
"fmt"
"io"
"io/fs"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"sort"
"strings"
"github.com/rjeczalik/notify"
)
type FileMapper interface {
GetFileDestPath(relSrcFile string, absSrcFile string, baseSrcFile string,
mappedRootSrcPath string, mappedRootDestPath string) (string, error)
}
type DelegatingFileMapper struct {
GetFileDestPathFunc func(relSrcFile string, absSrcFile string, baseSrcFile string,
mappedRootSrcPath string, mappedRootDestPath string) (string, error)
}
func (fm *DelegatingFileMapper) GetFileDestPath(relSrcFile string, absSrcFile string,
baseSrcFile string, mappedRootSrcPath string, mappedRootDestPath string) (string, error) {
return fm.GetFileDestPathFunc(relSrcFile, absSrcFile, baseSrcFile, mappedRootSrcPath, mappedRootDestPath)
}
func NewFileMapper(getFileDestPathFunc func(relSrcFile string, absSrcFile string,
baseSrcFile string, mappedRootSrcPath string, mappedRootDestPath string) (string, error)) *DelegatingFileMapper {
return &DelegatingFileMapper{getFileDestPathFunc}
}
var fileMappers map[string]FileMapper = make(map[string]FileMapper)
func SetFileMappers(newFileMappers map[string]FileMapper) {
fileMappers = newFileMappers
}
func RegisterFileMapper(name string, fileMapper FileMapper) {
fileMappers[name] = fileMapper
}
func GetFileMapper(fileMapperRef string, debug bool) (FileMapper, error) {
if debug {
fmt.Println("known mappers:", getFileMapperRefs())
}
if len(fileMapperRef) == 0 {
if len(fileMappers) == 1 {
for k := range fileMappers {
fileMapperRef = k
break
}
fmt.Println("using file mapper", fileMapperRef)
} else {
return nil, errors.New("FileMapper type was not provided, " + getFileMapperRefs())
}
}
fileMapper, ok := fileMappers[fileMapperRef]
if !ok {
//if we didn't find a known filen mapper, look for a matching exeternal executable
_, err := exec.LookPath(fileMapperRef)
if err == nil {
//create a fileMapper which will call the external executable as the mapper
fileMapper = NewExternalFileMapper(fileMapperRef)
ok = true
} else if strings.Contains(err.Error(), "permission denied") {
log.Fatal(err)
}
}
if !ok {
return nil, errors.New("mapper " + fileMapperRef + " does not exist, valid mappers: " + getFileMapperRefs())
}
return fileMapper, nil
}
func getFileMapperRefs() string {
refs := make([]string, 0)
for k := range fileMappers {
refs = append(refs, k)
}
sort.Strings(refs)
return strings.Join(refs, ", ")
}
func processFile(config Config, absSrcFile string) {
srcStat, err := os.Stat(absSrcFile)
if err != nil {
//if source file doesn't exist and not in debug mode, don't
//bother logging (file was deleted before we processed it)
if config.Debug || !errors.Is(err, os.ErrNotExist) {
log.Println(err)
}
return
}
srcDirAbs, _ := filepath.Abs(config.SrcDir)
relSrcFile := strings.TrimPrefix(absSrcFile, srcDirAbs+"/")
if srcStat.IsDir() {
if config.Debug {
log.Println("Ignoring directory", relSrcFile)
return
}
}
baseSrcFile := path.Base(absSrcFile)
//skip files starting with . //rsync prepends . to files currently being transferred
if baseSrcFile[0] == '.' {
if config.Debug {
log.Println("file", relSrcFile, "starts with ., ignoring")
}
return
}
var relToRelevantRootSrcFile, mappedRootSrcPath, mappedRootDestPath string
if len(config.RootPathMappings) == 0 {
//if root path mappings are not used, we set relToRelevantRootSrcFile
//to relSrcFile
relToRelevantRootSrcFile = relSrcFile
} else {
//if srcFile begins with a map key in config.RootPathMappings,
//prefix the destination with the map value
//to place all unmatched files into a directory, provide a root path mapping
//with a zero length string for a key (e.g. "":unmatched)
//otherwise files with unmatched root paths are ignored
//reverse sort config.RootPathMapping keys to find more specific matches first
rootPathMappingKeys := make([]string, 0, len(config.RootPathMappings))
for rootPath := range config.RootPathMappings {
rootPathMappingKeys = append(rootPathMappingKeys, rootPath)
}
sort.Sort(sort.Reverse(sort.StringSlice(rootPathMappingKeys)))
var mappedRootPathFound bool = false
for _, rootPath := range rootPathMappingKeys {
if strings.HasPrefix(relSrcFile, rootPath) {
mappedRootPathFound = true
relToRelevantRootSrcFile = strings.TrimPrefix(relSrcFile, rootPath+"/")
mappedRootSrcPath = rootPath
mappedRootDestPath = config.RootPathMappings[rootPath]
break
}
}
if !mappedRootPathFound {
log.Println("No root path mapping found for", relSrcFile, "(skipping)")
return
}
}
relDestFile, err := config.FileMapper.GetFileDestPath(
relToRelevantRootSrcFile, absSrcFile, baseSrcFile, mappedRootSrcPath, mappedRootDestPath)
if err != nil {
//TODO copy to unhandled directory?
if config.Debug {
log.Println(err)
}
return
}
//if relDestFile is multiline, just use the first (could be an artifact of external executables)
relDestFile = strings.Split(relDestFile, "\n")[0]
//append the mapped root to dest dir if mapped roots are configured
destDir := config.DestDir
if len(mappedRootDestPath) > 0 {
destDir += "/" + mappedRootDestPath
}
destFile, _ := filepath.Abs(destDir + "/" + relDestFile)
//make sure destFile is inside config root dest dir and mapped root (if applicable),
//aka relative paths weren't used to climb out of it
if !strings.HasPrefix(destFile, config.DestDir) {
log.Println(destFile, "is not contained by", config.DestDir, "(skipping)")
return
}
if len(mappedRootDestPath) > 0 && !strings.HasPrefix(destFile, destDir) {
log.Println(destFile, "is not contained by mapped root", destDir, "(skipping)")
return
}
destParentDir := path.Dir(destFile)
if !config.DryRun {
os.MkdirAll(destParentDir, os.ModePerm)
}
//check for existing file and delete if it's not the same
if destStat, err := os.Stat(destFile); err == nil {
if os.SameFile(srcStat, destStat) {
if config.Debug {
log.Println("file", destFile, "exists and is the same as src file, skipping")
}
return
} else {
if config.Debug {
log.Println("file", destFile, "exists and is not the same as src file, deleting")
}
if !config.DryRun {
if err := os.Remove(destFile); err != nil {
log.Println("failed to delete file", destFile, err)
return
}
}
}
}
var dryRunIndicator = ""
if config.DryRun {
dryRunIndicator = "-dry-run"
}
var operationType string
if config.Copy {
operationType = "copy"
} else {
operationType = "link"
}
operationIndicator := strings.Join([]string{"--", operationType, dryRunIndicator, "-->"}, "")
log.Println(absSrcFile, operationIndicator, destFile)
if config.Copy {
if !config.DryRun {
err = copyFile(absSrcFile, destFile)
}
} else {
if !config.DryRun {
err = os.Link(absSrcFile, destFile)
}
}
if err != nil {
log.Println(err)
return
}
}
func copyFile(srcPath string, destPath string) error {
srcFile, err := os.Open(srcPath)
if err != nil {
return err
}
defer srcFile.Close()
destFile, err := os.OpenFile(destPath, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
return err
}
defer destFile.Close()
_, err = io.Copy(destFile, srcFile)
if err != nil {
return err
}
return nil
}
func Watch(config Config) {
c := make(chan notify.EventInfo, config.Watch.EventBufferSize)
if !config.DryRun {
os.MkdirAll(config.SrcDir, os.ModePerm)
}
if err := notify.Watch(config.SrcDir+"/...", c, notify.All); err != nil {
log.Fatal(err)
}
defer notify.Stop(c)
for eventInfo := range c {
if config.Debug {
log.Println("Detected event", eventInfo.Event(), "for file", eventInfo.Path())
}
if eventInfo.Event() == notify.Create || eventInfo.Event() == notify.Write {
if config.Debug {
log.Println("Processing event", eventInfo.Event(), "for file", eventInfo.Path())
}
processFile(config, eventInfo.Path())
}
}
}
func Process(config Config) {
if _, err := os.Stat(config.SrcDir); err != nil {
log.Fatalf("Source directory " + config.SrcDir + " does not exist")
}
err := filepath.WalkDir(config.SrcDir, func(path string, d fs.DirEntry, err error) error {
if d.IsDir() {
//skip directories
return nil
}
processFile(config, path)
return nil
})
if err != nil {
log.Fatalf("Could not walk source directory " + config.SrcDir + ", " + err.Error())
}
}