mirror of https://github.com/1Panel-dev/1Panel
fix: 解决计算文件夹大小线程没有回收的问题 (#4866)
Refs https://github.com/1Panel-dev/1Panel/issues/4867pull/4874/head
parent
9719ef2edc
commit
7684066689
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
@ -312,12 +313,32 @@ func (f *FileService) FileDownload(d request.FileDownload) (string, 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()
|
||||
size, err := fo.GetDirSize(req.Path)
|
||||
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) {
|
||||
|
|
|
@ -15,7 +15,6 @@ import (
|
|||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"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) {
|
||||
var m sync.Map
|
||||
var wg sync.WaitGroup
|
||||
|
||||
wg.Add(1)
|
||||
go ScanDir(f.Fs, path, &m, &wg)
|
||||
wg.Wait()
|
||||
|
||||
var dirSize float64
|
||||
m.Range(func(k, v interface{}) bool {
|
||||
dirSize = dirSize + v.(float64)
|
||||
return true
|
||||
var size int64
|
||||
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !info.IsDir() {
|
||||
size += info.Size()
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
return dirSize, nil
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return float64(size), nil
|
||||
}
|
||||
|
||||
func getFormat(cType CompressType) archiver.CompressedArchive {
|
||||
|
|
|
@ -10,9 +10,6 @@ import (
|
|||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/spf13/afero"
|
||||
)
|
||||
|
||||
func IsSymlink(mode os.FileMode) bool {
|
||||
|
@ -63,22 +60,6 @@ func GetGroup(gid uint32) string {
|
|||
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
|
||||
|
||||
func IsHidden(path string) bool {
|
||||
|
|
Loading…
Reference in New Issue