-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssync.go
222 lines (187 loc) · 4.81 KB
/
ssync.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
package main
import (
"os"
"io/ioutil"
"fmt"
"strings"
"sort"
"log"
"path/filepath"
)
type Args struct {
Label string
Paths []string
In []string
Out []string
}
// main package entry
func main() {
args, cont := processFlags()
if !cont {
os.Exit(1)
}
if err := exec(args, os.Stdin); err != nil {
log.Fatalf("%v", err)
}
}
// main process
func exec(args []string, in *os.File) error {
a := &Args{ Label: args[0], Paths: []string{args[1], args[2]},
In: make([]string, 0), Out: make([]string, 0) }
// check paths exist and are directories
for x := range a.Paths {
p, err := checkDir(a.Paths[x])
if err != nil {
return err
}
a.Paths[x] = p
}
fmt.Printf("\nLabel: %s\nPath1: %s\nPath2: %s\n",
a.Label, a.Paths[0], a.Paths[1])
err := a.loadState()
if err != nil {
return err
}
err = a.process(in)
if err != nil {
return err
}
err = a.saveState()
if err != nil {
return err
}
fmt.Printf("\nssync finished.\n")
return nil
}
// load previous state
func (a *Args) loadState() error {
l := ".ssync-" + a.Label
s1, _ := stringSliceFromFile(filepath.Join(a.Paths[0], l))
s2, _ := stringSliceFromFile(filepath.Join(a.Paths[1], l))
// ensure states are equal
// error indicates accidental overwrite with another sync
if strings.Join(s1, "\n") != strings.Join(s2, "\n") {
return fmt.Errorf("shared state unequal")
}
a.In = s1
return nil
}
// delete, copy new, update
func (a *Args) process(in *os.File) error {
newList := make([][]string, 2)
for i := range a.Paths {
paths, err := stringSliceFromPathWalk(a.Paths[i])
if err != nil {
return err
}
fmt.Printf("\nProcessing: %v\n", a.Paths[i])
// handle deleted files
if len(a.In) > 0 {
deleteList := notIn(a.In, paths)
if len(deleteList) > 0 {
del := true
if flagConfirm {
// ask to confirm deleted
del = deleteConfirm(deleteList, a.Paths[1^i], in)
}
if del {
delete(deleteList, a.Paths[1^i])
} else {
// skip delete (add to a.Out to ask to confirm delete next time)
a.Out = append(a.Out, notIn(a.Out, deleteList)...)
}
}
}
// save new files
newList[i] = notIn(paths, a.In)
}
// update common files if one is more recently updated
if len(a.In) > 0 {
fmt.Printf("\nUpdate modified:\n")
for i := range a.In {
src, dest, found := mostRecentlyModified(a.In[i], a.Paths[0], a.Paths[1])
if found && len(src) > 0 && len(dest) > 0 {
err := copyFile(a.In[i], src, dest)
if err != nil {
return err
}
}
}
}
// rename folder where new folder exists on both sides
newList, err := a.commonFolders(newList)
if err != nil {
return err
}
// copy new files
for i := range newList {
if len(newList[i]) > 0 {
fmt.Printf("\nCopy new files to: %v\n", a.Paths[1^i])
err := copyAll(newList[i], a.Paths[i], a.Paths[1^i])
if err != nil {
return err
}
}
}
return nil
}
// renames folder to (1) in the instance where new folder exists on both sides
func (a *Args) commonFolders(files [][]string) ([][]string, error) {
m := make(map[string]bool)
for i := range files[0] {
_, err := checkDir(filepath.Join(a.Paths[0], files[0][i]))
if err == nil {
m[files[0][i]] = true
}
}
var prev, prevRep string
for i := range files[1] {
if prev != "" && prevRep != "" {
// replace new folder name if exists in path
files[1][i] = strings.Replace(files[1][i], prev, prevRep, 1)
}
f := files[1][i]
_, err := checkDir(filepath.Join(a.Paths[1], f))
if err != nil {
continue
}
_, ok := m[f]
if ok && (prev == "" || strings.Index(f, prev) != 0) {
// if a folder and not part of previous path
// rename folder appending (X), incrementing X until does not exist
fp := filepath.Join(a.Paths[1], f)
newFolder, err := RenameFolder(fp, fp)
if err != nil {
return files, err
}
// update file name in slice
files[1][i] = strings.Replace(newFolder, a.Paths[1], "", 1)[1:]
// save updated name to replace within subpaths
prev = f
prevRep = files[1][i]
}
}
return files, nil
}
// save shared state to file
func (a *Args) saveState() error {
// append to a.Out
for i := range a.Paths {
paths, err := stringSliceFromPathWalk(a.Paths[i])
if err != nil {
return err
}
a.Out = append(a.Out, notIn(paths, a.Out)...)
sort.Strings(a.Out)
}
// write a.Out state to file
l := ".ssync-" + a.Label
d1 := []byte(strings.Join(a.Out, "\n")+"\n")
for i := range a.Paths {
err := ioutil.WriteFile(filepath.Join(a.Paths[i], l), d1, 0644)
if err != nil {
return err
}
}
return nil
}