Skip to content

Commit a4b026e

Browse files
committed
Return errors encountered in writeChunks()
1 parent 38cb3ff commit a4b026e

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

pkg/rsstorage/internal/chunks.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package internal
55
import (
66
"context"
77
"encoding/json"
8+
"errors"
89
"fmt"
910
"io"
1011
"log/slog"
@@ -139,17 +140,28 @@ func (w *DefaultChunkUtils) writeChunks(
139140
results chan uint64,
140141
errs chan error,
141142
) {
142-
// TODO: Handle this error
143-
defer r.Close()
143+
defer func(r *io.PipeReader) {
144+
closeErr := r.Close()
145+
if closeErr != nil {
146+
errs <- closeErr
147+
}
148+
}(r)
144149
defer close(results)
145150
defer close(errs)
146151
for i := uint64(1); i <= numChunks; i++ {
147152
err := func() error {
153+
var copiedBytes uint64
148154
resolve := func(writer io.Writer) (dir, address string, err error) {
149-
_, err = io.CopyN(writer, r, int64(w.ChunkSize))
150-
if err != nil && err == io.EOF {
151-
err = nil
155+
written, err := io.CopyN(writer, r, int64(w.ChunkSize))
156+
if err != nil {
157+
// an End of File error should be considered a critical error if it is
158+
// returned before the last chunk
159+
if errors.Is(err, io.EOF) && i == numChunks {
160+
err = nil
161+
}
152162
}
163+
// record the number of bytes written
164+
copiedBytes = uint64(written)
153165
return
154166
}
155167

@@ -159,7 +171,9 @@ func (w *DefaultChunkUtils) writeChunks(
159171
return err
160172
}
161173

162-
results <- i
174+
// if no error was encountered, report the number of bytes copied so it can be
175+
// computed to ensure the download was successful
176+
results <- copiedBytes
163177
return nil
164178
}()
165179
if err != nil {

0 commit comments

Comments
 (0)