mirror of
https://github.com/safing/portbase
synced 2025-09-01 10:09:50 +00:00
Force permissions when creating directories
This commit is contained in:
parent
f999cb027f
commit
02a62c1126
2 changed files with 10 additions and 9 deletions
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue