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
|
|
|
)
|
|
|
|
|
refactor: use modern Protobuf encoder (#9817)
At a high level, this is what I've done and why:
- I'm moving the protobuf generation for the `protocol`, `discovery` and
`db` packages to the modern alternatives, and using `buf` to generate
because it's nice and simple.
- After trying various approaches on how to integrate the new types with
the existing code, I opted for splitting off our own data model types
from the on-the-wire generated types. This means we can have a
`FileInfo` type with nicer ergonomics and lots of methods, while the
protobuf generated type stays clean and close to the wire protocol. It
does mean copying between the two when required, which certainly adds a
small amount of inefficiency. If we want to walk this back in the future
and use the raw generated type throughout, that's possible, this however
makes the refactor smaller (!) as it doesn't change everything about the
type for everyone at the same time.
- I have simply removed in cold blood a significant number of old
database migrations. These depended on previous generations of generated
messages of various kinds and were annoying to support in the new
fashion. The oldest supported database version now is the one from
Syncthing 1.9.0 from Sep 7, 2020.
- I changed config structs to be regular manually defined structs.
For the sake of discussion, some things I tried that turned out not to
work...
### Embedding / wrapping
Embedding the protobuf generated structs in our existing types as a data
container and keeping our methods and stuff:
```
package protocol
type FileInfo struct {
*generated.FileInfo
}
```
This generates a lot of problems because the internal shape of the
generated struct is quite different (different names, different types,
more pointers), because initializing it doesn't work like you'd expect
(i.e., you end up with an embedded nil pointer and a panic), and because
the types of child types don't get wrapped. That is, even if we also
have a similar wrapper around a `Vector`, that's not the type you get
when accessing `someFileInfo.Version`, you get the `*generated.Vector`
that doesn't have methods, etc.
### Aliasing
```
package protocol
type FileInfo = generated.FileInfo
```
Doesn't help because you can't attach methods to it, plus all the above.
### Generating the types into the target package like we do now and
attaching methods
This fails because of the different shape of the generated type (as in
the embedding case above) plus the generated struct already has a bunch
of methods that we can't necessarily override properly (like `String()`
and a bunch of getters).
### Methods to functions
I considered just moving all the methods we attach to functions in a
specific package, so that for example
```
package protocol
func (f FileInfo) Equal(other FileInfo) bool
```
would become
```
package fileinfos
func Equal(a, b *generated.FileInfo) bool
```
and this would mostly work, but becomes quite verbose and cumbersome,
and somewhat limits discoverability (you can't see what methods are
available on the type in auto completions, etc). In the end I did this
in some cases, like in the database layer where a lot of things like
`func (fv *FileVersion) IsEmpty() bool` becomes `func fvIsEmpty(fv
*generated.FileVersion)` because they were anyway just internal methods.
Fixes #8247
2024-12-01 15:50:17 +00:00
|
|
|
type Size struct {
|
|
|
|
Value float64 `json:"value" xml:",chardata"`
|
|
|
|
Unit string `json:"unit" xml:"unit,attr"`
|
|
|
|
}
|
|
|
|
|
2017-04-12 09:01:19 +00:00
|
|
|
func ParseSize(s string) (Size, error) {
|
|
|
|
s = strings.TrimSpace(s)
|
2022-07-28 14:51:03 +00:00
|
|
|
if s == "" {
|
2017-04-12 09:01:19 +00:00
|
|
|
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]
|
|
|
|
}
|
2024-11-19 10:32:56 +00:00
|
|
|
i := len(num)
|
2017-04-12 09:01:19 +00:00
|
|
|
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
|
|
|
|
2020-10-02 13:22:28 +00:00
|
|
|
// 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()
|
2017-10-24 07:58:55 +00:00
|
|
|
if val <= 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-02 13:22:28 +00:00
|
|
|
if minFree.Percentage() {
|
2017-10-24 07:58:55 +00:00
|
|
|
freePct := (float64(usage.Free) / float64(usage.Total)) * 100
|
2018-08-25 08:16:38 +00:00
|
|
|
if freePct < val {
|
2022-02-24 16:07:51 +00:00
|
|
|
return fmt.Errorf("current %.2f %% < required %v", freePct, minFree)
|
2017-10-24 07:58:55 +00:00
|
|
|
}
|
2018-08-25 08:16:38 +00:00
|
|
|
} else if float64(usage.Free) < val {
|
2022-02-24 16:07:51 +00:00
|
|
|
return fmt.Errorf("current %sB < required %v", formatSI(usage.Free), minFree)
|
2017-10-24 07:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-08-02 12:43:05 +00:00
|
|
|
|
2020-10-02 13:22:28 +00:00
|
|
|
// checkAvailableSpace checks that the free space does not fall below the minimum
|
|
|
|
// required free space, considering additional required space for a future operation.
|
2022-02-24 16:07:51 +00:00
|
|
|
func checkAvailableSpace(req uint64, minFree Size, usage fs.Usage) error {
|
2020-10-02 13:22:28 +00:00
|
|
|
if usage.Free < req {
|
2022-02-24 16:07:51 +00:00
|
|
|
return fmt.Errorf("current %sB < required %sB", formatSI(usage.Free), formatSI(req))
|
2020-10-02 13:22:28 +00:00
|
|
|
}
|
|
|
|
usage.Free -= req
|
2022-02-24 16:07:51 +00:00
|
|
|
return CheckFreeSpace(minFree, usage)
|
2020-10-02 13:22:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func formatSI(b uint64) string {
|
2019-08-02 12:43:05 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|