Skip to content

Commit

Permalink
Merge pull request #528 from ipfs-force-community/fix/panic
Browse files Browse the repository at this point in the history
fix: divide by zero
  • Loading branch information
simlecode authored May 16, 2024
2 parents ff7aecd + 7931fef commit b3ca5a8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 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
10 changes: 9 additions & 1 deletion 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 Expand Up @@ -179,7 +183,11 @@ func serveContent(w http.ResponseWriter, r *http.Request, content io.ReadSeeker,
// Write a line to the log
end := time.Now()
total, count := writeErrWatcher.total, writeErrWatcher.count
avg := total / count
var avg uint64
if count != 0 {
avg = total / count
}

completeMsg := fmt.Sprintf("GET %s\t%s - %s: %s / %s transferred",
r.URL, end.Format(time.RFC3339), start.Format(time.RFC3339), time.Since(start),
fmt.Sprintf("total %s (%d B), average write %s ", types.SizeStr(types.NewInt(total)), total, types.SizeStr(types.NewInt(avg))))
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 b3ca5a8

Please sign in to comment.