Commit a92a1745 authored by Łukasz Magiera's avatar Łukasz Magiera

files2.0: return errors from ufsIterator properly

License: MIT
Signed-off-by: default avatarŁukasz Magiera <magik6k@gmail.com>
parent 351ed26b
......@@ -31,7 +31,8 @@ type ufsIterator struct {
curName string
curFile files.Node
err error
err error
errCh chan error
}
func (it *ufsIterator) Name() string {
......@@ -43,11 +44,31 @@ func (it *ufsIterator) Node() files.Node {
}
func (it *ufsIterator) Next() bool {
l, ok := <-it.files
if !ok {
if it.err != nil {
return false
}
var l *ipld.Link
var ok bool
for !ok {
if it.files == nil && it.errCh == nil {
return false
}
select {
case l, ok = <-it.files:
if !ok {
it.files = nil
}
case err := <-it.errCh:
it.errCh = nil
it.err = err
if err != nil {
return false
}
}
}
it.curFile = nil
nd, err := l.GetNode(it.ctx, it.dserv)
......@@ -71,8 +92,12 @@ func (d *ufsDirectory) Close() error {
func (d *ufsDirectory) Entries() files.DirIterator {
fileCh := make(chan *ipld.Link, prefetchFiles)
errCh := make(chan error, 1)
go func() {
d.dir.ForEachLink(d.ctx, func(link *ipld.Link) error {
errCh <- d.dir.ForEachLink(d.ctx, func(link *ipld.Link) error {
if d.ctx.Err() != nil {
return d.ctx.Err()
}
select {
case fileCh <- link:
case <-d.ctx.Done():
......@@ -81,12 +106,14 @@ func (d *ufsDirectory) Entries() files.DirIterator {
return nil
})
close(errCh)
close(fileCh)
}()
return &ufsIterator{
ctx: d.ctx,
files: fileCh,
errCh: errCh,
dserv: d.dserv,
}
}
......
......@@ -776,6 +776,53 @@ func TestLs(t *testing.T) {
}
}
func TestEntriesExpired(t *testing.T) {
ctx := context.Background()
node, api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}
r := strings.NewReader("content-of-file")
k, _, err := coreunix.AddWrapped(node, r, "name-of-file")
if err != nil {
t.Error(err)
}
parts := strings.Split(k, "/")
if len(parts) != 2 {
t.Errorf("unexpected path: %s", k)
}
p, err := coreiface.ParsePath("/ipfs/" + parts[0])
if err != nil {
t.Error(err)
}
ctx, cancel := context.WithCancel(ctx)
nd, err := api.Unixfs().Get(ctx, p)
if err != nil {
t.Error(err)
}
cancel()
it := files.ToDir(nd).Entries()
if it == nil {
t.Fatal("it was nil")
}
if it.Next() {
t.Fatal("Next succeeded")
}
if it.Err() != context.Canceled {
t.Fatalf("unexpected error %s", it.Err())
}
if it.Next() {
t.Fatal("Next succeeded")
}
}
func TestLsEmptyDir(t *testing.T) {
ctx := context.Background()
node, api, err := makeAPI(ctx)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment