소스 검색

Deletes generated profile.proto file after use in perf_to_profile conversion

Wade Simba Khadder 8 년 전
부모
커밋
02a0f96266
2개의 변경된 파일48개의 추가작업 그리고 18개의 파일을 삭제
  1. 14
    14
      internal/driver/fetch.go
  2. 34
    4
      internal/driver/tempfile.go

+ 14
- 14
internal/driver/fetch.go 파일 보기

@@ -16,10 +16,8 @@ package driver
16 16
 
17 17
 import (
18 18
 	"bytes"
19
-	"encoding/base64"
20 19
 	"fmt"
21 20
 	"io"
22
-	"math/rand"
23 21
 	"net/http"
24 22
 	"net/url"
25 23
 	"os"
@@ -407,11 +405,16 @@ func profileProtoReader(path string, ui plugin.UI) (io.ReadCloser, error) {
407 405
 	}
408 406
 	if bytes.Equal(actualHeader, perfHeader) {
409 407
 		sourceFile.Close()
410
-		profileFile, convertErr := convertPerfData(path, ui)
408
+		profileFilePath, convertErr := convertPerfData(path, ui)
411 409
 		if convertErr != nil {
412 410
 			return nil, convertErr
413 411
 		}
414
-		return os.Open(profileFile)
412
+		profileFile, openErr := os.Open(profileFilePath)
413
+		if openErr != nil {
414
+			os.Remove(profileFilePath)
415
+			return nil, openErr
416
+		}
417
+		return DeleteOnClose(profileFile), nil
415 418
 	}
416 419
 	return sourceFile, nil
417 420
 }
@@ -423,19 +426,16 @@ func convertPerfData(perfPath string, ui plugin.UI) (string, error) {
423 426
 	ui.Print(fmt.Sprintf(
424 427
 		"Converting %s to a profile.proto... (May take a few minutes)",
425 428
 		perfPath))
426
-	randomBytes := make([]byte, 32)
427
-	_, randErr := rand.Read(randomBytes)
428
-	if randErr != nil {
429
-		return "", randErr
430
-	}
431
-	randomFileName := "/tmp/pprof_" +
432
-		base64.StdEncoding.EncodeToString(randomBytes)
433
-	cmd := exec.Command("perf_to_profile", perfPath, randomFileName)
429
+	profilePath, err := newTempFilePath("/tmp", "pprof_", ".pb.gz")
430
+	if err != nil {
431
+		return "", err
432
+	}
433
+	cmd := exec.Command("perf_to_profile", perfPath, profilePath)
434 434
 	if err := cmd.Run(); err != nil {
435
+		os.Remove(profilePath)
435 436
 		return "", err
436 437
 	}
437
-
438
-	return randomFileName, nil
438
+	return profilePath, nil
439 439
 }
440 440
 
441 441
 // adjustURL validates if a profile source is a URL and returns an

+ 34
- 4
internal/driver/tempfile.go 파일 보기

@@ -21,16 +21,46 @@ import (
21 21
 	"sync"
22 22
 )
23 23
 
24
-// newTempFile returns an unused filename for output files.
25
-func newTempFile(dir, prefix, suffix string) (*os.File, error) {
24
+// newTempFilePath returns an unused path for output files.
25
+func newTempFilePath(dir, prefix, suffix string) (string, 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 os.Create(path)
29
+			return path, nil
30 30
 		}
31 31
 	}
32 32
 	// Give up
33
-	return nil, fmt.Errorf("could not create file of the form %s%03d%s", prefix, 1, suffix)
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)
43
+}
44
+
45
+type VolatileFile struct {
46
+	*os.File
47
+}
48
+
49
+func (file VolatileFile) Close() error {
50
+	if err := file.File.Close(); err != nil {
51
+		return err
52
+	}
53
+	if err := os.Remove(file.Name()); err != nil {
54
+		return err
55
+	}
56
+	return nil
57
+}
58
+
59
+// DeleteOnClose deletes file on Close().
60
+func DeleteOnClose(file *os.File) VolatileFile {
61
+	return VolatileFile {
62
+		File: file,
63
+	}
34 64
 }
35 65
 
36 66
 var tempFiles []string