Visitar URL original
Merge pull request #244 from thaJeztah/user_DRY · moby/sys@d6b812d · GitHub
Skip to content

Commit d6b812d

Browse files
authored
Merge pull request #244 from thaJeztah/user_DRY
user: MkdirAllAndChown, MkdirAndChown: DRY
2 parents faeea0e + d259da7 commit d6b812d

4 files changed

Lines changed: 16 additions & 19 deletions

File tree

‎user/idtools.go‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,7 @@ func WithOnlyNew(o *mkdirOptions) {
3131
// reapplies the requested permission bits after creation, so the resulting
3232
// permissions are not affected by the process umask.
3333
func MkdirAllAndChown(path string, mode os.FileMode, uid, gid int, opts ...MkdirOpt) error {
34-
var options mkdirOptions
35-
for _, opt := range opts {
36-
opt(&options)
37-
}
38-
39-
return mkdirAs(path, mode, uid, gid, true, options.onlyNew)
34+
return mkdirAs(path, mode, uid, gid, true, opts...)
4035
}
4136

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

5950
// getRootUIDGID retrieves the remapped root uid/gid pair from the set of maps.

‎user/idtools_unix.go‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ import (
1010
"syscall"
1111
)
1212

13-
func mkdirAs(path string, mode os.FileMode, uid, gid int, mkAll, onlyNew bool) error {
13+
func mkdirAs(path string, mode os.FileMode, uid, gid int, mkAll bool, opts ...MkdirOpt) error {
14+
var options mkdirOptions
15+
for _, opt := range opts {
16+
opt(&options)
17+
}
18+
1419
path, err := filepath.Abs(path)
1520
if err != nil {
1621
return err
@@ -21,7 +26,7 @@ func mkdirAs(path string, mode os.FileMode, uid, gid int, mkAll, onlyNew bool) e
2126
if !stat.IsDir() {
2227
return &os.PathError{Op: "mkdir", Path: path, Err: syscall.ENOTDIR}
2328
}
24-
if onlyNew {
29+
if options.onlyNew {
2530
return nil
2631
}
2732

‎user/idtools_unix_test.go‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -376,17 +376,18 @@ func TestToContainer(t *testing.T) {
376376
}
377377
}
378378

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

388-
err = mkdirAs(file.Name(), 0o755, 0, 0, false, false)
389-
if expected := "mkdir " + file.Name() + ": not a directory"; err.Error() != expected {
388+
expected := "mkdir " + file.Name() + ": not a directory"
389+
err = MkdirAndChown(file.Name(), 0o755, 0, 0)
390+
if err == nil || err.Error() != expected {
390391
t.Fatalf("expected error: %v, got: %v", expected, err)
391392
}
392393
}

‎user/idtools_windows.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ import (
88
// permissions aren't set through this path, the identity isn't utilized.
99
// Ownership is handled elsewhere, but in the future could be support here
1010
// too.
11-
func mkdirAs(path string, _ os.FileMode, _, _ int, _, _ bool) error {
11+
func mkdirAs(path string, _ os.FileMode, _, _ int, _ bool, _ ...MkdirOpt) error {
1212
return os.MkdirAll(path, 0)
1313
}

0 commit comments

Comments
 (0)