Skip to content

Commit

Permalink
fix: try to create data directory if it doesn't exist. (#646)
Browse files Browse the repository at this point in the history
* fix: try to create data directory if it doesn't exist.

* Replace DataDirectory by EnsureDataDirectory.

* don't pass errors twice 🤦
  • Loading branch information
abeaumont committed Dec 23, 2021
1 parent 6537dfe commit eac8c4e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
16 changes: 14 additions & 2 deletions pkg/adhoc/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,14 @@ func (s *server) AddRoutes(r *mux.Router) http.HandlerFunc {
// The profiles are retrieved every time the endpoint is requested,
// which should be good enough as massive access to this auth endpoint is not expected.
func (s *server) Profiles(w http.ResponseWriter, _ *http.Request) {
dataDir, err := util.EnsureDataDirectory()
if err != nil {
s.log.WithError(err).Errorf("Unable to create data directory")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
profiles := make(map[string]profile, 0)
err := filepath.Walk(util.DataDirectory(), func(path string, info fs.FileInfo, err error) error {
err = filepath.Walk(dataDir, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
Expand Down Expand Up @@ -101,7 +107,13 @@ func (s *server) Profile(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Not Found", http.StatusNotFound)
return
}
f, err := os.Open(filepath.Join(util.DataDirectory(), p.Name))
dataDir, err := util.EnsureDataDirectory()
if err != nil {
s.log.WithError(err).Errorf("Unable to create data directory")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
f, err := os.Open(filepath.Join(dataDir, p.Name))
if err != nil {
s.log.WithError(err).Error("Unable to open profile")
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down
11 changes: 8 additions & 3 deletions pkg/adhoc/util/util.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package util

import "path/filepath"
import (
"os"
"path/filepath"
)

func DataDirectory() string {
return filepath.Join(dataBaseDirectory(), "pyroscope")
// Retrieve pyroscope data directory, creating it if needed.
func EnsureDataDirectory() (string, error) {
dir := filepath.Join(dataBaseDirectory(), "pyroscope")
return dir, os.MkdirAll(dir, os.ModeDir|os.ModePerm)
}
4 changes: 2 additions & 2 deletions pkg/adhoc/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func newWriter(cfg *config.Adhoc, st *storage.Storage, logger *logrus.Logger) wr
}

func (w writer) write(t0, t1 time.Time) error {
dataDir := util.DataDirectory()
if err := os.MkdirAll(dataDir, os.ModeDir|os.ModePerm); err != nil {
dataDir, err := util.EnsureDataDirectory()
if err != nil {
return fmt.Errorf("could not create data directory: %w", err)
}

Expand Down

0 comments on commit eac8c4e

Please sign in to comment.