This repository has been archived by the owner on Jan 27, 2020. It is now read-only.
forked from a-m-s/syncthing-inotify
-
Notifications
You must be signed in to change notification settings - Fork 41
/
syncwatcher.go
1161 lines (1087 loc) · 32.6 KB
/
syncwatcher.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// syncwatcher.go
package main
import (
"bufio"
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
"encoding/pem"
"encoding/xml"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"runtime"
"sort"
"strconv"
"strings"
"text/tabwriter"
"time"
"github.com/cenkalti/backoff"
"github.com/syncthing/syncthing/lib/ignore"
"github.com/zillode/notify"
)
// Configuration is used in parsing response from ST
type Configuration struct {
Version int
Folders []FolderConfiguration
}
// FolderConfiguration holds information about shared folder in ST
type FolderConfiguration struct {
ID string
Label string
Path string
RescanIntervalS int
}
// Event holds full event data coming from Syncthing REST API
type Event struct {
ID int `json:"id"`
Time time.Time `json:"time"`
Type string `json:"type"`
Data interface{} `json:"data"`
}
// STEvent holds simplified data for Syncthing event. Path can be empty in the case of event.type="RemoteIndexUpdated"
type STEvent struct {
Path string
Finished bool
}
// STNestedConfig is used for unpacking config from XML format
type STNestedConfig struct {
Config STConfig `xml:"gui"`
}
// STConfig is used for unpacking gui part of config from XML format
type STConfig struct {
CsrfFile string
CertFile string
APIKey string `xml:"apikey"`
Target string `xml:"address"`
AuthUser string `xml:"user"`
AuthPass string `xml:"password"`
TLS bool `xml:"tls,attr"`
}
type folderSlice []string
type progressTime struct {
fsEvent bool // true - event was triggered by filesystem, false - by Syncthing
time time.Time
}
func (fs *folderSlice) String() string {
return fmt.Sprint(*fs)
}
func (fs *folderSlice) Set(value string) error {
for _, f := range strings.Split(value, ",") {
*fs = append(*fs, f)
}
return nil
}
// HTTP Authentication
var (
target string
cert *x509.Certificate
certFile string
authUser string
authPass string
csrfToken string
csrfFile string
apiKey string
)
// HTTP Timeouts
var (
requestTimeout = 180 * time.Second
)
// HTTP Debounce
var (
debounceTimeout = 500 * time.Millisecond
configSyncTimeout = 5 * time.Second
dirVsFiles = 128
maxFiles = 512
)
// Main
var (
stop = make(chan int)
versionFolder = ".stversions"
logFd = os.Stdout
Version = "unknown-dev"
Discard = log.New(ioutil.Discard, "", log.Ldate)
Warning = Discard // verbosity=1
OK = Discard // 2
Trace = Discard // 3
Debug = Discard // 4
watchFolders folderSlice
skipFolders folderSlice
delayScan = 3600
)
const (
pathSeparator = string(os.PathSeparator)
usage = "syncthing-inotify [options]"
extraUsage = `
The -logflags value is a sum of the following:
1 Date
2 Time
4 Microsecond time
8 Long filename
16 Short filename
I.e. to prefix each log line with date and time, set -logflags=3 (1 + 2 from
above). The value 0 is used to disable all of the above. The default is to
show time only (2).`
)
func setupLogging(verbosity int, logflags int) {
if verbosity >= 1 {
Warning = log.New(logFd, "[WARNING] ", logflags)
}
if verbosity >= 2 {
OK = log.New(logFd, "[OK] ", logflags)
}
if verbosity >= 3 {
Trace = log.New(logFd, "[TRACE] ", logflags)
}
if verbosity >= 4 {
Debug = log.New(logFd, "[DEBUG] ", logflags)
Debug.SetFlags(Debug.Flags() | log.Lmicroseconds)
}
}
func init() {
c, _ := getSTConfig(getSTDefaultConfDir())
if !strings.Contains(c.Target, "://") {
if c.TLS {
target = "https://" + c.Target
certFile = c.CertFile
} else {
target = "http://" + c.Target
}
}
var logFile string
var verbosity int
var logflags int
var home string
var apiKeyStdin bool
var authPassStdin bool
var showVersion bool
flag.DurationVar(&debounceTimeout, "interval", debounceTimeout,
"Accumulation interval, e.g. 5s or 1m")
flag.StringVar(&logFile, "logfile", "", "Log file")
flag.IntVar(&verbosity, "verbosity", 2, "Logging level [1..4]")
flag.IntVar(&logflags, "logflags", 2, "Select information in log line prefix")
flag.StringVar(&home, "home", home, "Specify the home Syncthing dir to sniff configuration settings")
flag.StringVar(&target, "target", target, "Target url (prepend with https:// for TLS)")
flag.StringVar(&certFile, "cert", certFile, "Server certificate file")
flag.StringVar(&authUser, "user", c.AuthUser, "Username")
flag.StringVar(&authPass, "password", "***", "Password")
flag.StringVar(&csrfFile, "csrf", "", "CSRF token file")
flag.StringVar(&apiKey, "api", c.APIKey, "API key")
flag.BoolVar(&apiKeyStdin, "api-stdin", false, "Provide API key through stdin")
flag.BoolVar(&authPassStdin, "password-stdin", false, "Provide password through stdin")
flag.Var(&watchFolders, "folders", "A comma-separated list of folder labels or IDs to watch (all by default)")
flag.Var(&skipFolders, "skip-folders", "A comma-separated list of folder labels or IDs to skip inotify watching")
flag.IntVar(&delayScan, "delay-scan", delayScan, "Automatically delay next scan interval (in seconds)")
flag.BoolVar(&showVersion, "version", false, "Show version")
flag.Usage = usageFor(flag.CommandLine, usage, fmt.Sprintf(extraUsage))
flag.Parse()
if showVersion {
fmt.Printf("syncthing-inotify %s (%s %s-%s)\n", Version, runtime.Version(), runtime.GOOS, runtime.GOARCH)
os.Exit(0)
}
if len(logFile) > 0 {
var err error
logFd, err = os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Fatalln(err)
}
}
setupLogging(verbosity, logflags)
if len(home) > 0 {
c, err := getSTConfig(home)
if err != nil {
log.Fatalln(err)
}
if !strings.Contains(c.Target, "://") {
if c.TLS {
target = "https://" + c.Target
certFile = c.CertFile
} else {
target = "http://" + c.Target
certFile = ""
}
target = strings.Replace(target, "0.0.0.0", "127.0.0.1", 1)
apiKey = c.APIKey
}
}
if !strings.Contains(target, "://") {
target = "http://" + target
}
if len(certFile) > 0 {
pemCerts, err := ioutil.ReadFile(certFile)
if err != nil {
log.Fatalln(err)
}
for {
var block *pem.Block
block, pemCerts = pem.Decode(pemCerts)
if block == nil {
break
}
if block.Type != "CERTIFICATE" {
continue
}
if len(block.Headers) > 0 {
log.Fatalln("Unsupported server certificate")
}
cert, err = x509.ParseCertificate(block.Bytes)
if err != nil {
log.Fatalln("Failed to parse server certificate:", err)
}
if cert != nil {
break
}
}
if cert == nil {
log.Fatalln("No certificate in server certificate file")
}
// Fake validity
cert.KeyUsage |= x509.KeyUsageCertSign
cert.IsCA = true
}
if len(csrfFile) > 0 {
fd, err := os.Open(csrfFile)
if err != nil {
log.Fatalln(err)
}
s := bufio.NewScanner(fd)
for s.Scan() {
csrfToken = s.Text()
}
fd.Close()
}
if apiKeyStdin && authPassStdin {
log.Fatalln("Either provide an API or password through stdin")
}
if apiKeyStdin {
stdin := bufio.NewReader(os.Stdin)
apiKey, _ = stdin.ReadString('\n')
}
if authPassStdin {
stdin := bufio.NewReader(os.Stdin)
authPass, _ = stdin.ReadString('\n')
}
if len(watchFolders) != 0 && len(skipFolders) != 0 {
log.Fatalln("Either provide a list of folders to be watched or to be ignored, not both.")
}
if delayScan > 0 && delayScan < 60 {
log.Fatalln("A delay scan interval shorter than 60 is not supported.")
}
}
// main reads configs, starts all gouroutines and waits until a message is in channel stop.
func main() {
backoff.Retry(testWebGuiPost, backoff.NewExponentialBackOff())
// Attempt to increase the limit on number of open files to the maximum allowed.
MaximizeOpenFileLimit()
allFolders := getFolders()
folders := filterFolders(allFolders)
stChans := make(map[string]chan STEvent, len(folders))
for _, folder := range folders {
Debug.Println("Installing watch for " + folder.Label)
stChan := make(chan STEvent)
stChans[folder.ID] = stChan
go watchFolder(folder, stChan)
}
// Note: Lose thread ownership of stChans
go watchSTEvents(stChans, allFolders)
code := <-stop
OK.Println("Exiting")
os.Exit(code)
}
// Restart uses path to itself and copy of environment to start new process.
// Then it sends message to stop channel to shutdown itself.
func restart() bool {
pgm, err := exec.LookPath(os.Args[0])
if err != nil {
Warning.Println("Cannot restart:", err)
return false
}
env := os.Environ()
newEnv := make([]string, 0, len(env))
for _, s := range env {
newEnv = append(newEnv, s)
}
proc, err := os.StartProcess(pgm, os.Args, &os.ProcAttr{
Env: newEnv,
Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
})
if err != nil {
Warning.Println("Cannot restart:", err)
return false
}
proc.Release()
stop <- 3
return true
}
// filterFolders refines folders list using global vars watchFolders and skipFolders
func filterFolders(folders []FolderConfiguration) []FolderConfiguration {
if len(watchFolders) > 0 {
var fs []FolderConfiguration
for _, f := range folders {
for _, watch := range watchFolders {
if f.ID == watch || f.Label == watch {
fs = append(fs, f)
break
}
}
}
return fs
}
if len(skipFolders) > 0 {
var fs []FolderConfiguration
for _, f := range folders {
keep := true
for _, skip := range skipFolders {
if f.ID == skip || f.Label == skip {
keep = false
break
}
}
if keep {
fs = append(fs, f)
}
}
return fs
}
return folders
}
func closeRequestResult(result *http.Response) {
if result != nil && result.Body != nil {
result.Body.Close()
}
}
// getFolders returns the list of folders configured in Syncthing. Blocks until ST responded successfully.
func getFolders() []FolderConfiguration {
Trace.Println("Getting Folders")
r, err := http.NewRequest("GET", target+"/rest/system/config", nil)
res, err := performRequest(r)
defer closeRequestResult(res)
if err != nil {
log.Fatalln("Failed to perform request /rest/system/config: ", err)
}
if res.StatusCode != 200 {
log.Fatalf("Status %d != 200 for GET /rest/system/config: ", res.StatusCode)
}
bs, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatalln(err)
}
var cfg Configuration
err = json.Unmarshal(bs, &cfg)
if err != nil {
log.Fatalln(err)
}
// Use folder label unless it's empty
folders := cfg.Folders
for f := range folders {
if len(folders[f].Label) == 0 {
folders[f].Label = folders[f].ID
}
}
return folders
}
// watchFolder installs inotify watcher for a folder, launches
// goroutine which receives changed items. It never exits.
func watchFolder(folder FolderConfiguration, stInput chan STEvent) {
folderPath, err := realPath(expandTilde(folder.Path))
if err != nil {
Warning.Println("Failed to install inotify handler for "+folder.Label+".", err)
informError("Failed to install inotify handler for " + folder.Label + ": " + err.Error())
return
}
Trace.Println("Getting ignore patterns for " + folder.Label)
ignoreFilter := createIgnoreFilter(folderPath)
absIgnoreFilter := func(absPath string) bool {
return ignoreFilter(relativePath(absPath, folderPath))
}
fsInput := make(chan string)
c := make(chan notify.EventInfo, maxFiles)
if err := notify.WatchWithFilter(filepath.Join(folderPath, "..."), c,
absIgnoreFilter, notify.All); err != nil {
if strings.Contains(err.Error(), "too many open files") || strings.Contains(err.Error(), "no space left on device") {
msg := "Failed to install inotify handler for " + folder.Label + ". Please increase inotify limits, see http://bit.ly/1PxkdUC for more information."
Warning.Println(msg, err)
informError(msg)
return
} else {
Warning.Println("Failed to install inotify handler for "+folder.Label+".", err)
informError("Failed to install inotify handler for " + folder.Label + ": " + err.Error())
return
}
}
defer notify.Stop(c)
go accumulateChanges(debounceTimeout, folder.ID, folderPath, dirVsFiles, stInput, fsInput, informChange)
OK.Println("Watching " + folder.Label + ": " + folderPath)
if folder.RescanIntervalS < 1800 && delayScan <= 0 {
OK.Printf("The rescan interval of folder %s can be increased to 3600 (an hour) or even 86400 (a day) as changes should be observed immediately while syncthing-inotify is running.", folder.Label)
}
// will we ever get out of this loop?
for {
evAbsolutePath := waitForEvent(c)
Debug.Println("Change detected in: " + evAbsolutePath + " (could still be ignored)")
evRelPath := relativePath(evAbsolutePath, folderPath)
if ignoreFilter(evRelPath) {
Debug.Println("Ignoring", evAbsolutePath)
continue
}
Trace.Println("Change detected in: " + evAbsolutePath)
fsInput <- evRelPath
}
}
func realPath(path string) (string, error) {
path, err := filepath.Abs(path)
if err != nil {
return "", err
}
return filepath.EvalSymlinks(path)
}
func relativePath(path string, folderPath string) string {
path = expandTilde(path)
path = strings.TrimPrefix(path, folderPath)
if len(path) == 0 {
return "."
}
if os.IsPathSeparator(path[0]) {
if len(path) == 1 {
return "."
}
return path[1:]
}
return path
}
// Returns a function to test whether a path should be ignored.
// The directory given by the absolute path "folderPath" must contain the
// ".stignore" file. The returned function expects the path of the file to be
// tested relative to its folders root.
func createIgnoreFilter(folderPath string) func(relPath string) bool {
ignores := ignore.New(ignore.WithCache(false))
ignores.Load(filepath.Join(folderPath, ".stignore"))
return ignores.ShouldIgnore
}
// waitForEvent waits for an event in a channel c and returns event.Path().
// When channel c is closed then it returns path for default event (not sure if this is used at all?)
func waitForEvent(c chan notify.EventInfo) string {
select {
case ev, ok := <-c:
if !ok {
// this is never reached b/c c is never closed
Warning.Println("Error: channel closed")
}
return ev.Path()
}
}
func prepareApiRequestForSyncthing(request *http.Request) (*http.Request, error) {
if request == nil {
return nil, errors.New("Invalid HTTP Request object")
}
if len(csrfToken) > 0 {
request.Header.Set("X-CSRF-Token", csrfToken)
}
if len(authUser) > 0 {
request.SetBasicAuth(authUser, authPass)
}
if len(apiKey) > 0 {
request.Header.Set("X-API-Key", apiKey)
}
return request, nil
}
// performRequest performs an HTTP request r to Synthing API
func performRequest(r *http.Request) (*http.Response, error) {
request, err := prepareApiRequestForSyncthing(r)
if request == nil {
return nil, err
}
var tlsCfg tls.Config
if cert != nil {
tlsCfg.RootCAs = x509.NewCertPool()
tlsCfg.RootCAs.AddCert(cert)
// Always use this certificate
tlsCfg.ServerName = cert.Subject.CommonName
}
tr := &http.Transport{
TLSClientConfig: &tlsCfg,
ResponseHeaderTimeout: requestTimeout,
DisableKeepAlives: true,
}
client := &http.Client{
Transport: tr,
Timeout: requestTimeout,
}
res, err := client.Do(request)
if res != nil && res.StatusCode == 403 {
Warning.Printf("Error: HTTP POST forbidden. Missing API key?")
return res, errors.New("HTTP POST forbidden")
}
return res, err
}
// testWebGuiPost tries to connect to Syncthing returning nil on success
func testWebGuiPost() error {
Trace.Println("Testing WebGUI")
r, err := http.NewRequest("GET", target+"/rest/404", nil)
res, err := performRequest(r)
defer closeRequestResult(res)
if err != nil {
Warning.Println("Cannot connect to Syncthing:", err)
return err
}
body, _ := ioutil.ReadAll(res.Body)
if res.StatusCode != 404 {
Warning.Printf("Cannot connect to Syncthing, Status %d != 404 for GET. Body: %v\n", res.StatusCode, string(body))
return errors.New("Invalid HTTP status code")
}
return nil
}
// informError sends a msg error to Syncthing
func informError(msg string) error {
Trace.Printf("Informing ST about inotify error: %v", msg)
r, _ := http.NewRequest("POST", target+"/rest/system/error", strings.NewReader("[Inotify] "+msg))
r.Header.Set("Content-Type", "plain/text")
res, err := performRequest(r)
defer closeRequestResult(res)
if err != nil {
Warning.Println("Failed to inform Syncthing about", msg, err)
return err
}
if res.StatusCode != 200 {
Warning.Printf("Error: Status %d != 200 for POST: %v\n", res.StatusCode, msg)
return errors.New("Invalid HTTP status code")
}
return err
}
// informChange sends a request to rescan folder and subs to Syncthing
func informChange(folder string, subs []string) error {
data := url.Values{}
data.Set("folder", folder)
if len(subs) != 1 || subs[0] != "" {
for _, sub := range subs {
data.Add("sub", sub)
}
}
if delayScan > 0 {
data.Set("next", strconv.Itoa(delayScan))
}
Trace.Printf("Informing ST: %v: %v", folder, subs)
r, _ := http.NewRequest("POST", target+"/rest/db/scan?"+data.Encode(), nil)
res, err := performRequest(r)
defer closeRequestResult(res)
if err != nil {
Warning.Println("Failed to perform request", err)
return err
}
if res.StatusCode != 200 {
msg, _ := ioutil.ReadAll(res.Body)
Warning.Println(target + "/rest/db/scan?" + data.Encode())
Warning.Printf("Error: Status %d != 200 for POST: %v, %s\n", res.StatusCode, folder, msg)
return errors.New("Invalid HTTP status code")
}
OK.Printf("Syncthing is indexing change in %v: %v", folder, subs)
// Wait until scan finishes
_, err = ioutil.ReadAll(res.Body)
return err
}
// InformCallback is a function which will be called from accumulateChanges and related functions when there is a change we need to inform Syncthing about
type InformCallback func(folder string, subs []string) error
func askToDelayScan(folder string, callback InformCallback) {
Trace.Println("Asking to delay full scanning of " + folder)
if err := callback(folder, []string{".stfolder"}); err != nil {
Warning.Printf("Request to delay scanning of " + folder + " failed")
}
}
// accumulateChanges filters out events that originate from ST.
// - it aggregates changes based on hierarchy structure
// - no redundant folder searches (abc + abc/d is useless)
// - no excessive large scans (abc/{1..1000} should become a scan of just abc folder)
// One of the difficulties is that we cannot know if deleted files were a directory or a file.
func accumulateChanges(debounceTimeout time.Duration,
folder string,
folderPath string,
dirVsFiles int,
stInput chan STEvent,
fsInput chan string,
callback InformCallback) func(string) {
var delayScanInterval time.Duration
if delayScan > 0 {
delayScanInterval = time.Duration(delayScan-5) * time.Second
Debug.Printf("Delay scan reminder interval for %s set to %.0f seconds\n", folder, delayScanInterval.Seconds())
} else {
// If delayScan is set to 0, then we never send requests to delay full scans.
// "9999 * time.Hour" here is an approximation of "forever".
delayScanInterval = 9999 * time.Hour
Debug.Println("Delay scan reminders are disabled")
}
inProgress := make(map[string]progressTime) // [path string]{fs, start}
currInterval := debounceTimeout // Timeout of the timer
if delayScan > 0 {
askToDelayScan(folder, callback)
}
nextScanTime := time.Now().Add(delayScanInterval) // Time to remind Syncthing to delay scan
flushTimer := time.NewTimer(0)
flushTimerNeedsReset := true
for {
if flushTimerNeedsReset {
flushTimerNeedsReset = false
if !flushTimer.Stop() {
select {
case <-flushTimer.C:
default:
}
}
flushTimer.Reset(currInterval)
}
select {
case item := <-stInput:
if item.Path == "" {
// Prepare for incoming changes
if currInterval != debounceTimeout {
currInterval = debounceTimeout
flushTimerNeedsReset = true
Debug.Println("[ST] Incoming Changes for "+folder+", speeding up inotify timeout parameters to", debounceTimeout)
} else {
Debug.Println("[ST] Incoming Changes for " + folder)
}
continue
}
if item.Finished {
// Ensure path is cleared when receiving itemFinished
delete(inProgress, item.Path)
Debug.Println("[ST] Removed tracking for " + item.Path)
continue
}
if len(inProgress) > maxFiles {
Debug.Println("[ST] Tracking too many files, aggregating STEvent: " + item.Path)
continue
}
Debug.Println("[ST] Incoming: " + item.Path)
inProgress[item.Path] = progressTime{false, time.Now()}
case item := <-fsInput:
if currInterval != debounceTimeout {
currInterval = debounceTimeout
flushTimerNeedsReset = true
Debug.Println("[FS] Incoming Changes for "+folder+", speeding up inotify timeout parameters to", debounceTimeout)
} else {
Debug.Println("[FS] Incoming Changes for " + folder)
}
p, ok := inProgress[item]
if ok && !p.fsEvent {
// Change originated from ST
delete(inProgress, item)
Debug.Println("[FS] Removed tracking for " + item)
continue
}
if len(inProgress) > maxFiles {
Debug.Println("[FS] Tracking too many files, aggregating FSEvent: " + item)
continue
}
Debug.Println("[FS] Tracking: " + item)
inProgress[item] = progressTime{true, time.Now()}
case <-flushTimer.C:
flushTimerNeedsReset = true
if delayScan > 0 && nextScanTime.Before(time.Now()) {
nextScanTime = time.Now().Add(delayScanInterval)
askToDelayScan(folder, callback)
}
if len(inProgress) == 0 {
if currInterval != delayScanInterval {
Debug.Println("Slowing down inotify timeout parameters for " + folder)
currInterval = delayScanInterval
}
continue
}
Debug.Println("Timeout AccumulateChanges")
var err error
var paths []string
expiry := time.Now().Add(-debounceTimeout * 10)
if len(inProgress) < maxFiles {
for path, progress := range inProgress {
// Clean up invalid and expired paths
if path == "" || (!progress.fsEvent && progress.time.Before(expiry)) {
delete(inProgress, path)
continue
}
if progress.fsEvent && time.Now().Sub(progress.time) > debounceTimeout {
paths = append(paths, path)
Debug.Println("Informing about " + path)
} else {
Debug.Println("Waiting for " + path)
}
}
if len(paths) == 0 {
Debug.Println("Empty paths")
continue
}
// Try to inform changes to syncthing and if succeeded, clean up
err = callback(folder, aggregateChanges(folderPath, dirVsFiles, paths, currentPathStatus))
if err == nil {
for _, path := range paths {
delete(inProgress, path)
Debug.Println("[INFORMED] Removed tracking for " + path)
}
}
} else {
// Do not track more than maxFiles changes, inform syncthing to rescan entire folder
err = callback(folder, []string{""})
if err == nil {
for path, progress := range inProgress {
if progress.fsEvent {
delete(inProgress, path)
Debug.Println("[INFORMED] Removed tracking for " + path)
}
}
}
}
if err == nil {
nextScanTime = time.Now().Add(delayScanInterval) // Scan was delayed
} else {
Warning.Println("Syncthing failed to index changes for ", folder, err)
time.Sleep(configSyncTimeout)
}
}
}
}
func cleanPaths(paths []string) {
for i := range paths {
paths[i] = filepath.Clean(paths[i])
}
}
func sortedUniqueAndCleanPaths(paths []string) []string {
cleanPaths(paths)
sort.Strings(paths)
var new_paths []string
previousPath := ""
for _, path := range paths {
if path == "." {
path = ""
}
if path != previousPath {
new_paths = append(new_paths, path)
}
previousPath = path
}
return new_paths
}
type PathStatus int
const (
deletedPath PathStatus = iota
directoryPath
filePath
)
func currentPathStatus(path string) PathStatus {
fileinfo, _ := os.Stat(path)
if fileinfo == nil {
return deletedPath
} else if fileinfo.IsDir() {
return directoryPath
}
return filePath
}
type statPathFunc func(name string) PathStatus
// AggregateChanges optimises tracking in two ways:
// - If there are more than `dirVsFiles` changes in a directory, we inform Syncthing to scan the entire directory
// - Directories with parent directory changes are aggregated. If A/B has 3 changes and A/C has 8, A will have 11 changes and if this is bigger than dirVsFiles we will scan A.
func aggregateChanges(folderPath string, dirVsFiles int, paths []string, pathStatus statPathFunc) []string {
// Map paths to scores; if score == -1 the path is a filename
trackedPaths := make(map[string]int)
// Map of directories
trackedDirs := make(map[string]bool)
// Make sure parent paths are processed first
paths = sortedUniqueAndCleanPaths(paths)
// First we collect all paths and calculate scores for them
for _, path := range paths {
pathstatus := pathStatus(path)
path = strings.TrimPrefix(path, folderPath)
path = strings.TrimPrefix(path, pathSeparator)
var dir string
if pathstatus == deletedPath {
// Definitely inform if the path does not exist anymore
dir = path
trackedPaths[path] = dirVsFiles
Debug.Println("[AG] Not found:", path)
} else if pathstatus == directoryPath {
// Definitely inform if a directory changed
dir = path
trackedPaths[path] = dirVsFiles
trackedDirs[dir] = true
Debug.Println("[AG] Is a dir:", dir)
} else {
Debug.Println("[AG] Is file:", path)
// Files are linked to -1 scores
// Also increment the parent path with 1
dir = filepath.Dir(path)
if dir == "." {
dir = ""
}
trackedPaths[path] = -1
trackedPaths[dir]++
trackedDirs[dir] = true
}
// Search for existing parent directory relations in the map
for trackedPath := range trackedPaths {
if trackedDirs[trackedPath] && strings.HasPrefix(dir, trackedPath+pathSeparator) {
// Increment score of tracked parent directory for each file
trackedPaths[trackedPath]++
Debug.Println("[AG] Increment:", trackedPath, trackedPaths, trackedPaths[trackedPath])
}
}
}
var keys []string
for k := range trackedPaths {
keys = append(keys, k)
}
sort.Strings(keys) // Sort directories before their own files
previousPath := ""
var scans []string
// Decide if we should inform about particular path based on dirVsFiles
for i := range keys {
trackedPath := keys[i]
trackedPathScore := trackedPaths[trackedPath]
if strings.HasPrefix(trackedPath, previousPath+pathSeparator) {
// Already informed parent directory change
continue
}
if trackedPathScore < dirVsFiles && trackedPathScore != -1 {
// Not enough files for this directory or it is a file
continue
}
previousPath = trackedPath
Debug.Println("[AG] Appending path:", trackedPath, previousPath)
scans = append(scans, trackedPath)
if trackedPath == "" {
// If we need to scan everything, skip the rest
break
}
}
return scans
}
// watchSTEvents reads events from Syncthing. For events of type ItemStarted and ItemFinished it puts
// them into aproppriate stChans, where key is a folder from event.
// For ConfigSaved event it spawns goroutine waitForSyncAndExitIfNeeded.
func watchSTEvents(stChans map[string]chan STEvent, folders []FolderConfiguration) {
lastSeenID := 0
for {
events, err := getSTEvents(lastSeenID)
if err != nil {
// Work-around for Go <1.5 (https://github.com/golang/go/issues/9405)
if strings.Contains(err.Error(), "use of closed network connection") {
continue
}
// Syncthing probably restarted
Debug.Println("Resetting STEvents", err)
lastSeenID = 0
time.Sleep(configSyncTimeout)
continue
}
if len(events) == 0 {
continue
}
for _, event := range events {
switch event.Type {
case "RemoteIndexUpdated":
data := event.Data.(map[string]interface{})
ch, ok := stChans[data["folder"].(string)]
if !ok {
continue
}
ch <- STEvent{Path: "", Finished: false}
case "ItemStarted":
data := event.Data.(map[string]interface{})
ch, ok := stChans[data["folder"].(string)]
if !ok {
continue
}
ch <- STEvent{Path: data["item"].(string), Finished: false}
case "ItemFinished":
data := event.Data.(map[string]interface{})
ch, ok := stChans[data["folder"].(string)]
if !ok {
continue
}
ch <- STEvent{Path: data["item"].(string), Finished: true}
case "ConfigSaved":
Trace.Println("ConfigSaved, exiting if folders changed")
go waitForSyncAndExitIfNeeded(folders)
}
}
lastSeenID = events[len(events)-1].ID
}
}
// getSTEvents returns a list of events which happened in Syncthing since lastSeenID.
func getSTEvents(lastSeenID int) ([]Event, error) {
Trace.Println("Requesting STEvents: " + strconv.Itoa(lastSeenID))
r, err := http.NewRequest("GET", target+"/rest/events?since="+strconv.Itoa(lastSeenID), nil)
res, err := performRequest(r)
defer closeRequestResult(res)
if err != nil {
Warning.Println("Failed to perform request", err)
return nil, err
}
if res.StatusCode != 200 {
Warning.Printf("Status %d != 200 for GET", res.StatusCode)
return nil, errors.New("Invalid HTTP status code")
}
bs, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
var events []Event
err = json.Unmarshal(bs, &events)
return events, err
}
// waitForSyncAndExitIfNeeded performs restart of itself if folders has different configuration in syncthing.
func waitForSyncAndExitIfNeeded(folders []FolderConfiguration) {
waitForSync()