This repository was archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 236
modifications to extract filesystem for kbuild #191
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -18,15 +18,14 @@ package util | |
|
||
import ( | ||
"archive/tar" | ||
"github.com/sirupsen/logrus" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func unpackTar(tr *tar.Reader, path string) error { | ||
func unpackTar(tr *tar.Reader, path string, whitelist []string) error { | ||
for { | ||
header, err := tr.Next() | ||
if err == io.EOF { | ||
|
@@ -37,7 +36,6 @@ func unpackTar(tr *tar.Reader, path string) error { | |
logrus.Error("Error getting next tar header") | ||
return err | ||
} | ||
|
||
if strings.Contains(header.Name, ".wh.") { | ||
rmPath := filepath.Join(path, header.Name) | ||
// Remove the .wh file if it was extracted. | ||
|
@@ -54,8 +52,11 @@ func unpackTar(tr *tar.Reader, path string) error { | |
} | ||
continue | ||
} | ||
|
||
target := filepath.Join(path, header.Name) | ||
// Make sure the target isn't part of the whitelist | ||
if checkWhitelist(target, whitelist) { | ||
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. A debug log here will come in handy later. 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. I added it in the checkWhitelist function |
||
continue | ||
} | ||
mode := header.FileInfo().Mode() | ||
switch header.Typeflag { | ||
|
||
|
@@ -65,7 +66,7 @@ func unpackTar(tr *tar.Reader, path string) error { | |
if err := os.MkdirAll(target, mode); err != nil { | ||
return err | ||
} | ||
} else { | ||
// In some cases, MkdirAll doesn't change the permissions, so run Chmod | ||
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's usually umask related, but I suppose if the dirs already exist it wouldn't change the permissions either |
||
if err := os.Chmod(target, mode); err != nil { | ||
return err | ||
} | ||
|
@@ -81,7 +82,6 @@ func unpackTar(tr *tar.Reader, path string) error { | |
return err | ||
} | ||
} | ||
|
||
// It's possible we end up creating files that can't be overwritten based on their permissions. | ||
// Explicitly delete an existing file before continuing. | ||
if _, err := os.Stat(target); !os.IsNotExist(err) { | ||
|
@@ -106,21 +106,43 @@ func unpackTar(tr *tar.Reader, path string) error { | |
return err | ||
} | ||
currFile.Close() | ||
} | ||
case tar.TypeSymlink: | ||
// It's possible we end up creating files that can't be overwritten based on their permissions. | ||
// Explicitly delete an existing file before continuing. | ||
if _, err := os.Stat(target); !os.IsNotExist(err) { | ||
logrus.Debugf("Removing %s to create symlink.", target) | ||
if err := os.RemoveAll(target); err != nil { | ||
logrus.Debugf("Unable to remove %s: %s", target, err) | ||
} | ||
} | ||
|
||
if err = os.Symlink(header.Linkname, target); err != nil { | ||
logrus.Errorf("Failed to create symlink between %s and %s: %s", header.Linkname, target, err) | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func checkWhitelist(target string, whitelist []string) bool { | ||
for _, w := range whitelist { | ||
if HasFilepathPrefix(target, w) { | ||
logrus.Debugf("Not extracting %s, as it has prefix %s which is whitelisted", target, w) | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// UnTar takes in a path to a tar file and writes the untarred version to the provided target. | ||
// Only untars one level, does not untar nested tars. | ||
func UnTar(r io.Reader, target string) error { | ||
func UnTar(r io.Reader, target string, whitelist []string) error { | ||
if _, ok := os.Stat(target); ok != nil { | ||
os.MkdirAll(target, 0775) | ||
} | ||
|
||
tr := tar.NewReader(r) | ||
if err := unpackTar(tr, target); err != nil { | ||
if err := unpackTar(tr, target, whitelist); err != nil { | ||
return err | ||
} | ||
return nil | ||
|
This file contains hidden or 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 hidden or 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
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.
Can you use filepath.Split here?
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 think it only splits along the last separator
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.
D'oh. Right.