diff --git a/unixfs/feather/entry.go b/unixfs/feather/entry.go index 2ab210f52..df1280ed3 100644 --- a/unixfs/feather/entry.go +++ b/unixfs/feather/entry.go @@ -210,26 +210,9 @@ func (d *downloader) Read(b []byte) (_ int, err error) { data = c.Hash() data = data[len(data)-pref.MhLength:] // extract digest default: - if err := verifcid.ValidateCid(verifcid.DefaultAllowlist, c); err != nil { - return 0, fmt.Errorf("cid %s don't pass safe test: %w", cidStringTruncate(c), err) - } - var errStartStream, errRead error - for { - if d.stream == nil { - if attempts := d.remainingAttempts; attempts != math.MaxUint { - if attempts == 0 { - return 0, errors.Join(errRead, errStartStream) - } - d.remainingAttempts = attempts - 1 - } - errStartStream = d.startStream(todo) - } - data, errRead = d.readBlockFromStream(c) - if errRead == nil { - break - } - d.stream.Close() - d.stream = nil + data, err = d.next(todo) + if err != nil { + return 0, err } } @@ -275,6 +258,34 @@ func (d *downloader) Read(b []byte) (_ int, err error) { return n, nil } +// next download the next block, it also handles performing retries if needed. +// The data return is hash correct. +func (d *downloader) next(todo region) ([]byte, error) { + c := todo.c + if err := verifcid.ValidateCid(verifcid.DefaultAllowlist, c); err != nil { + return nil, fmt.Errorf("cid %s don't pass safe test: %w", cidStringTruncate(c), err) + } + var errStartStream, errRead error + for { + if d.stream == nil { + if attempts := d.remainingAttempts; attempts != math.MaxUint { + if attempts == 0 { + return nil, fmt.Errorf("could not download next block: %w", errors.Join(errRead, errStartStream)) + } + d.remainingAttempts = attempts - 1 + } + errStartStream = d.startStream(todo) + } + var data []byte + data, errRead = d.readBlockFromStream(c) + if errRead == nil { + return data, nil + } + d.stream.Close() + d.stream = nil + } +} + // readBlockFromStream must perform hash verification on the input. // The slice returned only has to be valid between two readBlockFromStream and Close calls. // Implementations should reuse buffers to avoid allocations. diff --git a/unixfs/feather/feather_test.go b/unixfs/feather/feather_test.go index a7d7243b5..052420cf2 100644 --- a/unixfs/feather/feather_test.go +++ b/unixfs/feather/feather_test.go @@ -46,7 +46,6 @@ func newGateway(t *testing.T, fixture string) (*httptest.Server, cid.Cid) { ts := httptest.NewServer(handler) t.Cleanup(func() { ts.Close() }) - t.Logf("test server url: %s", ts.URL) return ts, cids[0] }