storage/piece_resource.go | 18 ++++++++++++++++-- diff --git a/storage/piece_resource.go b/storage/piece_resource.go index 36088f32cf26718558e687baa3e8c168d5a81820..02c1e7e8ad7e26b99bd017d65985e1cf58881a42 100644 --- a/storage/piece_resource.go +++ b/storage/piece_resource.go @@ -61,10 +61,14 @@ if ccw, ok := s.rp.(ConsecutiveChunkWriter); ok { if s.mustIsComplete() { return ccw.WriteConsecutiveChunks(s.completedInstancePath(), w) } else { - return ccw.WriteConsecutiveChunks(s.incompleteDirPath()+"/", w) + return s.writeConsecutiveIncompleteChunks(ccw, w) } } return io.Copy(w, io.NewSectionReader(s, 0, s.mp.Length())) +} + +func (s piecePerResourcePiece) writeConsecutiveIncompleteChunks(ccw ConsecutiveChunkWriter, w io.Writer) (int64, error) { + return ccw.WriteConsecutiveChunks(s.incompleteDirPath()+"/", w) } // Returns if the piece is complete. Ok should be true, because we are the definitive source of @@ -87,7 +91,17 @@ } func (s piecePerResourcePiece) MarkComplete() error { incompleteChunks := s.getChunks() - err := s.completed().Put(io.NewSectionReader(incompleteChunks, 0, s.mp.Length())) + r, w := io.Pipe() + go func() { + var err error + if ccw, ok := s.rp.(ConsecutiveChunkWriter); ok { + _, err = s.writeConsecutiveIncompleteChunks(ccw, w) + } else { + _, err = io.Copy(w, io.NewSectionReader(incompleteChunks, 0, s.mp.Length())) + } + w.CloseWithError(err) + }() + err := s.completed().Put(r) if err == nil { for _, c := range incompleteChunks { c.instance.Delete()