From 5263c8d0b5c76611b9b023297228d663fe362816 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Sun, 26 Sep 2021 07:54:53 -0700 Subject: [PATCH] paths: skip unix chmod if state directory is already 0700 Updates #2934 Signed-off-by: Brad Fitzpatrick --- paths/paths_unix.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/paths/paths_unix.go b/paths/paths_unix.go index 17662b0f8..9c0116bc3 100644 --- a/paths/paths_unix.go +++ b/paths/paths_unix.go @@ -8,6 +8,7 @@ package paths import ( + "fmt" "os" "path/filepath" "runtime" @@ -62,10 +63,21 @@ func xdgDataHome() string { return filepath.Join(os.Getenv("HOME"), ".local/share") } -func ensureStateDirPerms(dirPath string) error { - if filepath.Base(dirPath) != "tailscale" { +func ensureStateDirPerms(dir string) error { + if filepath.Base(dir) != "tailscale" { return nil } - - return os.Chmod(dirPath, 0700) + fi, err := os.Stat(dir) + if err != nil { + return err + } + if !fi.IsDir() { + return fmt.Errorf("expected %q to be a directory; is %v", dir, fi.Mode()) + } + const perm = 0700 + if fi.Mode().Perm() == perm { + // Already correct. + return nil + } + return os.Chmod(dir, perm) }