-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch.go
141 lines (116 loc) · 2.89 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
package main
import (
"b2backup/utils"
"fmt"
"io/ioutil"
"path/filepath"
"strings"
"github.com/fsnotify/fsnotify"
)
func watchDirectory(path *PathDetails) {
//apt-get install inotify-tools
watcher, err := fsnotify.NewWatcher()
if err != nil {
fmt.Println(err.Error())
}
defer watcher.Close()
done := make(chan bool)
go func() {
for run {
select {
case event, ok := <-watcher.Events:
if !ok {
fmt.Println("Watcher not ok (1)")
return
}
switch event.Op.String() {
case "WRITE": //CREATE,CHMOD
fmt.Println(event.Op.String() + " " + event.Name)
go b2NewFile(path, event.Name)
case "REMOVE", "RENAME":
fmt.Println(event.Op.String() + " " + event.Name)
go b2RemoveFile(path, event.Name)
}
case err, ok := <-watcher.Errors:
if !ok {
fmt.Println("Watcher not ok (2)")
return
}
fmt.Println("Error: ", err)
}
}
}()
//TODO: rescan every x mins
list := listDirectories(path.Source)
for _, folder := range list {
err = watcher.Add(folder)
if err != nil {
fmt.Println("There was an error startin up a path watcher: " + err.Error())
} else {
fmt.Println("Folder added: " + folder)
}
}
<-done
}
func listDirectories(path string) []string {
var list []string
list = append(list, path)
//Recursively scan folders
files, err := ioutil.ReadDir(path)
if err != nil {
fmt.Println("Error listing directory: " + err.Error())
}
for _, f := range files {
// Skip Hidden
if strings.HasPrefix(f.Name(), ".") {
continue
}
//Add its children
if f.IsDir() {
list2 := listDirectories(path + "/" + f.Name())
for _, folder := range list2 {
list = append(list, folder)
}
}
}
return list
}
//TODO: prevent overlapping uploads of the same file (ctrl+s trigger happy person), prolly some queue with a 5 second wait
func b2NewFile(path *PathDetails, file string) {
_, fileName := filepath.Split(file)
// Skip Hidden
if strings.HasPrefix(fileName, ".") {
return
}
sha1, errSha := utils.FileSha1(file)
if errSha != nil {
fmt.Println(errSha)
return
}
folderpath := strings.Replace(file, path.Source, "", 1)
// Check if the file exists with the same signature
for i := range path.Signatures {
if path.Signatures[i].Path == folderpath && path.Signatures[i].Hash == sha1 {
// Nothing about this file changed, lets go
return
}
}
// Prep the details
fileDetails := FileDetails{
B2Path: path.Destination + folderpath,
Compressed: false,
Hash: sha1,
Path: folderpath,
}
// Begin the upload
if err := path.uploadFile(path.B2Bucket, file, strings.Replace(fileDetails.B2Path, "/", "", 1)); err != nil {
fmt.Println("Something went wrong uploading file(" + file + "): " + err.Error())
return
}
// Lets add it
path.Signatures = append(path.Signatures, fileDetails)
saveSettings(*path)
}
func b2RemoveFile(path *PathDetails, file string) {
//TODO: all of this
}