-
Notifications
You must be signed in to change notification settings - Fork 113
repack: do not include Volume paths in new layers #127
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /* | ||
| * umoci: Umoci Modifies Open Containers' Images | ||
| * Copyright (C) 2017 SUSE LLC. | ||
| * | ||
| * 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 mtreefilter | ||
|
|
||
| import ( | ||
| "path/filepath" | ||
|
|
||
| "github.com/apex/log" | ||
| "github.com/vbatts/go-mtree" | ||
| ) | ||
|
|
||
| // FilterFunc is a function used when filtering deltas with FilterDeltas. | ||
| type FilterFunc func(path string) bool | ||
|
|
||
| // isParent returns whether the path a is lexically an ancestor of the path b. | ||
| func isParent(a, b string) bool { | ||
| a = filepath.Clean(a) | ||
| b = filepath.Clean(b) | ||
|
|
||
| for a != b && b != filepath.Dir(b) { | ||
| b = filepath.Dir(b) | ||
| } | ||
| return a == b | ||
| } | ||
|
|
||
| // MaskFilter is a factory for FilterFuncs that will mask all InodeDelta paths | ||
| // that are lexical children of any path in the mask slice. All paths are | ||
| // considered to be relative to '/'. | ||
| func MaskFilter(masks []string) FilterFunc { | ||
| return func(path string) bool { | ||
| // Convert the path to be cleaned and relative-to-root. | ||
| path = filepath.Join("/", path) | ||
|
|
||
| // Check that no masks are matched. | ||
| for _, mask := range masks { | ||
| // Mask also needs to be cleaned and relative-to-root. | ||
| mask = filepath.Join("/", mask) | ||
|
|
||
| // Is it a parent? | ||
| if isParent(mask, path) { | ||
| log.Debugf("maskfilter: ignoring path %q matched by mask %q", path, mask) | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| return true | ||
| } | ||
| } | ||
|
|
||
| // FilterDeltas is a helper function to easily filter []mtree.InodeDelta with a | ||
| // filter function. Only entries which have `filter(delta.Path()) == true` will | ||
| // be included in the returned slice. | ||
| func FilterDeltas(deltas []mtree.InodeDelta, filter FilterFunc) []mtree.InodeDelta { | ||
| var filtered []mtree.InodeDelta | ||
| for _, delta := range deltas { | ||
| if filter(delta.Path()) { | ||
| filtered = append(filtered, delta) | ||
| } | ||
| } | ||
| return filtered | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| /* | ||
| * umoci: Umoci Modifies Open Containers' Images | ||
| * Copyright (C) 2017 SUSE LLC. | ||
| * | ||
| * 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 mtreefilter | ||
|
|
||
| import ( | ||
| "io/ioutil" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/vbatts/go-mtree" | ||
| ) | ||
|
|
||
| func TestIsParent(t *testing.T) { | ||
| for _, test := range []struct { | ||
| parent, path string | ||
| expected bool | ||
| }{ | ||
| {"/", "/a", true}, | ||
| {"/", "/a/b/c", true}, | ||
| {"/", "/", true}, | ||
| {"/a path/", "/a path", true}, | ||
| {"/a nother path", "/a nother path/test", true}, | ||
| {"/a nother path", "/a nother path/test/1 2/ 33 /", true}, | ||
| {"/path1", "/path2", false}, | ||
| {"/pathA", "/PATHA", false}, | ||
| {"/pathC", "/path", false}, | ||
| {"/path9", "/", false}, | ||
| // Make sure it's not the same as filepath.HasPrefix. | ||
| {"/a/b/c", "/a/b/c/d", true}, | ||
| {"/a/b/c", "/a/b/cd", false}, | ||
| {"/a/b/c", "/a/bcd", false}, | ||
| {"/a/bc", "/a/bcd", false}, | ||
| {"/abc", "/abcd", false}, | ||
| } { | ||
| got := isParent(test.parent, test.path) | ||
| if got != test.expected { | ||
| t.Errorf("isParent(%q, %q) got %v expected %v", test.parent, test.path, got, test.expected) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestMaskDeltas(t *testing.T) { | ||
| dir, err := ioutil.TempDir("", "TestMaskDeltas-") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer os.RemoveAll(dir) | ||
|
|
||
| var mtreeKeywords []mtree.Keyword = append(mtree.DefaultKeywords, "sha256digest") | ||
|
|
||
| // Create some files. | ||
| if err != ioutil.WriteFile(filepath.Join(dir, "file1"), []byte("contents"), 0644) { | ||
| t.Fatal(err) | ||
| } | ||
| if err != ioutil.WriteFile(filepath.Join(dir, "file2"), []byte("another content"), 0644) { | ||
| t.Fatal(err) | ||
| } | ||
| if err != os.MkdirAll(filepath.Join(dir, "dir", "child"), 0755) { | ||
| t.Fatal(err) | ||
| } | ||
| if err != os.MkdirAll(filepath.Join(dir, "dir", "child2"), 0755) { | ||
| t.Fatal(err) | ||
| } | ||
| if err != ioutil.WriteFile(filepath.Join(dir, "dir", "file 3"), []byte("more content"), 0644) { | ||
| t.Fatal(err) | ||
| } | ||
| if err != ioutil.WriteFile(filepath.Join(dir, "dir", "child2", "4 files"), []byte("very content"), 0644) { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| // Generate a diff. | ||
| originalDh, err := mtree.Walk(dir, nil, mtreeKeywords, nil) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| // Modify the root. | ||
| if err := os.RemoveAll(filepath.Join(dir, "file2")); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := ioutil.WriteFile(filepath.Join(dir, "dir", "new"), []byte("more content"), 0755); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := ioutil.WriteFile(filepath.Join(dir, "file1"), []byte("different contents"), 0666); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| // Generate the set of diffs. | ||
| newDh, err := mtree.Walk(dir, nil, mtreeKeywords, nil) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| diff, err := mtree.Compare(originalDh, newDh, mtreeKeywords) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| for _, test := range []struct { | ||
| paths []string | ||
| }{ | ||
| {nil}, | ||
| {[]string{"/"}}, | ||
| {[]string{"dir"}}, | ||
| {[]string{filepath.Join("dir", "child2")}}, | ||
| {[]string{"file2"}}, | ||
| {[]string{"/", "file2"}}, | ||
| {[]string{"file2", filepath.Join("dir", "child2")}}, | ||
| } { | ||
| newDiff := FilterDeltas(diff, MaskFilter(test.paths)) | ||
| for _, delta := range newDiff { | ||
| if len(test.paths) == 0 { | ||
| if len(newDiff) != len(diff) { | ||
| t.Errorf("expected diff={} to give %d got %d", len(diff), len(newDiff)) | ||
| } | ||
| } else { | ||
| found := false | ||
| for _, path := range test.paths { | ||
| if !isParent(path, delta.Path()) { | ||
| found = true | ||
| } | ||
| } | ||
| if !found { | ||
| t.Errorf("expected one of %v to not be a parent of %q", test.paths, delta.Path()) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
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.
That's a very elegant function 👍
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.
Thanks. 😉