Просмотр исходного кода

Merge pull request #45 from rauls5382/nodir

More careful handling of temporary directories
Josef Jelinek 8 лет назад
Родитель
Сommit
8b3c6b12a7
2 измененных файлов: 14 добавлений и 15 удалений
  1. 1
    1
      internal/driver/commands.go
  2. 13
    14
      internal/driver/fetch.go

+ 1
- 1
internal/driver/commands.go Просмотреть файл

@@ -411,7 +411,7 @@ func invokeVisualizer(format PostProcessor, suffix string, visualizers []string)
411 411
 			return err
412 412
 		}
413 413
 
414
-		tempFile, err := newTempFile(os.Getenv("PPROF_TMPDIR"), "pprof", "."+suffix)
414
+		tempFile, err := newTempFile(os.TempDir(), "pprof", "."+suffix)
415 415
 		if err != nil {
416 416
 			return err
417 417
 		}

+ 13
- 14
internal/driver/fetch.go Просмотреть файл

@@ -37,10 +37,6 @@ import (
37 37
 // there are some failures. It will return an error if it is unable to
38 38
 // fetch any profiles.
39 39
 func fetchProfiles(s *source, o *plugin.Options) (*profile.Profile, error) {
40
-	if err := setTmpDir(o.UI); err != nil {
41
-		return nil, err
42
-	}
43
-
44 40
 	sources := make([]profileSource, 0, len(s.Sources)+len(s.Base))
45 41
 	for _, src := range s.Sources {
46 42
 		sources = append(sources, profileSource{
@@ -76,6 +72,11 @@ func fetchProfiles(s *source, o *plugin.Options) (*profile.Profile, error) {
76 72
 
77 73
 	// Save a copy of the merged profile if there is at least one remote source.
78 74
 	if save {
75
+		dir, err := setTmpDir(o.UI)
76
+		if err != nil {
77
+			return nil, err
78
+		}
79
+
79 80
 		prefix := "pprof."
80 81
 		if len(p.Mapping) > 0 && p.Mapping[0].File != "" {
81 82
 			prefix += filepath.Base(p.Mapping[0].File) + "."
@@ -84,7 +85,6 @@ func fetchProfiles(s *source, o *plugin.Options) (*profile.Profile, error) {
84 85
 			prefix += s.Type + "."
85 86
 		}
86 87
 
87
-		dir := os.Getenv("PPROF_TMPDIR")
88 88
 		tempFile, err := newTempFile(dir, prefix, ".pb.gz")
89 89
 		if err == nil {
90 90
 			if err = p.Write(tempFile); err == nil {
@@ -211,21 +211,20 @@ type profileSource struct {
211 211
 	err    error
212 212
 }
213 213
 
214
-// setTmpDir sets the PPROF_TMPDIR environment variable with a new
215
-// temp directory, if not already set.
216
-func setTmpDir(ui plugin.UI) error {
214
+// setTmpDir prepares the directory to use to save profiles retrieved
215
+// remotely. It is selected from PPROF_TMPDIR, defaults to $HOME/pprof.
216
+func setTmpDir(ui plugin.UI) (string, error) {
217 217
 	if profileDir := os.Getenv("PPROF_TMPDIR"); profileDir != "" {
218
-		return nil
218
+		return profileDir, nil
219 219
 	}
220
-	for _, tmpDir := range []string{os.Getenv("HOME") + "/pprof", "/tmp"} {
220
+	for _, tmpDir := range []string{os.Getenv("HOME") + "/pprof", os.TempDir()} {
221 221
 		if err := os.MkdirAll(tmpDir, 0755); err != nil {
222 222
 			ui.PrintErr("Could not use temp dir ", tmpDir, ": ", err.Error())
223 223
 			continue
224 224
 		}
225
-		os.Setenv("PPROF_TMPDIR", tmpDir)
226
-		return nil
225
+		return tmpDir, nil
227 226
 	}
228
-	return fmt.Errorf("failed to identify temp dir")
227
+	return "", fmt.Errorf("failed to identify temp dir")
229 228
 }
230 229
 
231 230
 // grabProfile fetches a profile. Returns the profile, sources for the
@@ -424,7 +423,7 @@ func convertPerfData(perfPath string, ui plugin.UI) (*os.File, error) {
424 423
 	ui.Print(fmt.Sprintf(
425 424
 		"Converting %s to a profile.proto... (May take a few minutes)",
426 425
 		perfPath))
427
-	profile, err := newTempFile("/tmp", "pprof_", ".pb.gz")
426
+	profile, err := newTempFile(os.TempDir(), "pprof_", ".pb.gz")
428 427
 	if err != nil {
429 428
 		return nil, err
430 429
 	}