-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
size.go
118 lines (100 loc) · 2.69 KB
/
size.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright (C) 2017 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
package config
import (
"fmt"
"strconv"
"strings"
"github.com/syncthing/syncthing/lib/fs"
)
func ParseSize(s string) (Size, error) {
s = strings.TrimSpace(s)
if s == "" {
return Size{}, nil
}
var num, unit string
for i := 0; i < len(s) && (s[i] >= '0' && s[i] <= '9' || s[i] == '.' || s[i] == ','); i++ {
num = s[:i+1]
}
var i = len(num)
for i < len(s) && s[i] == ' ' {
i++
}
unit = s[i:]
val, err := strconv.ParseFloat(num, 64)
if err != nil {
return Size{}, err
}
return Size{val, unit}, nil
}
func (s Size) BaseValue() float64 {
unitPrefix := s.Unit
if len(unitPrefix) > 1 {
unitPrefix = unitPrefix[:1]
}
mult := 1.0
switch unitPrefix {
case "k", "K":
mult = 1000
case "m", "M":
mult = 1000 * 1000
case "g", "G":
mult = 1000 * 1000 * 1000
case "t", "T":
mult = 1000 * 1000 * 1000 * 1000
}
return s.Value * mult
}
func (s Size) Percentage() bool {
return strings.Contains(s.Unit, "%")
}
func (s Size) String() string {
return fmt.Sprintf("%v %s", s.Value, s.Unit)
}
func (s *Size) ParseDefault(str string) error {
sz, err := ParseSize(str)
*s = sz
return err
}
// 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 minFree.Percentage() {
freePct := (float64(usage.Free) / float64(usage.Total)) * 100
if freePct < val {
return fmt.Errorf("current %.2f %% < required %v", freePct, minFree)
}
} else if float64(usage.Free) < val {
return fmt.Errorf("current %sB < required %v", formatSI(usage.Free), minFree)
}
return nil
}
// 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) error {
if usage.Free < req {
return fmt.Errorf("current %sB < required %sB", formatSI(usage.Free), formatSI(req))
}
usage.Free -= req
return CheckFreeSpace(minFree, usage)
}
func formatSI(b uint64) string {
switch {
case b < 1000:
return fmt.Sprintf("%d ", b)
case b < 1000*1000:
return fmt.Sprintf("%.1f K", float64(b)/1000)
case b < 1000*1000*1000:
return fmt.Sprintf("%.1f M", float64(b)/(1000*1000))
case b < 1000*1000*1000*1000:
return fmt.Sprintf("%.1f G", float64(b)/(1000*1000*1000))
default:
return fmt.Sprintf("%.1f T", float64(b)/(1000*1000*1000*1000))
}
}