提交 d1b6ccaa 作者: Łukasz Magiera

coreapi: stream only ls, handle storting in command

License: MIT
Signed-off-by: 's avatarŁukasz Magiera <magik6k@gmail.com>
上级 73f1e2db
......@@ -4,6 +4,7 @@ import (
"fmt"
"io"
"os"
"sort"
"text/tabwriter"
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
......@@ -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,
......@@ -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 {
......@@ -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 {
......
......@@ -43,8 +43,6 @@ type UnixfsAddSettings struct {
}
type UnixfsLsSettings struct {
Async bool
ResolveType bool
ResolveSize bool
}
......@@ -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,
}
......@@ -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
......
......@@ -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)
}
......@@ -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) {
......
......@@ -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)
}
......@@ -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)
}
......@@ -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 {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论