From ce65441431ce86ee85470d81c39053bcc7f1a8f2 Mon Sep 17 00:00:00 2001 From: luotianqi777 Date: Fri, 15 Dec 2023 10:54:40 +0800 Subject: [PATCH] fix: not remove temp dir --- opensca/common/temp.go | 2 +- opensca/walk/walk.go | 23 ++++++++++++----------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/opensca/common/temp.go b/opensca/common/temp.go index f4fa70d4..fd15e8fe 100644 --- a/opensca/common/temp.go +++ b/opensca/common/temp.go @@ -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) } diff --git a/opensca/walk/walk.go b/opensca/walk/walk.go index 07fdf44e..015479c1 100644 --- a/opensca/walk/walk.go +++ b/opensca/walk/walk.go @@ -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) @@ -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 { @@ -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 @@ -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) } }() @@ -109,7 +110,7 @@ func walk(ctx context.Context, parent *model.File, filterFunc ExtractFileFilter, return err } -// decompress 解压到指定位置 +// decompress 解压压缩包 // input: 压缩包绝对路径 // do: 对解压后目录的操作 // do.tmpdir: 临时解压目录绝对路径 需要手动删除目录