carlos / platform Public

assetcache: free space (Statfs, ErrUnsupported, DefaultBudget)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 71fee2f860117aaf165683ee46fc9e87ceb0876c parent 4449a6b
5 files changed, +96 −0
  • internal/assetcache/fs.go +22 −0
  • internal/assetcache/fs_linux_test.go +20 −0
  • internal/assetcache/fs_test.go +22 −0
  • internal/assetcache/statfs_linux.go +20 −0
  • internal/assetcache/statfs_other.go +12 −0

CI failed — run details

diff --git a/internal/assetcache/fs.go b/internal/assetcache/fs.go
index 34de260a3..7f44ba32e 100644
--- a/internal/assetcache/fs.go
+++ b/internal/assetcache/fs.go
@@ -1,5 +1,7 @@
package assetcache
+import "errors"
+
// FS is the store's view of the filesystem's free space. The Linux
// implementation is statfs; tests may supply a fake.
type FS interface {
@@ -7,3 +9,23 @@ type FS interface {
// available to an unprivileged writer.
Space(dir string) (total, free uint64, err error)
}
+
+// ErrUnsupported is returned by Statfs on systems where free space is not
+// measured.
+var ErrUnsupported = errors.New("assetcache: free space unknown on this system")
+
+const (
+ gib = 1 << 30
+
+ // maxDefaultBudget is the largest budget DefaultBudget returns.
+ maxDefaultBudget = 4 * gib
+)
+
+// DefaultBudget is the cache budget used when none is configured: the smaller
+// of 4 GiB and a tenth of the filesystem's size.
+func DefaultBudget(total uint64) int64 {
+ if total/10 >= maxDefaultBudget {
+ return maxDefaultBudget
+ }
+ return int64(total / 10)
+}
diff --git a/internal/assetcache/fs_linux_test.go b/internal/assetcache/fs_linux_test.go
new file mode 100644
index 000000000..120044d48
--- /dev/null
+++ b/internal/assetcache/fs_linux_test.go
@@ -0,0 +1,20 @@
+//go:build linux
+
+package assetcache
+
+import "testing"
+
+// TestStatfsReportsTheTempDir is the acceptance test for the real FS: it asks
+// the kernel about a real directory.
+func TestStatfsReportsTheTempDir(t *testing.T) {
+ total, free, err := Statfs.Space(t.TempDir())
+ if err != nil {
+ t.Fatal(err)
+ }
+ if total == 0 {
+ t.Fatal("total = 0, want the filesystem's size")
+ }
+ if free > total {
+ t.Fatalf("free %d > total %d", free, total)
+ }
+}
diff --git a/internal/assetcache/fs_test.go b/internal/assetcache/fs_test.go
new file mode 100644
index 000000000..5199e6a86
--- /dev/null
+++ b/internal/assetcache/fs_test.go
@@ -0,0 +1,22 @@
+package assetcache
+
+import "testing"
+
+func TestDefaultBudget(t *testing.T) {
+ const gib = int64(1) << 30
+ cases := []struct {
+ name string
+ total uint64
+ want int64
+ }{
+ {"20 GiB gives 2 GiB", uint64(20 * gib), 2 * gib},
+ {"60 GiB gives 4 GiB", uint64(60 * gib), 4 * gib},
+ {"40 GiB is exactly the cap", uint64(40 * gib), 4 * gib},
+ {"zero gives zero", 0, 0},
+ }
+ for _, c := range cases {
+ if got := DefaultBudget(c.total); got != c.want {
+ t.Errorf("%s: DefaultBudget(%d) = %d, want %d", c.name, c.total, got, c.want)
+ }
+ }
+}
diff --git a/internal/assetcache/statfs_linux.go b/internal/assetcache/statfs_linux.go
new file mode 100644
index 000000000..60de911e7
--- /dev/null
+++ b/internal/assetcache/statfs_linux.go
@@ -0,0 +1,20 @@
+//go:build linux
+
+package assetcache
+
+import "golang.org/x/sys/unix"
+
+type statfsFS struct{}
+
+// Statfs is the real FS: the kernel's answer for the filesystem holding dir.
+// free counts the blocks an unprivileged writer may use (Bavail).
+var Statfs FS = statfsFS{}
+
+func (statfsFS) Space(dir string) (total, free uint64, err error) {
+ var st unix.Statfs_t
+ if err := unix.Statfs(dir, &st); err != nil {
+ return 0, 0, err
+ }
+ bsize := uint64(st.Bsize)
+ return st.Blocks * bsize, st.Bavail * bsize, nil
+}
diff --git a/internal/assetcache/statfs_other.go b/internal/assetcache/statfs_other.go
new file mode 100644
index 000000000..c84d03195
--- /dev/null
+++ b/internal/assetcache/statfs_other.go
@@ -0,0 +1,12 @@
+//go:build !linux
+
+package assetcache
+
+type statfsFS struct{}
+
+// Statfs reports ErrUnsupported: free space is only measured on Linux.
+var Statfs FS = statfsFS{}
+
+func (statfsFS) Space(string) (total, free uint64, err error) {
+ return 0, 0, ErrUnsupported
+}