@@ -235,7 +235,7 @@ func (f *FolderConfiguration) SharedWith(device protocol.DeviceID) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (f *FolderConfiguration) CheckAvailableSpace(req int64) error {
|
||||
func (f *FolderConfiguration) CheckAvailableSpace(req uint64) error {
|
||||
val := f.MinDiskFree.BaseValue()
|
||||
if val <= 0 {
|
||||
return nil
|
||||
@@ -245,11 +245,8 @@ func (f *FolderConfiguration) CheckAvailableSpace(req int64) error {
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
usage.Free -= req
|
||||
if usage.Free > 0 {
|
||||
if err := CheckFreeSpace(f.MinDiskFree, usage); err == nil {
|
||||
return nil
|
||||
}
|
||||
if !checkAvailableSpace(req, f.MinDiskFree, usage) {
|
||||
return fmt.Errorf("insufficient space in %v %v", fs.Type(), fs.URI())
|
||||
}
|
||||
return fmt.Errorf("insufficient space in %v %v", fs.Type(), fs.URI())
|
||||
return nil
|
||||
}
|
||||
|
||||
+17
-6
@@ -73,25 +73,36 @@ func (s *Size) ParseDefault(str string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func CheckFreeSpace(req Size, usage fs.Usage) error {
|
||||
val := req.BaseValue()
|
||||
// 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 req.Percentage() {
|
||||
if minFree.Percentage() {
|
||||
freePct := (float64(usage.Free) / float64(usage.Total)) * 100
|
||||
if freePct < val {
|
||||
return fmt.Errorf("%.1f %% < %v", freePct, req)
|
||||
return fmt.Errorf("%.1f %% < %v", freePct, minFree)
|
||||
}
|
||||
} else if float64(usage.Free) < val {
|
||||
return fmt.Errorf("%sB < %v", formatSI(usage.Free), req)
|
||||
return fmt.Errorf("%sB < %v", formatSI(usage.Free), minFree)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func formatSI(b int64) string {
|
||||
// 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) bool {
|
||||
if usage.Free < req {
|
||||
return false
|
||||
}
|
||||
usage.Free -= req
|
||||
return CheckFreeSpace(minFree, usage) == nil
|
||||
}
|
||||
|
||||
func formatSI(b uint64) string {
|
||||
switch {
|
||||
case b < 1000:
|
||||
return fmt.Sprintf("%d ", b)
|
||||
|
||||
+35
-1
@@ -9,6 +9,7 @@ package config
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/syncthing/syncthing/lib/fs"
|
||||
"github.com/syncthing/syncthing/lib/util"
|
||||
)
|
||||
|
||||
@@ -66,6 +67,10 @@ func TestParseSize(t *testing.T) {
|
||||
// The empty string is a valid zero
|
||||
{"", true, 0, false},
|
||||
{" ", true, 0, false},
|
||||
// Just numbers are fine too
|
||||
{"0", true, 0, false},
|
||||
{"3", true, 3, false},
|
||||
{"34.3", true, 34.3, false},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
@@ -94,7 +99,7 @@ func TestParseSize(t *testing.T) {
|
||||
|
||||
func TestFormatSI(t *testing.T) {
|
||||
cases := []struct {
|
||||
bytes int64
|
||||
bytes uint64
|
||||
result string
|
||||
}{
|
||||
{
|
||||
@@ -130,3 +135,32 @@ func TestFormatSI(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckAvailableSize(t *testing.T) {
|
||||
cases := []struct {
|
||||
req, free, total uint64
|
||||
minFree string
|
||||
ok bool
|
||||
}{
|
||||
{10, 1e8, 1e9, "1%", true},
|
||||
{1e4, 1e3, 1e9, "1%", false},
|
||||
{1e2, 1e3, 1e9, "1%", false},
|
||||
{1e9, 1 << 62, 1 << 63, "1%", true},
|
||||
{10, 1e8, 1e9, "1M", true},
|
||||
{1e4, 1e3, 1e9, "1M", false},
|
||||
{1e2, 1e3, 1e9, "1M", false},
|
||||
{1e9, 1 << 62, 1 << 63, "1M", true},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
minFree, err := ParseSize(tc.minFree)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to parse %v: %v", tc.minFree, err)
|
||||
continue
|
||||
}
|
||||
usage := fs.Usage{Free: tc.free, Total: tc.total}
|
||||
if ok := checkAvailableSpace(tc.req, minFree, usage); ok != tc.ok {
|
||||
t.Errorf("checkAvailableSpace(%v, %v, %v) == %v, expected %v", tc.req, minFree, usage, ok, tc.ok)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user