From 81621396a044e0527b5e80e35807f1004607ccc1 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Mon, 4 Mar 2019 20:29:16 -0800 Subject: [PATCH] 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 --- core/commands/ls.go | 42 +++++++++++++++++++++++++++++------------- core/coreapi/unixfs.go | 32 ++++++++++++++++++++------------ 2 files changed, 49 insertions(+), 25 deletions(-) diff --git a/core/commands/ls.go b/core/commands/ls.go index 23cca60a66b..016116cf07c 100644 --- a/core/commands/ls.go +++ b/core/commands/ls.go @@ -11,6 +11,8 @@ import ( cmdkit "github.com/ipfs/go-ipfs-cmdkit" cmds "github.com/ipfs/go-ipfs-cmds" + unixfs "github.com/ipfs/go-unixfs" + unixfs_pb "github.com/ipfs/go-unixfs/pb" iface "github.com/ipfs/interface-go-ipfs-core" options "github.com/ipfs/interface-go-ipfs-core/options" ) @@ -19,7 +21,7 @@ import ( type LsLink struct { Name, Hash string Size uint64 - Type iface.FileType + Type unixfs_pb.Data_DataType } // LsObject is an element of LsOutput @@ -144,12 +146,21 @@ The JSON output contains type information. if link.Err != nil { return link.Err } + var ftype unixfs_pb.Data_DataType + switch link.Type { + case iface.TFile: + ftype = unixfs.TFile + case iface.TDirectory: + ftype = unixfs.TDirectory + case iface.TSymlink: + ftype = unixfs.TSymlink + } lsLink := LsLink{ - Name: link.Link.Name, - Hash: enc.Encode(link.Link.Cid), + Name: link.Name, + Hash: enc.Encode(link.Cid), Size: link.Size, - Type: link.Type, + Type: ftype, } if err := processLink(paths[i], lsLink); err != nil { return err @@ -227,15 +238,20 @@ func tabularOutput(req *cmds.Request, w io.Writer, out *LsOutput, lastObjectHash } for _, link := range object.Links { - s := "%[1]s\t%[3]s\n" - - switch { - case link.Type == iface.TDirectory && size: - s = "%[1]s\t-\t%[3]s/\n" - case link.Type == iface.TDirectory && !size: - s = "%[1]s\t%[3]s/\n" - case size: - s = "%s\t%v\t%s\n" + var s string + switch link.Type { + case unixfs.TDirectory, unixfs.THAMTShard, unixfs.TMetadata: + if size { + s = "%[1]s\t-\t%[3]s/\n" + } else { + s = "%[1]s\t%[3]s/\n" + } + default: + if size { + s = "%s\t%v\t%s\n" + } else { + s = "%[1]s\t%[3]s\n" + } } fmt.Fprintf(tw, s, link.Hash, link.Size, link.Name) diff --git a/core/coreapi/unixfs.go b/core/coreapi/unixfs.go index d773fe0c157..627a6d06eab 100644 --- a/core/coreapi/unixfs.go +++ b/core/coreapi/unixfs.go @@ -145,7 +145,7 @@ func (api *UnixfsAPI) Get(ctx context.Context, p coreiface.Path) (files.Node, er // Ls returns the contents of an IPFS or IPNS object(s) at path p, with the format: // ` ` -func (api *UnixfsAPI) Ls(ctx context.Context, p coreiface.Path, opts ...options.UnixfsLsOption) (<-chan coreiface.LsLink, error) { +func (api *UnixfsAPI) Ls(ctx context.Context, p coreiface.Path, opts ...options.UnixfsLsOption) (<-chan coreiface.DirEntry, error) { settings, err := options.UnixfsLsOptions(opts...) if err != nil { return nil, err @@ -170,26 +170,27 @@ func (api *UnixfsAPI) Ls(ctx context.Context, p coreiface.Path, opts ...options. return uses.lsFromLinksAsync(ctx, dir, settings) } -func (api *UnixfsAPI) processLink(ctx context.Context, linkres ft.LinkResult, settings *options.UnixfsLsSettings) coreiface.LsLink { - lnk := coreiface.LsLink{ - Link: linkres.Link, +func (api *UnixfsAPI) processLink(ctx context.Context, linkres ft.LinkResult, settings *options.UnixfsLsSettings) coreiface.DirEntry { + lnk := coreiface.DirEntry{ + Name: linkres.Link.Name, + Cid: linkres.Link.Cid, Err: linkres.Err, } if lnk.Err != nil { return lnk } - switch lnk.Link.Cid.Type() { + switch lnk.Cid.Type() { case cid.Raw: // No need to check with raw leaves lnk.Type = coreiface.TFile - lnk.Size = lnk.Link.Size + lnk.Size = linkres.Link.Size case cid.DagProtobuf: if !settings.ResolveChildren { break } - linkNode, err := lnk.Link.GetNode(ctx, api.dag) + linkNode, err := linkres.Link.GetNode(ctx, api.dag) if err != nil { lnk.Err = err break @@ -201,7 +202,14 @@ func (api *UnixfsAPI) processLink(ctx context.Context, linkres ft.LinkResult, se lnk.Err = err break } - lnk.Type = coreiface.FileType(d.Type()) + switch d.Type() { + case ft.TFile, ft.TRaw: + lnk.Type = coreiface.TFile + case ft.THAMTShard, ft.TDirectory, ft.TMetadata: + lnk.Type = coreiface.TDirectory + case ft.TSymlink: + lnk.Type = coreiface.TSymlink + } lnk.Size = d.FileSize() } } @@ -209,8 +217,8 @@ func (api *UnixfsAPI) processLink(ctx context.Context, linkres ft.LinkResult, se return lnk } -func (api *UnixfsAPI) lsFromLinksAsync(ctx context.Context, dir uio.Directory, settings *options.UnixfsLsSettings) (<-chan coreiface.LsLink, error) { - out := make(chan coreiface.LsLink) +func (api *UnixfsAPI) lsFromLinksAsync(ctx context.Context, dir uio.Directory, settings *options.UnixfsLsSettings) (<-chan coreiface.DirEntry, error) { + out := make(chan coreiface.DirEntry) go func() { defer close(out) @@ -226,8 +234,8 @@ func (api *UnixfsAPI) lsFromLinksAsync(ctx context.Context, dir uio.Directory, s return out, nil } -func (api *UnixfsAPI) lsFromLinks(ctx context.Context, ndlinks []*ipld.Link, settings *options.UnixfsLsSettings) (<-chan coreiface.LsLink, error) { - links := make(chan coreiface.LsLink, len(ndlinks)) +func (api *UnixfsAPI) lsFromLinks(ctx context.Context, ndlinks []*ipld.Link, settings *options.UnixfsLsSettings) (<-chan coreiface.DirEntry, error) { + links := make(chan coreiface.DirEntry, len(ndlinks)) for _, l := range ndlinks { lr := ft.LinkResult{Link: &ipld.Link{Name: l.Name, Size: l.Size, Cid: l.Cid}}