1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-03-17 03:04:46 +08:00

fix: 解决计算文件夹大小线程没有回收的问题 (#4866)

Refs https://github.com/1Panel-dev/1Panel/issues/4867
This commit is contained in:
zhengkunwang 2024-05-06 13:51:55 +08:00 committed by GitHub
parent 9719ef2edc
commit 7684066689
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 36 additions and 35 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"io/fs" "io/fs"
"os" "os"
"os/exec"
"path" "path"
"path/filepath" "path/filepath"
"strings" "strings"
@ -312,12 +313,32 @@ func (f *FileService) FileDownload(d request.FileDownload) (string, error) {
} }
func (f *FileService) DirSize(req request.DirSizeReq) (response.DirSizeRes, error) { func (f *FileService) DirSize(req request.DirSizeReq) (response.DirSizeRes, error) {
var (
res response.DirSizeRes
)
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() fo := files.NewFileOp()
size, err := fo.GetDirSize(req.Path) size, err := fo.GetDirSize(req.Path)
if err != nil { if err != nil {
return response.DirSizeRes{}, err return res, err
} }
return response.DirSizeRes{Size: size}, nil res.Size = size
return res, nil
} }
func (f *FileService) ReadLogByLine(req request.FileReadByLineReq) (*response.FileLineContent, error) { func (f *FileService) ReadLogByLine(req request.FileReadByLineReq) (*response.FileLineContent, error) {

View File

@ -15,7 +15,6 @@ import (
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
"github.com/1Panel-dev/1Panel/backend/utils/cmd" "github.com/1Panel-dev/1Panel/backend/utils/cmd"
@ -436,20 +435,20 @@ func (f FileOp) CopyFile(src, dst string) error {
} }
func (f FileOp) GetDirSize(path string) (float64, error) { func (f FileOp) GetDirSize(path string) (float64, error) {
var m sync.Map var size int64
var wg sync.WaitGroup err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
wg.Add(1) return err
go ScanDir(f.Fs, path, &m, &wg) }
wg.Wait() if !info.IsDir() {
size += info.Size()
var dirSize float64 }
m.Range(func(k, v interface{}) bool { return nil
dirSize = dirSize + v.(float64)
return true
}) })
if err != nil {
return dirSize, nil return 0, err
}
return float64(size), nil
} }
func getFormat(cType CompressType) archiver.CompressedArchive { func getFormat(cType CompressType) archiver.CompressedArchive {

View File

@ -10,9 +10,6 @@ import (
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"sync"
"github.com/spf13/afero"
) )
func IsSymlink(mode os.FileMode) bool { func IsSymlink(mode os.FileMode) bool {
@ -63,22 +60,6 @@ func GetGroup(gid uint32) string {
return usr.Name return usr.Name
} }
func ScanDir(fs afero.Fs, path string, dirMap *sync.Map, wg *sync.WaitGroup) {
afs := &afero.Afero{Fs: fs}
files, _ := afs.ReadDir(path)
for _, f := range files {
if f.IsDir() {
wg.Add(1)
go ScanDir(fs, filepath.Join(path, f.Name()), dirMap, wg)
} else {
if f.Size() > 0 {
dirMap.Store(filepath.Join(path, f.Name()), float64(f.Size()))
}
}
}
defer wg.Done()
}
const dotCharacter = 46 const dotCharacter = 46
func IsHidden(path string) bool { func IsHidden(path string) bool {