This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathdir.go
437 lines (362 loc) · 9.49 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
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
package mfs
import (
"context"
"errors"
"fmt"
"os"
"path"
"sync"
"time"
dag "github.com/ipfs/go-merkledag"
ft "github.com/ipfs/go-unixfs"
uio "github.com/ipfs/go-unixfs/io"
cid "github.com/ipfs/go-cid"
ipld "github.com/ipfs/go-ipld-format"
)
// Deprecated: use github.com/ipfs/boxo/mfs.ErrNotYetImplemented
var ErrNotYetImplemented = errors.New("not yet implemented")
// Deprecated: use github.com/ipfs/boxo/mfs.ErrInvalidChild
var ErrInvalidChild = errors.New("invalid child node")
// Deprecated: use github.com/ipfs/boxo/mfs.ErrDirExists
var ErrDirExists = errors.New("directory already has entry by that name")
// TODO: There's too much functionality associated with this structure,
// let's organize it (and if possible extract part of it elsewhere)
// and document the main features of `Directory` here.
//
// Deprecated: use github.com/ipfs/boxo/mfs.Directory
type Directory struct {
inode
// Internal cache with added entries to the directory, its cotents
// are synched with the underlying `unixfsDir` node in `sync()`.
entriesCache map[string]FSNode
lock sync.Mutex
// TODO: What content is being protected here exactly? The entire directory?
ctx context.Context
// UnixFS directory implementation used for creating,
// reading and editing directories.
unixfsDir uio.Directory
modTime time.Time
}
// NewDirectory constructs a new MFS directory.
//
// You probably don't want to call this directly. Instead, construct a new root
// using NewRoot.
//
// Deprecated: use github.com/ipfs/boxo/mfs.NewDirectory
func NewDirectory(ctx context.Context, name string, node ipld.Node, parent parent, dserv ipld.DAGService) (*Directory, error) {
db, err := uio.NewDirectoryFromNode(dserv, node)
if err != nil {
return nil, err
}
return &Directory{
inode: inode{
name: name,
parent: parent,
dagService: dserv,
},
ctx: ctx,
unixfsDir: db,
entriesCache: make(map[string]FSNode),
modTime: time.Now(),
}, nil
}
// GetCidBuilder gets the CID builder of the root node
func (d *Directory) GetCidBuilder() cid.Builder {
return d.unixfsDir.GetCidBuilder()
}
// SetCidBuilder sets the CID builder
func (d *Directory) SetCidBuilder(b cid.Builder) {
d.unixfsDir.SetCidBuilder(b)
}
// This method implements the `parent` interface. It first does the local
// update of the child entry in the underlying UnixFS directory and saves
// the newly created directory node with the updated entry in the DAG
// service. Then it propagates the update upwards (through this same
// interface) repeating the whole process in the parent.
func (d *Directory) updateChildEntry(c child) error {
newDirNode, err := d.localUpdate(c)
if err != nil {
return err
}
// Continue to propagate the update process upwards
// (all the way up to the root).
return d.parent.updateChildEntry(child{d.name, newDirNode})
}
// This method implements the part of `updateChildEntry` that needs
// to be locked around: in charge of updating the UnixFS layer and
// generating the new node reflecting the update. It also stores the
// new node in the DAG layer.
func (d *Directory) localUpdate(c child) (*dag.ProtoNode, error) {
d.lock.Lock()
defer d.lock.Unlock()
err := d.updateChild(c)
if err != nil {
return nil, err
}
// TODO: Clearly define how are we propagating changes to lower layers
// like UnixFS.
nd, err := d.unixfsDir.GetNode()
if err != nil {
return nil, err
}
pbnd, ok := nd.(*dag.ProtoNode)
if !ok {
return nil, dag.ErrNotProtobuf
}
err = d.dagService.Add(d.ctx, nd)
if err != nil {
return nil, err
}
return pbnd.Copy().(*dag.ProtoNode), nil
// TODO: Why do we need a copy?
}
// Update child entry in the underlying UnixFS directory.
func (d *Directory) updateChild(c child) error {
err := d.unixfsDir.AddChild(d.ctx, c.Name, c.Node)
if err != nil {
return err
}
d.modTime = time.Now()
return nil
}
func (d *Directory) Type() NodeType {
return TDir
}
// childNode returns a FSNode under this directory by the given name if it exists.
// it does *not* check the cached dirs and files
func (d *Directory) childNode(name string) (FSNode, error) {
nd, err := d.childFromDag(name)
if err != nil {
return nil, err
}
return d.cacheNode(name, nd)
}
// cacheNode caches a node into d.childDirs or d.files and returns the FSNode.
func (d *Directory) cacheNode(name string, nd ipld.Node) (FSNode, error) {
switch nd := nd.(type) {
case *dag.ProtoNode:
fsn, err := ft.FSNodeFromBytes(nd.Data())
if err != nil {
return nil, err
}
switch fsn.Type() {
case ft.TDirectory, ft.THAMTShard:
ndir, err := NewDirectory(d.ctx, name, nd, d, d.dagService)
if err != nil {
return nil, err
}
d.entriesCache[name] = ndir
return ndir, nil
case ft.TFile, ft.TRaw, ft.TSymlink:
nfi, err := NewFile(name, nd, d, d.dagService)
if err != nil {
return nil, err
}
d.entriesCache[name] = nfi
return nfi, nil
case ft.TMetadata:
return nil, ErrNotYetImplemented
default:
return nil, ErrInvalidChild
}
case *dag.RawNode:
nfi, err := NewFile(name, nd, d, d.dagService)
if err != nil {
return nil, err
}
d.entriesCache[name] = nfi
return nfi, nil
default:
return nil, fmt.Errorf("unrecognized node type in cache node")
}
}
// Child returns the child of this directory by the given name
func (d *Directory) Child(name string) (FSNode, error) {
d.lock.Lock()
defer d.lock.Unlock()
return d.childUnsync(name)
}
func (d *Directory) Uncache(name string) {
d.lock.Lock()
defer d.lock.Unlock()
delete(d.entriesCache, name)
}
// childFromDag searches through this directories dag node for a child link
// with the given name
func (d *Directory) childFromDag(name string) (ipld.Node, error) {
return d.unixfsDir.Find(d.ctx, name)
}
// childUnsync returns the child under this directory by the given name
// without locking, useful for operations which already hold a lock
func (d *Directory) childUnsync(name string) (FSNode, error) {
entry, ok := d.entriesCache[name]
if ok {
return entry, nil
}
return d.childNode(name)
}
// Deprecated: use github.com/ipfs/boxo/mfs.NodeListing
type NodeListing struct {
Name string
Type int
Size int64
Hash string
}
func (d *Directory) ListNames(ctx context.Context) ([]string, error) {
d.lock.Lock()
defer d.lock.Unlock()
var out []string
err := d.unixfsDir.ForEachLink(ctx, func(l *ipld.Link) error {
out = append(out, l.Name)
return nil
})
if err != nil {
return nil, err
}
return out, nil
}
func (d *Directory) List(ctx context.Context) ([]NodeListing, error) {
var out []NodeListing
err := d.ForEachEntry(ctx, func(nl NodeListing) error {
out = append(out, nl)
return nil
})
return out, err
}
func (d *Directory) ForEachEntry(ctx context.Context, f func(NodeListing) error) error {
d.lock.Lock()
defer d.lock.Unlock()
return d.unixfsDir.ForEachLink(ctx, func(l *ipld.Link) error {
c, err := d.childUnsync(l.Name)
if err != nil {
return err
}
nd, err := c.GetNode()
if err != nil {
return err
}
child := NodeListing{
Name: l.Name,
Type: int(c.Type()),
Hash: nd.Cid().String(),
}
if c, ok := c.(*File); ok {
size, err := c.Size()
if err != nil {
return err
}
child.Size = size
}
return f(child)
})
}
func (d *Directory) Mkdir(name string) (*Directory, error) {
d.lock.Lock()
defer d.lock.Unlock()
fsn, err := d.childUnsync(name)
if err == nil {
switch fsn := fsn.(type) {
case *Directory:
return fsn, os.ErrExist
case *File:
return nil, os.ErrExist
default:
return nil, fmt.Errorf("unrecognized type: %#v", fsn)
}
}
ndir := ft.EmptyDirNode()
ndir.SetCidBuilder(d.GetCidBuilder())
err = d.dagService.Add(d.ctx, ndir)
if err != nil {
return nil, err
}
err = d.unixfsDir.AddChild(d.ctx, name, ndir)
if err != nil {
return nil, err
}
dirobj, err := NewDirectory(d.ctx, name, ndir, d, d.dagService)
if err != nil {
return nil, err
}
d.entriesCache[name] = dirobj
return dirobj, nil
}
func (d *Directory) Unlink(name string) error {
d.lock.Lock()
defer d.lock.Unlock()
delete(d.entriesCache, name)
return d.unixfsDir.RemoveChild(d.ctx, name)
}
func (d *Directory) Flush() error {
nd, err := d.GetNode()
if err != nil {
return err
}
return d.parent.updateChildEntry(child{d.name, nd})
}
// AddChild adds the node 'nd' under this directory giving it the name 'name'
func (d *Directory) AddChild(name string, nd ipld.Node) error {
d.lock.Lock()
defer d.lock.Unlock()
_, err := d.childUnsync(name)
if err == nil {
return ErrDirExists
}
err = d.dagService.Add(d.ctx, nd)
if err != nil {
return err
}
err = d.unixfsDir.AddChild(d.ctx, name, nd)
if err != nil {
return err
}
d.modTime = time.Now()
return nil
}
func (d *Directory) sync() error {
for name, entry := range d.entriesCache {
nd, err := entry.GetNode()
if err != nil {
return err
}
err = d.updateChild(child{name, nd})
if err != nil {
return err
}
}
// TODO: Should we clean the cache here?
return nil
}
func (d *Directory) Path() string {
cur := d
var out string
for cur != nil {
switch parent := cur.parent.(type) {
case *Directory:
out = path.Join(cur.name, out)
cur = parent
case *Root:
return "/" + out
default:
panic("directory parent neither a directory nor a root")
}
}
return out
}
func (d *Directory) GetNode() (ipld.Node, error) {
d.lock.Lock()
defer d.lock.Unlock()
err := d.sync()
if err != nil {
return nil, err
}
nd, err := d.unixfsDir.GetNode()
if err != nil {
return nil, err
}
err = d.dagService.Add(d.ctx, nd)
if err != nil {
return nil, err
}
return nd.Copy(), err
}