Skip to content
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

IsGzipped consumes the io.Reader it's reading #112

Closed
imjasonh opened this issue Apr 25, 2018 · 3 comments
Closed

IsGzipped consumes the io.Reader it's reading #112

imjasonh opened this issue Apr 25, 2018 · 3 comments
Labels
bug Something isn't working lifecycle/frozen

Comments

@imjasonh
Copy link
Collaborator

func TestIsGzipped(t *testing.T) {
  var buf bytes.Buffer
  w := gzip.NewWriter(&buf)
  w.Write([]byte("hello"))

  if is, _ := IsGzipped(&buf); !is {
    t.Errorf("IsGzipped identified contents as not gzipped")
  }

  if is, _ := IsGzipped(&buf); !is {
    t.Errorf("IsGzipped identified contents as not gzipped on retry") // <-- boom
  }
}

IsGzipped determines whether the given reader represents gzipped data by consuming the first two bytes of the reader and looking for a magic header. This consumes the data, so subsequent IsGzipped checks will not identify the contents as gzipped.

This isn't a problem in practice while Readers are only checked once while reading tarballs, but this could become a problem in the future, or if a client decides to call IsGzipped themselves on a Reader before passing it to tarball.Image (naively wrapped in an Opener).

@imjasonh imjasonh added the bug Something isn't working label Apr 25, 2018
@jonjohnsonjr
Copy link
Collaborator

I think we just need something that implements Peek, like bufio.Reader:
https://golang.org/pkg/bufio/#Reader.Peek

Or something that implements ReadAt, like os.File:
https://golang.org/pkg/os/#File.ReadAt

For most things we're already passing around a File, but I think using a buffered Reader might be easier to bolt onto existing code.

@github-actions
Copy link

This issue is stale because it has been open for 90 days with no
activity. It will automatically close after 30 more days of
inactivity. Reopen the issue with /reopen. Mark the issue as
fresh by adding the comment /remove-lifecycle stale.

@jonjohnsonjr
Copy link
Collaborator

I'm pretty sure this is the reason I can't do this:

$ crane append -f <(tar -c vendor/ -f -) -t gcr.io/jonjohnson-test/some-vendor-directory 
2020/11/29 11:46:07 base unspecified, using empty image
2020/11/29 11:46:12 pushed blob: sha256:1b7c025f16477c734ce6b60bfbaa5d8398f9c66b96d53bdeea129445c3718de0
2020/11/29 11:46:13 pushing image gcr.io/jonjohnson-test/some-vendor-directory: PUT https://gcr.io/v2/jonjohnson-test/some-vendor-directory/blobs/uploads/AJAD5v2UpUS6IPDnYZRCKZWltOKRsrztLTaErFajji4ZaAAAOEBhi3jLCFwptDQ4Qha5CgcT1CZaIC1LLhXvQiLf3DbxOCz-K-emChYIfkSkqlrNEQXWbunn-wm8Ts--FeWsYedBb0bYDMSwblKhCbd4HQXd3kt7akPeSjYmu2MoQYrjkeyAocPT9QD_?digest=sha256%3A2378ec17076387a1b5a3663b6bad06e1cbd2e43716ceb4ca713d1db15b479d6b: DIGEST_INVALID: 'digest' parameter 'sha256:2378ec17076387a1b5a3663b6bad06e1cbd2e43716ceb4ca713d1db15b479d6b' does not match computed digest 'sha256:81da0491c5af5635831f6a3febb5d9bfd66987ba3ecc42e58dc3d80938c25705'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working lifecycle/frozen
Projects
None yet
Development

No branches or pull requests

2 participants