47 lines
1.2 KiB
Go
47 lines
1.2 KiB
Go
|
|
package local
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"math"
|
||
|
|
|
||
|
|
"git.apinb.com/ops/files/internal/storage"
|
||
|
|
"github.com/shirou/gopsutil/v3/disk"
|
||
|
|
)
|
||
|
|
|
||
|
|
func (c *Client) diskStatus() (*storage.DiskStatus, error) {
|
||
|
|
usage, err := disk.Usage(c.rootPath)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("读取本地存储磁盘容量: %w", err)
|
||
|
|
}
|
||
|
|
level := "normal"
|
||
|
|
if usage.Free < c.minimumFreeBytes {
|
||
|
|
level = "critical"
|
||
|
|
} else if c.minimumFreeBytes <= math.MaxUint64/2 && usage.Free < c.minimumFreeBytes*2 {
|
||
|
|
level = "warning"
|
||
|
|
}
|
||
|
|
return &storage.DiskStatus{
|
||
|
|
Path: c.rootPath,
|
||
|
|
TotalBytes: usage.Total,
|
||
|
|
UsedBytes: usage.Used,
|
||
|
|
FreeBytes: usage.Free,
|
||
|
|
UsedPercent: usage.UsedPercent,
|
||
|
|
MinimumFreeBytes: c.minimumFreeBytes,
|
||
|
|
Level: level,
|
||
|
|
}, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *Client) ensureCapacity(expectedSize int64) error {
|
||
|
|
if expectedSize < 0 {
|
||
|
|
return fmt.Errorf("文件大小不能为负数")
|
||
|
|
}
|
||
|
|
diskStatus, err := c.diskStatus()
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
requested := uint64(expectedSize)
|
||
|
|
if requested > math.MaxUint64-c.minimumFreeBytes || diskStatus.FreeBytes < requested+c.minimumFreeBytes {
|
||
|
|
return storage.ErrInsufficientSpace
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|