From 82e15cb3e15fe81b3e0338a2f20678b8bfc36583 Mon Sep 17 00:00:00 2001 From: zhengkunwang <31820853+zhengkunwang223@users.noreply.github.com> Date: Thu, 14 Nov 2024 18:38:01 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9AWAF=E3=80=81=E7=BD=91=E7=AB=99?= =?UTF-8?q?=E7=9B=91=E6=8E=A7=E5=A2=9E=E5=8A=A0=E6=97=A5=E5=BF=97=E5=AE=9A?= =?UTF-8?q?=E6=97=B6=E6=B8=85=E7=90=86=20(#7057)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- agent/app/dto/response/file.go | 2 +- agent/app/service/file.go | 14 -------------- agent/utils/files/file_op.go | 20 +++++++++++++++++--- frontend/src/styles/common.scss | 1 + 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/agent/app/dto/response/file.go b/agent/app/dto/response/file.go index ae756cedd..56c17bac6 100644 --- a/agent/app/dto/response/file.go +++ b/agent/app/dto/response/file.go @@ -24,7 +24,7 @@ type FileTree struct { } type DirSizeRes struct { - Size float64 `json:"size" validate:"required"` + Size int64 `json:"size" validate:"required"` } type FileProcessKeys struct { diff --git a/agent/app/service/file.go b/agent/app/service/file.go index 1008df7d2..32e0eb414 100644 --- a/agent/app/service/file.go +++ b/agent/app/service/file.go @@ -5,7 +5,6 @@ import ( "io" "io/fs" "os" - "os/exec" "path" "path/filepath" "strings" @@ -403,19 +402,6 @@ func (f *FileService) DirSize(req request.DirSizeReq) (response.DirSizeRes, erro if req.Path == "/proc" { return res, nil } - cmd := exec.Command("du", "-s", req.Path) - output, err := cmd.Output() - if err == nil { - fields := strings.Fields(string(output)) - if len(fields) == 2 { - var cmdSize int64 - _, err = fmt.Sscanf(fields[0], "%d", &cmdSize) - if err == nil { - res.Size = float64(cmdSize * 1024) - return res, nil - } - } - } fo := files.NewFileOp() size, err := fo.GetDirSize(req.Path) if err != nil { diff --git a/agent/utils/files/file_op.go b/agent/utils/files/file_op.go index 8ccd7c000..511066f3b 100644 --- a/agent/utils/files/file_op.go +++ b/agent/utils/files/file_op.go @@ -12,6 +12,7 @@ import ( "io/fs" "net/http" "os" + "os/exec" "path" "path/filepath" "strconv" @@ -502,9 +503,22 @@ func (f FileOp) CopyFile(src, dst string) error { return cmd.ExecCmd(fmt.Sprintf(`cp -f '%s' '%s'`, src, dst+"/")) } -func (f FileOp) GetDirSize(path string) (float64, error) { +func (f FileOp) GetDirSize(path string) (int64, error) { + duCmd := exec.Command("du", "-s", path) + output, err := duCmd.Output() + if err == nil { + fields := strings.Fields(string(output)) + if len(fields) == 2 { + var cmdSize int64 + _, err = fmt.Sscanf(fields[0], "%d", &cmdSize) + if err == nil { + return cmdSize * 1024, nil + } + } + } + var size int64 - err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error { + err = filepath.Walk(path, func(path string, info os.FileInfo, err error) error { if err != nil { return err } @@ -516,7 +530,7 @@ func (f FileOp) GetDirSize(path string) (float64, error) { if err != nil { return 0, err } - return float64(size), nil + return size, nil } func getFormat(cType CompressType) archiver.CompressedArchive { diff --git a/frontend/src/styles/common.scss b/frontend/src/styles/common.scss index cc843d3b1..0ed2d4cac 100644 --- a/frontend/src/styles/common.scss +++ b/frontend/src/styles/common.scss @@ -127,6 +127,7 @@ html { color: #8f959e; width: 100%; display: inline-block; + height: 12px; } .input-error {