Bladeren bron

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

Wade Simba Khadder 8 jaren geleden
bovenliggende
commit
269e7ed898
2 gewijzigde bestanden met toevoegingen van 9 en 19 verwijderingen
  1. 5
    6
      internal/driver/fetch.go
  2. 4
    13
      internal/driver/tempfile.go

+ 5
- 6
internal/driver/fetch.go Bestand weergeven

@@ -409,18 +409,17 @@ func convertPerfData(perfPath string, ui plugin.UI) (*os.File, error) {
409 409
 	ui.Print(fmt.Sprintf(
410 410
 		"Converting %s to a profile.proto... (May take a few minutes)",
411 411
 		perfPath))
412
-	profilePath, err := newTempFilePath("/tmp", "pprof_", ".pb.gz")
412
+	profile, err := newTempFile("/tmp", "pprof_", ".pb.gz")
413 413
 	if err != nil {
414 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 418
 	if err := cmd.Run(); err != nil {
419
+		profile.Close()
421 420
 		return nil, err
422 421
 	}
423
-	return os.Open(profilePath)
422
+	return profile, nil
424 423
 }
425 424
 
426 425
 // adjustURL validates if a profile source is a URL and returns an

+ 4
- 13
internal/driver/tempfile.go Bestand weergeven

@@ -21,25 +21,16 @@ import (
21 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 26
 	for index := 1; index < 10000; index++ {
27 27
 		path := filepath.Join(dir, fmt.Sprintf("%s%03d%s", prefix, index, suffix))
28 28
 		if _, err := os.Stat(path); err != nil {
29
-			return path, nil
29
+			return os.Create(path)
30 30
 		}
31 31
 	}
32 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 36
 var tempFiles []string