-
Notifications
You must be signed in to change notification settings - Fork 379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docker: tarfile: improve auto-decompression handling #427
Changes from all commits
b9827eb
246b92d
97c59da
52c5d96
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,13 +15,12 @@ import ( | |
|
||
func TestDetectCompression(t *testing.T) { | ||
cases := []struct { | ||
filename string | ||
unimplemented bool | ||
filename string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Non-blocking: This could become a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It could, but I prefered the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, this is fine as well. |
||
}{ | ||
{"fixtures/Hello.uncompressed", false}, | ||
{"fixtures/Hello.gz", false}, | ||
{"fixtures/Hello.bz2", false}, | ||
{"fixtures/Hello.xz", true}, | ||
{"fixtures/Hello.uncompressed"}, | ||
{"fixtures/Hello.gz"}, | ||
{"fixtures/Hello.bz2"}, | ||
{"fixtures/Hello.xz"}, | ||
} | ||
|
||
// The original stream is preserved. | ||
|
@@ -54,10 +53,6 @@ func TestDetectCompression(t *testing.T) { | |
switch { | ||
case decompressor == nil: | ||
uncompressedStream = updatedStream | ||
case c.unimplemented: | ||
_, err := decompressor(updatedStream) | ||
assert.Error(t, err) | ||
continue | ||
default: | ||
s, err := decompressor(updatedStream) | ||
require.NoError(t, err) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, it turns out that
gzip.NewReader
returns aReadCloser
, and the caller is expected to reallyClose
the stream; thecompression.DetectCompression
implementation missed that.Luckily(?) the
gzip.Reader.Close()
is (currently?) trivial, it only returns an error value whichRead()
would AFAICT would have returned anyway as long as the consumer is reading until EOF.But that complicates the
AutoDecompress
design; should it wrap uncompressed inputs inNopCloser
? Should it return a separateclose
callback instead of modifying theio.Reader
stream (likedirImageMockWithRef
does)?Or do we just pretend that
gzip.Reader.Close
does not exist? :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could change the API to return an
io.ReadCloser
. Sinceio.Reader
⊆io.ReadCloser
there shouldn't be a problem with any users of the interface (aside from the downside they probably won't notice they should callClose
now).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
… and that
is unintuitive. But then none of the alternatives I can think of are any more elegant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe, but if you compare it to
gzip.NewReader
you have to do a similar thing.We could work around it by doing some very dodgy
.(io.Closer)
handling in order to still allow users to passio.Reader
s that don't have a hiddenClose()
method. But that's probably what you were thinking when you said that you can't think of any more elegant workarounds. 😉There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The unintuitive part, to me, is that with raw
gzip
, there isreader.Close()
andreader
2
.Close()
; withAutoDecompress
, there would be tworeader.Close()
s, which looks pretty similar to a copy&paste bug.But I still can’t think of a much better API.
does not look noticeably better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the moment I'm just going to ignore the close method for
gzip
if that's okay with you. Otherwise we'd have toNopCloser
most of the other functions, andDetectCompression
's signature would look wrong.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m not really happy to ignore resource cleanup and error detection, no. It’s a time bomb, and doing the right thing seems plausible.
I can take on the work of updating the existing
compression
package and its users, if you don’t want to deal with the rest of c/image as part of this PR (apart from updating it to use the new API).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, in that case I'll work on doing that tomorrow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll work on fixing this up today.