Browse Source

尝试引入并发功能

master
xyiege 5 months ago
parent
commit
82592f8631
  1. 37
      scagent/core/JsonRpc.go
  2. 6
      vue/afvue/src/views/Flist.vue

37
scagent/core/JsonRpc.go

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"scagent/util" "scagent/util"
"go.uber.org/zap" "go.uber.org/zap"
@ -38,23 +39,31 @@ type FlpList struct {
// 获取文件路径 // 获取文件路径
func (f *FileRpcService) GetFilePath(args *Args, replay *string) error { func (f *FileRpcService) GetFilePath(args *Args, replay *string) error {
rootDir := args.FilePath
// 启用日志
logger := util.NewProductionLogger() logger := util.NewProductionLogger()
defer logger.Sync() defer logger.Sync()
// 并发处理
go scanFileUnderPath(args.FilePath, args.Scope, logger, replay)
runtime.Gosched()
//
return nil
}
// 扫描指定目录下的文件,可以全部,也可以局部
func scanFileUnderPath(rootDir string, scope string, logger *zap.Logger, replay *string) {
var flplist FlpList // 文件列表
// 遍历目录下的文件 // 遍历目录下的文件
var allFiles []string // 存储所有文件路径 var allFiles []string // 存储所有文件路径
var flplist FlpList // 文件列表
// 判断返回
// 读取所有的
// 递归遍历目录及其子目录 // 递归遍历目录及其子目录
err := filepath.WalkDir(rootDir, func(path string, d os.DirEntry, err error) error { err := filepath.WalkDir(rootDir, func(path string, d os.DirEntry, err error) error {
// 处理遍历过程中的错误(如权限不足) // 处理遍历过程中的错误(如权限不足)
if err != nil { if err != nil {
logger.Error("访问路径出错", zap.Error(err)) logger.Error("访问路径出错", zap.Error(err))
// // 判断扫描范围
if args.Scope == "all" { if scope == "all" {
return filepath.SkipDir return filepath.SkipDir
} else { } else {
return nil return nil
@ -70,9 +79,9 @@ func (f *FileRpcService) GetFilePath(args *Args, replay *string) error {
// 打印错误日志 // 打印错误日志
if err != nil { if err != nil {
logger.Error("遍历目录出错", zap.Error(err)) logger.Error("遍历目录出错", zap.Error(err))
return err
} }
//
// 打印所有文件路径 // 打印所有文件路径
for _, file := range allFiles { for _, file := range allFiles {
// 打印所有文件路径 // 打印所有文件路径
@ -87,18 +96,8 @@ func (f *FileRpcService) GetFilePath(args *Args, replay *string) error {
jsonStr, err := json.Marshal(flplist) jsonStr, err := json.Marshal(flplist)
if err != nil { if err != nil {
logger.Error("转换为json字符串出错", zap.Error(err)) logger.Error("转换为json字符串出错", zap.Error(err))
return err
} }
// fmt.Printf("jsonStr: %s\n", jsonStr) // 执行结果存到replay
*replay = string(jsonStr) *replay = string(jsonStr)
//
return nil
}
func (f *FileRpcService) GetFileHash(args *Args, replay *Reply) error {
// 计算文件hash
hash := util.CalacHash(args.FilePath)
replay.Redata = hash
return nil
} }

6
vue/afvue/src/views/Flist.vue

@ -7,7 +7,9 @@
</div> </div>
<div class="wflist"> <div class="wflist">
<div class="wfitem" v-for="item in flist" :key="item"> <div class="wfitem" v-for="item in flist" :key="item">
{{ item }} {{ item.path }}
{{ item.size }}
{{ item.hash }}
</div> </div>
</div> </div>
</div> </div>
@ -36,7 +38,7 @@ export default {
srcip: this.srcip srcip: this.srcip
}).then(res => { }).then(res => {
console.log(res) console.log(res)
this.flist = res.data this.flist = res.data.list
}) })
} }
} }

Loading…
Cancel
Save