devbox mode (#474): the spool counts blocks, not bytes
Every spool entry (file, directory, symlink) costs at least one 4 KiB block, in the upload scan and the usage walk alike, so an archive of empty files can no longer use up inodes while the byte count stays at zero.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+56
−12
internal/devbox/registrar.go+34 −9internal/devbox/registrar_test.go+22 −3
Intent
Attributed to this commit: 6.78 g CO₂e (822,670 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on design-devbox-mode, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.
diff --git a/internal/devbox/registrar.go b/internal/devbox/registrar.go| index 6e266034d..d0df84fa8 100644 |
| --- a/internal/devbox/registrar.go |
| +++ b/internal/devbox/registrar.go |
| @@ -63,6 +63,10 @@ const ( |
| // bytes, so without this a small archive of empty files could use up |
| // the disk's inodes. |
| maxFolderEntries = 100_000 |
| + // spoolBlock is what every spool entry costs at least, file, directory |
| + // or symlink: the disk spends a block and an inode on each, so a byte |
| + // count alone would let an archive of empty files fill the disk. |
| + spoolBlock = 4096 |
| // maxJSONBytes caps the JSON request bodies. |
| maxJSONBytes = 64 << 10 |
| // uploadReadTimeout bounds how long one upload may take to arrive, so a |
| @@ -577,7 +581,16 @@ func pruneSnapshots(dir, keep, prev string) { |
| } |
| } |
| -// spoolUsage is the bytes of regular files under root. WalkDir does not |
| +// blocks is what an entry of size bytes costs the spool: its size rounded |
| +// up to whole blocks, and never less than one block. |
| +func blocks(size int64) int64 { |
| + if size <= spoolBlock { |
| + return spoolBlock |
| + } |
| + return (size + spoolBlock - 1) / spoolBlock * spoolBlock |
| +} |
| + |
| +// spoolUsage is what everything under root costs (blocks). WalkDir does not |
| // follow symlinks, so a link in the spool counts only itself. |
| func spoolUsage(root string) (int64, error) { |
| var total int64 |
| @@ -588,20 +601,26 @@ func spoolUsage(root string) (int64, error) { |
| } |
| return err |
| } |
| + if p == root { |
| + return nil |
| + } |
| + var size int64 |
| if d.Type().IsRegular() { |
| fi, err := d.Info() |
| if err != nil { |
| return err |
| } |
| - total += fi.Size() |
| + size = fi.Size() |
| } |
| + total += blocks(size) |
| return nil |
| }) |
| return total, err |
| } |
| // scanSite reads a site.tar.gz the way edge.ExtractSite will, without |
| -// writing anything, and returns the bytes it would write. It refuses what |
| +// writing anything, and returns what the snapshot will cost the spool |
| +// (blocks, as spoolUsage counts it), the snapshot directory included. It refuses what |
| // ExtractSite would refuse (bad-request), and an archive that would unpack |
| // past limit or hold too many entries (folder-too-big). Decompression stops |
| // as soon as the declared sizes pass limit, so a gzip bomb costs little. |
| @@ -612,7 +631,7 @@ func scanSite(data []byte, limit int64) (int64, *refusal) { |
| } |
| defer gz.Close() |
| tr := tar.NewReader(gz) |
| - var total int64 |
| + total := int64(spoolBlock) // the snapshot directory itself |
| for entries := 0; ; entries++ { |
| hdr, err := tr.Next() |
| if err == io.EOF { |
| @@ -629,18 +648,24 @@ func scanSite(data []byte, limit int64) (int64, *refusal) { |
| return 0, refuse(http.StatusBadRequest, CodeBadRequest) |
| } |
| switch hdr.Typeflag { |
| - case tar.TypeDir, tar.TypeXGlobalHeader: |
| + case tar.TypeXGlobalHeader: |
| + continue |
| + case tar.TypeDir: |
| + // A directory ExtractSite makes implicitly for a file costs a |
| + // block too, and is not in the archive. Counting each file's |
| + // parents would be exact; the entry cap bounds the gap instead. |
| + total += spoolBlock |
| case tar.TypeReg: |
| if hdr.Size < 0 { |
| return 0, refuse(http.StatusBadRequest, CodeBadRequest) |
| } |
| - total += hdr.Size |
| - if total > limit { |
| - return 0, refuse(http.StatusRequestEntityTooLarge, CodeFolderTooBig) |
| - } |
| + total += blocks(hdr.Size) |
| default: |
| return 0, refuse(http.StatusBadRequest, CodeBadRequest) |
| } |
| + if total > limit { |
| + return 0, refuse(http.StatusRequestEntityTooLarge, CodeFolderTooBig) |
| + } |
| } |
| } |
diff --git a/internal/devbox/registrar_test.go b/internal/devbox/registrar_test.go| index 8805702e2..79f69dbcd 100644 |
| --- a/internal/devbox/registrar_test.go |
| +++ b/internal/devbox/registrar_test.go |
| @@ -563,7 +563,9 @@ func TestShareFolderOverCapRefused(t *testing.T) { |
| func TestSpoolFullRefused(t *testing.T) { |
| f := newFixture(t) |
| - f.r.SpoolCap = 3000 |
| + // The spool counts blocks: "one" costs its dir, current, the snapshot |
| + // dir and a.bin, a block each (16 KiB). A second upload needs two more. |
| + f.r.SpoolCap = 20000 |
| wantOK(t, f.shareFolder(t, 1001, "one", packSite(t, map[string]string{"a.bin": strings.Repeat("a", 2000)}))) |
| wantCode(t, f.shareFolder(t, 1002, "two", packSite(t, map[string]string{"b.bin": strings.Repeat("b", 2000)})), CodeSpoolFull) |
| if _, err := os.Stat(filepath.Join(f.spool, "two")); err == nil { |
| @@ -577,12 +579,29 @@ func TestSpoolFullRefused(t *testing.T) { |
| if rt := f.row(t, "two"); rt != nil { |
| t.Fatal("row written") |
| } |
| - // Re-sharing a folder counts the kept previous tree too (R7): a.bin |
| - // twice already fills the 3000-byte cap. |
| + // Re-sharing counts the trees already there, which go only after the |
| + // swap: the same upload again does not fit. |
| wantCode(t, f.shareFolder(t, 1001, "one", packSite(t, map[string]string{"a.bin": strings.Repeat("c", 2000)})), CodeSpoolFull) |
| if got := readCurrent(t, f.spool, "one", "a.bin"); got[0] != 'a' { |
| t.Fatal("current changed on a refused upload") |
| } |
| + |
| + // Empty files are not free: each costs a block, so an archive of many |
| + // empty files cannot fill the disk's inodes for nothing. |
| + g := newFixture(t) |
| + g.r.SpoolCap = 64 << 10 |
| + empty := map[string]string{} |
| + for i := 0; i < 20; i++ { |
| + empty[fmt.Sprintf("e%02d", i)] = "" |
| + } |
| + wantOK(t, g.shareFolder(t, 1001, "few", packSite(t, map[string]string{"e": ""}))) |
| + for i := 20; i < 40; i++ { |
| + empty[fmt.Sprintf("e%02d", i)] = "" |
| + } |
| + wantCode(t, g.shareFolder(t, 1001, "many", packSite(t, empty)), CodeFolderTooBig) |
| + if _, err := os.Stat(filepath.Join(g.spool, "many")); err == nil { |
| + t.Fatal("refused upload left a spool dir") |
| + } |
| } |
| func TestShareFolderRefusesEscapes(t *testing.T) { |