@@ -161,8 +161,8 @@ func newInputTarStreamCommon(
161161 r io.Reader ,
162162 p storage.Packer ,
163163 fp storage.FilePutter ,
164- withDone bool ,
165- ) (pr * io.PipeReader , done <- chan error ) {
164+ done chan <- error ,
165+ ) (pr * io.PipeReader ) {
166166 // What to do here... folks will want their own access to the Reader that is
167167 // their tar archive stream, but we'll need that same stream to use our
168168 // forked 'archive/tar'.
@@ -181,22 +181,14 @@ func newInputTarStreamCommon(
181181 // `archive/tar` doesn't care.
182182 pr , pw := io .Pipe ()
183183
184- // We need a putter that will generate the crc64 sums of file payloads.
185184 if fp == nil {
186185 fp = storage .NewDiscardFilePutter ()
187186 }
188187
189188 outputRdr := io .TeeReader (r , pw )
189+ go runInputTarStreamGoroutine (outputRdr , pw , p , fp , done )
190190
191- if withDone {
192- ch := make (chan error , 1 )
193- done = ch
194- go runInputTarStreamGoroutine (outputRdr , pw , p , fp , ch )
195- return pr , done
196- }
197-
198- go runInputTarStreamGoroutine (outputRdr , pw , p , fp , nil )
199- return pr , nil
191+ return pr
200192}
201193
202194// NewInputTarStream wraps the Reader stream of a tar archive and provides a
@@ -213,9 +205,9 @@ func newInputTarStreamCommon(
213205// If callers need to be able to abort early and/or wait for goroutine termination,
214206// prefer NewInputTarStreamWithDone.
215207//
216- // Deprecated: Use NewInputTarStreamWithDone instead .
208+ // Deprecated: This leaves a goroutine around if the consumer aborts without consuming .
217209func NewInputTarStream (r io.Reader , p storage.Packer , fp storage.FilePutter ) (io.Reader , error ) {
218- pr , _ := newInputTarStreamCommon (r , p , fp , false )
210+ pr := newInputTarStreamCommon (r , p , fp , nil )
219211 return pr , nil
220212}
221213
@@ -232,7 +224,12 @@ func NewInputTarStream(r io.Reader, p storage.Packer, fp storage.FilePutter) (io
232224// The returned reader is an io.ReadCloser so callers can stop early; closing it
233225// aborts the pipe so the internal goroutine can terminate promptly (rather than
234226// hanging on a blocked pipe write).
227+ //
228+ // The caller is expected to consume the returned reader fully until EOF
229+ // (not just the tar EOF marker); closing the returned reader earlier will
230+ // cause the done channel to return a failure.
235231func NewInputTarStreamWithDone (r io.Reader , p storage.Packer , fp storage.FilePutter ) (io.ReadCloser , <- chan error , error ) {
236- pr , done := newInputTarStreamCommon (r , p , fp , true )
232+ done := make (chan error , 1 )
233+ pr := newInputTarStreamCommon (r , p , fp , done )
237234 return pr , done , nil
238235}
0 commit comments