Merge pull request #7386 from mxpv/no-same-owner
This commit is contained in:
@@ -43,7 +43,7 @@ func TestPrefixHeaderReadable(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer r.Close()
|
||||
_, err = Apply(context.Background(), tmpDir, r)
|
||||
_, err = Apply(context.Background(), tmpDir, r, WithNoSameOwner())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -24,7 +24,6 @@ import (
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
@@ -290,7 +289,7 @@ func applyNaive(ctx context.Context, root string, r io.Reader, options ApplyOpti
|
||||
srcData := io.Reader(tr)
|
||||
srcHdr := hdr
|
||||
|
||||
if err := createTarFile(ctx, path, root, srcHdr, srcData); err != nil {
|
||||
if err := createTarFile(ctx, path, root, srcHdr, srcData, options.NoSameOwner); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
@@ -315,7 +314,7 @@ func applyNaive(ctx context.Context, root string, r io.Reader, options ApplyOpti
|
||||
return size, nil
|
||||
}
|
||||
|
||||
func createTarFile(ctx context.Context, path, extractDir string, hdr *tar.Header, reader io.Reader) error {
|
||||
func createTarFile(ctx context.Context, path, extractDir string, hdr *tar.Header, reader io.Reader, noSameOwner bool) error {
|
||||
// hdr.Mode is in linux format, which we can use for syscalls,
|
||||
// but for os.Foo() calls we need the mode converted to os.FileMode,
|
||||
// so use hdrInfo.Mode() (they differ for e.g. setuid bits)
|
||||
@@ -380,8 +379,7 @@ func createTarFile(ctx context.Context, path, extractDir string, hdr *tar.Header
|
||||
return fmt.Errorf("unhandled tar header type %d", hdr.Typeflag)
|
||||
}
|
||||
|
||||
// Lchown is not supported on Windows.
|
||||
if runtime.GOOS != "windows" {
|
||||
if !noSameOwner {
|
||||
if err := os.Lchown(path, hdr.Uid, hdr.Gid); err != nil {
|
||||
err = fmt.Errorf("failed to Lchown %q for UID %d, GID %d: %w", path, hdr.Uid, hdr.Gid, err)
|
||||
if errors.Is(err, syscall.EINVAL) && userns.RunningInUserNS() {
|
||||
|
||||
@@ -27,6 +27,7 @@ type ApplyOptions struct {
|
||||
Filter Filter // Filter tar headers
|
||||
ConvertWhiteout ConvertWhiteout // Convert whiteout files
|
||||
Parents []string // Parent directories to handle inherited attributes without CoW
|
||||
NoSameOwner bool // NoSameOwner will not attempt to preserve the owner specified in the tar archive.
|
||||
|
||||
applyFunc func(context.Context, string, io.Reader, ApplyOptions) (int64, error)
|
||||
}
|
||||
@@ -61,6 +62,15 @@ func WithConvertWhiteout(c ConvertWhiteout) ApplyOpt {
|
||||
}
|
||||
}
|
||||
|
||||
// WithNoSameOwner is same as '--no-same-owner` in 'tar' command.
|
||||
// It'll skip attempt to preserve the owner specified in the tar archive.
|
||||
func WithNoSameOwner() ApplyOpt {
|
||||
return func(options *ApplyOptions) error {
|
||||
options.NoSameOwner = true
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithParents provides parent directories for resolving inherited attributes
|
||||
// directory from the filesystem.
|
||||
// Inherited attributes are searched from first to last, making the first
|
||||
|
||||
Reference in New Issue
Block a user