/
ipld_car.go
57 lines (46 loc) · 1.16 KB
/
ipld_car.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
package internal
import (
"context"
"fmt"
"io"
"github.com/ipfs/go-cid"
"github.com/ipld/go-car/v2/storage"
"github.com/ipld/go-ipld-prime/linking"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
)
func LoadCAR(car_fd io.ReaderAt, root_cid cid.Cid) *IPLD_Storage {
rcar, err := storage.OpenReadable(car_fd)
if err != nil {
panic(err)
}
rcar.Index() // TODO: needed?
/*
fmt.Printf("CID: %s\n", root_cid) // XXX
ParseCID(root_cid.String()) // XXX
*/
root_cid_lnk := cidlink.Link{root_cid}
cid_exists, err := rcar.Has(context.TODO(), root_cid_lnk.Binary())
if err != nil {
panic(err)
}
if !cid_exists {
panic(fmt.Errorf("Your requested CID: %s, is not in CAR file.", root_cid))
}
//fmt.Printf("%v\n", roots) // XXX
fmt.Printf("\nRoots: \n")
roots := rcar.Roots()
for _, rcid := range roots {
fmt.Printf(" - %v\n", rcid)
}
fmt.Printf("\n")
lctx := linking.LinkContext{}
lsys := cidlink.DefaultLinkSystem()
lsys.SetReadStorage(rcar)
ipld_storage := NewIPLD_Storage(rcar, lctx, lsys)
root_folder, err := ipld_storage.read_folder(root_cid)
if err != nil {
panic(err)
}
fmt.Printf("%s\n", root_folder) // XXX
return ipld_storage
}