Skip to content

Commit 3fbe90a

Browse files
committed
archive: store the override xattr with the inode type
Fixes: #2174 Signed-off-by: Giuseppe Scrivano <[email protected]>
1 parent 40a1ea7 commit 3fbe90a

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

pkg/archive/archive.go

+24-14
Original file line numberDiff line numberDiff line change
@@ -655,12 +655,20 @@ func createTarFile(path, extractDir string, hdr *tar.Header, reader io.Reader, L
655655
// so use hdrInfo.Mode() (they differ for e.g. setuid bits)
656656
hdrInfo := hdr.FileInfo()
657657

658+
typeFlag := hdr.Typeflag
658659
mask := hdrInfo.Mode()
660+
661+
// update also the implementation of ForceMask in pkg/chunked
659662
if forceMask != nil {
660663
mask = *forceMask
664+
// If we have a forceMask, force the real type to either be a directory,
665+
// a link, or a regular file.
666+
if typeFlag != tar.TypeDir && typeFlag != tar.TypeSymlink && typeFlag != tar.TypeLink {
667+
typeFlag = tar.TypeReg
668+
}
661669
}
662670

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

731-
if forceMask != nil && (hdr.Typeflag != tar.TypeSymlink || runtime.GOOS == "darwin") {
732-
value := idtools.Stat{
733-
IDs: idtools.IDPair{UID: hdr.Uid, GID: hdr.Gid},
734-
Mode: hdrInfo.Mode() & 0o7777,
735-
}
736-
if err := idtools.SetContainersOverrideXattr(path, value); err != nil {
737-
return err
738-
}
739-
}
740-
741739
// Lchown is not supported on Windows.
742740
if Lchown && runtime.GOOS != windows {
743741
if chownOpts == nil {
@@ -807,6 +805,18 @@ func createTarFile(path, extractDir string, hdr *tar.Header, reader io.Reader, L
807805
}
808806
}
809807

808+
if forceMask != nil && (typeFlag == tar.TypeReg || typeFlag == tar.TypeDir || runtime.GOOS == "darwin") {
809+
value := idtools.Stat{
810+
IDs: idtools.IDPair{UID: hdr.Uid, GID: hdr.Gid},
811+
Mode: hdrInfo.Mode(),
812+
Major: int(hdr.Devmajor),
813+
Minor: int(hdr.Devminor),
814+
}
815+
if err := idtools.SetContainersOverrideXattr(path, value); err != nil {
816+
return err
817+
}
818+
}
819+
810820
// We defer setting flags on directories until the end of
811821
// Unpack or UnpackLayer in case setting them makes the
812822
// directory immutable.
@@ -1149,11 +1159,11 @@ loop:
11491159
}
11501160

11511161
if options.ForceMask != nil {
1152-
value := idtools.Stat{Mode: 0o755}
1162+
value := idtools.Stat{Mode: os.ModeDir | os.FileMode(0o755)}
11531163
if rootHdr != nil {
11541164
value.IDs.UID = rootHdr.Uid
11551165
value.IDs.GID = rootHdr.Gid
1156-
value.Mode = os.FileMode(rootHdr.Mode)
1166+
value.Mode = os.ModeDir | os.FileMode(rootHdr.Mode)
11571167
}
11581168
if err := idtools.SetContainersOverrideXattr(dest, value); err != nil {
11591169
return err
@@ -1379,7 +1389,7 @@ func remapIDs(readIDMappings, writeIDMappings *idtools.IDMappings, chownOpts *id
13791389
uid, gid = hdr.Uid, hdr.Gid
13801390
if xstat, ok := hdr.PAXRecords[PaxSchilyXattr+idtools.ContainersOverrideXattr]; ok {
13811391
attrs := strings.Split(string(xstat), ":")
1382-
if len(attrs) == 3 {
1392+
if len(attrs) >= 3 {
13831393
val, err := strconv.ParseUint(attrs[0], 10, 32)
13841394
if err != nil {
13851395
uid = int(val)

0 commit comments

Comments
 (0)