From 163a225fb653ec7d88711ec254b72ae34316969b Mon Sep 17 00:00:00 2001 From: Evan Jones Date: Wed, 3 Jun 2020 23:27:02 -0400 Subject: [PATCH] settings: Only call MkdirAll on save (#542) settings: Only call MkdirAll on save Previously settingsFileName() created the ~/.config/pprof directory if it did not exist. However, this is not possible for user nobody, since its home directory is set to "/nonexistent". Instead, only create the directory when we actually attempt to save the file, so the error will happen at the appropriate interaction. This als prevents pprof from creating empty settings directories. Fixes the following error when running pprof's web UI as user nobody: mkdir /nonexistent: permission denied --- internal/driver/settings.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/internal/driver/settings.go b/internal/driver/settings.go index 06e802f9..f72314b1 100644 --- a/internal/driver/settings.go +++ b/internal/driver/settings.go @@ -28,11 +28,7 @@ func settingsFileName() (string, error) { if err != nil { return "", err } - dir = filepath.Join(dir, "pprof") - if err := os.MkdirAll(dir, 0755); err != nil { - return "", err - } - return filepath.Join(dir, "settings.json"), nil + return filepath.Join(dir, "pprof", "settings.json"), nil } // readSettings reads settings from fname. @@ -60,6 +56,14 @@ func writeSettings(fname string, settings *settings) error { if err != nil { return fmt.Errorf("could not encode settings: %w", err) } + + // create the settings directory if it does not exist + // XDG specifies permissions 0700 when creating settings dirs: + // https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html + if err := os.MkdirAll(filepath.Dir(fname), 0700); err != nil { + return fmt.Errorf("failed to create settings directory: %w", err) + } + if err := ioutil.WriteFile(fname, data, 0644); err != nil { return fmt.Errorf("failed to write settings: %w", err) }