size.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 len(s) == 0 {
  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. var 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. func CheckFreeSpace(req Size, usage fs.Usage) error {
  67. val := req.BaseValue()
  68. if val <= 0 {
  69. return nil
  70. }
  71. if req.Percentage() {
  72. freePct := (float64(usage.Free) / float64(usage.Total)) * 100
  73. if freePct < val {
  74. return fmt.Errorf("%.1f %% < %v", freePct, req)
  75. }
  76. } else if float64(usage.Free) < val {
  77. return fmt.Errorf("%sB < %v", formatSI(usage.Free), req)
  78. }
  79. return nil
  80. }
  81. func formatSI(b int64) string {
  82. switch {
  83. case b < 1000:
  84. return fmt.Sprintf("%d ", b)
  85. case b < 1000*1000:
  86. return fmt.Sprintf("%.1f K", float64(b)/1000)
  87. case b < 1000*1000*1000:
  88. return fmt.Sprintf("%.1f M", float64(b)/(1000*1000))
  89. case b < 1000*1000*1000*1000:
  90. return fmt.Sprintf("%.1f G", float64(b)/(1000*1000*1000))
  91. default:
  92. return fmt.Sprintf("%.1f T", float64(b)/(1000*1000*1000*1000))
  93. }
  94. }