-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide richer data about disk space usage (#537)
- Loading branch information
Showing
12 changed files
with
691 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,65 @@ | ||
package space | ||
|
||
import ( | ||
"golang.org/x/sys/unix" | ||
) | ||
import "github.com/mtlynch/picoshare/v2/space/checkers" | ||
|
||
type ( | ||
FileSystemChecker interface { | ||
MeasureUsage() (checkers.PicoShareUsage, error) | ||
} | ||
|
||
DatabaseChecker interface { | ||
TotalSize() (uint64, error) | ||
} | ||
|
||
Checker struct { | ||
dataDir string | ||
fsChecker FileSystemChecker | ||
dbChecker DatabaseChecker | ||
} | ||
|
||
CheckResult struct { | ||
AvailableBytes uint64 | ||
TotalBytes uint64 | ||
Usage struct { | ||
// TotalServingBytes represents the sum total of the bytes of file data that | ||
// PicoShare has of file uploads in the database. This is just file bytes | ||
// and does not include PicoShare-specific metadata about the files. | ||
TotalServingBytes uint64 | ||
// DatabaseFileSize represents the total number of bytes on the filesystem | ||
// dedicated to storing PicoShare's SQLite database files. | ||
DatabaseFileSize uint64 | ||
// FileSystemUsedBytes represents total bytes in use on the filesystem where | ||
// PicoShare's database files are located. This represents the total of all | ||
// used bytes on the filesystem, not just PicoShare. | ||
FileSystemUsedBytes uint64 | ||
// FileSystemTotalBytes represents the total bytes available on the | ||
// filesystem where PicoShare's database files are located. | ||
FileSystemTotalBytes uint64 | ||
} | ||
) | ||
|
||
func NewChecker(dir string) Checker { | ||
return Checker{dir} | ||
func NewChecker(dbPath string, dbReader checkers.DatabaseMetadataReader) Checker { | ||
return NewCheckerFromCheckers(checkers.NewFileSystemChecker(dbPath), checkers.NewDatabaseChecker(dbReader)) | ||
} | ||
|
||
func (c Checker) Check() (CheckResult, error) { | ||
var stat unix.Statfs_t | ||
if err := unix.Statfs(c.dataDir, &stat); err != nil { | ||
return CheckResult{}, err | ||
func NewCheckerFromCheckers(fsChecker FileSystemChecker, dbChecker DatabaseChecker) Checker { | ||
return Checker{ | ||
fsChecker, | ||
dbChecker, | ||
} | ||
} | ||
|
||
func (c Checker) Check() (Usage, error) { | ||
fsUsage, err := c.fsChecker.MeasureUsage() | ||
if err != nil { | ||
return Usage{}, err | ||
} | ||
|
||
dbTotalSize, err := c.dbChecker.TotalSize() | ||
if err != nil { | ||
return Usage{}, err | ||
} | ||
|
||
return CheckResult{ | ||
AvailableBytes: stat.Bfree * uint64(stat.Bsize), | ||
TotalBytes: stat.Blocks * uint64(stat.Bsize), | ||
return Usage{ | ||
TotalServingBytes: dbTotalSize, | ||
DatabaseFileSize: fsUsage.PicoShareDbFileSize, | ||
FileSystemUsedBytes: fsUsage.UsedBytes, | ||
FileSystemTotalBytes: fsUsage.TotalBytes, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package space_test | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/mtlynch/picoshare/v2/space" | ||
"github.com/mtlynch/picoshare/v2/space/checkers" | ||
) | ||
|
||
type mockFileSystemChecker struct { | ||
usage checkers.PicoShareUsage | ||
err error | ||
} | ||
|
||
func (c mockFileSystemChecker) MeasureUsage() (checkers.PicoShareUsage, error) { | ||
return c.usage, c.err | ||
} | ||
|
||
type mockDatabaseChecker struct { | ||
totalSize uint64 | ||
err error | ||
} | ||
|
||
func (c mockDatabaseChecker) TotalSize() (uint64, error) { | ||
return c.totalSize, c.err | ||
} | ||
|
||
func TestCheck(t *testing.T) { | ||
dummyFileSystemErr := errors.New("dummy filesystem checker error") | ||
dummyDatabaseErr := errors.New("dummy database checker error") | ||
for _, tt := range []struct { | ||
description string | ||
fsUsage checkers.PicoShareUsage | ||
fsErr error | ||
dbUsage uint64 | ||
dbErr error | ||
usageExpected space.Usage | ||
errExpected error | ||
}{ | ||
{ | ||
description: "aggregates checker results correctly", | ||
fsUsage: checkers.PicoShareUsage{ | ||
FileSystemUsage: checkers.FileSystemUsage{ | ||
UsedBytes: 70, | ||
TotalBytes: 100, | ||
}, | ||
PicoShareDbFileSize: 65, | ||
}, | ||
fsErr: nil, | ||
dbUsage: 60, | ||
dbErr: nil, | ||
usageExpected: space.Usage{ | ||
TotalServingBytes: 60, | ||
DatabaseFileSize: 65, | ||
FileSystemUsedBytes: 70, | ||
FileSystemTotalBytes: 100, | ||
}, | ||
errExpected: nil, | ||
}, | ||
{ | ||
description: "returns error when filesystem checker fails", | ||
fsUsage: checkers.PicoShareUsage{}, | ||
fsErr: dummyFileSystemErr, | ||
dbUsage: 5, | ||
dbErr: nil, | ||
usageExpected: space.Usage{}, | ||
errExpected: dummyFileSystemErr, | ||
}, | ||
{ | ||
description: "returns error when database checker fails", | ||
fsUsage: checkers.PicoShareUsage{ | ||
PicoShareDbFileSize: 3, | ||
FileSystemUsage: checkers.FileSystemUsage{ | ||
UsedBytes: 5, | ||
TotalBytes: 7, | ||
}, | ||
}, | ||
fsErr: nil, | ||
dbUsage: 0, | ||
dbErr: dummyDatabaseErr, | ||
usageExpected: space.Usage{}, | ||
errExpected: dummyDatabaseErr, | ||
}, | ||
} { | ||
t.Run(tt.description, func(t *testing.T) { | ||
fsc := mockFileSystemChecker{ | ||
usage: tt.fsUsage, | ||
err: tt.fsErr, | ||
} | ||
dbc := mockDatabaseChecker{ | ||
totalSize: tt.dbUsage, | ||
err: tt.dbErr, | ||
} | ||
|
||
usage, err := space.NewCheckerFromCheckers(fsc, dbc).Check() | ||
if got, want := err, tt.errExpected; got != want { | ||
t.Fatalf("err=%v, want=%v", got, want) | ||
} | ||
|
||
if got, want := usage, tt.usageExpected; got != want { | ||
t.Errorf("usage=%+v, want=%+v", got, want) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.