Skip to content

Commit

Permalink
fix ls command to use the new coreinterface types
Browse files Browse the repository at this point in the history
See: ipfs/interface-go-ipfs-core#14

License: MIT
Signed-off-by: Steven Allen <steven@stebalien.com>
  • Loading branch information
Stebalien committed Mar 5, 2019
1 parent ab6733e commit d28c2ca
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 25 deletions.
42 changes: 29 additions & 13 deletions core/commands/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

cmdkit "github.com/ipfs/go-ipfs-cmdkit"
cmds "github.com/ipfs/go-ipfs-cmds"
unixfs_pb "github.com/ipfs/go-unixfs/pb"
unixfs "github.com/ipfs/go-unixfs/unixfs"
iface "github.com/ipfs/interface-go-ipfs-core"
options "github.com/ipfs/interface-go-ipfs-core/options"
)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
32 changes: 20 additions & 12 deletions core/coreapi/unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
// `<link base58 hash> <link size in bytes> <link name>`
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
Expand All @@ -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
Expand All @@ -201,16 +202,23 @@ 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()
}
}

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)
Expand All @@ -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}}

Expand Down

0 comments on commit d28c2ca

Please sign in to comment.