lib/config: Improve clarity of free space errors (fixes #8180) (#8191)

This commit is contained in:
Jakob Borg 2022-02-24 17:07:51 +01:00 committed by GitHub
parent 10f9d95cd2
commit f890fe6fd3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 10 deletions

View File

@ -261,8 +261,8 @@ func (f *FolderConfiguration) CheckAvailableSpace(req uint64) error {
if err != nil {
return nil
}
if !checkAvailableSpace(req, f.MinDiskFree, usage) {
return fmt.Errorf("insufficient space in %v %v", fs.Type(), fs.URI())
if err := checkAvailableSpace(req, f.MinDiskFree, usage); err != nil {
return fmt.Errorf("insufficient space in folder %v (%v): %w", f.Description(), fs.URI(), err)
}
return nil
}

View File

@ -83,10 +83,10 @@ func CheckFreeSpace(minFree Size, usage fs.Usage) error {
if minFree.Percentage() {
freePct := (float64(usage.Free) / float64(usage.Total)) * 100
if freePct < val {
return fmt.Errorf("%.1f %% < %v", freePct, minFree)
return fmt.Errorf("current %.2f %% < required %v", freePct, minFree)
}
} else if float64(usage.Free) < val {
return fmt.Errorf("%sB < %v", formatSI(usage.Free), minFree)
return fmt.Errorf("current %sB < required %v", formatSI(usage.Free), minFree)
}
return nil
@ -94,12 +94,12 @@ func CheckFreeSpace(minFree Size, usage fs.Usage) error {
// 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 {
func checkAvailableSpace(req uint64, minFree Size, usage fs.Usage) error {
if usage.Free < req {
return false
return fmt.Errorf("current %sB < required %sB", formatSI(usage.Free), formatSI(req))
}
usage.Free -= req
return CheckFreeSpace(minFree, usage) == nil
return CheckFreeSpace(minFree, usage)
}
func formatSI(b uint64) string {

View File

@ -159,8 +159,10 @@ func TestCheckAvailableSize(t *testing.T) {
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)
err = checkAvailableSpace(tc.req, minFree, usage)
t.Log(err)
if (err == nil) != tc.ok {
t.Errorf("checkAvailableSpace(%v, %v, %v) == %v, expected %v", tc.req, minFree, usage, err, tc.ok)
}
}
}

View File

@ -329,7 +329,7 @@ func (f *folder) getHealthErrorWithoutIgnores() error {
dbPath := locations.Get(locations.Database)
if usage, err := fs.NewFilesystem(fs.FilesystemTypeBasic, dbPath).Usage("."); err == nil {
if err = config.CheckFreeSpace(f.model.cfg.Options().MinHomeDiskFree, usage); err != nil {
return errors.Wrapf(err, "insufficient space on disk for database (%v)", dbPath)
return fmt.Errorf("insufficient space on disk for database (%v): %w", dbPath, err)
}
}