-
Notifications
You must be signed in to change notification settings - Fork 23
fix: ensure archive dir is copied deterministically #301
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
Tonisal-byte
merged 3 commits into
microsoft:main
from
Tonisal-byte:asalinas/deterministic-archive-fix
Jul 28, 2026
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| //go:build linux | ||
|
|
||
| package archive_test | ||
|
|
||
| import ( | ||
| "archive/tar" | ||
| "os" | ||
| "path/filepath" | ||
| "syscall" | ||
| "testing" | ||
|
|
||
| "github.com/microsoft/azure-linux-dev-tools/internal/utils/archive" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestExtract_PreservesModesUnderRestrictiveUmask(t *testing.T) { | ||
| tmpDir := t.TempDir() | ||
| archivePath := filepath.Join(tmpDir, "source.tar.gz") | ||
|
|
||
| createTestTarGz(t, archivePath, []testTarEntry{ | ||
| {name: "implicit/file.txt", typeflag: tar.TypeReg, content: "content", mode: 0o666}, | ||
| }) | ||
|
|
||
| repack := func(name string, umask int) []byte { | ||
| extractDir := filepath.Join(tmpDir, name) | ||
| repackedPath := filepath.Join(tmpDir, name+".tar.gz") | ||
|
|
||
| previousUmask := syscall.Umask(umask) | ||
| defer syscall.Umask(previousUmask) | ||
|
|
||
| require.NoError(t, archive.Extract(archivePath, extractDir, archive.CompressionGzip)) | ||
| require.NoError(t, archive.CreateDeterministicArchive(repackedPath, extractDir, archive.CompressionGzip)) | ||
|
|
||
| directoryInfo, err := os.Stat(filepath.Join(extractDir, "implicit")) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, os.FileMode(0o755), directoryInfo.Mode().Perm()) | ||
|
|
||
| fileInfo, err := os.Stat(filepath.Join(extractDir, "implicit", "file.txt")) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, os.FileMode(0o666), fileInfo.Mode().Perm()) | ||
|
|
||
| data, err := os.ReadFile(repackedPath) | ||
| require.NoError(t, err) | ||
|
|
||
| return data | ||
| } | ||
|
|
||
| standard := repack("standard", 0o022) | ||
| restrictive := repack("restrictive", 0o077) | ||
| assert.Equal(t, standard, restrictive, "repacked archive must not depend on the process umask") | ||
| } | ||
|
|
||
| func TestExtract_ImplicitDirectorySymlinkAliasDoesNotOverrideExplicitMode(t *testing.T) { | ||
| tmpDir := t.TempDir() | ||
| archivePath := filepath.Join(tmpDir, "source.tar.gz") | ||
| extractDir := filepath.Join(tmpDir, "extracted") | ||
|
|
||
| createTestTarGz(t, archivePath, []testTarEntry{ | ||
| {name: "real/", typeflag: tar.TypeDir, mode: 0o700}, | ||
| {name: "x", typeflag: tar.TypeSymlink, linkname: "real"}, | ||
| {name: "x/subdir/file", typeflag: tar.TypeReg, content: "content"}, | ||
| {name: "real/subdir/", typeflag: tar.TypeDir, mode: 0o700}, | ||
| }) | ||
|
|
||
| require.NoError(t, archive.Extract(archivePath, extractDir, archive.CompressionGzip)) | ||
|
|
||
| for _, path := range []string{"real/subdir", "x/subdir"} { | ||
| info, err := os.Stat(filepath.Join(extractDir, path)) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, os.FileMode(0o700), info.Mode().Perm(), "directory %#q mode", path) | ||
| } | ||
| } | ||
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
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.
nit: file name doesn't make a lot of sense.