-
Notifications
You must be signed in to change notification settings - Fork 542
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
Towards eliminating v1util #872
Merged
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
102d921
Move logic out of v1util into other packages, leave aliases for back-…
mattmoor a8de621
Eliminate our own reliance on v1util
mattmoor 4717181
Add deprecation notices to trigger golangci-lint
mattmoor ba6afc8
Fix linting issues
mattmoor 2eae8f8
Use 'Deprecated:'
mattmoor 5aeddf7
Add TODO(issue) for removing deprecated things
mattmoor 1b8e580
Move verification to internal package
mattmoor e9eb297
Eliminate stutter
mattmoor 245630d
Fix victor linter
mattmoor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Copyright 2020 Google LLC All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package gzip | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"io" | ||
|
||
"github.com/google/go-containerregistry/pkg/v1/internal/and" | ||
) | ||
|
||
var gzipMagicHeader = []byte{'\x1f', '\x8b'} | ||
|
||
// GzipReadCloser reads uncompressed input data from the io.ReadCloser and | ||
// returns an io.ReadCloser from which compressed data may be read. | ||
// This uses gzip.BestSpeed for the compression level. | ||
func GzipReadCloser(r io.ReadCloser) io.ReadCloser { | ||
return GzipReadCloserLevel(r, gzip.BestSpeed) | ||
} | ||
|
||
// GzipReadCloserLevel reads uncompressed input data from the io.ReadCloser and | ||
// returns an io.ReadCloser from which compressed data may be read. | ||
// Refer to compress/gzip for the level: | ||
// https://golang.org/pkg/compress/gzip/#pkg-constants | ||
func GzipReadCloserLevel(r io.ReadCloser, level int) io.ReadCloser { | ||
pr, pw := io.Pipe() | ||
|
||
// Returns err so we can pw.CloseWithError(err) | ||
go func() error { | ||
// TODO(go1.14): Just defer {pw,gw,r}.Close like you'd expect. | ||
// Context: https://golang.org/issue/24283 | ||
gw, err := gzip.NewWriterLevel(pw, level) | ||
if err != nil { | ||
return pw.CloseWithError(err) | ||
} | ||
|
||
if _, err := io.Copy(gw, r); err != nil { | ||
defer r.Close() | ||
defer gw.Close() | ||
return pw.CloseWithError(err) | ||
} | ||
defer pw.Close() | ||
defer r.Close() | ||
defer gw.Close() | ||
|
||
return nil | ||
}() | ||
|
||
return pr | ||
} | ||
|
||
// GunzipReadCloser reads compressed input data from the io.ReadCloser and | ||
// returns an io.ReadCloser from which uncompessed data may be read. | ||
func GunzipReadCloser(r io.ReadCloser) (io.ReadCloser, error) { | ||
gr, err := gzip.NewReader(r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &and.ReadCloser{ | ||
Reader: gr, | ||
CloseFunc: func() error { | ||
// If the unzip fails, then this seems to return the same | ||
// error as the read. We don't want this to interfere with | ||
// us closing the main ReadCloser, since this could leave | ||
// an open file descriptor (fails on Windows). | ||
gr.Close() | ||
return r.Close() | ||
}, | ||
}, nil | ||
} | ||
|
||
// IsGzipped detects whether the input stream is compressed. | ||
func IsGzipped(r io.Reader) (bool, error) { | ||
magicHeader := make([]byte, 2) | ||
n, err := r.Read(magicHeader) | ||
if n == 0 && err == io.EOF { | ||
return false, nil | ||
} | ||
if err != nil { | ||
return false, err | ||
} | ||
return bytes.Equal(magicHeader, gzipMagicHeader), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Drop the stutter?
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.
What do you want to call the Gunzip variant :)
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.
You got me there.
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 was piggybacking on the sentiment @imjasonh had below that renaming things becomes reeeeaaaaal easy under
internal/
, so this is trivial to fixup later if we find a name we like. 🤷