-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathlist.go
232 lines (214 loc) · 5.54 KB
/
list.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
package main
import (
"bytes"
"fmt"
"io"
"os"
"path"
"github.com/dustin/go-humanize"
"github.com/ipfs/go-cid"
data "github.com/ipfs/go-unixfsnode/data"
"github.com/ipfs/go-unixfsnode/hamt"
carv2 "github.com/ipld/go-car/v2"
"github.com/ipld/go-car/v2/blockstore"
dagpb "github.com/ipld/go-codec-dagpb"
"github.com/ipld/go-ipld-prime"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/multiformats/go-multicodec"
"github.com/urfave/cli/v2"
)
// ListCar is a command to output the cids in a car.
func ListCar(c *cli.Context) error {
var err error
outStream := os.Stdout
if c.Args().Len() >= 2 {
outStream, err = os.Create(c.Args().Get(1))
if err != nil {
return err
}
}
defer outStream.Close()
if c.Bool("unixfs") || c.Bool("unixfs-blocks") {
return listUnixfs(c, outStream)
}
inStream := os.Stdin
if c.Args().Len() >= 1 {
inStream, err = os.Open(c.Args().First())
if err != nil {
return err
}
defer inStream.Close()
}
rd, err := carv2.NewBlockReader(inStream)
if err != nil {
return err
}
for {
blk, err := rd.Next()
if err != nil {
if err == io.EOF {
break
}
return err
}
if c.Bool("verbose") {
fmt.Fprintf(outStream, "%s: %s\n",
multicodec.Code(blk.Cid().Prefix().Codec).String(),
blk.Cid())
if blk.Cid().Prefix().Codec == uint64(multicodec.DagPb) {
// parse as dag-pb
builder := dagpb.Type.PBNode.NewBuilder()
if err := dagpb.DecodeBytes(builder, blk.RawData()); err != nil {
fmt.Fprintf(outStream, "\tnot interpretable as dag-pb: %s\n", err)
continue
}
n := builder.Build()
pbn, ok := n.(dagpb.PBNode)
if !ok {
continue
}
dl := 0
if pbn.Data.Exists() {
dl = len(pbn.Data.Must().Bytes())
}
fmt.Fprintf(outStream, "\t%d links. %d bytes\n", pbn.Links.Length(), dl)
// example link:
li := pbn.Links.ListIterator()
max := 3
for !li.Done() {
_, l, _ := li.Next()
max--
pbl, ok := l.(dagpb.PBLink)
if ok && max >= 0 {
hsh := "<unknown>"
lnk, ok := pbl.Hash.Link().(cidlink.Link)
if ok {
hsh = lnk.Cid.String()
}
name := "<no name>"
if pbl.Name.Exists() {
name = pbl.Name.Must().String()
}
size := 0
if pbl.Tsize.Exists() {
size = int(pbl.Tsize.Must().Int())
}
fmt.Fprintf(outStream, "\t\t%s[%s] %s\n", name, humanize.Bytes(uint64(size)), hsh)
}
}
if max < 0 {
fmt.Fprintf(outStream, "\t\t(%d total)\n", 3-max)
}
// see if it's unixfs.
ufd, err := data.DecodeUnixFSData(pbn.Data.Must().Bytes())
if err != nil {
fmt.Fprintf(outStream, "\tnot interpretable as unixfs: %s\n", err)
continue
}
fmt.Fprintf(outStream, "\tUnixfs %s\n", data.DataTypeNames[ufd.FieldDataType().Int()])
}
} else {
fmt.Fprintf(outStream, "%s\n", blk.Cid())
}
}
return err
}
func listUnixfs(c *cli.Context, outStream io.Writer) error {
if c.Args().Len() == 0 {
return fmt.Errorf("must provide file to read from. unixfs reading requires random access")
}
bs, err := blockstore.OpenReadOnly(c.Args().First())
if err != nil {
return err
}
ls := cidlink.DefaultLinkSystem()
ls.TrustedStorage = true
ls.StorageReadOpener = func(_ ipld.LinkContext, l ipld.Link) (io.Reader, error) {
cl, ok := l.(cidlink.Link)
if !ok {
return nil, fmt.Errorf("not a cidlink")
}
blk, err := bs.Get(c.Context, cl.Cid)
if err != nil {
return nil, err
}
return bytes.NewBuffer(blk.RawData()), nil
}
roots, err := bs.Roots()
if err != nil {
return err
}
for _, r := range roots {
if err := printUnixFSNode(c, "", r, &ls, outStream); err != nil {
return err
}
}
return nil
}
func printUnixFSNode(c *cli.Context, prefix string, node cid.Cid, ls *ipld.LinkSystem, outStream io.Writer) error {
// it might be a raw file (bytes) node. if so, not actually an error.
if node.Prefix().Codec == cid.Raw {
return nil
}
pbn, err := ls.Load(ipld.LinkContext{}, cidlink.Link{Cid: node}, dagpb.Type.PBNode)
if err != nil {
return err
}
pbnode := pbn.(dagpb.PBNode)
ufd, err := data.DecodeUnixFSData(pbnode.Data.Must().Bytes())
if err != nil {
return err
}
if ufd.FieldDataType().Int() == data.Data_Directory {
i := pbnode.Links.Iterator()
for !i.Done() {
_, l := i.Next()
name := path.Join(prefix, l.Name.Must().String())
if c.Bool("unixfs-blocks") {
cidL, _ := l.Hash.AsLink()
fmt.Fprintf(outStream, "%s %s\n", cidL.(cidlink.Link).Cid, name)
} else {
fmt.Fprintf(outStream, "%s\n", name)
}
// recurse into the file/directory
cl, err := l.Hash.AsLink()
if err != nil {
return err
}
if cidl, ok := cl.(cidlink.Link); ok {
if err := printUnixFSNode(c, name, cidl.Cid, ls, outStream); err != nil {
return err
}
}
}
} else if ufd.FieldDataType().Int() == data.Data_HAMTShard {
hn, err := hamt.AttemptHAMTShardFromNode(c.Context, pbn, ls)
if err != nil {
return err
}
i := hn.Iterator()
for !i.Done() {
n, l := i.Next()
if c.Bool("unixfs-blocks") {
cl, _ := l.AsLink()
fmt.Fprintf(outStream, "%s %s\n", cl.(cidlink.Link).Cid, path.Join(prefix, n.String()))
} else {
fmt.Fprintf(outStream, "%s\n", path.Join(prefix, n.String()))
}
// recurse into the file/directory
cl, err := l.AsLink()
if err != nil {
return err
}
if cidl, ok := cl.(cidlink.Link); ok {
if err := printUnixFSNode(c, path.Join(prefix, n.String()), cidl.Cid, ls, outStream); err != nil {
return err
}
}
}
} else {
// file, file chunk, symlink, other un-named entities.
return nil
}
return nil
}