From 4267c7df9d009d3d65ed99ab80749eb47d51caa7 Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Tue, 8 Oct 2024 16:33:43 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=BD=91=E5=85=83=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E6=94=B9=E7=94=A8find=E8=AF=BB=E5=8F=96?= =?UTF-8?q?=E9=81=BF=E5=85=8DArgument=20list=20too=20long=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/framework/utils/ssh/files.go | 34 +++++++++++-------- .../network_element/controller/action.go | 16 ++++----- src/modules/trace/service/trace_task_hlr.go | 2 +- 3 files changed, 27 insertions(+), 25 deletions(-) diff --git a/src/framework/utils/ssh/files.go b/src/framework/utils/ssh/files.go index d9d5f425..187d9c86 100644 --- a/src/framework/utils/ssh/files.go +++ b/src/framework/utils/ssh/files.go @@ -24,9 +24,8 @@ type FileListRow struct { // 文件列表 // search 文件名后模糊* // -// return 目录大小,行记录,异常 -func FileList(sshClient *ConnSSH, path, search string) (string, []FileListRow, error) { - totalSize := "" +// return 行记录,异常 +func FileList(sshClient *ConnSSH, path, search string) ([]FileListRow, error) { var rows []FileListRow rowStr := "" @@ -35,40 +34,37 @@ func FileList(sshClient *ConnSSH, path, search string) (string, []FileListRow, e if search != "" { searchStr = search + searchStr } - cmdStr := fmt.Sprintf("cd %s && ls -lthd --time-style=+%%s %s", path, searchStr) + // cd /var/log && find. -maxdepth 1 -name'mme*' -exec ls -lthd --time-style=+%s {} + + cmdStr := fmt.Sprintf("cd %s && find . -maxdepth 1 -name '%s' -exec ls -lthd --time-style=+%%s {} +", path, searchStr) + // cd /var/log && ls -lthd --time-style=+%s mme* + // cmdStr := fmt.Sprintf("cd %s && ls -lthd --time-style=+%%s %s", path, searchStr) // 是否远程客户端读取 if sshClient == nil { resultStr, err := cmd.Execf(cmdStr) if err != nil { logger.Errorf("Ne FileList Path: %s, Search: %s, Error:%s", path, search, err.Error()) - return totalSize, rows, err + return rows, err } rowStr = resultStr } else { resultStr, err := sshClient.RunCMD(cmdStr) if err != nil { logger.Errorf("Ne FileList Path: %s, Search: %s, Error:%s", path, search, err.Error()) - return totalSize, rows, err + return rows, err } rowStr = resultStr } // 遍历组装 rowStrList := strings.Split(rowStr, "\n") - for i, rowStr := range rowStrList { + for _, rowStr := range rowStrList { if rowStr == "" { continue } // 使用空格对字符串进行切割 fields := strings.Fields(rowStr) - // 无查询过滤会有total总计 - if i == 0 && searchStr == "" { - totalSize = fields[1] - continue - } - // 拆分不足7位跳过 if len(fields) != 7 { continue @@ -83,6 +79,14 @@ func FileList(sshClient *ConnSSH, path, search string) (string, []FileListRow, e fileType = "symlink" } + // 文件名 + fileName := fields[6] + if fileName == "." { + continue + } else if strings.HasPrefix(fileName, "./") { + fileName = strings.TrimPrefix(fileName, "./") + } + // 提取各个字段的值 rows = append(rows, FileListRow{ FileMode: fileMode, @@ -92,8 +96,8 @@ func FileList(sshClient *ConnSSH, path, search string) (string, []FileListRow, e Group: fields[3], Size: fields[4], ModifiedTime: parse.Number(fields[5]), - FileName: fields[6], + FileName: fileName, }) } - return totalSize, rows, nil + return rows, nil } diff --git a/src/modules/network_element/controller/action.go b/src/modules/network_element/controller/action.go index 9c44ca9d..0d121b7d 100644 --- a/src/modules/network_element/controller/action.go +++ b/src/modules/network_element/controller/action.go @@ -181,13 +181,12 @@ func (s *NeActionController) Files(c *gin.Context) { defer sshClient.Close() // 获取文件列表 - totalSize, rows, err := ssh.FileList(sshClient, querys.Path, querys.Search) + rows, err := ssh.FileList(sshClient, querys.Path, querys.Search) if err != nil { c.JSON(200, result.Ok(map[string]any{ - "path": querys.Path, - "totalSize": totalSize, - "total": len(rows), - "rows": []ssh.FileListRow{}, + "path": querys.Path, + "total": len(rows), + "rows": []ssh.FileListRow{}, })) return } @@ -206,10 +205,9 @@ func (s *NeActionController) Files(c *gin.Context) { } c.JSON(200, result.Ok(map[string]any{ - "path": querys.Path, - "totalSize": totalSize, - "total": lenNum, - "rows": splitRows, + "path": querys.Path, + "total": lenNum, + "rows": splitRows, })) } diff --git a/src/modules/trace/service/trace_task_hlr.go b/src/modules/trace/service/trace_task_hlr.go index 2cad7167..835f1c11 100644 --- a/src/modules/trace/service/trace_task_hlr.go +++ b/src/modules/trace/service/trace_task_hlr.go @@ -188,7 +188,7 @@ func (r *TraceTaskHlr) File(traceId, dirPath string) ([]map[string]any, error) { // 获取文件列表 fileName := fmt.Sprintf("%s_%s", neInfo.NeName, traceId) - _, rows, err := ssh.FileList(sshClient, filepath.ToSlash(dirPath), fileName) + rows, err := ssh.FileList(sshClient, filepath.ToSlash(dirPath), fileName) if err != nil { hlrItem["err"] = "file not found" hlrList = append(hlrList, hlrItem)