-
Notifications
You must be signed in to change notification settings - Fork 23
/
sync.go
300 lines (263 loc) · 9.22 KB
/
sync.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package cmd
import (
"fmt"
"os"
"strings"
"sync"
"github.com/0chain/gosdk/zboxcore/fileref"
"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/0chain/zboxcli/util"
"github.com/spf13/cobra"
)
func printTable(files []sdk.FileDiff) {
header := []string{"Operation", "Path"}
data := make([][]string, len(files))
for idx, child := range files {
data[idx] = []string{child.Op, child.Path}
}
util.WriteTable(os.Stdout, header, []string{}, data)
fmt.Println("")
}
func saveCache(allocationObj *sdk.Allocation, path string, exclPath []string) {
if len(path) > 0 {
err := allocationObj.SaveRemoteSnapshot(path, exclPath)
if err != nil {
PrintError("Failed to save local cache.", err)
os.Exit(1)
}
fmt.Println("Local cache saved.")
}
}
func filterOperations(lDiff []sdk.FileDiff) (filterDiff []sdk.FileDiff, exclPath []string) {
for _, f := range lDiff {
if f.Op == sdk.Update || f.Op == sdk.Upload {
filterDiff = append(filterDiff, f)
} else {
exclPath = append(exclPath, f.Path)
}
}
return
}
func commitDiff(lDiff []sdk.FileDiff, allocationObj *sdk.Allocation, fileMetas map[string]*sdk.ConsolidatedFileMeta) {
wg := &sync.WaitGroup{}
statusBar := &StatusBar{wg: wg}
for _, f := range lDiff {
switch f.Op {
case sdk.Upload:
wg.Add(1)
commitMetaTxn(f.Path, "Upload", "", "", allocationObj, nil, statusBar)
case sdk.Update:
wg.Add(1)
commitMetaTxn(f.Path, "Update", "", "", allocationObj, nil, statusBar)
case sdk.Download:
wg.Add(1)
commitMetaTxn(f.Path, "Download", "", "", allocationObj, nil, statusBar)
case sdk.Delete:
fileMeta, ok := fileMetas[f.Path]
if !ok {
PrintError("Unable to commit metaData for :", f.Path)
break
}
wg.Add(1)
commitMetaTxn(f.Path, "Delete", "", "", allocationObj, fileMeta, statusBar)
}
}
statusBar.wg.Wait()
}
// syncCmd represents sync command
var syncCmd = &cobra.Command{
Use: "sync",
Short: "Sync files to/from blobbers",
Long: `Sync all files to/from blobbers from/to a localpath`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
fflags := cmd.Flags() // fflags is a *flag.FlagSet
if fflags.Changed("localpath") == false {
PrintError("Error: localpath flag is missing")
os.Exit(1)
}
localpath := cmd.Flag("localpath").Value.String()
if len(localpath) == 0 {
PrintError("Error: localpath flag is missing")
os.Exit(1)
}
encryptpath := cmd.Flag("encryptpath").Value.String()
if fflags.Changed("allocation") == false { // check if the flag "path" is set
PrintError("Error: allocation flag is missing") // If not, we'll let the user know
os.Exit(1) // and os.Exit(1)
}
allocationID := cmd.Flag("allocation").Value.String()
localcache := ""
if fflags.Changed("localcache") {
localcache = cmd.Flag("localcache").Value.String()
}
exclPath := []string{}
if fflags.Changed("excludepath") {
exclPath, _ = cmd.Flags().GetStringArray("excludepath")
}
allocationObj, err := sdk.GetAllocation(allocationID)
if err != nil {
PrintError("Error fetching the allocation", err)
os.Exit(1)
}
fileMetas := make(map[string]*sdk.ConsolidatedFileMeta)
wg := &sync.WaitGroup{}
statusBar := &StatusBar{wg: wg}
// Create filter
filter := []string{".DS_Store", ".git"}
uploadOnly, _ := cmd.Flags().GetBool("uploadonly")
commit, _ := cmd.Flags().GetBool("commit")
chunkSize, _ := cmd.Flags().GetInt("chunksize")
lDiff, err := allocationObj.GetAllocationDiff(localcache, localpath, filter, exclPath)
if err != nil {
PrintError("Error getting diff.", err)
os.Exit(1)
}
if uploadOnly {
var otherPaths []string
lDiff, otherPaths = filterOperations(lDiff)
exclPath = append(exclPath, otherPaths...)
}
if len(lDiff) > 0 {
printTable(lDiff)
} else {
fmt.Println("Already up to date")
saveCache(allocationObj, localcache, exclPath)
return
}
for _, f := range lDiff {
localpath = strings.TrimRight(localpath, "/")
lPath := localpath + f.Path
switch f.Op {
case sdk.Download:
wg.Add(1)
err = allocationObj.DownloadFile(lPath, f.Path, statusBar)
case sdk.Upload:
wg.Add(1)
var attrs fileref.Attributes
encrypt := len(encryptpath) != 0 && strings.Contains(lPath, encryptpath)
err = startChunkedUpload(cmd, allocationObj, lPath, "", f.Path, encrypt, chunkSize, attrs, statusBar, false)
// if len(encryptpath) != 0 && strings.Contains(lPath, encryptpath) {
// err = allocationObj.EncryptAndUploadFile(lPath, f.Path, attrs, statusBar)
// } else {
// err = allocationObj.UploadFile(lPath, f.Path, attrs, statusBar)
// }
case sdk.Update:
wg.Add(1)
encrypt := len(encryptpath) != 0 && strings.Contains(lPath, encryptpath)
err = startChunkedUpload(cmd, allocationObj, lPath, "", f.Path, encrypt, chunkSize, f.Attributes, statusBar, true)
// if len(encryptpath) != 0 && strings.Contains(lPath, encryptpath) {
// err = allocationObj.EncryptAndUpdateFile(lPath, f.Path,
// getRemoteFileAttributes(allocationObj, f.Path),
// statusBar)
// } else {
// err = allocationObj.UpdateFile(lPath, f.Path,
// getRemoteFileAttributes(allocationObj, f.Path),
// statusBar)
// }
case sdk.Delete:
fileMeta, err := allocationObj.GetFileMeta(f.Path)
if err != nil {
PrintError("Error fetching metaData :", err.Error())
}
fileMetas[f.Path] = fileMeta
// TODO: User confirm??
fmt.Printf("Deleting remote %s...\n", f.Path)
err = allocationObj.DeleteFile(f.Path)
if err != nil {
PrintError("Error deleting remote file,", err.Error())
}
continue
case sdk.LocalDelete:
// TODO: User confirm??
fmt.Printf("Deleting local %s...\n", lPath)
err = os.RemoveAll(lPath)
if err != nil {
PrintError("Error deleting local file.", err.Error())
}
continue
}
if err == nil {
//wg.Wait()
} else {
PrintError(err.Error())
}
}
if commit {
commitDiff(lDiff, allocationObj, fileMetas)
}
fmt.Println("\nSync Complete")
saveCache(allocationObj, localcache, exclPath)
return
},
}
// The getUploadCostCmd returns value in tokens to upload a file.
var getDiffCmd = &cobra.Command{
Use: "get-diff",
Short: "Get difference of local and allocation root",
Long: `Get difference of local and allocation root`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
fflags := cmd.Flags() // fflags is a *flag.FlagSet
if fflags.Changed("localpath") == false {
PrintError("Error: localpath flag is missing")
os.Exit(1)
}
localpath := cmd.Flag("localpath").Value.String()
if len(localpath) == 0 {
PrintError("Error: localpath flag is missing")
os.Exit(1)
}
if fflags.Changed("allocation") == false { // check if the flag "path" is set
PrintError("Error: allocation flag is missing") // If not, we'll let the user know
os.Exit(1) // and os.Exit(1)
}
allocationID := cmd.Flag("allocation").Value.String()
localcache := ""
if fflags.Changed("localcache") {
localcache = cmd.Flag("localcache").Value.String()
}
exclPath := []string{}
if fflags.Changed("excludepath") {
exclPath, _ = cmd.Flags().GetStringArray("excludepath")
}
allocationObj, err := sdk.GetAllocation(allocationID)
if err != nil {
PrintError("Error fetching the allocation", err)
os.Exit(1)
}
// Create filter
filter := []string{".DS_Store", ".git"}
lDiff, err := allocationObj.GetAllocationDiff(localcache, localpath, filter, exclPath)
if err != nil {
PrintError("Error getting diff.", err)
os.Exit(1)
}
util.PrintJSON(lDiff)
},
}
// statsCmd.Flags().Bool("json", false, "pass this option to print response as json data")
func init() {
rootCmd.AddCommand(syncCmd)
rootCmd.AddCommand(getDiffCmd)
syncCmd.PersistentFlags().String("allocation", "", "Allocation ID")
syncCmd.PersistentFlags().String("localpath", "", "Local dir path to sync")
syncCmd.PersistentFlags().String("encryptpath", "", "Local dir path to upload as encrypted")
syncCmd.PersistentFlags().String("localcache", "", `Local cache of remote snapshot.
If file exists, this will be used for comparison with remote.
After sync complete, remote snapshot will be updated to the same file for next use.`)
syncCmd.PersistentFlags().StringArray("excludepath", []string{}, "Remote folder paths exclude to sync")
syncCmd.MarkFlagRequired("allocation")
syncCmd.MarkFlagRequired("localpath")
syncCmd.Flags().Bool("uploadonly", false, "pass this option to only upload/update the files")
syncCmd.Flags().Bool("commit", false, "pass this option to commit the metadata transaction - only works with uploadonly")
syncCmd.Flags().Int("chunksize", sdk.CHUNK_SIZE, "chunk size")
getDiffCmd.PersistentFlags().String("allocation", "", "Allocation ID")
getDiffCmd.PersistentFlags().String("localpath", "", "Local dir path to sync")
getDiffCmd.PersistentFlags().String("localcache", "", `Local cache of remote snapshot.
If file exists, this will be used for comparison with remote.
After sync complete, remote snapshot will be updated to the same file for next use.`)
getDiffCmd.PersistentFlags().StringArray("excludepath", []string{}, "Remote folder paths exclude to sync")
getDiffCmd.MarkFlagRequired("allocation")
getDiffCmd.MarkFlagRequired("localpath")
}