fix: 网元文件获取改用find读取避免Argument list too long错误

This commit is contained in:
TsMask
2024-10-08 16:33:43 +08:00
parent d0f946cc85
commit 4267c7df9d
3 changed files with 27 additions and 25 deletions

View File

@@ -24,9 +24,8 @@ type FileListRow struct {
// 文件列表 // 文件列表
// search 文件名后模糊* // search 文件名后模糊*
// //
// return 目录大小,行记录,异常 // return 行记录,异常
func FileList(sshClient *ConnSSH, path, search string) (string, []FileListRow, error) { func FileList(sshClient *ConnSSH, path, search string) ([]FileListRow, error) {
totalSize := ""
var rows []FileListRow var rows []FileListRow
rowStr := "" rowStr := ""
@@ -35,40 +34,37 @@ func FileList(sshClient *ConnSSH, path, search string) (string, []FileListRow, e
if search != "" { if search != "" {
searchStr = search + searchStr 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 { if sshClient == nil {
resultStr, err := cmd.Execf(cmdStr) resultStr, err := cmd.Execf(cmdStr)
if err != nil { if err != nil {
logger.Errorf("Ne FileList Path: %s, Search: %s, Error:%s", path, search, err.Error()) logger.Errorf("Ne FileList Path: %s, Search: %s, Error:%s", path, search, err.Error())
return totalSize, rows, err return rows, err
} }
rowStr = resultStr rowStr = resultStr
} else { } else {
resultStr, err := sshClient.RunCMD(cmdStr) resultStr, err := sshClient.RunCMD(cmdStr)
if err != nil { if err != nil {
logger.Errorf("Ne FileList Path: %s, Search: %s, Error:%s", path, search, err.Error()) logger.Errorf("Ne FileList Path: %s, Search: %s, Error:%s", path, search, err.Error())
return totalSize, rows, err return rows, err
} }
rowStr = resultStr rowStr = resultStr
} }
// 遍历组装 // 遍历组装
rowStrList := strings.Split(rowStr, "\n") rowStrList := strings.Split(rowStr, "\n")
for i, rowStr := range rowStrList { for _, rowStr := range rowStrList {
if rowStr == "" { if rowStr == "" {
continue continue
} }
// 使用空格对字符串进行切割 // 使用空格对字符串进行切割
fields := strings.Fields(rowStr) fields := strings.Fields(rowStr)
// 无查询过滤会有total总计
if i == 0 && searchStr == "" {
totalSize = fields[1]
continue
}
// 拆分不足7位跳过 // 拆分不足7位跳过
if len(fields) != 7 { if len(fields) != 7 {
continue continue
@@ -83,6 +79,14 @@ func FileList(sshClient *ConnSSH, path, search string) (string, []FileListRow, e
fileType = "symlink" fileType = "symlink"
} }
// 文件名
fileName := fields[6]
if fileName == "." {
continue
} else if strings.HasPrefix(fileName, "./") {
fileName = strings.TrimPrefix(fileName, "./")
}
// 提取各个字段的值 // 提取各个字段的值
rows = append(rows, FileListRow{ rows = append(rows, FileListRow{
FileMode: fileMode, FileMode: fileMode,
@@ -92,8 +96,8 @@ func FileList(sshClient *ConnSSH, path, search string) (string, []FileListRow, e
Group: fields[3], Group: fields[3],
Size: fields[4], Size: fields[4],
ModifiedTime: parse.Number(fields[5]), ModifiedTime: parse.Number(fields[5]),
FileName: fields[6], FileName: fileName,
}) })
} }
return totalSize, rows, nil return rows, nil
} }

View File

@@ -181,13 +181,12 @@ func (s *NeActionController) Files(c *gin.Context) {
defer sshClient.Close() 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 { if err != nil {
c.JSON(200, result.Ok(map[string]any{ c.JSON(200, result.Ok(map[string]any{
"path": querys.Path, "path": querys.Path,
"totalSize": totalSize, "total": len(rows),
"total": len(rows), "rows": []ssh.FileListRow{},
"rows": []ssh.FileListRow{},
})) }))
return return
} }
@@ -206,10 +205,9 @@ func (s *NeActionController) Files(c *gin.Context) {
} }
c.JSON(200, result.Ok(map[string]any{ c.JSON(200, result.Ok(map[string]any{
"path": querys.Path, "path": querys.Path,
"totalSize": totalSize, "total": lenNum,
"total": lenNum, "rows": splitRows,
"rows": splitRows,
})) }))
} }

View File

@@ -188,7 +188,7 @@ func (r *TraceTaskHlr) File(traceId, dirPath string) ([]map[string]any, error) {
// 获取文件列表 // 获取文件列表
fileName := fmt.Sprintf("%s_%s", neInfo.NeName, traceId) 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 { if err != nil {
hlrItem["err"] = "file not found" hlrItem["err"] = "file not found"
hlrList = append(hlrList, hlrItem) hlrList = append(hlrList, hlrItem)