Skip to content

Commit

Permalink
coreapi: stream only ls, handle storting in command
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <[email protected]>
  • Loading branch information
magik6k committed Feb 2, 2019
1 parent 73f1e2d commit d1b6cca
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 53 deletions.
12 changes: 6 additions & 6 deletions core/commands/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"os"
"sort"
"text/tabwriter"

cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
Expand Down Expand Up @@ -112,6 +113,10 @@ The JSON output contains type information.
return nil
}, func(i int) {
// after each dir
sort.Slice(outputLinks, func(i, j int) bool {
return outputLinks[i].Name < outputLinks[j].Name
})

output[i] = LsObject{
Hash: paths[i],
Links: outputLinks,
Expand All @@ -131,7 +136,6 @@ The JSON output contains type information.
}

results, err := api.Unixfs().Ls(req.Context, p,
options.Unixfs.Async(stream),
options.Unixfs.ResolveType(resolveType),
options.Unixfs.ResolveSize(resolveSize))
if err != nil {
Expand All @@ -156,11 +160,7 @@ The JSON output contains type information.
}
dirDone(i)
}
if err := done(); err != nil {
return err
}

return nil
return done()
},
PostRun: cmds.PostRunMap{
cmds.CLI: func(res cmds.Response, re cmds.ResponseEmitter) error {
Expand Down
14 changes: 0 additions & 14 deletions core/coreapi/interface/options/unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ type UnixfsAddSettings struct {
}

type UnixfsLsSettings struct {
Async bool

ResolveType bool
ResolveSize bool
}
Expand Down Expand Up @@ -132,8 +130,6 @@ func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, cid.Prefix,

func UnixfsLsOptions(opts ...UnixfsLsOption) (*UnixfsLsSettings, error) {
options := &UnixfsLsSettings{
Async: true,

ResolveSize: true,
ResolveType: true,
}
Expand Down Expand Up @@ -317,16 +313,6 @@ func (unixfsOpts) Nocopy(enable bool) UnixfsAddOption {
}
}

// Async tells ls to return results as soon as they are available, which can be
// useful for listing HAMT directories. When this option is set to true returned
// results won't be returned in order
func (unixfsOpts) Async(async bool) UnixfsLsOption {
return func(settings *UnixfsLsSettings) error {
settings.Async = async
return nil
}
}

func (unixfsOpts) ResolveSize(resolve bool) UnixfsLsOption {
return func(settings *UnixfsLsSettings) error {
settings.ResolveSize = resolve
Expand Down
21 changes: 1 addition & 20 deletions core/coreapi/interface/tests/unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ func (tp *provider) TestLs(t *testing.T) {
t.Error(err)
}

links, err := api.Unixfs().Ls(ctx, p, options.Unixfs.Async(false))
links, err := api.Unixfs().Ls(ctx, p)
if err != nil {
t.Error(err)
}
Expand All @@ -767,25 +767,6 @@ func (tp *provider) TestLs(t *testing.T) {
if _, ok := <-links; ok {
t.Errorf("didn't expect a second link")
}

links, err = api.Unixfs().Ls(ctx, p, options.Unixfs.Async(true))
if err != nil {
t.Error(err)
}

link = (<-links).Link
if link.Size != 23 {
t.Fatalf("expected size = 23, got %d", link.Size)
}
if link.Name != "name-of-file" {
t.Fatalf("expected name = name-of-file, got %s", link.Name)
}
if link.Cid.String() != "QmX3qQVKxDGz3URVC3861Z3CKtQKGBn6ffXRBBWGMFz9Lr" {
t.Fatalf("expected cid = QmX3qQVKxDGz3URVC3861Z3CKtQKGBn6ffXRBBWGMFz9Lr, got %s", link.Cid)
}
if _, ok := <-links; ok {
t.Errorf("didn't expect a second link")
}
}

func (tp *provider) TestEntriesExpired(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion core/coreapi/interface/unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type UnixfsAPI interface {
// to operations performed on the returned file
Get(context.Context, Path) (files.Node, error)

// Ls returns the list of links in a directory
// Ls returns the list of links in a directory. Links aren't guaranteed to be
// returned in order
Ls(context.Context, Path, ...options.UnixfsLsOption) (<-chan LsLink, error)
}
12 changes: 0 additions & 12 deletions core/coreapi/unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,6 @@ func (api *UnixfsAPI) Ls(ctx context.Context, p coreiface.Path, opts ...options.
return nil, err
}

if !settings.Async {
return uses.lsFromDir(ctx, dir, settings)
}

return uses.lsFromLinksAsync(ctx, dir, settings)
}

Expand Down Expand Up @@ -234,14 +230,6 @@ func (api *UnixfsAPI) lsFromLinksAsync(ctx context.Context, dir uio.Directory, s
return out, nil
}

func (api *UnixfsAPI) lsFromDir(ctx context.Context, dir uio.Directory, settings *options.UnixfsLsSettings) (<-chan coreiface.LsLink, error) {
l, err := dir.Links(ctx)
if err != nil {
return nil, err
}
return api.lsFromLinks(ctx, l, settings)
}

func (api *UnixfsAPI) lsFromLinks(ctx context.Context, ndlinks []*ipld.Link, settings *options.UnixfsLsSettings) (<-chan coreiface.LsLink, error) {
links := make(chan coreiface.LsLink, len(ndlinks))
for _, l := range ndlinks {
Expand Down

0 comments on commit d1b6cca

Please sign in to comment.