The syscall package is locked down and the comment in [1] advises to
switch code to use the corresponding package from golang.org/x/sys. Do
so and replace usage of package syscall with package
golang.org/x/sys/{unix,windows} where applicable.
  [1] https://github.com/golang/go/blob/master/src/syscall/syscall.go#L21-L24
This will also allow to get updates and fixes for syscall wrappers
without having to use a new go version.
Errno, Signal and SysProcAttr aren't changed as they haven't been
implemented in x/sys/. Stat_t from syscall is used if standard library
packages (e.g. os) require it. syscall.ENOTSUP, syscall.SIGKILL and
syscall.SIGTERM are used for cross-platform files.
Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
		
	
		
			
				
	
	
		
			135 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			135 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
// +build !windows
 | 
						|
 | 
						|
package archive
 | 
						|
 | 
						|
import (
 | 
						|
	"os"
 | 
						|
	"sync"
 | 
						|
	"syscall"
 | 
						|
 | 
						|
	"github.com/containerd/continuity/sysx"
 | 
						|
	"github.com/dmcgowan/go-tar"
 | 
						|
	"github.com/opencontainers/runc/libcontainer/system"
 | 
						|
	"github.com/pkg/errors"
 | 
						|
	"golang.org/x/sys/unix"
 | 
						|
)
 | 
						|
 | 
						|
func tarName(p string) (string, error) {
 | 
						|
	return p, nil
 | 
						|
}
 | 
						|
 | 
						|
func chmodTarEntry(perm os.FileMode) os.FileMode {
 | 
						|
	return perm
 | 
						|
}
 | 
						|
 | 
						|
func setHeaderForSpecialDevice(hdr *tar.Header, name string, fi os.FileInfo) error {
 | 
						|
	s, ok := fi.Sys().(*syscall.Stat_t)
 | 
						|
	if !ok {
 | 
						|
		return errors.New("unsupported stat type")
 | 
						|
	}
 | 
						|
 | 
						|
	// Currently go does not fill in the major/minors
 | 
						|
	if s.Mode&syscall.S_IFBLK != 0 ||
 | 
						|
		s.Mode&syscall.S_IFCHR != 0 {
 | 
						|
		hdr.Devmajor = int64(major(uint64(s.Rdev)))
 | 
						|
		hdr.Devminor = int64(minor(uint64(s.Rdev)))
 | 
						|
	}
 | 
						|
 | 
						|
	return nil
 | 
						|
}
 | 
						|
 | 
						|
func major(device uint64) uint64 {
 | 
						|
	return (device >> 8) & 0xfff
 | 
						|
}
 | 
						|
 | 
						|
func minor(device uint64) uint64 {
 | 
						|
	return (device & 0xff) | ((device >> 12) & 0xfff00)
 | 
						|
}
 | 
						|
 | 
						|
func mkdev(major int64, minor int64) uint32 {
 | 
						|
	return uint32(((minor & 0xfff00) << 12) | ((major & 0xfff) << 8) | (minor & 0xff))
 | 
						|
}
 | 
						|
 | 
						|
func open(p string) (*os.File, error) {
 | 
						|
	return os.Open(p)
 | 
						|
}
 | 
						|
 | 
						|
func openFile(name string, flag int, perm os.FileMode) (*os.File, error) {
 | 
						|
	return os.OpenFile(name, flag, perm)
 | 
						|
}
 | 
						|
 | 
						|
func mkdirAll(path string, perm os.FileMode) error {
 | 
						|
	return os.MkdirAll(path, perm)
 | 
						|
}
 | 
						|
 | 
						|
func prepareApply() func() {
 | 
						|
	// Unset unmask before doing an apply operation,
 | 
						|
	// restore unmask when complete
 | 
						|
	oldmask := unix.Umask(0)
 | 
						|
	return func() {
 | 
						|
		unix.Umask(oldmask)
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
func skipFile(*tar.Header) bool {
 | 
						|
	return false
 | 
						|
}
 | 
						|
 | 
						|
var (
 | 
						|
	inUserNS bool
 | 
						|
	nsOnce   sync.Once
 | 
						|
)
 | 
						|
 | 
						|
func setInUserNS() {
 | 
						|
	inUserNS = system.RunningInUserNS()
 | 
						|
}
 | 
						|
 | 
						|
// handleTarTypeBlockCharFifo is an OS-specific helper function used by
 | 
						|
// createTarFile to handle the following types of header: Block; Char; Fifo
 | 
						|
func handleTarTypeBlockCharFifo(hdr *tar.Header, path string) error {
 | 
						|
	nsOnce.Do(setInUserNS)
 | 
						|
	if inUserNS {
 | 
						|
		// cannot create a device if running in user namespace
 | 
						|
		return nil
 | 
						|
	}
 | 
						|
 | 
						|
	mode := uint32(hdr.Mode & 07777)
 | 
						|
	switch hdr.Typeflag {
 | 
						|
	case tar.TypeBlock:
 | 
						|
		mode |= unix.S_IFBLK
 | 
						|
	case tar.TypeChar:
 | 
						|
		mode |= unix.S_IFCHR
 | 
						|
	case tar.TypeFifo:
 | 
						|
		mode |= unix.S_IFIFO
 | 
						|
	}
 | 
						|
 | 
						|
	return unix.Mknod(path, mode, int(mkdev(hdr.Devmajor, hdr.Devminor)))
 | 
						|
}
 | 
						|
 | 
						|
func handleLChmod(hdr *tar.Header, path string, hdrInfo os.FileInfo) error {
 | 
						|
	if hdr.Typeflag == tar.TypeLink {
 | 
						|
		if fi, err := os.Lstat(hdr.Linkname); err == nil && (fi.Mode()&os.ModeSymlink == 0) {
 | 
						|
			if err := os.Chmod(path, hdrInfo.Mode()); err != nil {
 | 
						|
				return err
 | 
						|
			}
 | 
						|
		}
 | 
						|
	} else if hdr.Typeflag != tar.TypeSymlink {
 | 
						|
		if err := os.Chmod(path, hdrInfo.Mode()); err != nil {
 | 
						|
			return err
 | 
						|
		}
 | 
						|
	}
 | 
						|
	return nil
 | 
						|
}
 | 
						|
 | 
						|
func getxattr(path, attr string) ([]byte, error) {
 | 
						|
	b, err := sysx.LGetxattr(path, attr)
 | 
						|
	if err == unix.ENOTSUP || err == unix.ENODATA {
 | 
						|
		return nil, nil
 | 
						|
	}
 | 
						|
	return b, err
 | 
						|
}
 | 
						|
 | 
						|
func setxattr(path, key, value string) error {
 | 
						|
	return sysx.LSetxattr(path, key, []byte(value), 0)
 | 
						|
}
 |