lib: Use uint64 for disk stats (ref #3930) (#7019)

This commit is contained in:
Simon Frei
2020-10-02 15:22:28 +02:00
committed by GitHub
parent a20c6ca868
commit 48da6f0f22
6 changed files with 62 additions and 20 deletions
+17 -6
View File
@@ -73,25 +73,36 @@ func (s *Size) ParseDefault(str string) error {
return err
}
func CheckFreeSpace(req Size, usage fs.Usage) error {
val := req.BaseValue()
// CheckFreeSpace checks that the free space does not fall below the minimum required free space.
func CheckFreeSpace(minFree Size, usage fs.Usage) error {
val := minFree.BaseValue()
if val <= 0 {
return nil
}
if req.Percentage() {
if minFree.Percentage() {
freePct := (float64(usage.Free) / float64(usage.Total)) * 100
if freePct < val {
return fmt.Errorf("%.1f %% < %v", freePct, req)
return fmt.Errorf("%.1f %% < %v", freePct, minFree)
}
} else if float64(usage.Free) < val {
return fmt.Errorf("%sB < %v", formatSI(usage.Free), req)
return fmt.Errorf("%sB < %v", formatSI(usage.Free), minFree)
}
return nil
}
func formatSI(b int64) string {
// checkAvailableSpace checks that the free space does not fall below the minimum
// required free space, considering additional required space for a future operation.
func checkAvailableSpace(req uint64, minFree Size, usage fs.Usage) bool {
if usage.Free < req {
return false
}
usage.Free -= req
return CheckFreeSpace(minFree, usage) == nil
}
func formatSI(b uint64) string {
switch {
case b < 1000:
return fmt.Sprintf("%d ", b)