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 文件名后模糊*
//
// 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
}

View File

@@ -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,
}))
}

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)
_, 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)