From 3279353a66b0161aa14c14b4c1353eaad4467a1f Mon Sep 17 00:00:00 2001 From: xc Date: Mon, 8 Sep 2025 14:31:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=89=AB=E6=8F=8F=E5=8D=95?= =?UTF-8?q?=E4=B8=AA=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scagent/core/JsonRpc.go | 49 +++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/scagent/core/JsonRpc.go b/scagent/core/JsonRpc.go index 1dd93f7..fb1670c 100644 --- a/scagent/core/JsonRpc.go +++ b/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)