Skip to content

Commit

Permalink
Fix exception when file type contains colon
Browse files Browse the repository at this point in the history
Signed-off-by: cui fliter <[email protected]>
  • Loading branch information
cuishuang committed Dec 4, 2023
1 parent 4ca4178 commit 0da03b3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
24 changes: 15 additions & 9 deletions internal/driver/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,17 +492,23 @@ mapping:
func fetch(source string, duration, timeout time.Duration, ui plugin.UI, tr http.RoundTripper) (p *profile.Profile, src string, err error) {
var f io.ReadCloser

if sourceURL, timeout := adjustURL(source, duration, timeout); sourceURL != "" {
ui.Print("Fetching profile over HTTP from " + sourceURL)
if duration > 0 {
ui.Print(fmt.Sprintf("Please wait... (%v)", duration))
// first determine whether the source is a file, if not, it will be treated as a URL.
if _, openErr := os.Stat(source); openErr == nil {
if isPerfFile(source) {
f, err = convertPerfData(source, ui)
} else {
f, err = os.Open(source)
}
f, err = fetchURL(sourceURL, timeout, tr)
src = sourceURL
} else if isPerfFile(source) {
f, err = convertPerfData(source, ui)
} else {
f, err = os.Open(source)
sourceURL, timeout := adjustURL(source, duration, timeout)
if sourceURL != "" {
ui.Print("Fetching profile over HTTP from " + sourceURL)
if duration > 0 {
ui.Print(fmt.Sprintf("Please wait... (%v)", duration))
}
f, err = fetchURL(sourceURL, timeout, tr)
src = sourceURL
}
}
if err == nil {
defer f.Close()
Expand Down
22 changes: 19 additions & 3 deletions internal/driver/fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,28 @@ func TestFetch(t *testing.T) {
type testcase struct {
source, execName string
}

for _, tc := range []testcase{
ts := []testcase{
{path + "go.crc32.cpu", ""},
{path + "go.nomappings.crash", "/bin/gotest.exe"},
{"http://localhost/profile?file=cppbench.cpu", ""},
} {
}
// Test that paths with a colon character are recognized as file paths
// if the file exists, rather than as a URL. We have to skip this test
// on Windows since the colon char is not allowed in Windows paths.
if runtime.GOOS != "windows" {
src := filepath.Join(path, "go.crc32.cpu")
dst := filepath.Join(t.TempDir(), "go.crc32.cpu_2023-11-11_01:02:03")
data, err := os.ReadFile(src)
if err != nil {
t.Fatalf("read src file %s failed: %#v", src, err)
}
err = os.WriteFile(dst, data, 0644)
if err != nil {
t.Fatalf("create dst file %s failed: %#v", dst, err)
}
ts = append(ts, testcase{dst, ""})
}
for _, tc := range ts {
p, _, _, err := grabProfile(&source{ExecName: tc.execName}, tc.source, nil, testObj{}, &proftest.TestUI{T: t}, &httpTransport{})
if err != nil {
t.Fatalf("%s: %s", tc.source, err)
Expand Down

0 comments on commit 0da03b3

Please sign in to comment.