-
Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathexpand.go
96 lines (83 loc) · 2.36 KB
/
expand.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
package command
import (
"context"
"errors"
"sync"
"sync/atomic"
"github.com/peak/s5cmd/v2/storage"
"github.com/peak/s5cmd/v2/storage/url"
)
// expandSource returns the full list of objects from the given src argument.
// If src is an expandable URL, such as directory, prefix or a glob, all
// objects are returned by walking the source.
func expandSource(
ctx context.Context,
client storage.Storage,
followSymlinks bool,
srcurl *url.URL,
) (<-chan *storage.Object, error) {
var objType storage.ObjectType
// if the source is local, we send a Stat call to know if we have
// directory or file to walk. For remote storage, we don't want to send
// Stat since it doesn't have any folder semantics.
if !srcurl.IsWildcard() && !srcurl.IsRemote() {
obj, err := client.Stat(ctx, srcurl)
if err != nil {
return nil, err
}
objType = obj.Type
}
// call storage.List for only walking operations.
if srcurl.IsWildcard() || srcurl.AllVersions || objType.IsDir() {
return client.List(ctx, srcurl, followSymlinks), nil
}
ch := make(chan *storage.Object, 1)
if storage.ShouldProcessURL(srcurl, followSymlinks) {
ch <- &storage.Object{URL: srcurl, Type: objType}
}
close(ch)
return ch, nil
}
// expandSources is a non-blocking argument dispatcher. It creates a object
// channel by walking and expanding the given source urls. If the url has a
// glob, it creates a goroutine to list storage items and sends them to object
// channel, otherwise it creates storage object from the original source.
func expandSources(
ctx context.Context,
client storage.Storage,
followSymlinks bool,
srcurls ...*url.URL,
) <-chan *storage.Object {
ch := make(chan *storage.Object)
go func() {
defer close(ch)
var wg sync.WaitGroup
var objFound atomic.Bool
for _, origSrc := range srcurls {
wg.Add(1)
go func(origSrc *url.URL) {
defer wg.Done()
objch, err := expandSource(ctx, client, followSymlinks, origSrc)
if err != nil {
var objNotFound *storage.ErrGivenObjectNotFound
if !errors.As(err, &objNotFound) {
ch <- &storage.Object{Err: err}
}
return
}
for object := range objch {
if object.Err == storage.ErrNoObjectFound {
continue
}
ch <- object
objFound.Store(true)
}
}(origSrc)
}
wg.Wait()
if !objFound.Load() {
ch <- &storage.Object{Err: storage.ErrNoObjectFound}
}
}()
return ch
}