-
Notifications
You must be signed in to change notification settings - Fork 239
/
fs.go
269 lines (221 loc) · 5.4 KB
/
fs.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
package storage
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/karrick/godirwalk"
"github.com/termie/go-shutil"
"github.com/peak/s5cmd/v2/storage/url"
)
// Filesystem is the Storage implementation of a local filesystem.
type Filesystem struct {
dryRun bool
}
// Stat returns the Object structure describing object.
func (f *Filesystem) Stat(ctx context.Context, url *url.URL) (*Object, error) {
st, err := os.Stat(url.Absolute())
if err != nil {
if os.IsNotExist(err) {
return nil, &ErrGivenObjectNotFound{ObjectAbsPath: url.Absolute()}
}
return nil, err
}
mod := st.ModTime()
return &Object{
URL: url,
Type: ObjectType{st.Mode()},
Size: st.Size(),
ModTime: &mod,
Etag: "",
}, nil
}
// List returns the objects and directories reside in given src.
func (f *Filesystem) List(ctx context.Context, src *url.URL, followSymlinks bool) <-chan *Object {
if src.IsWildcard() {
return f.expandGlob(ctx, src, followSymlinks)
}
obj, err := f.Stat(ctx, src)
isDir := err == nil && obj.Type.IsDir()
if isDir {
return f.walkDir(ctx, src, followSymlinks)
}
return f.listSingleObject(ctx, src)
}
func (f *Filesystem) listSingleObject(ctx context.Context, src *url.URL) <-chan *Object {
ch := make(chan *Object, 1)
defer close(ch)
object, err := f.Stat(ctx, src)
if err != nil {
object = &Object{Err: err}
}
ch <- object
return ch
}
func (f *Filesystem) expandGlob(ctx context.Context, src *url.URL, followSymlinks bool) <-chan *Object {
ch := make(chan *Object)
go func() {
defer close(ch)
matchedFiles, err := filepath.Glob(src.Absolute())
if err != nil {
sendError(ctx, err, ch)
return
}
if len(matchedFiles) == 0 {
err := fmt.Errorf("no match found for %q", src)
sendError(ctx, err, ch)
return
}
for _, filename := range matchedFiles {
filename := filename
fileurl, err := url.New(filename)
if err != nil {
sendError(ctx, err, ch)
return
}
fileurl.SetRelative(src)
obj, err := f.Stat(ctx, fileurl)
if err != nil {
sendError(ctx, err, ch)
return
}
if !obj.Type.IsDir() {
sendObject(ctx, obj, ch)
continue
}
walkDir(ctx, f, fileurl, followSymlinks, func(obj *Object) {
sendObject(ctx, obj, ch)
})
}
}()
return ch
}
func walkDir(ctx context.Context, fs *Filesystem, src *url.URL, followSymlinks bool, fn func(o *Object)) {
//skip if symlink is pointing to a dir and --no-follow-symlink
if !ShouldProcessURL(src, followSymlinks) {
return
}
err := godirwalk.Walk(src.Absolute(), &godirwalk.Options{
Callback: func(pathname string, dirent *godirwalk.Dirent) error {
// we're interested in files
if dirent.IsDir() {
return nil
}
fileurl, err := url.New(pathname)
if err != nil {
return err
}
fileurl.SetRelative(src)
//skip if symlink is pointing to a file and --no-follow-symlink
if !ShouldProcessURL(fileurl, followSymlinks) {
return nil
}
obj, err := fs.Stat(ctx, fileurl)
if err != nil {
return err
}
fn(obj)
return nil
},
FollowSymbolicLinks: followSymlinks,
})
if err != nil {
obj := &Object{Err: err}
fn(obj)
}
}
func (f *Filesystem) walkDir(ctx context.Context, src *url.URL, followSymlinks bool) <-chan *Object {
ch := make(chan *Object)
go func() {
defer close(ch)
walkDir(ctx, f, src, followSymlinks, func(obj *Object) {
sendObject(ctx, obj, ch)
})
}()
return ch
}
// Copy copies given source to destination.
func (f *Filesystem) Copy(ctx context.Context, src, dst *url.URL, _ Metadata) error {
if f.dryRun {
return nil
}
if err := os.MkdirAll(dst.Dir(), os.ModePerm); err != nil {
return err
}
_, err := shutil.Copy(src.Absolute(), dst.Absolute(), true)
return err
}
// Delete deletes given file.
func (f *Filesystem) Delete(ctx context.Context, url *url.URL) error {
if f.dryRun {
return nil
}
return os.Remove(url.Absolute())
}
// MultiDelete deletes all files returned from given channel.
func (f *Filesystem) MultiDelete(ctx context.Context, urlch <-chan *url.URL) <-chan *Object {
resultch := make(chan *Object)
go func() {
defer close(resultch)
for url := range urlch {
err := f.Delete(ctx, url)
obj := &Object{
URL: url,
Err: err,
}
resultch <- obj
}
}()
return resultch
}
// MkdirAll calls os.MkdirAll.
func (f *Filesystem) MkdirAll(path string) error {
if f.dryRun {
return nil
}
return os.MkdirAll(path, os.ModePerm)
}
// Create creates a new os.File.
func (f *Filesystem) Create(path string) (*os.File, error) {
if f.dryRun {
return &os.File{}, nil
}
return os.Create(path)
}
// Open opens the given source.
func (f *Filesystem) Open(path string) (*os.File, error) {
file, err := os.OpenFile(path, os.O_RDONLY, 0644)
if err != nil {
return nil, err
}
return file, nil
}
// CreateTemp creates a new temporary file
func (f *Filesystem) CreateTemp(dir, pattern string) (*os.File, error) {
if f.dryRun {
return &os.File{}, nil
}
file, err := os.CreateTemp(dir, pattern)
if err != nil {
return nil, err
}
err = file.Chmod(0644)
return file, err
}
// Rename a file
func (f *Filesystem) Rename(file *os.File, newpath string) error {
if f.dryRun {
return nil
}
return os.Rename(file.Name(), newpath)
}
func sendObject(ctx context.Context, obj *Object, ch chan *Object) {
select {
case <-ctx.Done():
case ch <- obj:
}
}
func sendError(ctx context.Context, err error, ch chan *Object) {
obj := &Object{Err: err}
sendObject(ctx, obj, ch)
}