Skip to content

Commit

Permalink
Merge pull request #188 from luotianqi777/fix_mult
Browse files Browse the repository at this point in the history
fix: not remove temp dir
  • Loading branch information
luotianqi777 authored Dec 15, 2023
2 parents d145736 + ce65441 commit 56828d9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion opensca/common/temp.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var tempdir = ".temp"
func init() {
excpath, _ := os.Executable()
tempdir = filepath.Join(filepath.Dir(excpath), tempdir)
os.RemoveAll(tempdir)
// os.RemoveAll(tempdir)
os.MkdirAll(tempdir, 0755)
}

Expand Down
23 changes: 12 additions & 11 deletions opensca/walk/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ import (
"github.com/xmirrorsecurity/opensca-cli/v3/opensca/sca/filter"
)

var (
wg = sync.WaitGroup{}
)

type ExtractFileFilter func(relpath string) bool
type WalkFileFunc func(parent *model.File, files []*model.File)

Expand All @@ -29,15 +25,18 @@ type WalkFileFunc func(parent *model.File, files []*model.File)
// size: 检测文件大小
func Walk(ctx context.Context, name, origin string, filter ExtractFileFilter, do WalkFileFunc) (size int64, err error) {

defer wg.Wait()

delete, filepath, err := download(origin)
if err != nil {
return
}

logs.Debugf("walk %s", filepath)

if delete {
defer os.RemoveAll(filepath)
defer func() {
logs.Debugf("remove %s", filepath)
os.RemoveAll(filepath)
}()
}

if f, xerr := os.Stat(filepath); xerr == nil {
Expand All @@ -51,11 +50,13 @@ func Walk(ctx context.Context, name, origin string, filter ExtractFileFilter, do
}

parent := model.NewFile(filepath, name)
err = walk(ctx, parent, filter, do)
wg := &sync.WaitGroup{}
err = walk(ctx, wg, parent, filter, do)
wg.Wait()
return
}

func walk(ctx context.Context, parent *model.File, filterFunc ExtractFileFilter, walkFunc WalkFileFunc) error {
func walk(ctx context.Context, wg *sync.WaitGroup, parent *model.File, filterFunc ExtractFileFilter, walkFunc WalkFileFunc) error {

var files []*model.File

Expand Down Expand Up @@ -96,7 +97,7 @@ func walk(ctx context.Context, parent *model.File, filterFunc ExtractFileFilter,
defer wg.Done()
defer os.RemoveAll(dir)
parent := model.NewFile(dir, rel)
if err := walk(ctx, parent, filterFunc, walkFunc); err != nil {
if err := walk(ctx, wg, parent, filterFunc, walkFunc); err != nil {
logs.Warn(err)
}
}()
Expand All @@ -109,7 +110,7 @@ func walk(ctx context.Context, parent *model.File, filterFunc ExtractFileFilter,
return err
}

// decompress 解压到指定位置
// decompress 解压压缩包
// input: 压缩包绝对路径
// do: 对解压后目录的操作
// do.tmpdir: 临时解压目录绝对路径 需要手动删除目录
Expand Down

0 comments on commit 56828d9

Please sign in to comment.