@cryptotaxi247 / kubo / commits / 81621396a

fix ls command to use the new coreinterface types

See: https://github.com/ipfs/interface-go-ipfs-core/pull/14 License: MIT Signed-off-by: Steven Allen <steven@stebalien.com>

Steven Allen committed Mar 4, 2019 at 20:29 UTC 81621396a044e0527b5e80e35807f1004607ccc1
2 files changed +49 -25
core/commands/ls.go
+29 -13
@@ -11,6 +11,8 @@ import (
11
12 cmdkit "github.com/ipfs/go-ipfs-cmdkit"
13 cmds "github.com/ipfs/go-ipfs-cmds"
14 + unixfs "github.com/ipfs/go-unixfs"
15 + unixfs_pb "github.com/ipfs/go-unixfs/pb"
16 iface "github.com/ipfs/interface-go-ipfs-core"
17 options "github.com/ipfs/interface-go-ipfs-core/options"
18 )
@@ -19,7 +21,7 @@ import (
21 type LsLink struct {
22 Name, Hash string
23 Size uint64
22 - Type iface.FileType
24 + Type unixfs_pb.Data_DataType
25 }
26
27 // LsObject is an element of LsOutput
@@ -144,12 +146,21 @@ The JSON output contains type information.
146 if link.Err != nil {
147 return link.Err
148 }
149 + var ftype unixfs_pb.Data_DataType
150 + switch link.Type {
151 + case iface.TFile:
152 + ftype = unixfs.TFile
153 + case iface.TDirectory:
154 + ftype = unixfs.TDirectory
155 + case iface.TSymlink:
156 + ftype = unixfs.TSymlink
157 + }
158 lsLink := LsLink{
148 - Name: link.Link.Name,
149 - Hash: enc.Encode(link.Link.Cid),
159 + Name: link.Name,
160 + Hash: enc.Encode(link.Cid),
161
162 Size: link.Size,
152 - Type: link.Type,
163 + Type: ftype,
164 }
165 if err := processLink(paths[i], lsLink); err != nil {
166 return err
@@ -227,15 +238,20 @@ func tabularOutput(req *cmds.Request, w io.Writer, out *LsOutput, lastObjectHash
238 }
239
240 for _, link := range object.Links {
230 - s := "%[1]s\t%[3]s\n"
231 -
232 - switch {
233 - case link.Type == iface.TDirectory && size:
234 - s = "%[1]s\t-\t%[3]s/\n"
235 - case link.Type == iface.TDirectory && !size:
236 - s = "%[1]s\t%[3]s/\n"
237 - case size:
238 - s = "%s\t%v\t%s\n"
241 + var s string
242 + switch link.Type {
243 + case unixfs.TDirectory, unixfs.THAMTShard, unixfs.TMetadata:
244 + if size {
245 + s = "%[1]s\t-\t%[3]s/\n"
246 + } else {
247 + s = "%[1]s\t%[3]s/\n"
248 + }
249 + default:
250 + if size {
251 + s = "%s\t%v\t%s\n"
252 + } else {
253 + s = "%[1]s\t%[3]s\n"
254 + }
255 }
256
257 fmt.Fprintf(tw, s, link.Hash, link.Size, link.Name)
core/coreapi/unixfs.go
+20 -12
@@ -145,7 +145,7 @@ func (api *UnixfsAPI) Get(ctx context.Context, p coreiface.Path) (files.Node, er
145
146 // Ls returns the contents of an IPFS or IPNS object(s) at path p, with the format:
147 // `<link base58 hash> <link size in bytes> <link name>`
148 -func (api *UnixfsAPI) Ls(ctx context.Context, p coreiface.Path, opts ...options.UnixfsLsOption) (<-chan coreiface.LsLink, error) {
148 +func (api *UnixfsAPI) Ls(ctx context.Context, p coreiface.Path, opts ...options.UnixfsLsOption) (<-chan coreiface.DirEntry, error) {
149 settings, err := options.UnixfsLsOptions(opts...)
150 if err != nil {
151 return nil, err
@@ -170,26 +170,27 @@ func (api *UnixfsAPI) Ls(ctx context.Context, p coreiface.Path, opts ...options.
170 return uses.lsFromLinksAsync(ctx, dir, settings)
171 }
172
173 -func (api *UnixfsAPI) processLink(ctx context.Context, linkres ft.LinkResult, settings *options.UnixfsLsSettings) coreiface.LsLink {
174 - lnk := coreiface.LsLink{
175 - Link: linkres.Link,
173 +func (api *UnixfsAPI) processLink(ctx context.Context, linkres ft.LinkResult, settings *options.UnixfsLsSettings) coreiface.DirEntry {
174 + lnk := coreiface.DirEntry{
175 + Name: linkres.Link.Name,
176 + Cid: linkres.Link.Cid,
177 Err: linkres.Err,
178 }
179 if lnk.Err != nil {
180 return lnk
181 }
182
182 - switch lnk.Link.Cid.Type() {
183 + switch lnk.Cid.Type() {
184 case cid.Raw:
185 // No need to check with raw leaves
186 lnk.Type = coreiface.TFile
186 - lnk.Size = lnk.Link.Size
187 + lnk.Size = linkres.Link.Size
188 case cid.DagProtobuf:
189 if !settings.ResolveChildren {
190 break
191 }
192
192 - linkNode, err := lnk.Link.GetNode(ctx, api.dag)
193 + linkNode, err := linkres.Link.GetNode(ctx, api.dag)
194 if err != nil {
195 lnk.Err = err
196 break
@@ -201,7 +202,14 @@ func (api *UnixfsAPI) processLink(ctx context.Context, linkres ft.LinkResult, se
202 lnk.Err = err
203 break
204 }
204 - lnk.Type = coreiface.FileType(d.Type())
205 + switch d.Type() {
206 + case ft.TFile, ft.TRaw:
207 + lnk.Type = coreiface.TFile
208 + case ft.THAMTShard, ft.TDirectory, ft.TMetadata:
209 + lnk.Type = coreiface.TDirectory
210 + case ft.TSymlink:
211 + lnk.Type = coreiface.TSymlink
212 + }
213 lnk.Size = d.FileSize()
214 }
215 }
@@ -209,8 +217,8 @@ func (api *UnixfsAPI) processLink(ctx context.Context, linkres ft.LinkResult, se
217 return lnk
218 }
219
212 -func (api *UnixfsAPI) lsFromLinksAsync(ctx context.Context, dir uio.Directory, settings *options.UnixfsLsSettings) (<-chan coreiface.LsLink, error) {
213 - out := make(chan coreiface.LsLink)
220 +func (api *UnixfsAPI) lsFromLinksAsync(ctx context.Context, dir uio.Directory, settings *options.UnixfsLsSettings) (<-chan coreiface.DirEntry, error) {
221 + out := make(chan coreiface.DirEntry)
222
223 go func() {
224 defer close(out)
@@ -226,8 +234,8 @@ func (api *UnixfsAPI) lsFromLinksAsync(ctx context.Context, dir uio.Directory, s
234 return out, nil
235 }
236
229 -func (api *UnixfsAPI) lsFromLinks(ctx context.Context, ndlinks []*ipld.Link, settings *options.UnixfsLsSettings) (<-chan coreiface.LsLink, error) {
230 - links := make(chan coreiface.LsLink, len(ndlinks))
237 +func (api *UnixfsAPI) lsFromLinks(ctx context.Context, ndlinks []*ipld.Link, settings *options.UnixfsLsSettings) (<-chan coreiface.DirEntry, error) {
238 + links := make(chan coreiface.DirEntry, len(ndlinks))
239 for _, l := range ndlinks {
240 lr := ft.LinkResult{Link: &ipld.Link{Name: l.Name, Size: l.Size, Cid: l.Cid}}
241