From 02a62c112622f7b409f8a9b834a920a7ae5a0557 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 7 Oct 2021 11:53:04 +0200 Subject: [PATCH] Force permissions when creating directories --- utils/fs.go | 5 ++--- utils/structure_test.go | 14 ++++++++------ 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/utils/fs.go b/utils/fs.go index 6cc3b4a..ac72c91 100644 --- a/utils/fs.go +++ b/utils/fs.go @@ -10,7 +10,6 @@ const isWindows = runtime.GOOS == "windows" // EnsureDirectory ensures that the given directory exists and that is has the given permissions set. // If path is a file, it is deleted and a directory created. -// If a directory is created, also all missing directories up to the required one are created with the given permissions. func EnsureDirectory(path string, perm os.FileMode) error { // open path f, err := os.Stat(path) @@ -33,11 +32,11 @@ func EnsureDirectory(path string, perm os.FileMode) error { } // file does not exist (or has been deleted) if err == nil || os.IsNotExist(err) { - err = os.MkdirAll(path, perm) + err = os.Mkdir(path, perm) if err != nil { return fmt.Errorf("could not create dir %s: %s", path, err) } - return nil + return os.Chmod(path, perm) } // other error opening path return fmt.Errorf("failed to access %s: %s", path, err) diff --git a/utils/structure_test.go b/utils/structure_test.go index 77fdc36..59cc611 100644 --- a/utils/structure_test.go +++ b/utils/structure_test.go @@ -14,11 +14,13 @@ func ExampleDirStructure() { // output: // / [755] // /repo [777] - // /repo/b [755] + // /repo/b [707] // /repo/b/c [750] - // /repo/b/d [755] - // /repo/b/d/e [755] - // /repo/b/d/f [755] + // /repo/b/d [707] + // /repo/b/d/e [707] + // /repo/b/d/f [707] + // /repo/b/d/f/g [707] + // /repo/b/d/f/g/h [707] // /secret [700] basePath, err := ioutil.TempDir("", "") @@ -31,7 +33,7 @@ func ExampleDirStructure() { secret := ds.ChildDir("secret", 0700) repo := ds.ChildDir("repo", 0777) _ = repo.ChildDir("a", 0700) - b := repo.ChildDir("b", 0755) + b := repo.ChildDir("b", 0707) c := b.ChildDir("c", 0750) err = ds.Ensure() @@ -54,7 +56,7 @@ func ExampleDirStructure() { fmt.Println(err) } - err = b.EnsureRelPath("d/f") + err = b.EnsureRelPath("d/f/g/h") if err != nil { fmt.Println(err) }