Refactor main.ensureDir()

ensureDir() did not handle one last error case and there was some logic
in the main() function that belonged to ensureDir() as well. It was also
creating a directory with a hardcoded 0700 mode, regardless of what mode
was passed to it.

This refactors it a little to fix the broken behavior, avoid redundant
checks by taking advantage of the behavior of MkdirAll, and move the
extra logic from main() into ensureDir().
This commit is contained in:
Anderson Mesquita 2015-12-16 15:03:20 -05:00
parent 0ca4482977
commit a0b7ac402d

View File

@ -289,10 +289,6 @@ func main() {
return return
} }
if info, err := os.Stat(baseDirs["config"]); err == nil && !info.IsDir() {
l.Fatalln("Config directory", baseDirs["config"], "is not a directory")
}
// Ensure that our home directory exists. // Ensure that our home directory exists.
ensureDir(baseDirs["config"], 0700) ensureDir(baseDirs["config"], 0700)
@ -1007,15 +1003,16 @@ func shutdown() {
stop <- exitSuccess stop <- exitSuccess
} }
func ensureDir(dir string, mode int) { func ensureDir(dir string, mode os.FileMode) {
fi, err := os.Stat(dir) err := osutil.MkdirAll(dir, mode)
if os.IsNotExist(err) { if err != nil {
err := osutil.MkdirAll(dir, 0700) l.Fatalln(err)
if err != nil { }
l.Fatalln(err)
} fi, _ := os.Stat(dir)
} else if mode >= 0 && err == nil && int(fi.Mode()&0777) != mode { currentMode := fi.Mode() & 0777
err := os.Chmod(dir, os.FileMode(mode)) if mode >= 0 && currentMode != mode {
err := os.Chmod(dir, mode)
// This can fail on crappy filesystems, nothing we can do about it. // This can fail on crappy filesystems, nothing we can do about it.
if err != nil { if err != nil {
l.Warnln(err) l.Warnln(err)