Skip to content
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

Add option to preserve file attributes #106

Open
wants to merge 49 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
8dbb570
invented option to preserve filemode
NodyHub Dec 5, 2024
49b513b
added support for updating ownership, file permissions and change time
NodyHub Dec 9, 2024
1605a68
code clean up
NodyHub Dec 9, 2024
0405d3f
Further adjustments
NodyHub Dec 9, 2024
576854a
adjusted cli help output
NodyHub Dec 9, 2024
2160ffe
adjusted build tag specification
NodyHub Dec 10, 2024
4c99ad2
corrected mod time adjustment
NodyHub Dec 10, 2024
fd3a385
removed code comment and fixed timestamp of archive file
NodyHub Dec 10, 2024
1ded861
rely on built-in unix.Timeval conversion
samsalisbury Dec 10, 2024
593b990
Merge pull request #109 from hashicorp/simplify-unix-time-handling
NodyHub Dec 10, 2024
03a7cb6
unexpose internal functions
NodyHub Dec 10, 2024
fb7132a
Update target_memory_test.go
NodyHub Dec 11, 2024
f9b2710
check for differences >= 1 micro second
NodyHub Dec 11, 2024
9e69c9d
simplified test print statement
NodyHub Dec 11, 2024
7fa9d28
invent testing as root to adjust ownership
NodyHub Dec 11, 2024
8498e88
transformed canMaintainSymlinkTimestamps into a function and relocate…
NodyHub Dec 11, 2024
900c4d0
skip sudo test on windows
NodyHub Dec 11, 2024
96659b8
added some error handling
NodyHub Dec 11, 2024
f4bfff1
adjusted comment that rar does not support uig/gid
NodyHub Dec 16, 2024
e9c3941
located chown in platform specific implementation
NodyHub Dec 16, 2024
9ab3635
marked in test case that rar does not support file ownership
NodyHub Dec 16, 2024
00f38eb
separated ownership preservation in an own flag
NodyHub Dec 16, 2024
cb45ca4
relocated test data
NodyHub Dec 16, 2024
2e7c380
added copyright header
NodyHub Dec 16, 2024
dacbd03
updated comments to remark that ownership is only transported by tar …
NodyHub Dec 16, 2024
3fb2d38
verified overwrite behaviour for double-symlinks in archive
NodyHub Dec 16, 2024
668eb6b
adjusted error handling and wrapping
NodyHub Dec 17, 2024
e1ce17d
Return current Uid/Gid if archive does not support carriage of owners…
NodyHub Dec 17, 2024
03a9e84
inverted logic to preserve file attributes (mod/access time and file …
NodyHub Dec 17, 2024
f5b7936
removed windows specifc code
NodyHub Dec 17, 2024
ccf382e
adjusted test cases file split
NodyHub Dec 17, 2024
111a61f
adjusted test output log
NodyHub Dec 17, 2024
b4c67e9
drop write permissions on directory if no access is granted
NodyHub Dec 17, 2024
7671e15
drop permission check for directories on windows, bc/ they create uns…
NodyHub Dec 17, 2024
879edcd
updated test case comment
NodyHub Dec 17, 2024
450aba3
clarified test in comment
NodyHub Dec 17, 2024
3c7c310
updated readme
NodyHub Dec 17, 2024
5ff6279
renamed config.noPreserveFileAttributes to config.dropFileAttributes
NodyHub Dec 18, 2024
668a07e
Update target_disk_unix.go
NodyHub Dec 18, 2024
49171eb
fixed import
NodyHub Dec 18, 2024
9d10a84
adjusted short version of flag PreserveOwner from -o to -p , bc/ it i…
NodyHub Dec 18, 2024
5580e05
adjusted comments
NodyHub Dec 18, 2024
bf3804a
removed unused field from test
NodyHub Jan 6, 2025
f59ea35
Update unpack_unix_test.go
NodyHub Jan 8, 2025
6c8d47a
dropped uid/gid from upper test case level and aligned the test case
NodyHub Jan 8, 2025
edfd38f
simplified chown check
NodyHub Jan 8, 2025
064184a
adjusted the current-user-tar test-case to invalid-uid-tar so that ev…
NodyHub Jan 8, 2025
45cb851
adjusted test for invalid uid/gid
NodyHub Jan 8, 2025
5c74796
adjusted uid casting
NodyHub Jan 8, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions unpack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1611,8 +1611,6 @@ var testCases = []struct {
doesNotSupportModTime bool
doesNotSupportOwner bool
expectError bool
uid int
gid int
}{
{
name: "tar",
Expand All @@ -1622,8 +1620,6 @@ var testCases = []struct {
{Name: "sub/test", Content: []byte("hello world"), Mode: 0777, AccessTime: baseTime, ModTime: baseTime, Uid: testDataUid, Gid: testDataGid},
{Name: "link", Mode: fs.ModeSymlink | 0777, Linktarget: "sub/test", AccessTime: baseTime, ModTime: baseTime, Uid: testDataUid, Gid: testDataGid},
},
uid: testDataUid,
gid: testDataGid,
packer: packTar,
},
{
Expand All @@ -1634,8 +1630,6 @@ var testCases = []struct {
{Name: "sub/test", Content: []byte("hello world"), Mode: 0777, AccessTime: baseTime, ModTime: baseTime, Uid: testDataRootUid, Gid: testDataWheelGid},
{Name: "link", Mode: fs.ModeSymlink | 0777, Linktarget: "sub/test", AccessTime: baseTime, ModTime: baseTime, Uid: testDataRootUid, Gid: testDataWheelGid},
},
uid: testDataRootUid,
gid: testDataWheelGid,
packer: packTar,
},
{
Expand All @@ -1646,25 +1640,19 @@ var testCases = []struct {
{Name: "sub/test", Content: []byte("hello world"), Mode: 0644, AccessTime: baseTime, ModTime: baseTime, Uid: os.Getuid(), Gid: os.Getgid()},
{Name: "link", Mode: fs.ModeSymlink | 0777, Linktarget: "sub/test", AccessTime: baseTime, ModTime: baseTime, Uid: os.Getuid(), Gid: os.Getgid()},
},
uid: os.Getgid(), // bc/ of the way zip works, we can't set the uid and the created files are owned by the user running the test
gid: os.Getgid(), // bc/ of the way zip works, we can't set the gid and the created files are owned by the user running the test
doesNotSupportOwner: true,
packer: packZip,
},
{
name: "rar",
contents: contentsRar2,
uid: os.Getuid(), // bc/ of the way rar works, we can't set the uid and the created files are owned by the user running the test
gid: os.Getgid(), // bc/ of the way rar works, we can't set the gid and the created files are owned by the user running the test
doesNotSupportOwner: true,
doesNotSupportModTime: true,
packer: packRar2,
},
{
name: "7z",
contents: contents7z2,
uid: os.Getuid(), // bc/ of the way 7z works, we can't set the uid and the created files are owned by the user running the test
gid: os.Getgid(), // bc/ of the way 7z works, we can't set the gid and the created files are owned by the user running the test
doesNotSupportOwner: true,
packer: pack7z2,
},
Expand Down
12 changes: 2 additions & 10 deletions unpack_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,14 @@ func TestUnpackWithPreserveOwnershipAsNonRoot(t *testing.T) {
src = asIoReader(t, tc.packer(t, tc.contents))
cfg = extract.NewConfig(extract.WithPreserveOwner(true))
)
// compare archive uid/gid with current user
archiveEntriesEqualCurrentOwner := tc.doesNotSupportOwner

// If the archive supports owner information, check if the current user is the owner of the test data
if !tc.doesNotSupportOwner && (os.Getuid() != tc.uid || os.Getgid() != tc.gid) {
archiveEntriesEqualCurrentOwner = false
}

// Unpack should fail if the user is not root and the uid/gid
// in the archive is different from the current user (only
// if the archive supports owner information)
err := extract.Unpack(ctx, dst, src, cfg)

// chown will only fail if the user is not root and the uid/gid is different
if archiveEntriesEqualCurrentOwner && err != nil {
// chown will only fail if the user is not root
if !tc.doesNotSupportOwner && err == nil {
t.Fatalf("error unpacking archive: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be != nil ? As the log message references the error...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is sadly made to an out-of-date state of this PR and already adjusted. The ask for the err == nil is addressed in #106 (comment).

The check for tc.doesNotSupportOwner is moved to the top of the test-case:

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// skip test if the archive does not store ownership information
if tc.doesNotSupportOwner {
t.Skipf("archive %s does not store ownership information", tc.name)
}

Background: If an archive format does not contain ownership details, we can skip the archive format, bc/ the test for failing chown will never fail, bc/ extracted files will always be the uid/gid of the process and the archive entries will for, e.g. zip (which does not contain file ownership information), will return always the current uid/gid (ref)

}
})
Expand Down Expand Up @@ -122,7 +115,6 @@ func TestUnpackWithPreserveOwnershipAsRoot(t *testing.T) {

func TestWithCustomMode(t *testing.T) {
umask := sniffUmask(t)
// expectedMode &= ^umask // Adjust for umask on non-Windows systems

tests := []struct {
name string
Expand Down
Loading