/
sync.go
449 lines (409 loc) · 14.2 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
// Copyright 2019 Huawei Technologies Co.,Ltd.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
package command
import (
"assist"
"command/i18n"
"os"
"path/filepath"
"progress"
"strings"
)
type syncCommand struct {
transferCommand
del bool
}
func (c *syncCommand) doUpload(url1, url2 string) error {
url1, err := filepath.Abs(url1)
if err != nil {
printError(err)
return assist.ErrInvalidArgs
}
stat, err := os.Lstat(url1)
if err != nil {
printError(err)
return assist.ErrFileNotFound
}
linkFolder := false
relativeFolder := ""
if stat.Mode()&os.ModeSymlink == os.ModeSymlink {
if c.link {
_url1, _stat, _err := assist.GetRealPath(url1)
if _err != nil {
printError(_err)
return assist.ErrFileNotFound
}
if _stat.IsDir() {
if !c.flat {
relativeFolder = c.getRelativeFolder(url1)
}
linkFolder = true
}
url1 = _url1
stat = _stat
} else {
if _stat, _err := os.Stat(url1); _err == nil && _stat.IsDir() {
stat = _stat
}
}
}
aclType, storageClassType, metadata, succeed := c.checkParams()
if !succeed {
return assist.ErrInvalidArgs
}
bucket, keyOrDir, err := c.splitCloudUrl(url2)
if err != nil {
printError(err)
return assist.ErrInvalidArgs
}
if checkEmptyFolder(bucket, keyOrDir, um) {
printf("Error: Cannot upload to [%s], the url contains empty folder", url2)
return assist.ErrInvalidArgs
}
if c.verifyMd5 {
c.checkBucketVersion(bucket)
}
c.printStart()
c.ensureParentFolder(bucket, keyOrDir)
if !stat.IsDir() {
key := keyOrDir
if key == "" || isObsFolder(key) {
key += stat.Name()
}
arcPath := ""
if c.arcDir != "" {
arcPath = c.arcDir + "/" + stat.Name()
}
if c.forceRecord {
return c.ensureBucketsAndStartAction([]string{bucket}, func() error {
c.printParams(true, true, true, false)
c.recordStartFuncForUpload()
ret := c.uploadFile(bucket, key, arcPath, url1, stat, metadata, aclType, storageClassType, nil, nil, 1, nil)
if ret >= 1 {
progress.AddSucceedCount(1)
return nil
}
progress.AddFailedCount(1)
return assist.ErrExecuting
}, true)
}
c.printParams(false, true, true, false)
ret := c.uploadFile(bucket, key, arcPath, url1, stat, metadata, aclType, storageClassType, nil, nil, 0, nil)
if warn, ok := c.warn.Load().(error); ok {
printWarn(warn)
}
if ret == 0 {
return assist.ErrExecuting
}
return nil
}
return c.ensureBucketsAndStartAction([]string{bucket}, func() error {
dir := keyOrDir
c.printParams(true, true, true, false)
doLog(LEVEL_INFO, "Upload objects from local folder [%s] to cloud folder [%s] in the bucket [%s]", url1, dir, bucket)
arcDir := c.arcDir
if linkFolder {
if dir != "" && !isObsFolder(dir) {
dir = dir + "/"
}
dir += relativeFolder
if arcDir != "" {
if !isObsFolder(arcDir) {
arcDir = arcDir + "/"
}
arcDir += relativeFolder
}
}
if c.del {
//TODO delete
}
return c.uploadDir(bucket, dir, arcDir, url1, linkFolder, stat, metadata, aclType, storageClassType)
}, false)
}
func (c *syncCommand) doDownload(url1, url2 string) error {
url2, err := filepath.Abs(url2)
if err != nil {
printError(err)
return assist.ErrInvalidArgs
}
bucket, keyOrDir, err := c.splitCloudUrl(url1)
if err != nil {
printError(err)
return assist.ErrInvalidArgs
}
if checkEmptyFolder(bucket, keyOrDir, dm) {
printf("Error: Cannot download from [%s], the url contains empty folder", url1)
return assist.ErrInvalidArgs
}
if _err := c.ensureBucket(bucket); _err != nil {
printError(_err)
doLog(LEVEL_ERROR, _err.Error())
return assist.ErrCheckBucketStatus
}
c.printStart()
stat, err := os.Lstat(url2)
if err != nil {
if err = assist.MkdirAll(url2, os.ModePerm); err != nil {
printError(err)
return assist.ErrInvalidArgs
}
stat, err = os.Lstat(url2)
if err != nil {
printError(err)
return assist.ErrFileNotFound
}
} else if !stat.IsDir() {
printf("Error: Cannot download to the folder [%s] due to a file with the same name exits!", url2)
return assist.ErrInvalidArgs
}
dir := keyOrDir
if _err := c.ensureOutputDirectory(); _err != nil {
printError(_err)
return assist.ErrInvalidArgs
}
c.printParams(true, true, false, true)
if _err := c.startLogger(true); _err != nil {
printError(_err)
return assist.ErrInvalidArgs
}
doLog(LEVEL_INFO, "Download objects from cloud folder [%s] in the bucket [%s] to local folder [%s] ", dir, bucket, url2)
defer c.endLogger()
if c.del {
//TODO delete
}
return c.downloadDir(bucket, dir, url2, stat)
}
func (c *syncCommand) doCopy(url1, url2 string) error {
if c.needCheckNestedUrl() && url1 == url2 {
printf("The source url and the destination url are same!")
return assist.ErrInvalidArgs
}
aclType, storageClassType, metadata, succeed := c.checkParams()
if !succeed {
return assist.ErrInvalidArgs
}
srcBucket, srcKeyOrDir, err := c.splitCloudUrl(url1)
if err != nil {
printError(err)
return assist.ErrInvalidArgs
}
dstBucket, dstKeyOrDir, err := c.splitCloudUrl(url2)
if err != nil {
printError(err)
return assist.ErrInvalidArgs
}
if checkEmptyFolder(srcBucket, srcKeyOrDir, cm) {
printf("Error: Cannot copy from [%s], the url contains empty folder", url1)
return assist.ErrInvalidArgs
}
if checkEmptyFolder(dstBucket, dstKeyOrDir, cm) {
printf("Error: Cannot copy to [%s], the url contains empty folder", url2)
return assist.ErrInvalidArgs
}
if c.crr {
if !c.createObsClientCrr() {
return assist.ErrInitializing
}
if c.verifyMd5 {
c.checkBucketVersion(dstBucket)
}
}
if c.needCheckNestedUrl() {
if dstBucket == srcBucket {
if index := strings.Index(url2, url1); index >= 0 {
printf("The source url and the destination url are nested")
return assist.ErrInvalidArgs
}
if index := strings.Index(url1, url2); index >= 0 {
printf("The source url and the destination url are nested")
return assist.ErrInvalidArgs
}
}
}
c.printStart()
c.ensureParentFolder(dstBucket, dstKeyOrDir)
if c.crr {
return c.ensureBucketsAndStartActionCrr(srcBucket, dstBucket, func() error {
c.printParams(true, true, false, false)
srcDir := srcKeyOrDir
dstDir := dstKeyOrDir
doLog(LEVEL_INFO, "Copy objects from cloud folder [%s] in the bucket [%s] to cloud folder [%s] in the bucket [%s] ",
srcDir, srcBucket, dstDir, dstBucket)
if c.del {
//TODO delete
}
return c.copyDir(srcBucket, srcDir, dstBucket, dstDir, metadata, aclType, storageClassType)
}, false)
}
if succeed := c.compareLocation(srcBucket, dstBucket); !succeed {
return assist.ErrInvalidArgs
}
return c.ensureBucketsAndStartAction([]string{srcBucket, dstBucket}, func() error {
c.printParams(true, false, false, false)
srcDir := srcKeyOrDir
dstDir := dstKeyOrDir
doLog(LEVEL_INFO, "Copy objects from cloud folder [%s] in the bucket [%s] to cloud folder [%s] in the bucket [%s] ",
srcDir, srcBucket, dstDir, dstBucket)
if c.del {
//TODO delete
}
return c.copyDir(srcBucket, srcDir, dstBucket, dstDir, metadata, aclType, storageClassType)
}, false)
}
func initSync() command {
c := &syncCommand{}
c.key = "sync"
c.usage = []string{
"file_url cloud_url [options...]",
"cloud_url file_url [options...]",
"cloud_url cloud_url [options...]",
}
c.description = "synchronize objects from the source to the destination"
c.force = true
c.recursive = true
c.flat = true
c.update = true
c.define = func() {
c.init()
c.defineBasic()
c.flagSet.BoolVar(&c.del, "delete", false, "")
}
c.action = func() error {
args := c.flagSet.Args()
if len(args) == 0 {
c.showHelp()
printf("Error: Invalid args, please refer to help doc")
return assist.ErrInvalidArgs
}
url1, url2, mode, err := c.prepareUrls(args)
if err != nil {
printError(err)
return assist.ErrInvalidArgs
}
if !c.prepareOptions() {
return assist.ErrInvalidArgs
}
var ret error
if mode == um {
ret = c.doUpload(url1, url2)
} else if mode == dm {
ret = c.doDownload(url1, url2)
} else if mode == cm {
//TODO
if !c.crr {
if c.jobs > 10 {
printf("Error: The max jobs for copy is 10")
return assist.ErrInvalidArgs
}
if c.parallel > 10 {
printf("Error: The max parallel for copy is 10")
return assist.ErrInvalidArgs
}
}
ret = c.doCopy(url1, url2)
}
return ret
}
c.help = func() {
p := i18n.GetCurrentPrinter()
p.Printf("Summary:")
printf("%2s%s", "", p.Sprintf("synchronize objects from the source to the destination"))
printf("")
p.Printf("Syntax 1:")
printf("%2s%s", "", "obsutil sync file_url obs://bucket[/key] [-dryRun] [-link] [-vlength] [-vmd5] [-p=1] [-threshold=52428800] [-acl=xxx] [-sc=xxx] [-meta=aaa:bbb#ccc:ddd] [-ps=auto] [-cpd=xxx] [-fr] [-arcDir=xxx] [-o=xxx] [-config=xxx]"+commandCommonSyntax())
printf("")
p.Printf("Syntax 2:")
printf("%2s%s", "", "obsutil sync folder_url obs://bucket[/prefix] [-dryRun] [-link] [-vlength] [-vmd5] [-j=1] [-p=1] [-threshold=52428800] [-acl=xxx] [-sc=xxx] [-meta=aaa:bbb#ccc:ddd] [-ps=auto] [-include=*.xxx] [-exclude=*.xxx] [-timeRange=time1-time2] [-at] [-mf] [-arcDir=xxx] [-o=xxx] [-cpd=xxx] [-config=xxx]"+commandCommonSyntax())
printf("")
p.Printf("Syntax 3:")
printf("%2s%s", "", "obsutil sync obs://bucket[/prefix] folder_url [-dryRun] [-tempFileDir=xxx] [-vlength] [-vmd5] [-j=1] [-p=1] [-threshold=52428800] [-ps=auto] [-include=*.xxx] [-exclude=*.xxx] [-timeRange=time1-time2] [-mf] [-o=xxx] [-cpd=xxx] [-config=xxx]"+commandCommonSyntax())
printf("")
p.Printf("Syntax 4:")
printf("%2s%s", "", "obsutil sync obs://srcbucket[/src_prefix] obs://dstbucket[/dest_prefix] [-dryRun] [-crr] [-vlength] [-vmd5] [-j=1] [-p=1] [-threshold=52428800] [-acl=xxx] [-sc=xxx] [-meta=aaa:bbb#ccc:ddd] [-ps=auto] [-include=*.xxx] [-exclude=*.xxx] [-timeRange=time1-time2] [-mf] [-o=xxx] [-cpd=xxx] [-config=xxx]"+commandCommonSyntax())
printf("")
p.Printf("Options:")
printf("%2s%s", "", "-dryRun")
printf("%4s%s", "", p.Sprintf("conduct a dry run"))
printf("")
printf("%2s%s", "", "-tempFileDir=xxx")
printf("%4s%s", "", p.Sprintf("the temp file dir, used to save temporary files during the objects are downloading"))
printf("")
printf("%2s%s", "", "-link")
printf("%4s%s", "", p.Sprintf("upload the actual path of the symbolic-link file/folder"))
printf("")
// printf("")
// printf("%2s%s", "", "-delete")
// printf("%4s%s", "", "delete objects inconsistent with those on the source from the destination")
printf("%2s%s", "", "-fr")
printf("%4s%s", "", p.Sprintf("force to generate the record files"))
printf("")
printf("%2s%s", "", "-crr")
printf("%4s%s", "", p.Sprintf("copy the source object(s) through the client-side cross region replication mode"))
printf("")
printf("%2s%s", "", "-vlength")
printf("%4s%s", "", p.Sprintf("verify the size after the objects are uploaded or downloaded or copied through crr"))
printf("")
printf("%2s%s", "", "-vmd5")
printf("%4s%s", "", p.Sprintf("verify the MD5 value after the objects are uploaded or downloaded or copied through crr"))
printf("")
printf("%2s%s", "", "-cpd=xxx")
printf("%4s%s", "", p.Sprintf("the directory where the part records reside, used to record the synchronization progress"))
printf("")
printf("%2s%s", "", "-j=1")
printf("%4s%s", "", p.Sprintf("the maximum number of concurrent synchronization jobs, the default value can be set in the config file"))
printf("")
printf("%2s%s", "", "-p=1")
printf("%4s%s", "", p.Sprintf("the maximum number of concurrent synchronization tasks (a task is a sub-job), the default value can be set in the config file"))
printf("")
printf("%2s%s", "", "-ps=auto")
printf("%4s%s", "", p.Sprintf("the part size of each synchronization task, the default value can be set in the config file"))
printf("")
printf("%2s%s", "", "-threshold=52428800")
printf("%4s%s", "", p.Sprintf("the threshold, if it is exceeded, the synchronization job will be divided into multiple tasks by the part size, the default value can be set in the config file"))
printf("")
printf("%2s%s", "", "-meta=aaa:bbb#ccc:ddd")
printf("%4s%s", "", p.Sprintf("the customized metadata of each object to be uploaded or copied"))
printf("")
printf("%2s%s", "", "-acl=xxx")
printf("%4s%s", "", p.Sprintf("the ACL of each object to be uploaded or copied, possible values are [private|public-read|public-read-write|bucket-owner-full-control]"))
printf("")
printf("%2s%s", "", "-sc=xxx")
printf("%4s%s", "", p.Sprintf("the storage class of each object to be uploaded or copied, possible values are [standard|warm|cold]"))
printf("")
printf("%2s%s", "", "-include=*.xxx")
printf("%4s%s", "", p.Sprintf("the objects whose names match this pattern will be included"))
printf("")
printf("%2s%s", "", "-exclude=*.xxx")
printf("%4s%s", "", p.Sprintf("the objects whose names match this pattern will be excluded"))
printf("")
printf("%2s%s", "", "-at")
printf("%4s%s", "", p.Sprintf("the files whose latest access time falls into the time range (-timeRange option) will be uploaded"))
printf("")
printf("%2s%s", "", "-timeRange=time1-time2")
printf("%4s%s", "", p.Sprintf("the time range for last modified time, between which the objects will be uploaded, downloaded or copied"))
printf("")
printf("%2s%s", "", "-mf")
printf("%4s%s", "", p.Sprintf("the including pattern, the excluding pattern and the time range pattern will task effect on folders"))
printf("")
printf("%2s%s", "", "-arcDir=xxx")
printf("%4s%s", "", p.Sprintf("the archive dir, used to archive the successful uploaded file(s)"))
printf("")
printf("%2s%s", "", "-o=xxx")
printf("%4s%s", "", p.Sprintf("the output dir, used to record the synchronization results"))
printf("")
printf("%2s%s", "", "-config=xxx")
printf("%4s%s", "", p.Sprintf("the path to the custom config file when running this command"))
printf("")
commandCommonHelp(p)
}
return c
}