Skip to content

Commit 2bffb5f

Browse files
committed
FreeBSD: fix tar headers & the nil check on getxattr
On FreeBSD + zfs, stat call seem to return garbage in RDev for regular files & folders. The value returned is large enough not to fit into `Devmajor` & `Devminor` fields of the tar header. Fortunately, these fields are required just for special devices. This change * adds a check into `setHeaderForSpecialDevice` that the input header represents a special device. If it's not the case, we don't set the Devmajor & Devminor fields. * fixes the nil check on `getxattr`: it never returns nils, but rather an empty slice instead Signed-off-by: Artem Khramov <[email protected]>
1 parent cbf9d88 commit 2bffb5f

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

archive/tar.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ func (cw *ChangeWriter) HandleChange(k fs.ChangeKind, p string, f os.FileInfo, e
598598

599599
if capability, err := getxattr(source, "security.capability"); err != nil {
600600
return errors.Wrap(err, "failed to get capabilities xattr")
601-
} else if capability != nil {
601+
} else if len(capability) > 0 {
602602
if hdr.PAXRecords == nil {
603603
hdr.PAXRecords = map[string]string{}
604604
}

archive/tar_unix.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package archive
2222
import (
2323
"archive/tar"
2424
"os"
25+
"runtime"
2526
"strings"
2627
"syscall"
2728

@@ -41,6 +42,20 @@ func chmodTarEntry(perm os.FileMode) os.FileMode {
4142
}
4243

4344
func setHeaderForSpecialDevice(hdr *tar.Header, name string, fi os.FileInfo) error {
45+
// Devmajor and Devminor are only needed for special devices.
46+
47+
// In FreeBSD, RDev for regular files is -1 (unless overridden by FS):
48+
// https://cgit.freebsd.org/src/tree/sys/kern/vfs_default.c?h=stable/13#n1531
49+
// (NODEV is -1: https://cgit.freebsd.org/src/tree/sys/sys/param.h?h=stable/13#n241).
50+
51+
// ZFS in particular does not override the default:
52+
// https://cgit.freebsd.org/src/tree/sys/contrib/openzfs/module/os/freebsd/zfs/zfs_vnops_os.c?h=stable/13#n2027
53+
54+
// Since `Stat_t.Rdev` is uint64, the cast turns -1 into (2^64 - 1).
55+
// Such large values cannot be encoded in a tar header.
56+
if runtime.GOOS == "freebsd" && hdr.Typeflag != tar.TypeBlock && hdr.Typeflag != tar.TypeChar {
57+
return nil
58+
}
4459
s, ok := fi.Sys().(*syscall.Stat_t)
4560
if !ok {
4661
return errors.New("unsupported stat type")

0 commit comments

Comments
 (0)