-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch.go
177 lines (156 loc) · 3.93 KB
/
watch.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
package fonctions
import (
"fmt"
"os"
"os/signal"
"path/filepath"
"time"
"github.com/fsnotify/fsnotify"
log "github.com/sirupsen/logrus"
)
var toUpload []string
var toDelete []string
var toUploadd []string
var actuels []string
var actuelsd []string
var rootpath string
var watcher *fsnotify.Watcher
// Config stores all the configuration for dir watched and remote syncing.
type Config struct {
Watched string
Host string
RemoteRoot string
RemoteUser string
Interval time.Duration
KeyFile string
Passphrase []byte
Type string
Resume bool // Skip deleting the remote path first
}
// WatchDir Tous les interval, recupere et synchronise les fichiers modifiés du repertoire watched.
func WatchDir(config *Config) error {
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
TimestampFormat: time.Stamp,
})
rootpath = config.Watched
// Ecoute les signaux, et notifie le ctrl-c
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
// Block until a signal is received.
s := <-c
log.Println("Got signal:", s)
os.Exit(0)
}()
log.Infof("Base path : [%s]", filepath.Base(rootpath))
err := Clear(config)
if err != nil {
return err
}
watcher, err = fsnotify.NewWatcher()
if err != nil {
log.Fatal("Cannot create fsnotify", err)
}
defer watcher.Close()
err = filepath.Walk(config.Watched, visit)
if err != nil {
return fmt.Errorf("filepath.Walk() returned %v", err)
}
err = Upload(config, actuels, nil, actuelsd)
if err != nil {
return fmt.Errorf("Upload() returned %v", err)
}
ticker := time.NewTicker(config.Interval * time.Second)
go func() {
for range ticker.C {
timedLoop(config)
}
}()
defer ticker.Stop()
done := make(chan bool)
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
// Calcul de l'emplacement du fichier par rapport au répertoire observé
relative, relerr := filepath.Rel(rootpath, event.Name)
if relerr != nil {
log.Warningf("Cannot process filepath [%s] : [%v]", event.Name, relerr)
return
}
switch event.Op {
case fsnotify.Write, fsnotify.Create, fsnotify.Rename:
log.Debugf("modified/created [%v]", event)
info, err := os.Stat(event.Name)
if err != nil {
log.Warningf("Cannot stat on [%s] : [%v]", event.Name, err)
break
}
watcher.Add(event.Name)
if info.IsDir() {
toUploadd = append(toUploadd, relative)
} else {
toUpload = append(toUpload, relative)
}
case fsnotify.Remove:
log.Debugf("removed [%v]\n", event)
toDelete = append(toDelete, relative)
watcher.Remove(event.Name)
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Error("error:", err)
}
}
}()
err = watcher.Add(config.Watched)
if err != nil {
log.Fatalf("Cannot add watcher on [%s] : [%v]", config.Watched, err)
}
<-done
return nil
}
func visit(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
log.Debugf("Visited: [%s] [%s]", path, info.ModTime())
relative, relerr := filepath.Rel(rootpath, path)
if relerr != nil || relative == "." {
return relerr
}
if info.IsDir() {
actuelsd = append(actuelsd, relative)
return watcher.Add(path)
}
actuels = append(actuels, relative)
return err
}
func timedLoop(config *Config) {
toUpload = prune(toUpload)
toUploadd = prune(toUploadd)
toDelete = prune(toDelete)
toUploadd = remove(toUploadd, ".")
// toUpload, toDelete = removeIntersec(toUpload, toDelete)
// toUploadd, toDelete = removeIntersec(toUploadd, toDelete)
if len(toUploadd) > 0 || len(toUpload) > 0 || len(toDelete) > 0 {
log.Info("")
log.Infof("Dirs to upload [%v]", toUploadd)
log.Infof("Files to upload [%v]", toUpload)
log.Infof("Files to delete [%v]", toDelete)
log.Info("")
err := Upload(config, toUpload, toDelete, toUploadd)
if err != nil {
log.Fatalf("Upload() returned [%v]", err)
}
}
toUploadd = nil
toUpload = nil
toDelete = nil
}