-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilesync.go
184 lines (158 loc) · 4.12 KB
/
filesync.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
package esc
import (
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"time"
)
var fileIn chan *File
var fileOut chan *File
var fileRm chan string
var newNode chan string
func startDirSync() {
newNode = make(chan string, 4)
fileIn = make(chan *File, 4)
fileOut = make(chan *File, 4)
fileRm = make(chan string, 4)
On("*", "fileSync", onFileChanged)
On("*", "fileRemoved", onFileRemoved)
On("*", "connected", onNewNode)
if config.Directory != "" {
DirSync(config.Directory)
}
}
func onNewNode(message *Message) {
if message.Payload == Self() {
return
}
log.Println("Sending files cause of new node", message.Payload)
newNode <- message.Payload
}
func onFileRemoved(message *Message) {
if message.From == Self() {
return
}
fileRm <- message.Payload
}
func onFileChanged(message *Message) {
parts := strings.SplitN(message.Payload, ",", 3)
fileName := parts[0]
time := time.Now()
time.UnmarshalText([]byte(parts[1]))
content, _ := base64.StdEncoding.DecodeString(parts[2])
hasher := sha256.New()
hasher.Write(content)
hash := hex.EncodeToString(hasher.Sum(nil))
file := File{
Name: fileName,
Content: content,
Time: time,
Hash: hash,
}
fileIn <- &file
}
func ScanDir(registeredFiles map[string]*File, got map[string]bool, dirName string) {
dir, _ := os.Open(dirName)
files, _ := dir.Readdir(0)
for _, fileInfo := range files {
singleName := fileInfo.Name()
fileName := fmt.Sprintf("%s/%s", dirName, singleName)
if strings.HasPrefix(singleName, ".") || strings.HasSuffix(singleName, "~") {
// skip
} else if fileInfo.IsDir() {
ScanDir(registeredFiles, got, fileName)
} else {
got[fileName] = true
content, err := ioutil.ReadFile(fileName)
if err != nil {
log.Println(err)
continue
}
hasher := sha256.New()
hasher.Write(content)
hash := hex.EncodeToString(hasher.Sum(nil))
file := File{
Name: fileName,
Content: content,
Time: fileInfo.ModTime(),
Hash: hash,
}
if _, ok := registeredFiles[fileName]; !ok {
log.Println("Sending new file", fileName)
go func() {
fileOut <- &file
}()
} else if registeredFiles[fileName].Hash != hash {
log.Println("Sending changed file", fileName)
go func() {
fileOut <- &file
}()
}
registeredFiles[fileName] = &file
}
}
}
func DirSync(dirName string) {
registeredFiles := make(map[string]*File)
ticker := time.NewTicker(6 * time.Second)
go func() {
_, errStat := os.Lstat(dirName)
if errStat != nil {
log.Println("Not syncing", dirName, errStat)
return
}
for {
select {
case <-newNode:
log.Println("new node, sending files")
for _, file := range registeredFiles {
go func() {
fileOut <- file
}()
}
case file := <-fileIn:
log.Println("Got file", file.Name)
if _, ok := registeredFiles[file.Name]; !ok {
log.Println("New file", file.Name)
folder := file.Name[0:strings.LastIndex(file.Name, "/")]
os.MkdirAll(folder, 0755)
ioutil.WriteFile(file.Name, file.Content, 0755)
registeredFiles[file.Name] = file
} else if registeredFiles[file.Name].Hash != file.Hash {
log.Println("Changed file", file.Name)
if registeredFiles[file.Name].Time.Before(file.Time) {
log.Println("Their file is newer", file.Name)
ioutil.WriteFile(file.Name, file.Content, 0755)
registeredFiles[file.Name] = file
} else {
log.Println("Our", file.Name, "is older")
}
} else {
log.Println("Same file", file.Name)
}
case file := <-fileRm:
delete(registeredFiles, file)
os.Remove(file)
case file := <-fileOut:
log.Println("Sending out file", file.Name)
time, _ := file.Time.MarshalText()
SendC("*", "fileSync", fmt.Sprintf("%s,%s,%s", file.Name, time, base64.StdEncoding.EncodeToString(file.Content)), true)
case <-ticker.C:
got := make(map[string]bool)
ScanDir(registeredFiles, got, dirName)
for name, _ := range registeredFiles {
if _, ok := got[name]; !ok {
log.Println("Removed file", name)
Send("*", "fileRemoved", name)
fileRm <- name
}
}
}
}
}()
}