-
Notifications
You must be signed in to change notification settings - Fork 4
/
wnfs.go
841 lines (711 loc) · 20.9 KB
/
wnfs.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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
package wnfs
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/fs"
"io/ioutil"
"time"
blocks "github.com/ipfs/go-block-format"
blockservice "github.com/ipfs/go-blockservice"
cid "github.com/ipfs/go-cid"
cbornode "github.com/ipfs/go-ipld-cbor"
golog "github.com/ipfs/go-log"
base "github.com/qri-io/wnfs-go/base"
private "github.com/qri-io/wnfs-go/private"
ratchet "github.com/qri-io/wnfs-go/private/ratchet"
public "github.com/qri-io/wnfs-go/public"
)
var log = golog.Logger("wnfs")
const (
// PreviousLinkName is the string for a historical backpointer in wnfs
PreviousLinkName = "previous"
// FileHierarchyNamePrivate is the root of encrypted files on WNFS
FileHierarchyNamePrivate = "private"
// FileHierarchyNamePublic is the root of public files on WNFS
FileHierarchyNamePublic = "public"
// FileHierarchyNamePretty is a link to a read-only branch at the root of a WNFS
FileHierarchyNamePretty = "p"
)
type WNFS interface {
fs.FS
fs.ReadDirFile // wnfs root is a directory file
PosixFS
PrivateFS
Cid() cid.Cid
History(ctx context.Context, pathStr string, generations int) ([]HistoryEntry, error)
Commit() (CommitResult, error)
}
type PosixFS interface {
// directories (trees)
Ls(pathStr string) ([]fs.DirEntry, error)
Mkdir(pathStr string) error
// files
Write(pathStr string, f fs.File) error
Cat(pathStr string) ([]byte, error)
Open(pathStr string) (fs.File, error)
// general
// Mv(from, to string) error
Cp(pathStr, srcPathStr string, src fs.FS) error
Rm(pathStr string) error
}
type (
Node = base.Node
HistoryEntry = base.HistoryEntry
PrivateName = private.Name
Key = private.Key
)
var NewKey = private.NewKey
type PrivateFS interface {
RootKey() private.Key
PrivateName() (PrivateName, error)
}
type fileSystem struct {
store public.Store
ctx context.Context
root *rootTree
}
var _ WNFS = (*fileSystem)(nil)
func NewEmptyFS(ctx context.Context, bserv blockservice.BlockService, rs ratchet.Store, rootKey Key) (WNFS, error) {
store := public.NewStore(ctx, bserv)
fs := &fileSystem{
ctx: ctx,
store: store,
}
root, err := newEmptyRootTree(store, rs, rootKey)
if err != nil {
return nil, err
}
fs.root = root
// put all root tree to establish base hashes for all top level directories in
// the file hierarchy
if _, err := root.Public.Put(); err != nil {
return nil, err
}
if _, err := root.Private.Put(); err != nil {
return nil, err
}
if _, err := root.Put(); err != nil {
return nil, err
}
return fs, nil
}
func FromCID(ctx context.Context, bserv blockservice.BlockService, rs ratchet.Store, id cid.Cid, rootKey Key, rootName PrivateName) (WNFS, error) {
log.Debugw("FromCID", "cid", id, "key", rootKey.Encode(), "name", string(rootName))
store := public.NewStore(ctx, bserv)
fs := &fileSystem{
ctx: ctx,
store: store,
}
root, err := loadRoot(ctx, store, rs, id, rootKey, rootName)
if err != nil {
return nil, fmt.Errorf("opening root tree %s:\n%w", id, err)
}
fs.root = root
return fs, nil
}
func (fsys *fileSystem) Context() context.Context { return fsys.ctx }
func (fsys *fileSystem) Name() string { return fsys.root.Name() }
func (fsys *fileSystem) Cid() cid.Cid { return fsys.root.Cid() }
func (fsys *fileSystem) Size() int64 { return fsys.root.Size() }
func (fsys *fileSystem) Links() base.Links { return fsys.root.Links() }
func (fsys *fileSystem) Stat() (fs.FileInfo, error) {
return fsys.root.Stat()
}
func (fsys *fileSystem) Read(p []byte) (n int, err error) {
return -1, errors.New("cannot read bytes of filsystem root")
}
func (fsys *fileSystem) Close() error { return nil }
func (fsys *fileSystem) ReadDir(n int) ([]fs.DirEntry, error) {
return fsys.root.ReadDir(n)
}
func (fsys *fileSystem) RootKey() Key {
return fsys.root.Private.Key()
}
func (fsys *fileSystem) PrivateName() (PrivateName, error) {
pn, err := fsys.root.Private.PrivateName()
if err != nil {
return "", err
}
return pn, nil
}
func (fsys *fileSystem) Ls(pathStr string) ([]fs.DirEntry, error) {
log.Debugw("fileSystem.Ls", "pathStr", pathStr)
tree, path, err := fsys.fsHierarchyDirectoryNode(pathStr)
if err != nil {
return nil, err
}
var file fs.File
if head, _ := path.Shift(); head == "" {
file = tree.(fs.File)
} else {
file, err = tree.Get(path)
if err != nil {
return nil, err
}
}
dir, ok := file.(fs.ReadDirFile)
if !ok {
return nil, fmt.Errorf("path %q is not a directory", pathStr)
}
return dir.ReadDir(-1)
}
func (fsys *fileSystem) Mkdir(pathStr string) error {
log.Debugw("fileSystem.Mkdir", "pathStr", pathStr)
tree, path, err := fsys.fsHierarchyDirectoryNode(pathStr)
if err != nil {
return err
}
_, err = tree.Mkdir(path)
return err
}
func (fsys *fileSystem) Open(pathStr string) (fs.File, error) {
log.Debugw("fileSystem.Open", "pathStr", pathStr)
tree, path, err := fsys.fsHierarchyDirectoryNode(pathStr)
if err != nil {
return nil, err
}
return tree.Get(path)
}
func (fsys *fileSystem) Cat(pathStr string) ([]byte, error) {
f, err := fsys.Open(pathStr)
if err != nil {
return nil, fmt.Errorf("opening %s:\n%w", pathStr, err)
}
return ioutil.ReadAll(f)
}
func (fsys *fileSystem) Cp(pathStr, srcPath string, src fs.FS) error {
log.Debugw("fileSystem.Cp", "pathStr", pathStr, "srcPath", srcPath)
node, relPath, err := fsys.fsHierarchyDirectoryNode(pathStr)
if err != nil {
return err
}
_, err = node.Copy(relPath, srcPath, src)
return err
}
func (fsys *fileSystem) Write(pathStr string, f fs.File) error {
log.Debugw("fileSystem.Write", "pathStr", pathStr)
_, isDataFile := f.(StructuredDataFile)
fi, err := f.Stat()
if err != nil {
return err
}
if fi.IsDir() && !isDataFile {
return errors.New("write only accepts files")
}
node, relPath, err := fsys.fsHierarchyDirectoryNode(pathStr)
if err != nil {
return err
}
_, err = node.Add(relPath, f)
return err
}
func (fsys *fileSystem) Rm(pathStr string) error {
log.Debugw("fileSystem.Rm", "pathStr", pathStr)
tree, relPath, err := fsys.fsHierarchyDirectoryNode(pathStr)
if err != nil {
return err
}
_, err = tree.Rm(relPath)
return err
}
func (fsys *fileSystem) History(ctx context.Context, pathStr string, max int) ([]HistoryEntry, error) {
if pathStr == "." || pathStr == "" {
return fsys.root.history(max)
}
node, relPath, err := fsys.fsHierarchyDirectoryNode(pathStr)
if err != nil {
return nil, err
}
f, err := node.Get(relPath)
if err != nil {
return nil, err
}
ch, ok := f.(base.Node)
if !ok {
return nil, fmt.Errorf("path %q is not a node", pathStr)
}
return ch.History(ctx, max)
}
func (fsys *fileSystem) fsHierarchyDirectoryNode(pathStr string) (dir base.Tree, relPath base.Path, err error) {
path, err := base.NewPath(pathStr)
if err != nil {
return nil, path, err
}
head, tail := path.Shift()
log.Debugw("fsHierarchyDirectoryNode", "head", head, "path", path)
switch head {
case "", ".":
return fsys.root, tail, nil
case FileHierarchyNamePublic:
return fsys.root.Public, tail, nil
case FileHierarchyNamePrivate:
return fsys.root.Private, tail, nil
// case FileHierarchyNamePretty:
// return fsys.root.Pretty, relPath, nil
default:
return nil, path, fmt.Errorf("%q is not a valid filesystem path", path)
}
}
type CommitResult struct {
Root cid.Cid
PrivateName *PrivateName
PrivateKey *Key
}
func (fsys *fileSystem) Commit() (res CommitResult, err error) {
if err = fsys.root.Commit(); err != nil {
return res, err
}
pn, err := fsys.PrivateName()
if err != nil {
return res, err
}
key := fsys.RootKey()
return CommitResult{
Root: fsys.root.id,
PrivateName: &pn,
PrivateKey: &key,
}, err
}
type rootHeader struct {
Info *public.Info
Previous *cid.Cid
Metadata *cid.Cid
Pretty *cid.Cid
Public *cid.Cid
Private *cid.Cid
}
func (h *rootHeader) encodeBlock() (blocks.Block, error) {
header := map[string]interface{}{
"info": h.Info.Map(),
"metadata": h.Metadata,
"previous": h.Previous,
base.PublicLinkName: h.Public,
base.PrivateLinkName: h.Private,
}
return cbornode.WrapObject(header, base.DefaultMultihashType, -1)
}
func decodeRootHeader(blk blocks.Block) (*rootHeader, error) {
env := map[string]interface{}{}
if err := cbornode.DecodeInto(blk.RawData(), &env); err != nil {
return nil, err
}
info, ok := env["info"].(map[string]interface{})
if !ok {
return nil, fmt.Errorf("header block is missing info field")
}
h := &rootHeader{
Info: public.InfoFromMap(info),
}
nd, err := cbornode.DecodeBlock(blk)
if err != nil {
return nil, err
}
for _, l := range nd.Links() {
switch l.Name {
case base.PreviousLinkName:
h.Previous = &l.Cid
case base.PublicLinkName:
h.Public = &l.Cid
case base.PrivateLinkName:
h.Private = &l.Cid
case base.PrettyLinkName:
h.Pretty = &l.Cid
case base.MetadataLinkName:
h.Metadata = &l.Cid
}
}
return h, nil
}
type rootTree struct {
store public.Store
pstore private.Store
id cid.Cid
tx cid.Cid // transaction start CID
rootKey Key
h *rootHeader
// Pretty *base.BareTree
metadata *public.LDFile
Public *public.Tree
Private *private.Root
}
var _ base.Tree = (*rootTree)(nil)
func newEmptyRootTree(store public.Store, rs ratchet.Store, rootKey Key) (root *rootTree, err error) {
root = &rootTree{
store: store,
rootKey: rootKey,
h: &rootHeader{
Info: public.NewInfo(base.NTDir),
},
Public: public.NewEmptyTree(store, FileHierarchyNamePublic),
// Pretty: &base.BareTree{},
}
root.pstore, err = private.NewStore(context.TODO(), store.Blockservice(), rs)
if err != nil {
return nil, err
}
privateRoot, err := private.NewEmptyRoot(store.Context(), root.pstore, FileHierarchyNamePrivate, rootKey)
if err != nil {
return nil, err
}
root.Private = privateRoot
return root, nil
}
func loadRoot(ctx context.Context, store public.Store, rs ratchet.Store, id cid.Cid, rootKey Key, rootName PrivateName) (r *rootTree, err error) {
r = &rootTree{store: store, id: id, tx: id, rootKey: rootKey}
blk, err := store.Blockservice().GetBlock(ctx, id)
if err != nil {
return nil, fmt.Errorf("loading root header block: %w", err)
}
if r.h, err = decodeRootHeader(blk); err != nil {
return nil, fmt.Errorf("decoding root header block: %w", err)
}
if r.h.Public != nil {
if r.Public, err = public.LoadTree(ctx, store, FileHierarchyNamePublic, *r.h.Public); err != nil {
return nil, fmt.Errorf("opening /%s tree %s:\n%w", FileHierarchyNamePublic, r.h.Public, err)
}
} else {
r.Public = public.NewEmptyTree(store, FileHierarchyNamePublic)
}
if r.h.Private != nil && !rootKey.IsEmpty() {
if r.pstore, err = private.LoadStore(ctx, store.Blockservice(), rs, *r.h.Private); err != nil {
return nil, err
}
if r.Private, err = private.LoadRoot(store.Context(), r.pstore, FileHierarchyNamePrivate, rootKey, rootName); err != nil {
return nil, fmt.Errorf("opening private root:\n%w", err)
}
} else {
if r.pstore, err = private.LoadStore(ctx, store.Blockservice(), rs, cid.Undef); err != nil {
return nil, err
}
if r.Private, err = private.LoadRoot(store.Context(), r.pstore, FileHierarchyNamePrivate, rootKey, rootName); err != nil {
return nil, fmt.Errorf("opening private root:\n%w", err)
}
}
return r, nil
}
func (r *rootTree) Put() (result base.PutResult, err error) {
ctx := context.TODO()
r.h.Info.Mtime = base.Timestamp().Unix()
if r.Public != nil {
id := r.Public.Cid()
r.h.Public = &id
}
if r.Private != nil {
id := r.Private.Cid()
r.h.Private = &id
}
if r.metadata != nil {
if _, err = r.metadata.Put(); err != nil {
return result, err
}
id := r.metadata.Cid()
r.h.Metadata = &id
}
// TODO(by): build pretty link
blk, err := r.h.encodeBlock()
if err != nil {
return result, fmt.Errorf("constructing root header block: %w", err)
}
if err = r.store.Blockservice().AddBlock(ctx, blk); err != nil {
return result, fmt.Errorf("storing root header block: %w", err)
}
r.id = blk.Cid()
log.Debugw("rootTree.Put", "linksLen", r.Links().Len(), "cid", r.id)
return result, nil
}
func (r *rootTree) Commit() error {
if r.tx.Defined() {
r.h.Previous = &r.tx
}
if _, err := r.Put(); err != nil {
return err
}
r.tx = r.id
return nil
}
func (r *rootTree) Cid() cid.Cid { return r.id }
func (r *rootTree) Name() string { return "wnfs" }
func (r *rootTree) Size() int64 { return r.h.Info.Size }
func (r *rootTree) IsDir() bool { return true }
func (r *rootTree) Mode() fs.FileMode { return fs.FileMode(r.h.Info.Mode) }
func (r *rootTree) Type() base.NodeType { return r.h.Info.Type }
func (r *rootTree) ModTime() time.Time { return time.Unix(r.h.Info.Mtime, 0) }
func (r *rootTree) Sys() interface{} { return r.store }
func (r *rootTree) SetMetadata(md interface{}) error {
r.metadata = public.NewLDFile(r.store, "", md)
return nil
}
func (r *rootTree) Metadata() (f base.LDFile, err error) {
if r.metadata == nil && r.h.Metadata != nil {
r.metadata, err = public.LoadLDFile(r.store.Context(), r.store, base.MetadataLinkName, *r.h.Metadata)
}
return r.metadata, err
}
func (r *rootTree) Links() base.Links {
links := base.NewLinks(
// base.Link{Cid: r.Pretty, Size: r.Pretty.Size(), Name: FileHierarchyNamePretty},
base.Link{Cid: r.Public.Cid(), Size: r.Public.Size(), Name: FileHierarchyNamePublic},
base.Link{Cid: r.Private.Cid(), Size: r.Private.Size(), Name: FileHierarchyNamePrivate},
)
if r.h.Previous != nil && !r.id.Equals(cid.Undef) {
links.Add(base.Link{Cid: *r.h.Previous, Name: PreviousLinkName})
}
return links
}
func (r *rootTree) Get(path base.Path) (f fs.File, err error) {
head, tail := path.Shift()
log.Debugw("rootTree.Get", "head", head, "path", path)
switch head {
case "", ".":
return r, nil
case FileHierarchyNamePublic:
if r.Public == nil {
r.Public = public.NewEmptyTree(r.store, FileHierarchyNamePublic)
}
return r.Public.Get(tail)
case FileHierarchyNamePrivate:
if r.Private == nil {
r.Private, err = private.NewEmptyRoot(r.store.Context(), r.pstore, FileHierarchyNamePrivate, r.rootKey)
if err != nil {
return nil, err
}
}
return r.Private.Get(tail)
// case FileHierarchyNamePretty:
// return fsys.root.Pretty, relPath, nil
default:
return nil, fmt.Errorf("%q is not a valid filesystem path", path)
}
}
func (r *rootTree) Copy(path base.Path, srcPathStr string, srcFS fs.FS) (res base.PutResult, err error) {
return nil, fmt.Errorf("cannot copy directly into root directory, only /public or /private")
}
func (r *rootTree) Add(path base.Path, f fs.File) (res base.PutResult, err error) {
return nil, fmt.Errorf("cannot write directly into root directory, only /public or /private")
}
func (r *rootTree) Mkdir(path base.Path) (res base.PutResult, err error) {
return nil, fmt.Errorf("cannot create directory within root. only /public or /private")
}
func (r *rootTree) Rm(path base.Path) (res base.PutResult, err error) {
return nil, fmt.Errorf("cannot remove directory from root")
}
func (r *rootTree) Stat() (fi fs.FileInfo, err error) {
return base.NewFSFileInfo(
"",
r.Size(),
fs.ModeDir,
// TODO (b5):
// mtime: time.Unix(t.metadata.UnixMeta.Mtime, 0),
time.Unix(0, 0),
r.store,
), nil
}
func (r *rootTree) ReadDir(n int) ([]fs.DirEntry, error) {
if n != -1 {
return nil, errors.New("wnfs root only supports n= -1 for directory listing")
}
links := []fs.DirEntry{}
if r.Private != nil {
links = append(links, base.NewFSDirEntry(FileHierarchyNamePrivate, false))
}
if r.Public != nil {
links = append(links, base.NewFSDirEntry(FileHierarchyNamePublic, false))
}
// TODO(b5): pretty dir
return links, nil
}
func (r *rootTree) Read([]byte) (int, error) {
return 0, fmt.Errorf("not a file")
}
func (r *rootTree) Close() error {
return nil
}
func (r *rootTree) AsHistoryEntry() base.HistoryEntry {
ent := base.HistoryEntry{
Cid: r.id,
Size: r.h.Info.Size,
Mtime: r.h.Info.Mtime,
Type: r.h.Info.Type,
Previous: r.h.Previous,
}
if r.Private != nil {
if n, err := r.Private.PrivateName(); err == nil {
ent.PrivateName = string(n)
}
ent.Key = r.Private.Key().Encode()
}
return ent
}
func (r *rootTree) History(context.Context, int) ([]base.HistoryEntry, error) {
return nil, fmt.Errorf("unfinished: root tree History method")
}
func (r *rootTree) history(max int) (hist []base.HistoryEntry, err error) {
ctx := r.store.Context()
store := r.store
hist = []base.HistoryEntry{
r.AsHistoryEntry(),
}
var privHist []base.HistoryEntry
if r.Private != nil {
if privHist, err = r.Private.History(ctx, -1); err != nil {
return hist, err
}
if len(privHist) >= 1 {
privHist = privHist[1:]
}
}
log.Debugw("private history", "history", privHist)
prev := hist[0].Previous
i := 0
for prev != nil {
blk, err := store.Blockservice().GetBlock(ctx, *prev)
if err != nil {
return nil, err
}
h, err := decodeRootHeader(blk)
if err != nil {
return nil, err
}
ent := base.HistoryEntry{
Cid: *prev,
Size: h.Info.Size,
Mtime: h.Info.Mtime,
Type: h.Info.Type,
Previous: h.Previous,
}
if len(privHist) > i {
ent.Key = privHist[i].Key
ent.PrivateName = privHist[i].PrivateName
}
i++
hist = append(hist, ent)
prev = ent.Previous
if len(hist) == max {
break
}
}
return hist, nil
}
func (r *rootTree) MergeDiverged(n base.Node) (result base.MergeResult, err error) {
return base.MergeResult{}, fmt.Errorf("unfinished: root tree MergeDiverged")
}
func Merge(ctx context.Context, aFs, bFs WNFS) (err error) {
a, ok := aFs.(*fileSystem)
if !ok {
return fmt.Errorf("'a' is not a wnfs filesystem")
}
b, ok := bFs.(*fileSystem)
if !ok {
return fmt.Errorf("'b' is not a wnfs filesystem")
}
log.Debugw("Merge", "acid", a.Cid(), "bcid", b.Cid())
if a.root.Public != nil && b.root.Public != nil {
res, err := public.Merge(ctx, a.root.Public, b.root.Public)
if err != nil {
return err
}
log.Debugw("merged public", "result", res.Cid)
fmt.Printf("/public:\t%s\n", res.Type)
a.root.Public, err = public.LoadTree(ctx, a.root.store, FileHierarchyNamePublic, res.Cid)
if err != nil {
return err
}
}
if a.root.Private != nil && b.root.Private != nil {
res, err := private.Merge(ctx, a.root.Private, b.root.Private)
if err != nil {
return err
}
log.Debugw("merged private", "result", res.Cid)
fmt.Printf("/private:\t%s\n", res.Type)
pk := &private.Key{}
if err := pk.Decode(res.Key); err != nil {
return err
}
a.root.Private, err = private.LoadRoot(a.root.store.Context(), a.root.pstore, FileHierarchyNamePrivate, *pk, private.Name(res.PrivateName))
if err != nil {
return err
}
}
return nil
}
func HAMTContents(ctx context.Context, bs blockservice.BlockService, id cid.Cid) (map[string]string, error) {
h, err := private.LoadHAMT(ctx, bs.Blockstore(), id)
if err != nil {
return nil, err
}
return h.Diagnostic(ctx), nil
}
type StructuredDataFile interface {
fs.File
Data() (interface{}, error)
}
type dataFile struct {
name string
content interface{}
}
var (
_ StructuredDataFile = (*dataFile)(nil)
_ fs.FileInfo = (*dataFile)(nil)
)
func NewLDFile(name string, data interface{}) StructuredDataFile {
return &dataFile{
name: name,
content: data,
}
}
func (df *dataFile) Stat() (fs.FileInfo, error) { return df, nil }
func (df *dataFile) Name() string { return df.name }
func (df *dataFile) IsDir() bool { return true }
func (df *dataFile) Size() int64 { return -1 }
func (df *dataFile) ModTime() time.Time { return time.Time{} }
func (df *dataFile) Mode() fs.FileMode { return base.ModeDefault }
func (df *dataFile) Sys() interface{} { return nil }
func (df *dataFile) Read(p []byte) (int, error) {
return 0, fmt.Errorf("not implemented: dataFile.Read")
}
func (df *dataFile) Close() error { return nil }
func (df *dataFile) Data() (interface{}, error) {
// TODO(b5): horrible. This is just to coerce to usable types. In the real
// world we need a cbor serialization lib that can handle arbitrary types
// *and* recognizes cid.CID natively
data, err := json.Marshal(df.content)
if err != nil {
return nil, err
}
res := map[string]interface{}{}
err = json.Unmarshal(data, &res)
return res, err
}
type Factory struct {
BlockService blockservice.BlockService
Ratchets ratchet.Store
Decryption private.DecryptionStore
}
func (fac Factory) Load(ctx context.Context, id cid.Cid) (fs WNFS, err error) {
var (
name private.Name
key private.Key
)
if fac.Decryption != nil {
name, key, err = fac.Decryption.DecryptionFields(id)
if err != nil && !errors.Is(err, base.ErrNotFound) {
return nil, err
}
err = nil
}
return FromCID(ctx, fac.BlockService, fac.Ratchets, id, key, name)
}
func (fac Factory) LoadWithDecryption(ctx context.Context, id cid.Cid, name private.Name, key private.Key) (fs WNFS, err error) {
return FromCID(ctx, fac.BlockService, fac.Ratchets, id, key, name)
}
func NodeIsPrivate(n Node) bool {
switch n.(type) {
case *private.Root, *private.Tree, *private.File, *private.LDFile:
return true
default:
return false
}
}