Skip to content

Commit

Permalink
archive: store the override xattr with the inode type
Browse files Browse the repository at this point in the history
Fixes: #2174

Signed-off-by: Giuseppe Scrivano <[email protected]>
  • Loading branch information
giuseppe committed Dec 4, 2024
1 parent 237fa19 commit 98ff561
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions pkg/archive/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,12 +655,19 @@ func createTarFile(path, extractDir string, hdr *tar.Header, reader io.Reader, L
// so use hdrInfo.Mode() (they differ for e.g. setuid bits)
hdrInfo := hdr.FileInfo()

typeFlag := hdr.Typeflag
mask := hdrInfo.Mode()

if forceMask != nil {
mask = *forceMask
// If we have a forceMask, force the real type to either be a directory,
// a symlink, or a regular file.
if typeFlag != tar.TypeDir && typeFlag != tar.TypeSymlink {
typeFlag = tar.TypeReg
}
}

switch hdr.Typeflag {
switch typeFlag {
case tar.TypeDir:
// Create directory unless it exists as a directory already.
// In that case we just want to merge the two
Expand Down Expand Up @@ -728,16 +735,6 @@ func createTarFile(path, extractDir string, hdr *tar.Header, reader io.Reader, L
return fmt.Errorf("unhandled tar header type %d", hdr.Typeflag)
}

if forceMask != nil && (hdr.Typeflag != tar.TypeSymlink || runtime.GOOS == "darwin") {
value := idtools.Stat{
IDs: idtools.IDPair{UID: hdr.Uid, GID: hdr.Gid},
Mode: hdrInfo.Mode() & 0o7777,
}
if err := idtools.SetContainersOverrideXattr(path, value); err != nil {
return err
}
}

// Lchown is not supported on Windows.
if Lchown && runtime.GOOS != windows {
if chownOpts == nil {
Expand Down Expand Up @@ -807,6 +804,18 @@ func createTarFile(path, extractDir string, hdr *tar.Header, reader io.Reader, L
}
}

if forceMask != nil && (typeFlag == tar.TypeReg || typeFlag == tar.TypeDir || runtime.GOOS == "darwin") {
value := idtools.Stat{
IDs: idtools.IDPair{UID: hdr.Uid, GID: hdr.Gid},
Mode: hdrInfo.Mode(),
Major: int(hdr.Devmajor),
Minor: int(hdr.Devminor),
}
if err := idtools.SetContainersOverrideXattr(path, value); err != nil {
return err
}
}

// We defer setting flags on directories until the end of
// Unpack or UnpackLayer in case setting them makes the
// directory immutable.
Expand Down Expand Up @@ -1149,11 +1158,11 @@ loop:
}

if options.ForceMask != nil {
value := idtools.Stat{Mode: 0o755}
value := idtools.Stat{Mode: os.ModeDir | os.FileMode(0o755)}
if rootHdr != nil {
value.IDs.UID = rootHdr.Uid
value.IDs.GID = rootHdr.Gid
value.Mode = os.FileMode(rootHdr.Mode)
value.Mode = os.ModeDir | os.FileMode(rootHdr.Mode)
}
if err := idtools.SetContainersOverrideXattr(dest, value); err != nil {
return err
Expand Down

0 comments on commit 98ff561

Please sign in to comment.