Skip to content

Commit

Permalink
fix: avoid getting the wrong readers
Browse files Browse the repository at this point in the history
  • Loading branch information
simlecode committed May 11, 2024
1 parent 10794b5 commit 7931fef
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
18 changes: 12 additions & 6 deletions piecestorage/store_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,12 @@ func (sw *storeWrapper) GetReaderCloser(ctx context.Context, s string) (io.ReadC
var rc io.ReadCloser
var err error
for _, name := range extendPiece(s) {
rc, err = sw.IPieceStorage.GetReaderCloser(ctx, name)
if err == nil {
return rc, nil
has, _ := sw.IPieceStorage.Has(ctx, name)
if has {
rc, err = sw.IPieceStorage.GetReaderCloser(ctx, name)
if err == nil {
return rc, nil
}
}
}

Expand All @@ -70,9 +73,12 @@ func (sw *storeWrapper) GetMountReader(ctx context.Context, s string) (mount.Rea
var reader mount.Reader
var err error
for _, name := range extendPiece(s) {
reader, err = sw.IPieceStorage.GetMountReader(ctx, name)
if err == nil {
return reader, nil
has, _ := sw.IPieceStorage.Has(ctx, name)
if has {
reader, err = sw.IPieceStorage.GetMountReader(ctx, name)
if err == nil {
return reader, nil
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions retrievalprovider/httpretrieval/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ func (s *Server) retrievalByPieceCID(w http.ResponseWriter, r *http.Request) {
}
defer mountReader.Close() // nolint

var buf [32]byte
_, _ = mountReader.Read(buf[:])
fmt.Println("xxxx", string(buf[:]))

contentReader, err := handleRangeHeader(r.Header.Get("Range"), mountReader, len)
if err != nil {
log.Warnf("handleRangeHeader failed, Range: %s, error: %v", r.Header.Get("Range"), err)
Expand Down
3 changes: 2 additions & 1 deletion retrievalprovider/httpretrieval/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ func TestRetrievalByPiece(t *testing.T) {
piece, err := cid.Decode(pieceStr)
assert.NoError(t, err)
buf := &bytes.Buffer{}
f, err := os.Create(filepath.Join(tmpDri, pieceStr))
f, err := os.Create(filepath.Join(tmpDri, pieceStr+".car"))
assert.NoError(t, err)
assert.NoError(t, os.MkdirAll(filepath.Join(tmpDri, pieceStr), os.ModePerm))
for i := 0; i < 100; i++ {
buf.WriteString("TEST TEST\n")
}
Expand Down

0 comments on commit 7931fef

Please sign in to comment.