Browse Source

Deletes generated profile.proto file after use in perf_to_profile conversion

Wade Simba Khadder 8 years ago
parent
commit
02a0f96266
2 changed files with 48 additions and 18 deletions
  1. 14
    14
      internal/driver/fetch.go
  2. 34
    4
      internal/driver/tempfile.go

+ 14
- 14
internal/driver/fetch.go View File

16
 
16
 
17
 import (
17
 import (
18
 	"bytes"
18
 	"bytes"
19
-	"encoding/base64"
20
 	"fmt"
19
 	"fmt"
21
 	"io"
20
 	"io"
22
-	"math/rand"
23
 	"net/http"
21
 	"net/http"
24
 	"net/url"
22
 	"net/url"
25
 	"os"
23
 	"os"
407
 	}
405
 	}
408
 	if bytes.Equal(actualHeader, perfHeader) {
406
 	if bytes.Equal(actualHeader, perfHeader) {
409
 		sourceFile.Close()
407
 		sourceFile.Close()
410
-		profileFile, convertErr := convertPerfData(path, ui)
408
+		profileFilePath, convertErr := convertPerfData(path, ui)
411
 		if convertErr != nil {
409
 		if convertErr != nil {
412
 			return nil, convertErr
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
 	return sourceFile, nil
419
 	return sourceFile, nil
417
 }
420
 }
423
 	ui.Print(fmt.Sprintf(
426
 	ui.Print(fmt.Sprintf(
424
 		"Converting %s to a profile.proto... (May take a few minutes)",
427
 		"Converting %s to a profile.proto... (May take a few minutes)",
425
 		perfPath))
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
 	if err := cmd.Run(); err != nil {
434
 	if err := cmd.Run(); err != nil {
435
+		os.Remove(profilePath)
435
 		return "", err
436
 		return "", err
436
 	}
437
 	}
437
-
438
-	return randomFileName, nil
438
+	return profilePath, nil
439
 }
439
 }
440
 
440
 
441
 // adjustURL validates if a profile source is a URL and returns an
441
 // adjustURL validates if a profile source is a URL and returns an

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

21
 	"sync"
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
 	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 os.Create(path)
29
+			return path, nil
30
 		}
30
 		}
31
 	}
31
 	}
32
 	// Give up
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
 var tempFiles []string
66
 var tempFiles []string