自动更新管控端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

152 lines
3.7 KiB

package core
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"scagent/util"
"go.uber.org/zap"
)
// 传入的参数
// scope: all 所有文件
// scope: dir 目录下的文件
type Args struct {
FilePath, Scope string
}
// 返回的参数
type Reply struct {
Redata string
}
// 返回的json数据
type FileRpcService struct{}
// 返回的文件json结构体
type FloopJson struct {
Path string `json:"path"`
Size string `json:"size"`
Hash string `json:"hash"`
}
// 文件列表
type FlpList struct {
Flist []FloopJson `json:"list"`
}
// 获取文件路径
func (f *FileRpcService) GetFilePath(args *Args, replay *string) error {
// 启用日志
logger := util.NewProductionLogger()
defer logger.Sync()
// 并发处理
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 // 文件列表
// 遍历所有的文件,时间长
if scope == "all" {
// 遍历目录下的文件
var allFiles []string // 存储所有文件路径
// 递归遍历目录及其子目录
err := filepath.WalkDir(rootDir, func(path string, d os.DirEntry, err error) error {
// 处理遍历过程中的错误(如权限不足)
if err != nil {
logger.Error("访问路径出错", zap.Error(err))
// 判断扫描范围
return filepath.SkipDir
}
// 只收集文件(排除目录)
if !d.IsDir() {
allFiles = append(allFiles, path)
}
return nil
})
// 打印错误日志
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", float64(d.Size())/1024)
flplist.Flist = append(flplist.Flist, FloopJson{Path: file, Size: fsize, Hash: hash})
}
}
// 遍历对一个的目录
if scope == "dir" {
fmt.Printf("目录路径: %s\n", rootDir)
// 遍历目录下的文件
finfo, err := os.Stat(rootDir)
// fmt.Printf("目录信息: %v\n", finfo)
if err != nil {
logger.Error("获取目录信息出错", zap.Error(err))
}
// 遍历目录下的文件
if finfo.IsDir() {
files, err := os.ReadDir(rootDir)
if err != nil {
logger.Error("读取目录出错", zap.Error(err))
}
// fmt.Printf("目录下的文件: %v\n", files)
for _, file := range files {
// 拼装路径
realpath := filepath.Join(rootDir, file.Name())
// fmt.Printf("文件路径: %s\n", file.Name())
fileinfo, err := os.Stat(realpath)
// fmt.Printf("文件信息: %v\n", fileinfo)
if err != nil {
logger.Error("获取文件信息出错", zap.Error(err))
}
var fsize string
fsize = "0kb"
//
if file.IsDir() {
fsize = "0"
} else {
fsize = fmt.Sprintf("%d", fileinfo.Size())
}
// 加入到列表
hash := util.CalacHash(realpath)
flplist.Flist = append(flplist.Flist, FloopJson{Path: filepath.Join(rootDir, file.Name()), Size: fsize, Hash: hash})
// allFiles = append(allFiles, filepath.Join(rootDir, file.Name()))
}
} else {
ssize := fmt.Sprintf("%.2f KB", float64(finfo.Size())/1024)
flplist.Flist = append(flplist.Flist, FloopJson{Path: rootDir, Size: ssize, Hash: util.CalacHash(rootDir)})
}
}
//
// 将文件列表转换为json字符串
jsonStr, err := json.Marshal(flplist)
if err != nil {
logger.Error("转换为json字符串出错", zap.Error(err))
}
// fmt.Printf("json字符串: %s\n", jsonStr)
// 执行结果存到replay
*replay = string(jsonStr)
}