Skip to content

Commit

Permalink
bugfix: should create directory when dump path does not exist. fix: #89
Browse files Browse the repository at this point in the history
… (#92)
  • Loading branch information
songzhibin97 authored Mar 31, 2022
1 parent d10d32f commit 5ef1645
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
5 changes: 1 addition & 4 deletions holmes.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"bytes"
"fmt"
"io/ioutil"
"os"
"runtime"
"runtime/pprof"
"sync"
Expand Down Expand Up @@ -571,9 +570,7 @@ func (h *Holmes) cpuProfile(curCPUUsage int, c typeOption) bool {
c.TriggerMin, c.TriggerDiff, c.TriggerAbs, NotSupportTypeMaxConfig,
h.cpuStats.data, curCPUUsage)

binFileName := getBinaryFileName(h.opts.DumpPath, cpu, "")

bf, err := os.OpenFile(binFileName, defaultLoggerFlags, defaultLoggerPerm)
bf, binFileName, err := getBinaryFileNameAndCreate(h.opts.DumpPath, cpu, "")
if err != nil {
h.Errorf("[Holmes] failed to create cpu profile file: %v", err.Error())
return false
Expand Down
21 changes: 18 additions & 3 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,23 @@ func getBinaryFileName(filePath string, dumpType configureType, eventID string)
return path.Join(filePath, check2name[dumpType]+"."+eventID+"."+suffix)
}

// getBinaryFileNameAndCreate 获取文件路径并创建
// fix #89
func getBinaryFileNameAndCreate(dump string, dumpType configureType, eventID string) (*os.File, string, error) {
filepath := getBinaryFileName(dump, dumpType, eventID)
f, err := os.OpenFile(filepath, defaultLoggerFlags, defaultLoggerPerm)
if err != nil && os.IsNotExist(err) {
if err = os.MkdirAll(dump, 0o755); err != nil {
return nil, filepath, err
}
f, err = os.OpenFile(filepath, defaultLoggerFlags, defaultLoggerPerm)
if err != nil {
return nil, filepath, err
}
}
return f, filepath, err
}

func writeFile(data bytes.Buffer, dumpType configureType, dumpOpts *DumpOptions, eventID string) (string, error) {
var buf []byte
if dumpOpts.DumpProfileType == textDump && !dumpOpts.DumpFullStack {
Expand All @@ -212,9 +229,7 @@ func writeFile(data bytes.Buffer, dumpType configureType, dumpOpts *DumpOptions,
buf = data.Bytes()
}

fileName := getBinaryFileName(dumpOpts.DumpPath, dumpType, eventID)

file, err := os.OpenFile(fileName, defaultLoggerFlags, defaultLoggerPerm) // nolint:gosec
file, fileName, err := getBinaryFileNameAndCreate(dumpOpts.DumpPath, dumpType, eventID)
if err != nil {
return fileName, fmt.Errorf("pprof %v open file failed : %w", type2name[dumpType], err)
}
Expand Down

0 comments on commit 5ef1645

Please sign in to comment.