Skip to content

Commit

Permalink
settings: Only call MkdirAll on save (#542)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
evanj authored Jun 4, 2020
1 parent 427632f commit 163a225
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions internal/driver/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 163a225

Please sign in to comment.