Browse Source

完善扫描单个目录

master
xyiege 6 months ago
parent
commit
3279353a66
  1. 49
      scagent/core/JsonRpc.go

49
scagent/core/JsonRpc.go

@ -2,6 +2,7 @@ package core
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"runtime"
@ -28,7 +29,7 @@ type FileRpcService struct{}
// 返回的文件json结构体
type FloopJson struct {
Path string `json:"path"`
Size int64 `json:"size"`
Size string `json:"size"`
Hash string `json:"hash"`
}
@ -64,11 +65,7 @@ func scanFileUnderPath(rootDir string, scope string, logger *zap.Logger, replay
if err != nil {
logger.Error("访问路径出错", zap.Error(err))
// 判断扫描范围
if scope == "all" {
return filepath.SkipDir
} else {
return nil
}
return filepath.SkipDir
}
// 只收集文件(排除目录)
@ -81,6 +78,18 @@ func scanFileUnderPath(rootDir string, scope string, logger *zap.Logger, replay
if err != nil {
logger.Error("遍历目录出错", zap.Error(err))
}
// 打印所有文件路径
for _, file := range allFiles {
// 打印所有文件路径
// fmt.Printf("%d. %s\n", i+1, file)
d, _ := os.Stat(file)
// 加入到列表
hash := util.CalacHash(file)
// 文件大小
fsize := fmt.Sprintf("%.2f KB", d.Size()/1024)
flplist.Flist = append(flplist.Flist, FloopJson{Path: file, Size: fsize, Hash: hash})
}
}
// 遍历对一个的目录
if scope == "dir" {
@ -96,22 +105,30 @@ func scanFileUnderPath(rootDir string, scope string, logger *zap.Logger, replay
logger.Error("读取目录出错", zap.Error(err))
}
for _, file := range files {
allFiles = append(allFiles, filepath.Join(rootDir, file.Name()))
// fmt.Printf("文件路径: %s\n", file.Name())
fileinfo, err := os.Stat(file.Name())
if err != nil {
logger.Error("获取文件信息出错", zap.Error(err))
continue
}
var fsize string
if file.IsDir() {
fsize = "0"
} else {
fsize = fmt.Sprintf("%d", fileinfo.Size())
}
// 加入到列表
hash := util.CalacHash(file.Name())
flplist.Flist = append(flplist.Flist, FloopJson{Path: filepath.Join(rootDir, file.Name()), Size: fsize, Hash: hash})
// allFiles = append(allFiles, filepath.Join(rootDir, file.Name()))
}
}
// 遍历文件
}
//
// 打印所有文件路径
for _, file := range allFiles {
// 打印所有文件路径
// fmt.Printf("%d. %s\n", i+1, file)
d, _ := os.Stat(file)
// 加入到列表
hash := util.CalacHash(file)
flplist.Flist = append(flplist.Flist, FloopJson{Path: file, Size: d.Size(), Hash: hash})
}
// 将文件列表转换为json字符串
jsonStr, err := json.Marshal(flplist)

Loading…
Cancel
Save