mirror of
https://github.com/syncthing/syncthing.git
synced 2024-11-16 10:28:49 -07:00
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:
parent
0ca4482977
commit
a0b7ac402d
@ -289,10 +289,6 @@ func main() {
|
||||
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.
|
||||
ensureDir(baseDirs["config"], 0700)
|
||||
|
||||
@ -1007,15 +1003,16 @@ func shutdown() {
|
||||
stop <- exitSuccess
|
||||
}
|
||||
|
||||
func ensureDir(dir string, mode int) {
|
||||
fi, err := os.Stat(dir)
|
||||
if os.IsNotExist(err) {
|
||||
err := osutil.MkdirAll(dir, 0700)
|
||||
if err != nil {
|
||||
l.Fatalln(err)
|
||||
}
|
||||
} else if mode >= 0 && err == nil && int(fi.Mode()&0777) != mode {
|
||||
err := os.Chmod(dir, os.FileMode(mode))
|
||||
func ensureDir(dir string, mode os.FileMode) {
|
||||
err := osutil.MkdirAll(dir, mode)
|
||||
if err != nil {
|
||||
l.Fatalln(err)
|
||||
}
|
||||
|
||||
fi, _ := os.Stat(dir)
|
||||
currentMode := fi.Mode() & 0777
|
||||
if mode >= 0 && currentMode != mode {
|
||||
err := os.Chmod(dir, mode)
|
||||
// This can fail on crappy filesystems, nothing we can do about it.
|
||||
if err != nil {
|
||||
l.Warnln(err)
|
||||
|
Loading…
Reference in New Issue
Block a user