Merge pull request #138 from safing/fix/dir-structure-permissions

Force permissions when creating directories
This commit is contained in:
Daniel 2021-10-11 13:47:36 +02:00 committed by GitHub
commit f7eafb9674
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 9 deletions

View file

@ -10,7 +10,6 @@ const isWindows = runtime.GOOS == "windows"
// EnsureDirectory ensures that the given directory exists and that is has the given permissions set. // 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 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 { func EnsureDirectory(path string, perm os.FileMode) error {
// open path // open path
f, err := os.Stat(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) // file does not exist (or has been deleted)
if err == nil || os.IsNotExist(err) { if err == nil || os.IsNotExist(err) {
err = os.MkdirAll(path, perm) err = os.Mkdir(path, perm)
if err != nil { if err != nil {
return fmt.Errorf("could not create dir %s: %s", path, err) return fmt.Errorf("could not create dir %s: %s", path, err)
} }
return nil return os.Chmod(path, perm)
} }
// other error opening path // other error opening path
return fmt.Errorf("failed to access %s: %s", path, err) return fmt.Errorf("failed to access %s: %s", path, err)

View file

@ -14,11 +14,13 @@ func ExampleDirStructure() {
// output: // output:
// / [755] // / [755]
// /repo [777] // /repo [777]
// /repo/b [755] // /repo/b [707]
// /repo/b/c [750] // /repo/b/c [750]
// /repo/b/d [755] // /repo/b/d [707]
// /repo/b/d/e [755] // /repo/b/d/e [707]
// /repo/b/d/f [755] // /repo/b/d/f [707]
// /repo/b/d/f/g [707]
// /repo/b/d/f/g/h [707]
// /secret [700] // /secret [700]
basePath, err := ioutil.TempDir("", "") basePath, err := ioutil.TempDir("", "")
@ -31,7 +33,7 @@ func ExampleDirStructure() {
secret := ds.ChildDir("secret", 0700) secret := ds.ChildDir("secret", 0700)
repo := ds.ChildDir("repo", 0777) repo := ds.ChildDir("repo", 0777)
_ = repo.ChildDir("a", 0700) _ = repo.ChildDir("a", 0700)
b := repo.ChildDir("b", 0755) b := repo.ChildDir("b", 0707)
c := b.ChildDir("c", 0750) c := b.ChildDir("c", 0750)
err = ds.Ensure() err = ds.Ensure()
@ -54,7 +56,7 @@ func ExampleDirStructure() {
fmt.Println(err) fmt.Println(err)
} }
err = b.EnsureRelPath("d/f") err = b.EnsureRelPath("d/f/g/h")
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }