Browse Source

Creates a temp file directly and uses that rather than just a path

Wade Simba Khadder 8 years ago
parent
commit
269e7ed898
2 changed files with 9 additions and 19 deletions
  1. 5
    6
      internal/driver/fetch.go
  2. 4
    13
      internal/driver/tempfile.go

+ 5
- 6
internal/driver/fetch.go View File

409
 	ui.Print(fmt.Sprintf(
409
 	ui.Print(fmt.Sprintf(
410
 		"Converting %s to a profile.proto... (May take a few minutes)",
410
 		"Converting %s to a profile.proto... (May take a few minutes)",
411
 		perfPath))
411
 		perfPath))
412
-	profilePath, err := newTempFilePath("/tmp", "pprof_", ".pb.gz")
412
+	profile, err := newTempFile("/tmp", "pprof_", ".pb.gz")
413
 	if err != nil {
413
 	if err != nil {
414
 		return nil, err
414
 		return nil, err
415
 	}
415
 	}
416
-	cmd := exec.Command("perf_to_profile", perfPath, profilePath)
417
-	// If perf_to_profile failed before generating the file, this defer
418
-	// is just a no-op.
419
-	deferDeleteTempFile(profilePath)
416
+	deferDeleteTempFile(profile.Name())
417
+	cmd := exec.Command("perf_to_profile", perfPath, profile.Name())
420
 	if err := cmd.Run(); err != nil {
418
 	if err := cmd.Run(); err != nil {
419
+		profile.Close()
421
 		return nil, err
420
 		return nil, err
422
 	}
421
 	}
423
-	return os.Open(profilePath)
422
+	return profile, nil
424
 }
423
 }
425
 
424
 
426
 // adjustURL validates if a profile source is a URL and returns an
425
 // adjustURL validates if a profile source is a URL and returns an

+ 4
- 13
internal/driver/tempfile.go View File

21
 	"sync"
21
 	"sync"
22
 )
22
 )
23
 
23
 
24
-// newTempFilePath returns an unused path for output files.
25
-func newTempFilePath(dir, prefix, suffix string) (string, error) {
24
+// newTempFile returns a new output file in dir with the provided prefix and suffix.
25
+func newTempFile(dir, prefix, suffix string) (*os.File, error) {
26
 	for index := 1; index < 10000; index++ {
26
 	for index := 1; index < 10000; index++ {
27
 		path := filepath.Join(dir, fmt.Sprintf("%s%03d%s", prefix, index, suffix))
27
 		path := filepath.Join(dir, fmt.Sprintf("%s%03d%s", prefix, index, suffix))
28
 		if _, err := os.Stat(path); err != nil {
28
 		if _, err := os.Stat(path); err != nil {
29
-			return path, nil
29
+			return os.Create(path)
30
 		}
30
 		}
31
 	}
31
 	}
32
 	// Give up
32
 	// Give up
33
-	return "", fmt.Errorf("could not create file of the form %s%03d%s", prefix, 1, suffix)
34
-}
35
-
36
-// newTempFile returns a new file with a random name for output files.
37
-func newTempFile(dir, prefix, suffix string) (*os.File, error) {
38
-	path, err := newTempFilePath(dir, prefix, suffix)
39
-	if err != nil {
40
-		return nil, err
41
-	}
42
-	return os.Create(path)
33
+	return nil, fmt.Errorf("could not create file of the form %s%03d%s", prefix, 1, suffix)
43
 }
34
 }
44
 
35
 
45
 var tempFiles []string
36
 var tempFiles []string