Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions user/idtools.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@ func WithOnlyNew(o *mkdirOptions) {
// reapplies the requested permission bits after creation, so the resulting
// permissions are not affected by the process umask.
func MkdirAllAndChown(path string, mode os.FileMode, uid, gid int, opts ...MkdirOpt) error {
var options mkdirOptions
for _, opt := range opts {
opt(&options)
}

return mkdirAs(path, mode, uid, gid, true, options.onlyNew)
return mkdirAs(path, mode, uid, gid, true, opts...)
}

// MkdirAndChown creates a directory named path and applies the requested
Expand All @@ -49,11 +44,7 @@ func MkdirAllAndChown(path string, mode os.FileMode, uid, gid int, opts ...Mkdir
// exists as a directory. The resulting permission bits are not affected by
// the process umask.
func MkdirAndChown(path string, mode os.FileMode, uid, gid int, opts ...MkdirOpt) error {
var options mkdirOptions
for _, opt := range opts {
opt(&options)
}
return mkdirAs(path, mode, uid, gid, false, options.onlyNew)
return mkdirAs(path, mode, uid, gid, false, opts...)
}

// getRootUIDGID retrieves the remapped root uid/gid pair from the set of maps.
Expand Down
9 changes: 7 additions & 2 deletions user/idtools_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import (
"syscall"
)

func mkdirAs(path string, mode os.FileMode, uid, gid int, mkAll, onlyNew bool) error {
func mkdirAs(path string, mode os.FileMode, uid, gid int, mkAll bool, opts ...MkdirOpt) error {
var options mkdirOptions
for _, opt := range opts {
opt(&options)
}
Comment thread
thaJeztah marked this conversation as resolved.

path, err := filepath.Abs(path)
if err != nil {
return err
Expand All @@ -21,7 +26,7 @@ func mkdirAs(path string, mode os.FileMode, uid, gid int, mkAll, onlyNew bool) e
if !stat.IsDir() {
return &os.PathError{Op: "mkdir", Path: path, Err: syscall.ENOTDIR}
}
if onlyNew {
if options.onlyNew {
return nil
}

Expand Down
11 changes: 6 additions & 5 deletions user/idtools_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,17 +376,18 @@ func TestToContainer(t *testing.T) {
}
}

// TestMkdirIsNotDir checks that mkdirAs() function (used by MkdirAll...)
// returns a correct error in case a directory which it is about to create
// already exists but is a file (rather than a directory).
// TestMkdirIsNotDir checks that MkdirAndChown returns a correct error in case
// a directory which it is about to create already exists but is a file (rather
// than a directory).
func TestMkdirIsNotDir(t *testing.T) {
file, err := os.CreateTemp(t.TempDir(), t.Name())
if err != nil {
t.Fatalf("Couldn't create temp dir: %v", err)
}

err = mkdirAs(file.Name(), 0o755, 0, 0, false, false)
if expected := "mkdir " + file.Name() + ": not a directory"; err.Error() != expected {
expected := "mkdir " + file.Name() + ": not a directory"
err = MkdirAndChown(file.Name(), 0o755, 0, 0)
if err == nil || err.Error() != expected {
t.Fatalf("expected error: %v, got: %v", expected, err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion user/idtools_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import (
// permissions aren't set through this path, the identity isn't utilized.
// Ownership is handled elsewhere, but in the future could be support here
// too.
func mkdirAs(path string, _ os.FileMode, _, _ int, _, _ bool) error {
func mkdirAs(path string, _ os.FileMode, _, _ int, _ bool, _ ...MkdirOpt) error {
return os.MkdirAll(path, 0)
}
Loading