-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathprofile_utils.go
More file actions
99 lines (86 loc) · 3.39 KB
/
profile_utils.go
File metadata and controls
99 lines (86 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
)
// getProfileAsFile 获取 profile 文件。
// - 如果输入不包含 "://", 则视为本地文件路径(相对或绝对)。
// - 如果是 file:// URI,直接使用其路径。
// - 如果是 http:// 或 https:// URI,下载到临时文件并返回其路径。
// 返回最终的文件路径、一个用于清理临时文件的函数(如果创建了临时文件)以及错误。
func getProfileAsFile(uriStr string) (filePath string, cleanup func(), err error) {
cleanup = func() {} // 默认清理函数为空操作
// 检查输入是否包含协议头,如果没有,则假定为本地文件路径
if !strings.Contains(uriStr, "://") {
log.Printf("Input '%s' does not contain '://', treating as local file path.", uriStr)
absPath, err := filepath.Abs(uriStr)
if err != nil {
return "", nil, fmt.Errorf("failed to get absolute path for '%s': %w", uriStr, err)
}
log.Printf("Using absolute local path: %s", absPath)
// 可以在这里添加 os.Stat 检查文件是否存在且可读
// _, statErr := os.Stat(absPath)
// if statErr != nil {
// return "", nil, fmt.Errorf("local file '%s' (resolved to '%s') error: %w", uriStr, absPath, statErr)
// }
return absPath, cleanup, nil
}
// 如果包含 "://", 则按 URI 处理
parsedURI, err := url.Parse(uriStr)
if err != nil {
return "", nil, fmt.Errorf("invalid profile URI '%s': %w", uriStr, err)
}
switch parsedURI.Scheme {
case "file":
filePath = parsedURI.Path
if filePath == "" {
return "", nil, fmt.Errorf("invalid file path derived from URI '%s'", uriStr)
}
log.Printf("Using local profile file: %s", filePath)
return filePath, cleanup, nil
case "http", "https":
log.Printf("Attempting to download profile from URL: %s", uriStr)
resp, err := http.Get(uriStr)
if err != nil {
return "", nil, fmt.Errorf("failed to download profile from '%s': %w", uriStr, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", nil, fmt.Errorf("failed to download profile from '%s': received status code %d", uriStr, resp.StatusCode)
}
// 创建临时文件来存储下载的内容
tempFile, err := os.CreateTemp("", "pprof-*") // 使用通用模式
if err != nil {
return "", nil, fmt.Errorf("failed to create temporary file for download: %w", err)
}
filePath = tempFile.Name()
log.Printf("Downloading profile to temporary file: %s", filePath)
// 定义清理函数,用于删除临时文件
cleanup = func() {
log.Printf("Cleaning up temporary file: %s", filePath)
err := os.Remove(filePath)
if err != nil && !os.IsNotExist(err) {
log.Printf("Warning: failed to remove temporary file '%s': %v", filePath, err)
}
}
_, err = io.Copy(tempFile, resp.Body)
closeErr := tempFile.Close()
if err != nil {
cleanup() // 如果复制失败,尝试清理临时文件
return "", nil, fmt.Errorf("failed to write downloaded content to temporary file '%s': %w", filePath, err)
}
if closeErr != nil {
log.Printf("Warning: failed to close temporary file handle for '%s': %v", filePath, closeErr)
}
log.Printf("Successfully downloaded profile to %s", filePath)
return filePath, cleanup, nil
default:
return "", nil, fmt.Errorf("unsupported URI scheme '%s', only 'file://', 'http://', 'https://', or a plain local path are supported", parsedURI.Scheme)
}
}