Sfoglia il codice sorgente

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
Evan Jones 4 anni fa
parent
commit
163a225fb6
No account linked to committer's email address
1 ha cambiato i file con 9 aggiunte e 5 eliminazioni
  1. 9
    5
      internal/driver/settings.go

+ 9
- 5
internal/driver/settings.go Vedi File

@@ -28,11 +28,7 @@ func settingsFileName() (string, error) {
28 28
 	if err != nil {
29 29
 		return "", err
30 30
 	}
31
-	dir = filepath.Join(dir, "pprof")
32
-	if err := os.MkdirAll(dir, 0755); err != nil {
33
-		return "", err
34
-	}
35
-	return filepath.Join(dir, "settings.json"), nil
31
+	return filepath.Join(dir, "pprof", "settings.json"), nil
36 32
 }
37 33
 
38 34
 // readSettings reads settings from fname.
@@ -60,6 +56,14 @@ func writeSettings(fname string, settings *settings) error {
60 56
 	if err != nil {
61 57
 		return fmt.Errorf("could not encode settings: %w", err)
62 58
 	}
59
+
60
+	// create the settings directory if it does not exist
61
+	// XDG specifies permissions 0700 when creating settings dirs:
62
+	// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
63
+	if err := os.MkdirAll(filepath.Dir(fname), 0700); err != nil {
64
+		return fmt.Errorf("failed to create settings directory: %w", err)
65
+	}
66
+
63 67
 	if err := ioutil.WriteFile(fname, data, 0644); err != nil {
64 68
 		return fmt.Errorf("failed to write settings: %w", err)
65 69
 	}