/
livesyncd.go
234 lines (176 loc) · 4.61 KB
/
livesyncd.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
package main
import (
"flag"
"github.com/howeyc/fsnotify"
"io/ioutil"
"log"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"
)
// Start forwarding events from the watcher's channel to the
// Sync Backend implementation. Quits when true is sent to the
// "quit" channel.
func startWatchLoop(sync chan *SyncEvent, quit chan bool, watcher *fsnotify.Watcher) {
go func() {
reIndexTick := time.NewTicker(60 * time.Second)
for {
select {
case ev := <-watcher.Event:
log.Printf("Received event: %+v", ev)
go func() {
if isExcluded(ev.Name) {
return
}
syncEv := &SyncEvent{Name: ev.Name, FileEvent: ev}
switch {
case ev.IsCreate(), ev.IsModify():
syncEv.Type = SYNC_PUT
// Delete the file, regardless if it was deleted or renamed.
// When it was renamed, then the uploading of the new file
// will already be catched by an CREATE event.
case ev.IsDelete(), ev.IsRename():
syncEv.Type = SYNC_DELETE
}
// Notify the sync backend.
sync <- syncEv
}()
case <-reIndexTick.C:
log.Println("Refreshing Index")
go refreshIndex(root, watcher, sync)
case err := <-watcher.Error:
log.Printf("Received error: %s", err)
case <-quit:
return
}
}
}()
}
// Adds all sub directories of the given directory to the watcher
// and returns the number of watched directories.
func addWatchesRecursive(dir string, watcher *fsnotify.Watcher) int {
var watched int
entries, err := ioutil.ReadDir(dir)
if err != nil {
log.Println(err)
return 0
}
watcher.Watch(dir)
index[dir] = true
watched++
for _, e := range entries {
name := e.Name()
if e.IsDir() && !strings.HasPrefix(name, ".") {
watched += addWatchesRecursive(filepath.Join(dir, name), watcher)
}
}
return watched
}
func refreshIndex(dir string, watcher *fsnotify.Watcher, sync chan *SyncEvent) {
entries, err := ioutil.ReadDir(dir)
if err != nil {
log.Println(err)
return
}
_, inIndex := index[dir]
if !inIndex {
watcher.Watch(dir)
}
index[dir] = true
for _, e := range entries {
name := e.Name()
if e.IsDir() && !strings.HasPrefix(name, ".") {
refreshIndex(filepath.Join(dir, name), watcher, sync)
// Schedule the file for syncing if the directory was not found
// within the index
} else if !inIndex {
sync <- &SyncEvent{Name: filepath.Join(dir, name), Type: SYNC_PUT}
}
}
}
func isExcluded(name string) bool {
for _, p := range config.Ignore {
rel, _ := filepath.Rel(root, name)
matched, err := filepath.Match(p, rel)
if err != nil {
log.Printf("Error in Pattern %q: %s", p, err)
}
if matched {
log.Printf("Ignore: %q matched pattern %q", name, p)
return true
}
}
return false
}
// Daemon Configuration
type Config struct {
RemoteHost string
RemoteRoot string
Ignore []string
}
var (
// Root of the directory to watch
root string
// Configuration
config Config
// Index of all watched directories (key: path)
index map[string]bool
)
func init() {
var ignore string
flag.StringVar(&config.RemoteHost, "remote-host", "", "Remote Host Name ([user@]host)")
flag.StringVar(&config.RemoteRoot, "remote-root", "", "Remote Root")
flag.StringVar(&ignore, "ignore", "", "Comma delimited set of ignore patterns")
flag.Parse()
if config.RemoteHost == "" {
log.Fatalln("Missing --remote-host")
}
if config.RemoteRoot == "" {
log.Fatalln("Missing --remote-root")
}
index = make(map[string]bool)
config.Ignore = strings.Split(ignore, ",")
root, _ = os.Getwd()
}
func main() {
log.Println("livesyncd running")
log.Println("Stop with [CTRL] + [c]")
log.Println("Ignore: ", config.Ignore)
rlimit := new(syscall.Rlimit)
syscall.Getrlimit(syscall.RLIMIT_NOFILE, rlimit)
rlimit.Cur = rlimit.Max
err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, rlimit)
if err != nil {
log.Panicf("Could not change Rlimit: %q", err)
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Panicln(err)
}
events := make(chan *SyncEvent)
quitSync := make(chan bool)
quitWatcher := make(chan bool)
sigInt := make(chan os.Signal)
signal.Notify(sigInt, os.Interrupt)
StartSFTPSync(events, quitSync, config)
startWatchLoop(events, quitWatcher, watcher)
watched := addWatchesRecursive(root, watcher)
log.Printf("Found %d directories to watch\n", watched)
select {
case <-sigInt:
log.Println("Stopping to watch...")
// Wait until the watcher has finished quitting
quitWatcher <- true
log.Println("Done")
// Close all file handles, opened by the watcher
watcher.Close()
log.Println("Stopping Sync Backend...")
quitSync <- true
<-quitSync
log.Println("Done")
return
}
}