forked from jszwec/s3fs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dir.go
230 lines (193 loc) · 3.63 KB
/
dir.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
package s3fs
import (
"context"
"errors"
"io"
"io/fs"
"path"
"sort"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
var _ fs.ReadDirFile = (*dir)(nil)
type dir struct {
fileInfo
s3cl Client
bucket string
marker *string
done bool
buf []fs.DirEntry
dirs map[dirEntry]bool
}
func (d *dir) Stat() (fs.FileInfo, error) {
return &d.fileInfo, nil
}
func (d *dir) Read([]byte) (int, error) {
return 0, &fs.PathError{
Op: "read",
Path: d.name,
Err: errors.New("is a directory"),
}
}
func (d *dir) Close() error {
return nil
}
func (d *dir) ReadDir(n int) (des []fs.DirEntry, err error) {
if n <= 0 {
switch err := d.readAll(); {
case err == nil:
case errors.Is(err, io.EOF):
return []fs.DirEntry{}, nil
default:
return nil, err
}
des, d.buf = d.buf, nil
return des, nil
}
loop:
for len(d.buf) < n {
switch err := d.readNext(); {
case err == nil:
continue
case errors.Is(err, io.EOF):
break loop
default:
return nil, err
}
}
offset := min(n, len(d.buf))
des, d.buf = d.buf[:offset:offset], d.buf[offset:]
if d.done && len(d.buf) == 0 {
err = io.EOF
}
return des, err
}
func (d *dir) readAll() error {
for !d.done {
switch err := d.readNext(); {
case err == nil:
continue
case errors.Is(err, io.EOF):
return nil
default:
return err
}
}
return io.EOF
}
func (d *dir) readNext() error {
if d.done {
return io.EOF
}
name := strings.TrimRight(d.name, "/")
switch {
case name == ".":
name = ""
default:
name += "/"
}
out, err := d.s3cl.ListObjects(
context.Background(),
&s3.ListObjectsInput{
Bucket: &d.bucket,
Delimiter: ptr("/"),
Prefix: &name,
Marker: d.marker,
})
if err != nil {
return err
}
if d.name != "." && len(out.CommonPrefixes)+len(out.Contents) == 0 {
return &fs.PathError{
Op: "readdir",
Path: strings.TrimSuffix(name, "/"),
Err: fs.ErrNotExist,
}
}
d.marker = out.NextMarker
d.done = out.IsTruncated != nil && !(*out.IsTruncated)
if d.dirs == nil {
d.dirs = make(map[dirEntry]bool)
}
for _, p := range out.CommonPrefixes {
if p.Prefix == nil {
continue
}
de := dirEntry{
fileInfo: fileInfo{
name: path.Base(*p.Prefix),
mode: fs.ModeDir,
},
}
if _, ok := d.dirs[de]; !ok {
d.dirs[de] = false
}
}
for _, o := range out.Contents {
if o.Key == nil {
continue
}
d.buf = append(d.buf, dirEntry{
fileInfo: fileInfo{
name: path.Base(*o.Key),
size: derefInt64(o.Size),
modTime: derefTime(o.LastModified),
},
})
}
d.mergeDirFiles()
if d.done {
return io.EOF
}
return nil
}
func (d *dir) mergeDirFiles() {
if d.buf == nil {
// according to fs docs ReadDir should never return nil slice,
// so we set it here.
d.buf = []fs.DirEntry{}
}
// we need a current len for sort.Search that doesn't change; otherwise
// we could not append to the same slice.
l := len(d.buf)
for de, used := range d.dirs {
if used {
continue
}
i := sort.Search(l, func(i int) bool {
return d.buf[i].Name() >= de.Name()
})
if i == l && !d.done {
continue
}
d.buf = append(d.buf, de)
d.dirs[de] = true
}
sort.Slice(d.buf, func(i, j int) bool {
return d.buf[i].Name() < d.buf[j].Name()
})
}
type dirEntry struct {
fileInfo
}
func (de dirEntry) Type() fs.FileMode { return de.Mode().Type() }
func (de dirEntry) Info() (fs.FileInfo, error) { return de.fileInfo, nil }
func min(a, b int) int {
if a < b {
return a
}
return b
}
func derefInt64(n *int64) int64 {
if n != nil {
return *n
}
return 0
}
func derefTime(t *time.Time) time.Time {
if t != nil {
return *t
}
return time.Time{}
}