Skip to content

Commit 5181b9c

Browse files
Merge pull request #3320 from nalind/copier-xattrs
copier.Put(): set xattrs after ownership
2 parents 28ea559 + 854b38c commit 5181b9c

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

copier/copier.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,14 +1745,6 @@ func copierHandlerPut(bulkReader io.Reader, req request, idMappings *idtools.IDM
17451745
if err != nil {
17461746
return errors.Wrapf(err, "copier: put: error creating %q", path)
17471747
}
1748-
// restore xattrs
1749-
if !req.PutOptions.StripXattrs {
1750-
if err = Lsetxattrs(path, hdr.Xattrs); err != nil { // nolint:staticcheck
1751-
if !req.PutOptions.IgnoreXattrErrors {
1752-
return errors.Wrapf(err, "copier: put: error setting extended attributes on %q", path)
1753-
}
1754-
}
1755-
}
17561748
// set ownership
17571749
if err = lchown(path, hdr.Uid, hdr.Gid); err != nil {
17581750
return errors.Wrapf(err, "copier: put: error setting ownership of %q to %d:%d", path, hdr.Uid, hdr.Gid)
@@ -1778,6 +1770,14 @@ func copierHandlerPut(bulkReader io.Reader, req request, idMappings *idtools.IDM
17781770
return errors.Wrapf(err, "error setting additional permissions on %q to 0%o", path, mode)
17791771
}
17801772
}
1773+
// set xattrs, including some that might have been reset by chown()
1774+
if !req.PutOptions.StripXattrs {
1775+
if err = Lsetxattrs(path, hdr.Xattrs); err != nil { // nolint:staticcheck
1776+
if !req.PutOptions.IgnoreXattrErrors {
1777+
return errors.Wrapf(err, "copier: put: error setting extended attributes on %q", path)
1778+
}
1779+
}
1780+
}
17811781
// set time
17821782
if hdr.AccessTime.IsZero() || hdr.AccessTime.Before(hdr.ModTime) {
17831783
hdr.AccessTime = hdr.ModTime

tests/copy.bats

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,3 +445,26 @@ stuff/mystuff"
445445

446446
run_buildah 1 run $from ls -l subdir/sub2.txt
447447
}
448+
449+
@test "copy-preserving-extended-attributes" {
450+
createrandom ${TESTDIR}/randomfile
451+
image="registry.fedoraproject.org/fedora-minimal"
452+
_prefetch $image
453+
run_buildah from --quiet --signature-policy ${TESTSDIR}/policy.json $image
454+
first="$output"
455+
run_buildah run $first microdnf -y install /usr/bin/getfattr /usr/bin/setfattr /usr/sbin/setcap
456+
run_buildah copy $first ${TESTDIR}/randomfile /
457+
# set security.capability
458+
run buildah run $first setcap cap_setuid=ep /randomfile
459+
# set user.something
460+
run buildah run $first setfattr user.yeah=butno /randomfile
461+
# copy the file to a second container
462+
run_buildah from --quiet --signature-policy ${TESTSDIR}/policy.json $image
463+
second="$output"
464+
run_buildah copy --from $first $second /randomfile /
465+
# compare what the extended attributes look like. if we're on a system with SELinux, there's a label in here, too
466+
run buildah run $first sh -c "getfattr -d -m . --absolute-names /randomfile | sort"
467+
expected="$output"
468+
run buildah run $first sh -c "getfattr -d -m . --absolute-names /randomfile | sort"
469+
expect_output "$expected"
470+
}

0 commit comments

Comments
 (0)