2017-04-12 09:01:19 +00:00
|
|
|
// 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"
|
2017-10-24 07:58:55 +00:00
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
2017-04-12 09:01:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Size struct {
|
|
|
|
Value float64 `json:"value" xml:",chardata"`
|
|
|
|
Unit string `json:"unit" xml:"unit,attr"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseSize(s string) (Size, error) {
|
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
if len(s) == 0 {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-02-12 06:58:24 +00:00
|
|
|
func (s *Size) ParseDefault(str string) error {
|
|
|
|
sz, err := ParseSize(str)
|
|
|
|
*s = sz
|
|
|
|
return err
|
2017-04-12 09:01:19 +00:00
|
|
|
}
|
2017-10-24 07:58:55 +00:00
|
|
|
|
2019-02-12 12:25:11 +00:00
|
|
|
func CheckFreeSpace(req Size, usage fs.Usage) error {
|
2017-10-24 07:58:55 +00:00
|
|
|
val := req.BaseValue()
|
|
|
|
if val <= 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.Percentage() {
|
|
|
|
freePct := (float64(usage.Free) / float64(usage.Total)) * 100
|
2018-08-25 08:16:38 +00:00
|
|
|
if freePct < val {
|
2019-08-02 12:43:05 +00:00
|
|
|
return fmt.Errorf("%.1f %% < %v", freePct, req)
|
2017-10-24 07:58:55 +00:00
|
|
|
}
|
2018-08-25 08:16:38 +00:00
|
|
|
} else if float64(usage.Free) < val {
|
2019-08-02 12:43:05 +00:00
|
|
|
return fmt.Errorf("%sB < %v", formatSI(usage.Free), req)
|
2017-10-24 07:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-08-02 12:43:05 +00:00
|
|
|
|
|
|
|
func formatSI(b int64) 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))
|
|
|
|
}
|
|
|
|
}
|