From 4a97aa12d6586e836812b42e2def6843037d3224 Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Mon, 21 Dec 2015 08:35:24 +0100 Subject: [PATCH] Don't crash on stat error in ensureDir (fixes #2608) I'm not really sure under what circumstances MkdirAll returns a nil error but a subsequent stat fails, but apparently it can happen and we need to handle it. The "mode >= 0" was a no-op, and we never call ensureDir anyway without the intention of ensuring the mode, so removed that. --- cmd/syncthing/main.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/cmd/syncthing/main.go b/cmd/syncthing/main.go index 29adaf0ef..c12ca302c 100644 --- a/cmd/syncthing/main.go +++ b/cmd/syncthing/main.go @@ -1036,13 +1036,17 @@ func ensureDir(dir string, mode os.FileMode) { 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) + if fi, err := os.Stat(dir); err == nil { + // Apprently the stat may fail even though the mkdirall passed. If it + // does, we'll just assume things are in order and let other things + // fail (like loading or creating the config...). + currentMode := fi.Mode() & 0777 + if 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) + } } } }