提交 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 ( ...@@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"sort"
"text/tabwriter" "text/tabwriter"
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv" cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
...@@ -112,6 +113,10 @@ The JSON output contains type information. ...@@ -112,6 +113,10 @@ The JSON output contains type information.
return nil return nil
}, func(i int) { }, func(i int) {
// after each dir // after each dir
sort.Slice(outputLinks, func(i, j int) bool {
return outputLinks[i].Name < outputLinks[j].Name
})
output[i] = LsObject{ output[i] = LsObject{
Hash: paths[i], Hash: paths[i],
Links: outputLinks, Links: outputLinks,
...@@ -131,7 +136,6 @@ The JSON output contains type information. ...@@ -131,7 +136,6 @@ The JSON output contains type information.
} }
results, err := api.Unixfs().Ls(req.Context, p, results, err := api.Unixfs().Ls(req.Context, p,
options.Unixfs.Async(stream),
options.Unixfs.ResolveType(resolveType), options.Unixfs.ResolveType(resolveType),
options.Unixfs.ResolveSize(resolveSize)) options.Unixfs.ResolveSize(resolveSize))
if err != nil { if err != nil {
...@@ -156,11 +160,7 @@ The JSON output contains type information. ...@@ -156,11 +160,7 @@ The JSON output contains type information.
} }
dirDone(i) dirDone(i)
} }
if err := done(); err != nil { return done()
return err
}
return nil
}, },
PostRun: cmds.PostRunMap{ PostRun: cmds.PostRunMap{
cmds.CLI: func(res cmds.Response, re cmds.ResponseEmitter) error { cmds.CLI: func(res cmds.Response, re cmds.ResponseEmitter) error {
......
...@@ -43,8 +43,6 @@ type UnixfsAddSettings struct { ...@@ -43,8 +43,6 @@ type UnixfsAddSettings struct {
} }
type UnixfsLsSettings struct { type UnixfsLsSettings struct {
Async bool
ResolveType bool ResolveType bool
ResolveSize bool ResolveSize bool
} }
...@@ -132,8 +130,6 @@ func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, cid.Prefix, ...@@ -132,8 +130,6 @@ func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, cid.Prefix,
func UnixfsLsOptions(opts ...UnixfsLsOption) (*UnixfsLsSettings, error) { func UnixfsLsOptions(opts ...UnixfsLsOption) (*UnixfsLsSettings, error) {
options := &UnixfsLsSettings{ options := &UnixfsLsSettings{
Async: true,
ResolveSize: true, ResolveSize: true,
ResolveType: true, ResolveType: true,
} }
...@@ -317,16 +313,6 @@ func (unixfsOpts) Nocopy(enable bool) UnixfsAddOption { ...@@ -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 { func (unixfsOpts) ResolveSize(resolve bool) UnixfsLsOption {
return func(settings *UnixfsLsSettings) error { return func(settings *UnixfsLsSettings) error {
settings.ResolveSize = resolve settings.ResolveSize = resolve
......
...@@ -749,7 +749,7 @@ func (tp *provider) TestLs(t *testing.T) { ...@@ -749,7 +749,7 @@ func (tp *provider) TestLs(t *testing.T) {
t.Error(err) t.Error(err)
} }
links, err := api.Unixfs().Ls(ctx, p, options.Unixfs.Async(false)) links, err := api.Unixfs().Ls(ctx, p)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
...@@ -767,25 +767,6 @@ func (tp *provider) TestLs(t *testing.T) { ...@@ -767,25 +767,6 @@ func (tp *provider) TestLs(t *testing.T) {
if _, ok := <-links; ok { if _, ok := <-links; ok {
t.Errorf("didn't expect a second link") 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) { func (tp *provider) TestEntriesExpired(t *testing.T) {
......
...@@ -38,6 +38,7 @@ type UnixfsAPI interface { ...@@ -38,6 +38,7 @@ type UnixfsAPI interface {
// to operations performed on the returned file // to operations performed on the returned file
Get(context.Context, Path) (files.Node, error) 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) 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. ...@@ -167,10 +167,6 @@ func (api *UnixfsAPI) Ls(ctx context.Context, p coreiface.Path, opts ...options.
return nil, err return nil, err
} }
if !settings.Async {
return uses.lsFromDir(ctx, dir, settings)
}
return uses.lsFromLinksAsync(ctx, dir, settings) return uses.lsFromLinksAsync(ctx, dir, settings)
} }
...@@ -234,14 +230,6 @@ func (api *UnixfsAPI) lsFromLinksAsync(ctx context.Context, dir uio.Directory, s ...@@ -234,14 +230,6 @@ func (api *UnixfsAPI) lsFromLinksAsync(ctx context.Context, dir uio.Directory, s
return out, nil 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) { func (api *UnixfsAPI) lsFromLinks(ctx context.Context, ndlinks []*ipld.Link, settings *options.UnixfsLsSettings) (<-chan coreiface.LsLink, error) {
links := make(chan coreiface.LsLink, len(ndlinks)) links := make(chan coreiface.LsLink, len(ndlinks))
for _, l := range ndlinks { for _, l := range ndlinks {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论