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
Fix tar PAX format handling #1414
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ import ( | |
"io" | ||
"io/ioutil" | ||
"path/filepath" | ||
"runtime" | ||
"strings" | ||
"time" | ||
|
||
|
@@ -244,6 +245,8 @@ func extract(img v1.Image, w io.Writer) error { | |
if err != nil { | ||
return fmt.Errorf("retrieving image layers: %w", err) | ||
} | ||
|
||
isWin := runtime.GOOS == "windows" | ||
// we iterate through the layers in reverse order because it makes handling | ||
// whiteout layers more efficient, since we can just keep track of the removed | ||
// files as we see .wh. layers and ignore those in previous layers. | ||
|
@@ -268,6 +271,14 @@ func extract(img v1.Image, w io.Writer) error { | |
// name, we may have duplicate entries, which angers tar-split. | ||
header.Name = filepath.Clean(header.Name) | ||
|
||
// tar.Next() sometimes mistakenly guesses format as USTAR, which creates a problem: | ||
// if the header.Name is > 100 characters long, WriteHeader() returns an error like | ||
// "archive/tar: cannot encode header: Format specifies USTAR; and USTAR cannot encode Name=...". | ||
// To fix, change format from USTAR to PAX on Windows | ||
if isWin && header.Format == tar.FormatUSTAR { | ||
header.Format = tar.FormatPAX | ||
} | ||
|
||
basename := filepath.Base(header.Name) | ||
dirname := filepath.Dir(header.Name) | ||
tombstone := strings.HasPrefix(basename, whiteoutPrefix) | ||
|
@@ -297,7 +308,9 @@ func extract(img v1.Image, w io.Writer) error { | |
// any entries with a matching (or child) name | ||
fileMap[name] = tombstone || !(header.Typeflag == tar.TypeDir) | ||
if !tombstone { | ||
tarWriter.WriteHeader(header) | ||
if err := tarWriter.WriteHeader(header); err != nil { | ||
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. Thank you! |
||
return err | ||
} | ||
if header.Size > 0 { | ||
if _, err := io.CopyN(tarWriter, tarReader, header.Size); err != nil { | ||
return err | ||
|
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.
Ugh. This seems like the kind of thing I'd expect archive/tar to handle for us... :-/
Can you add a link to the issue in the comment, and maybe any other helpful issues you've found while debugging this? That will help remind me better why this is here when I rediscover it in six months, or if I have the same Windows-specific issue elsewhere and come back here to try to remember what the deal was.
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.
There is also viable path that linuxkit chose - to force PAX on output linuxkit/linuxkit#3141
Maybe force PAX and document it using reasoning in pull request above?
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.
something like here (with linked pull request)(https://github.com/linuxkit/linuxkit/blob/master/src/cmd/linuxkit/moby/image.go#L216)
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'd be okay with just always setting this.
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.
SGTM 👍