forked from minio/mc
/
cast-url.go
377 lines (345 loc) · 13.3 KB
/
cast-url.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
/*
* Minio Client (C) 2015 Minio, Inc.
*
* 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 main
import (
"path/filepath"
"strings"
"github.com/minio/cli"
"github.com/minio/mc/pkg/client"
"github.com/minio/mc/pkg/console"
"github.com/minio/minio/pkg/iodine"
)
type castURLs struct {
SourceContent *client.Content
TargetContents []*client.Content
Error error `json:"-"`
}
type castURLsType uint8
const (
castURLsTypeInvalid castURLsType = iota
castURLsTypeA
castURLsTypeB
castURLsTypeC
)
// NOTE: All the parse rules should reduced to A: Cast(Source, []Target).
//
// * CAST ARGS - VALID CASES
// =========================
// A: cast(f, []f) -> cast(f, []f)
// B: cast(f, [](d | f)) -> cast(f, [](d/f | f)) -> A:
// C: cast(d1..., [](d2)) -> []cast(d1/f, [](d2/d1/f)) -> []A:
// checkCastSyntax(URLs []string)
func checkCastSyntax(ctx *cli.Context) {
if len(ctx.Args()) < 2 || ctx.Args().First() == "help" {
cli.ShowCommandHelpAndExit(ctx, "cast", 1) // last argument is exit code.
}
// extract URLs.
URLs, err := args2URLs(ctx.Args())
if err != nil {
console.Fatalf("One or more unknown URL types found %s. %s\n", ctx.Args(), NewIodine(iodine.New(err, nil)))
}
srcURL := URLs[0]
tgtURLs := URLs[1:]
/****** Generic rules *******/
// Source cannot be a folder (except when recursive)
if !isURLRecursive(srcURL) {
_, srcContent, err := url2Stat(srcURL)
// Source exist?.
if err != nil {
console.Fatalf("Unable to stat source ‘%s’. %s\n", srcURL, NewIodine(iodine.New(err, nil)))
}
if !srcContent.Type.IsRegular() {
if srcContent.Type.IsDir() {
console.Fatalf("Source ‘%s’ is a folder. Please use ‘%s...’ to recursively copy this folder and its contents.\n", srcURL, srcURL)
}
console.Fatalf("Source ‘%s’ is not a regular file.\n", srcURL)
}
}
// Recursive URLs are not allowed in target.
for _, tgtURL := range tgtURLs {
if isURLRecursive(tgtURL) {
console.Fatalf("Target ‘%s’ cannot be recursive. %s\n", tgtURL, NewIodine(iodine.New(errInvalidArgument{}, nil)))
}
}
for _, tgtURL := range tgtURLs {
url, err := client.Parse(tgtURL)
if err != nil {
console.Fatalf("Unable to parse target ‘%s’ argument. %s\n", tgtURL, NewIodine(iodine.New(err, nil)))
}
if url.Host != "" {
if url.Path == string(url.Separator) {
console.Fatalf("Bucket creation detected for %s, cloud storage URL's should use ‘mc mb’ to create buckets\n", tgtURL)
}
}
}
switch guessCastURLType(srcURL, tgtURLs) {
case castURLsTypeA: // File -> File.
checkCastSyntaxTypeA(srcURL, tgtURLs)
case castURLsTypeB: // File -> Folder.
checkCastSyntaxTypeB(srcURL, tgtURLs)
case castURLsTypeC: // Folder -> Folder.
checkCastSyntaxTypeC(srcURL, tgtURLs)
default:
console.Fatalln("Invalid arguments. Unable to determine how to cast. Please report this issue at https://github.com/minio/mc/issues")
}
}
func checkCastSyntaxTypeA(srcURL string, tgtURLs []string) {
if len(tgtURLs) == 0 && tgtURLs == nil {
console.Fatalf("Invalid number of target arguments to cast command. %s\n", NewIodine(iodine.New(errInvalidArgument{}, nil)))
}
_, srcContent, err := url2Stat(srcURL)
// Source exist?.
if err != nil {
console.Fatalf("Unable to stat source ‘%s’. %s\n", srcURL, NewIodine(iodine.New(err, nil)))
}
if srcContent.Type.IsDir() {
console.Fatalf("Source ‘%s’ is a folder. Use ‘%s...’ argument to cast this folder and its contents recursively. %s\n", srcURL, srcURL, NewIodine(iodine.New(errInvalidArgument{}, nil)))
}
if !srcContent.Type.IsRegular() {
console.Fatalf("Source ‘%s’ is not a file. %s\n", srcURL, NewIodine(iodine.New(errInvalidArgument{}, nil)))
}
for _, tgtURL := range tgtURLs {
_, tgtContent, err := url2Stat(tgtURL)
// Target exist?.
if err == nil {
if !tgtContent.Type.IsRegular() {
console.Fatalf("Target ‘%s’ is not a file. %s\n", tgtURL, NewIodine(iodine.New(errInvalidArgument{}, nil)))
}
}
}
}
func checkCastSyntaxTypeB(srcURL string, tgtURLs []string) {
if len(tgtURLs) == 0 && tgtURLs == nil {
console.Fatalf("Invalid number of target arguments to cast command. %s\n", NewIodine(iodine.New(errInvalidArgument{}, nil)))
}
_, srcContent, err := url2Stat(srcURL)
// Source exist?.
if err != nil {
console.Fatalf("Unable to stat source ‘%s’. %s\n", srcURL, NewIodine(iodine.New(err, nil)))
}
if srcContent.Type.IsDir() {
console.Fatalf("Source ‘%s’ is a folder. Use ‘%s...’ argument to cast this folder and its contents recursively. %s\n", srcURL, srcURL, NewIodine(iodine.New(errInvalidArgument{}, nil)))
}
if !srcContent.Type.IsRegular() {
console.Fatalf("Source ‘%s’ is not a file. %s\n", srcURL, NewIodine(iodine.New(errInvalidArgument{}, nil)))
}
// targetURL can be folder or file, internally TypeB calls TypeA if it finds a file
}
func checkCastSyntaxTypeC(srcURL string, tgtURLs []string) {
if len(tgtURLs) == 0 && tgtURLs == nil {
console.Fatalf("Invalid number of target arguments to cast command. %s\n", NewIodine(iodine.New(errInvalidArgument{}, nil)))
}
srcURL = stripRecursiveURL(srcURL)
_, srcContent, err := url2Stat(srcURL)
// Source exist?.
if err != nil {
console.Fatalf("Unable to stat source ‘%s’. %s\n", srcURL, NewIodine(iodine.New(err, nil)))
}
if srcContent.Type.IsRegular() { // Ellipses is supported only for folders.
console.Fatalf("Source ‘%s’ is not a folder. %s\n", stripRecursiveURL(srcURL), NewIodine(iodine.New(err, nil)))
}
for _, tgtURL := range tgtURLs {
_, content, err := url2Stat(tgtURL)
if err == nil {
if !content.Type.IsDir() {
console.Fatalf("One of the target ‘%s’ is not a folder. cannot have mixtures of directories and files while copying directories recursively. %s\n", tgtURL, NewIodine(iodine.New(errInvalidArgument{}, nil)))
}
}
}
}
// guessCastURLType guesses the type of URL. This approach all allows prepareURL
// functions to accurately report failure causes.
func guessCastURLType(sourceURL string, targetURLs []string) castURLsType {
if targetURLs == nil || len(targetURLs) == 0 { // Target is empty
return castURLsTypeInvalid
}
if sourceURL == "" { // Source is empty
return castURLsTypeInvalid
}
for _, targetURL := range targetURLs {
if targetURL == "" { // One of the target is empty
return castURLsTypeInvalid
}
}
if isURLRecursive(sourceURL) { // Type C
return castURLsTypeC
} // else Type A or Type B
for _, targetURL := range targetURLs {
if isTargetURLDir(targetURL) { // Type B
return castURLsTypeB
}
} // else Type A
return castURLsTypeA
}
// prepareSingleCastURLTypeA - prepares a single source and single target argument for casting.
func prepareSingleCastURLsTypeA(sourceURL string, targetURL string) castURLs {
_, sourceContent, err := url2Stat(sourceURL)
if err != nil { // Source does not exist or insufficient privileges.
return castURLs{Error: NewIodine(iodine.New(err, nil))}
}
if !sourceContent.Type.IsRegular() { // Source is not a regular file
return castURLs{Error: NewIodine(iodine.New(errInvalidSource{URL: sourceURL}, nil))}
}
// All OK.. We can proceed. Type A
sourceContent.Name = sourceURL
return castURLs{SourceContent: sourceContent, TargetContents: []*client.Content{{Name: targetURL}}}
}
// prepareCastURLsTypeA - A: cast(f, f) -> cast(f, f)
func prepareCastURLsTypeA(sourceURL string, targetURLs []string) castURLs {
var sURLs castURLs
for _, targetURL := range targetURLs { // Prepare each target separately
URLs := prepareSingleCastURLsTypeA(sourceURL, targetURL)
if URLs.Error != nil {
return castURLs{Error: NewIodine(iodine.New(URLs.Error, nil))}
}
sURLs.SourceContent = URLs.SourceContent
sURLs.TargetContents = append(sURLs.TargetContents, URLs.TargetContents...)
}
return sURLs
}
// prepareSingleCastURLsTypeB - prepares a single target and single source URLs for casting.
func prepareSingleCastURLsTypeB(sourceURL string, targetURL string) castURLs {
_, sourceContent, err := url2Stat(sourceURL)
if err != nil {
// Source does not exist or insufficient privileges.
return castURLs{Error: NewIodine(iodine.New(err, nil))}
}
if !sourceContent.Type.IsRegular() {
// Source is not a regular file.
return castURLs{Error: NewIodine(iodine.New(errInvalidSource{URL: sourceURL}, nil))}
}
_, targetContent, err := url2Stat(targetURL)
if err != nil {
// Source and target are files. Already reduced to Type A.
return prepareSingleCastURLsTypeA(sourceURL, targetURL)
}
if targetContent.Type.IsRegular() { // File to File
// Source and target are files. Already reduced to Type A.
return prepareSingleCastURLsTypeA(sourceURL, targetURL)
}
// Source is a file, target is a folder and exists.
sourceURLParse, err := client.Parse(sourceURL)
if err != nil {
return castURLs{Error: NewIodine(iodine.New(errInvalidSource{URL: sourceURL}, nil))}
}
targetURLParse, err := client.Parse(targetURL)
if err != nil {
return castURLs{Error: NewIodine(iodine.New(errInvalidTarget{URL: targetURL}, nil))}
}
// Reduce Type B to Type A.
targetURLParse.Path = filepath.Join(targetURLParse.Path, filepath.Base(sourceURLParse.Path))
return prepareSingleCastURLsTypeA(sourceURL, targetURLParse.String())
}
// prepareCastURLsTypeB - B: cast(f, d) -> cast(f, d/f) -> A
func prepareCastURLsTypeB(sourceURL string, targetURLs []string) castURLs {
var sURLs castURLs
for _, targetURL := range targetURLs {
URLs := prepareSingleCastURLsTypeB(sourceURL, targetURL)
if URLs.Error != nil {
return castURLs{Error: NewIodine(iodine.New(URLs.Error, nil))}
}
sURLs.SourceContent = URLs.SourceContent
sURLs.TargetContents = append(sURLs.TargetContents, URLs.TargetContents[0])
}
return sURLs
}
// prepareCastURLsTypeC - C:
func prepareCastURLsTypeC(sourceURL string, targetURLs []string) <-chan castURLs {
castURLsCh := make(chan castURLs)
go func() {
defer close(castURLsCh)
if !isURLRecursive(sourceURL) {
// Source is not of recursive type.
castURLsCh <- castURLs{Error: NewIodine(iodine.New(errSourceNotRecursive{URL: sourceURL}, nil))}
return
}
// add `/` after trimming off `...` to emulate folders
sourceURL = stripRecursiveURL(sourceURL)
sourceClient, sourceContent, err := url2Stat(sourceURL)
// Source exist?
if err != nil {
// Source does not exist or insufficient privileges.
castURLsCh <- castURLs{Error: NewIodine(iodine.New(err, nil))}
return
}
if !sourceContent.Type.IsDir() {
// Source is not a dir.
castURLsCh <- castURLs{Error: NewIodine(iodine.New(errSourceIsNotDir{URL: sourceURL}, nil))}
return
}
for sourceContent := range sourceClient.List(true) {
if sourceContent.Err != nil {
// Listing failed.
castURLsCh <- castURLs{Error: NewIodine(iodine.New(sourceContent.Err, nil))}
continue
}
if !sourceContent.Content.Type.IsRegular() {
// Source is not a regular file. Skip it for cast.
continue
}
// All OK.. We can proceed. Type B: source is a file, target is a folder and exists.
sourceURLParse, err := client.Parse(sourceURL)
if err != nil {
castURLsCh <- castURLs{Error: NewIodine(iodine.New(errInvalidSource{URL: sourceURL}, nil))}
continue
}
var newTargetURLs []string
var sourceContentParse *client.URL
for _, targetURL := range targetURLs {
targetURLParse, err := client.Parse(targetURL)
if err != nil {
castURLsCh <- castURLs{Error: NewIodine(iodine.New(errInvalidTarget{URL: targetURL}, nil))}
continue
}
sourceURLDelimited := sourceURLParse.String()[:strings.LastIndex(sourceURLParse.String(),
string(sourceURLParse.Separator))+1]
sourceContentName := sourceContent.Content.Name
sourceContentURL := sourceURLDelimited + sourceContentName
sourceContentParse, err = client.Parse(sourceContentURL)
if err != nil {
castURLsCh <- castURLs{Error: NewIodine(iodine.New(errInvalidSource{URL: sourceContentName}, nil))}
continue
}
// Construct target path from recursive path of source without its prefix dir.
newTargetURLParse := *targetURLParse
newTargetURLParse.Path = filepath.Join(newTargetURLParse.Path, sourceContentName)
newTargetURLs = append(newTargetURLs, newTargetURLParse.String())
}
castURLsCh <- prepareCastURLsTypeA(sourceContentParse.String(), newTargetURLs)
}
}()
return castURLsCh
}
// prepareCastURLs - prepares target and source URLs for casting.
func prepareCastURLs(sourceURL string, targetURLs []string) <-chan castURLs {
castURLsCh := make(chan castURLs)
go func() {
defer close(castURLsCh)
switch guessCastURLType(sourceURL, targetURLs) {
case castURLsTypeA:
castURLsCh <- prepareCastURLsTypeA(sourceURL, targetURLs)
case castURLsTypeB:
castURLsCh <- prepareCastURLsTypeB(sourceURL, targetURLs)
case castURLsTypeC:
for sURLs := range prepareCastURLsTypeC(sourceURL, targetURLs) {
castURLsCh <- sURLs
}
default:
castURLsCh <- castURLs{Error: NewIodine(iodine.New(errInvalidArgument{}, nil))}
}
}()
return castURLsCh
}