buildah/copier/syscall_unix.go

93 lines
2.0 KiB
Go
Raw Normal View History

//go:build !windows
// +build !windows
package copier
import (
"fmt"
"os"
"syscall"
"time"
"golang.org/x/sys/unix"
)
var canChroot = os.Getuid() == 0
func chroot(root string) (bool, error) {
if canChroot {
if err := os.Chdir(root); err != nil {
return false, fmt.Errorf("error changing to intended-new-root directory %q: %w", root, err)
}
if err := unix.Chroot(root); err != nil {
return false, fmt.Errorf("error chrooting to directory %q: %w", root, err)
}
if err := os.Chdir(string(os.PathSeparator)); err != nil {
return false, fmt.Errorf("error changing to just-became-root directory %q: %w", root, err)
}
return true, nil
}
return false, nil
}
func chrMode(mode os.FileMode) uint32 {
return uint32(unix.S_IFCHR | mode)
}
func blkMode(mode os.FileMode) uint32 {
return uint32(unix.S_IFBLK | mode)
}
func mkdev(major, minor uint32) uint64 {
return unix.Mkdev(major, minor)
}
func mkfifo(path string, mode uint32) error {
return unix.Mkfifo(path, mode)
}
copier: add Mkdir() Add a function for doing Mkdir-possibly-in-a-chroot, for ensuring that a directory exists without having to possibly create it directly from outside of a chroot. Make use of filepath.ToSlash() and filepath.FromSlash() where it's appropriate. Add more unit tests. Address some review comments: * Check for ERANGE instead of E2BIG errors from llistxattr() and lgetxattr() as indicators that the buffer we passed to them is too small. * Factor an `isRelevantXattr` helper out of Lgetxattrs() and Lsetxattrs(). * Drop our getcwd() function in favor of using os.Getwd(). * Adjust the comment describing the GetOptions.KeepDirectoryNames field. * Clean hdr.Name before attempting to compute where an item being extracted should go. * When writing items to the filesystem, create with 0?00 permissions, set ownership, then set the correct permissions. * Merge StatResponse.Error, GetResponse.Error, and PutResponse.Error into Response.Error. * When reading items from the filesystem, if a glob matches multiple items, and one of the items is excluded, continue checking the other items. * Make sure we always Wait() on a child process if we spawned one. * Clean up the cleanup logic for pipes that we use to communicate with a child process. * Clean up the kill-the-child-process logic we call when we encounter an error communicating with the child process. * Drop the separate Options structure, use helper methods to simplify pulling out the right ID maps and exclusions list for the request. Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2020-07-31 02:00:25 +08:00
func chmod(path string, mode os.FileMode) error {
return os.Chmod(path, mode)
}
func chown(path string, uid, gid int) error {
return os.Chown(path, uid, gid)
}
func lchown(path string, uid, gid int) error {
return os.Lchown(path, uid, gid)
}
func lutimes(isSymlink bool, path string, atime, mtime time.Time) error {
if atime.IsZero() || mtime.IsZero() {
now := time.Now()
if atime.IsZero() {
atime = now
}
if mtime.IsZero() {
mtime = now
}
}
return unix.Lutimes(path, []unix.Timeval{unix.NsecToTimeval(atime.UnixNano()), unix.NsecToTimeval(mtime.UnixNano())})
}
copier: add Mkdir() Add a function for doing Mkdir-possibly-in-a-chroot, for ensuring that a directory exists without having to possibly create it directly from outside of a chroot. Make use of filepath.ToSlash() and filepath.FromSlash() where it's appropriate. Add more unit tests. Address some review comments: * Check for ERANGE instead of E2BIG errors from llistxattr() and lgetxattr() as indicators that the buffer we passed to them is too small. * Factor an `isRelevantXattr` helper out of Lgetxattrs() and Lsetxattrs(). * Drop our getcwd() function in favor of using os.Getwd(). * Adjust the comment describing the GetOptions.KeepDirectoryNames field. * Clean hdr.Name before attempting to compute where an item being extracted should go. * When writing items to the filesystem, create with 0?00 permissions, set ownership, then set the correct permissions. * Merge StatResponse.Error, GetResponse.Error, and PutResponse.Error into Response.Error. * When reading items from the filesystem, if a glob matches multiple items, and one of the items is excluded, continue checking the other items. * Make sure we always Wait() on a child process if we spawned one. * Clean up the cleanup logic for pipes that we use to communicate with a child process. * Clean up the kill-the-child-process logic we call when we encounter an error communicating with the child process. * Drop the separate Options structure, use helper methods to simplify pulling out the right ID maps and exclusions list for the request. Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2020-07-31 02:00:25 +08:00
// sameDevice returns true unless we're sure that they're not on the same device
func sameDevice(a, b os.FileInfo) bool {
aSys := a.Sys()
bSys := b.Sys()
if aSys == nil || bSys == nil {
return true
}
au, aok := aSys.(*syscall.Stat_t)
bu, bok := bSys.(*syscall.Stat_t)
if !aok || !bok {
return true
}
return au.Dev == bu.Dev
}
copier: add Mkdir() Add a function for doing Mkdir-possibly-in-a-chroot, for ensuring that a directory exists without having to possibly create it directly from outside of a chroot. Make use of filepath.ToSlash() and filepath.FromSlash() where it's appropriate. Add more unit tests. Address some review comments: * Check for ERANGE instead of E2BIG errors from llistxattr() and lgetxattr() as indicators that the buffer we passed to them is too small. * Factor an `isRelevantXattr` helper out of Lgetxattrs() and Lsetxattrs(). * Drop our getcwd() function in favor of using os.Getwd(). * Adjust the comment describing the GetOptions.KeepDirectoryNames field. * Clean hdr.Name before attempting to compute where an item being extracted should go. * When writing items to the filesystem, create with 0?00 permissions, set ownership, then set the correct permissions. * Merge StatResponse.Error, GetResponse.Error, and PutResponse.Error into Response.Error. * When reading items from the filesystem, if a glob matches multiple items, and one of the items is excluded, continue checking the other items. * Make sure we always Wait() on a child process if we spawned one. * Clean up the cleanup logic for pipes that we use to communicate with a child process. * Clean up the kill-the-child-process logic we call when we encounter an error communicating with the child process. * Drop the separate Options structure, use helper methods to simplify pulling out the right ID maps and exclusions list for the request. Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2020-07-31 02:00:25 +08:00
const (
testModeMask = int64(os.ModePerm)
testIgnoreSymlinkDates = false
)