size.go 2.8 KB

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