-
Notifications
You must be signed in to change notification settings - Fork 2
/
shared.go
460 lines (429 loc) · 11.6 KB
/
shared.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
450
451
452
453
454
455
456
457
458
459
460
package ipfs
import (
"context"
"errors"
"fmt"
"io"
"io/fs"
"strings"
"time"
"github.com/djdv/go-filesystem-utils/internal/filesystem"
fserrors "github.com/djdv/go-filesystem-utils/internal/filesystem/errors"
"github.com/djdv/go-filesystem-utils/internal/generic"
coreiface "github.com/ipfs/boxo/coreiface"
dag "github.com/ipfs/boxo/ipld/merkledag"
"github.com/ipfs/boxo/ipld/unixfs"
unixpb "github.com/ipfs/boxo/ipld/unixfs/pb"
"github.com/ipfs/go-cid"
ipfscmds "github.com/ipfs/go-ipfs-cmds"
cbor "github.com/ipfs/go-ipld-cbor"
ipld "github.com/ipfs/go-ipld-format"
"github.com/multiformats/go-multibase"
)
type (
// TODO: docs; commonly shared options.
Options interface {
IPFSOption | PinFSOption |
IPNSOption | KeyFSOption
}
nodeInfo struct {
modTime time.Time
name string
size int64
mode fs.FileMode
}
emptyRoot struct{ info *nodeInfo }
ctxChan[T any] struct {
context.Context
context.CancelFunc
ch <-chan T
}
entryStream = ctxChan[filesystem.StreamDirEntry]
errorEntry struct {
fs.DirEntry // Should always be nil.
error
}
coreDirEntry struct {
coreiface.DirEntry
modTime time.Time
permissions fs.FileMode
}
getNodeFunc func(cid.Cid) (ipld.Node, error)
)
const (
ErrPath = generic.ConstError("path not valid")
ErrNotFound = generic.ConstError("file not found")
ErrNotOpen = generic.ConstError("file was not open")
ErrIsDir = generic.ConstError("file is a directory")
ErrIsNotDir = generic.ConstError("file is not a directory")
errUnexpectedType = generic.ConstError("unexpected type")
rootName = "."
// If this is encountered, it is an implementation error.
// All option types must be handled explicitly.
unhandledOption = "unhandled option case"
executeAll = filesystem.ExecuteUser | filesystem.ExecuteGroup | filesystem.ExecuteOther
readAll = filesystem.ReadUser | filesystem.ReadGroup | filesystem.ReadOther
)
var _ fs.FileInfo = (*nodeInfo)(nil)
func (ee errorEntry) Error() error { return ee.error }
func newErrorEntry(err error) filesystem.StreamDirEntry {
return errorEntry{error: err}
}
func WithContext[OT Options](ctx context.Context) (option OT) {
switch fn := any(&option).(type) {
default:
panic(unhandledOption)
case *IPFSOption:
*fn = func(ifs *ipfsSettings) error { ifs.ctx, ifs.cancel = context.WithCancel(ctx); return nil }
case *PinFSOption:
*fn = func(pfs *PinFS) error { pfs.ctx, pfs.cancel = context.WithCancel(ctx); return nil }
case *IPNSOption:
*fn = func(nfs *ipnsSettings) error { nfs.ctx, nfs.cancel = context.WithCancel(ctx); return nil }
case *KeyFSOption:
*fn = func(kfs *KeyFS) error { kfs.ctx, kfs.cancel = context.WithCancel(ctx); return nil }
}
return
}
func WithPermissions[OT Options](permissions fs.FileMode) (option OT) {
switch fn := any(&option).(type) {
default:
panic(unhandledOption)
case *PinFSOption:
*fn = func(pfs *PinFS) error {
pfs.info.permissions = permissions.Perm()
return nil
}
case *IPFSOption:
*fn = func(ifs *ipfsSettings) error {
ifs.info.mode = ifs.info.mode.Type() | permissions.Perm()
return nil
}
case *KeyFSOption:
*fn = func(kfs *KeyFS) error {
kfs.permissions = permissions.Perm()
return nil
}
}
return
}
func (ni *nodeInfo) Name() string { return ni.name }
func (ni *nodeInfo) Size() int64 { return ni.size }
func (ni *nodeInfo) Mode() fs.FileMode { return ni.mode }
func (ni *nodeInfo) ModTime() time.Time { return ni.modTime }
func (ni *nodeInfo) IsDir() bool { return ni.Mode().IsDir() }
func (ni *nodeInfo) Sys() any { return ni }
func (cde *coreDirEntry) Name() string { return cde.DirEntry.Name }
func (cde *coreDirEntry) IsDir() bool { return cde.Type().IsDir() }
func (cde *coreDirEntry) Info() (fs.FileInfo, error) { return cde, nil }
func (cde *coreDirEntry) Size() int64 { return int64(cde.DirEntry.Size) }
func (cde *coreDirEntry) ModTime() time.Time { return cde.modTime }
func (cde *coreDirEntry) Mode() fs.FileMode { return cde.Type() | cde.permissions }
func (cde *coreDirEntry) Sys() any { return cde }
func (cde *coreDirEntry) Error() error { return cde.DirEntry.Err }
func (cde *coreDirEntry) Type() fs.FileMode {
switch cde.DirEntry.Type {
case coreiface.TDirectory:
return fs.ModeDir
case coreiface.TFile:
return fs.FileMode(0)
case coreiface.TSymlink:
return fs.ModeSymlink
default:
return fs.ModeIrregular
}
}
func (er emptyRoot) Stat() (fs.FileInfo, error) { return er.info, nil }
func (emptyRoot) Close() error { return nil }
func (emptyRoot) Read([]byte) (int, error) {
const op = "emptyRoot.Read"
return -1, newFSError(op, rootName, ErrIsDir, fserrors.IsDir)
}
func (emptyRoot) ReadDir(count int) ([]fs.DirEntry, error) {
if count > 0 {
return nil, io.EOF
}
return nil, nil
}
func statNode(node ipld.Node, info *nodeInfo) error {
switch typedNode := node.(type) {
case *dag.ProtoNode:
return statProto(typedNode, info)
case *cbor.Node:
return statCbor(typedNode, info)
default:
return statGeneric(node, info)
}
}
func statProto(node *dag.ProtoNode, info *nodeInfo) error {
ufsNode, err := unixfs.ExtractFSNode(node)
if err != nil {
return err
}
info.size = int64(ufsNode.FileSize())
switch ufsNode.Type() {
case unixpb.Data_Directory, unixpb.Data_HAMTShard:
info.mode |= fs.ModeDir
case unixpb.Data_Symlink:
info.mode |= fs.ModeSymlink
case unixpb.Data_File, unixpb.Data_Raw:
// NOOP: stat.mode |= fs.FileMode(0)
default:
info.mode |= fs.ModeIrregular
}
return nil
}
func statGeneric(node ipld.Node, info *nodeInfo) error {
nodeStat, err := node.Stat()
if err != nil {
return err
}
info.size = int64(nodeStat.CumulativeSize)
return nil
}
func generateEntryChan(ctx context.Context, values []filesystem.StreamDirEntry) <-chan filesystem.StreamDirEntry {
out := make(chan filesystem.StreamDirEntry, 1)
go func() {
defer close(out)
for _, value := range values {
if err := ctx.Err(); err != nil {
drainThenSendErr(out, err)
return
}
select {
case out <- value:
case <-ctx.Done():
drainThenSendErr(out, ctx.Err())
return
}
}
}()
return out
}
// readEntries handles different behaviour expected by
// [fs.ReadDirFile].
// Specifically in regard to the returned values.
func readEntries(ctx context.Context,
entries <-chan filesystem.StreamDirEntry, count int,
) (requested []fs.DirEntry, err error) {
readAll := count <= 0
if readAll {
requested = make([]fs.DirEntry, 0, cap(entries))
} else {
const upperBound = 16
requested = make([]fs.DirEntry, 0, generic.Min(count, upperBound))
}
requested, err = readEntriesCount(ctx, entries, requested, count)
if err != nil && !readAll {
requested = nil
}
return
}
// readEntriesCount does the actual readdir operation.
// It always returns `requested`.
func readEntriesCount(ctx context.Context,
entries <-chan filesystem.StreamDirEntry,
requested []fs.DirEntry,
count int,
) ([]fs.DirEntry, error) {
for {
select {
case entry, ok := <-entries:
if !ok {
if len(requested) == 0 {
return requested, io.EOF
}
return requested, nil
}
if err := entry.Error(); err != nil {
return requested, err
}
requested = append(requested, entry)
if count--; count == 0 {
return requested, nil
}
case <-ctx.Done():
return requested, ctx.Err()
}
}
}
func readdirErr(op, path string, err error) error {
if err == io.EOF {
return err
}
return newFSError(op, path, err, fserrors.IO)
}
func newFSError(op, path string, err error, kind fserrors.Kind) error {
return &fserrors.Error{
PathError: fs.PathError{
Op: op,
Path: path,
Err: err,
},
Kind: kind,
}
}
func cidErrKind(err error) fserrors.Kind {
if errors.Is(err, multibase.ErrUnsupportedEncoding) {
return fserrors.NotExist
}
return fserrors.IO
}
func resolveErrKind(err error) fserrors.Kind {
// XXX: Upstream doesn't define error values
// to compare against. We have to fallback to strings.
// This could break at any time.
//
// TODO: split this function?
// 1 for errors returned from core API,
// 1 for ipld only?
const (
notFoundCore = "no link named"
// Specifically for OS sidecar files
// that will not be valid CIDs.
// E.g. `/$ns/desktop.ini`, `/$ns/.DS_Store`, et al.
invalid = "invalid path"
)
var cmdsErr *ipfscmds.Error
if errors.As(err, &cmdsErr) &&
cmdsErr.Code == ipfscmds.ErrNormal &&
(strings.Contains(cmdsErr.Message, notFoundCore) ||
strings.Contains(cmdsErr.Message, invalid)) {
return fserrors.NotExist
}
const notFoundIPLD = "no link by that name"
if strings.Contains(err.Error(), notFoundIPLD) {
return fserrors.NotExist
}
return fserrors.IO
}
func newStreamChan[T any](ch <-chan T) chan filesystem.StreamDirEntry {
// +1 relay must account for ctx error.
return make(chan filesystem.StreamDirEntry, cap(ch)+1)
}
// accumulateRelayClose accumulates and relays
// from `entries`.
// `ctx` applies only to sends on `relay`.
// Regardless of cancellation, values will be
// received from `entries` and accumulated until it's closed.
// `relay` must have a cap of at least 1
// and will (eventually) be closed by this call.
func accumulateRelayClose(ctx context.Context,
entries <-chan filesystem.StreamDirEntry,
relay chan filesystem.StreamDirEntry,
accumulator []filesystem.StreamDirEntry,
) (sawError bool, _ []filesystem.StreamDirEntry) {
var (
sent int
unsent []filesystem.StreamDirEntry
canceled func() bool
relayFn func()
)
canceled = func() bool {
if err := ctx.Err(); err != nil {
drainThenSendErr(relay, err)
close(relay)
canceled = func() bool { return true }
return true
}
return false
}
relayFn = func() {
if canceled() {
relayFn, unsent = func() {}, nil
return
}
unsent = accumulator[sent:]
select {
case relay <- unsent[0]:
sent++
unsent = unsent[1:]
default: // Don't wait on relay; keep caching.
}
}
for entry := range entries {
if entry.Error() != nil {
sawError = true
}
accumulator = append(accumulator, entry)
relayFn()
}
if canceled() {
return sawError, accumulator
}
if len(unsent) == 0 {
close(relay)
return sawError, accumulator
}
// NOTE: `unsent` is slice of `accumulator`.
clone := generic.CloneSlice(unsent) // (which could be modified by the caller.)
go func() {
defer close(relay)
for _, entry := range clone {
select {
case relay <- entry:
case <-ctx.Done():
drainThenSendErr(relay, ctx.Err())
return
}
}
}()
return sawError, accumulator
}
func drainThenSendErr(ch chan filesystem.StreamDirEntry, err error) {
generic.DrainBuffer(ch)
ch <- newErrorEntry(err)
}
func walkLinks(root cid.Cid, names []string, getNodeFn getNodeFunc) (cid.Cid, error) {
node, err := getNodeFn(root)
if err != nil {
return cid.Cid{}, err
}
var (
leafCid cid.Cid
end = len(names) - 1
)
for i, name := range names {
object, _, err := node.Resolve([]string{name})
if err != nil {
return cid.Cid{}, err
}
link, ok := object.(*ipld.Link)
if !ok {
return cid.Cid{}, fmt.Errorf(
"expected: `%T` got: `%T`",
link, object,
)
}
leafCid = link.Cid
if i == end {
break
}
node, err = getNodeFn(leafCid)
if err != nil {
return cid.Cid{}, err
}
}
return leafCid, nil
}
func fsTypeName(mode fs.FileMode) string {
switch mode.Type() {
case fs.FileMode(0):
return "regular"
case fs.ModeDir:
return "directory"
case fs.ModeSymlink:
return "symbolic link"
case fs.ModeNamedPipe:
return "named pipe"
case fs.ModeSocket:
return "socket"
case fs.ModeDevice:
return "device"
case fs.ModeCharDevice:
return "character device"
case fs.ModeIrregular:
fallthrough
default:
return "irregular"
}
}