Implements Increment 1 of `docs/superpowers/specs/2026-07-04-crucible-depot-core-design.md`: a new stdlib-only `internal/depot` package wired into the dispatcher. - `crucible depot status|push|verify|get|pull` with backends `local`/`cdn`/`bunny`; exit contract `0` clean / `1` drift / `2` unconfirmed-only / `64` usage / `70` internal. - Presence is always probed against the real target (IPv4 1-byte range GET; HEAD is banned with a regression-tripwire test). `unconfirmed` is a distinct state, never collapsed into `missing`. - No prompting anywhere: missing `BUNNY_STORAGE_*` env fails closed (read path included), enforced by a no-stdin test. - Uploads: probe-then-PUT with `Checksum: <UPPER-sha>`; read/write key split (`BUNNY_STORAGE_READ_PASSWORD` falls back to `BUNNY_STORAGE_PASSWORD`). - Field-driven fix included: per-probe transient retry (curl `--retry 2` equivalent) — without it a real 1490-blob CDN sweep reported 1222 false-unconfirmed; with it, 1490/1490 present in 74s, exit 0. - Registry: depot `Wired: true`, joins the interactive menu; stale "(SeaweedFS)" wording removed. Tests: unit + httptest fake-Bunny (probe sequence, Checksum header, key split, 428 throttling → exit 2) + local→bunny integration (drift → push → clean → idempotent no-second-PUT; incremental pull). `make check` green. **Merge ordering:** this merges FIRST; the companion `sow-assets-manifest#crucible-depot-cutover` PR needs its flake input bumped to include this. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Reviewed-on: #30 Reviewed-by: xtul <mpiasecki720@protonmail.com> Co-authored-by: vickydotbat <vickydotbat@tutamail.com> Co-committed-by: vickydotbat <vickydotbat@tutamail.com>
114 lines
2.4 KiB
Go
114 lines
2.4 KiB
Go
package depot
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
// LocalBackend implements Backend for local filesystem storage.
|
|
type LocalBackend struct {
|
|
Root string
|
|
}
|
|
|
|
// Name returns the backend name.
|
|
func (b *LocalBackend) Name() string {
|
|
return "local"
|
|
}
|
|
|
|
// Probe checks if a blob exists.
|
|
func (b *LocalBackend) Probe(ctx context.Context, sha string) (ProbeState, bool, error) {
|
|
blobPath := filepath.Join(b.Root, BlobKey(sha))
|
|
_, err := os.Stat(blobPath)
|
|
if err == nil {
|
|
return Present, false, nil
|
|
}
|
|
if os.IsNotExist(err) {
|
|
return Absent, false, nil
|
|
}
|
|
// Real I/O error (permission, etc.)
|
|
return Absent, false, err
|
|
}
|
|
|
|
// Put copies the file from src to the blob storage.
|
|
func (b *LocalBackend) Put(ctx context.Context, sha, src string) error {
|
|
blobPath := filepath.Join(b.Root, BlobKey(sha))
|
|
blobDir := filepath.Dir(blobPath)
|
|
|
|
// Create parent directories
|
|
if err := os.MkdirAll(blobDir, 0755); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Create temp file in the same directory for atomic rename
|
|
tmpFile, err := os.CreateTemp(blobDir, ".tmp-")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer os.Remove(tmpFile.Name())
|
|
|
|
// Copy source to temp file
|
|
srcFile, err := os.Open(src)
|
|
if err != nil {
|
|
tmpFile.Close()
|
|
return err
|
|
}
|
|
defer srcFile.Close()
|
|
|
|
_, err = io.Copy(tmpFile, srcFile)
|
|
tmpFile.Close()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Atomic rename
|
|
return os.Rename(tmpFile.Name(), blobPath)
|
|
}
|
|
|
|
// Get fetches the blob, re-hashes it, and deletes it if the hash doesn't match.
|
|
func (b *LocalBackend) Get(ctx context.Context, sha, dest string) error {
|
|
blobPath := filepath.Join(b.Root, BlobKey(sha))
|
|
|
|
// Create temp file in dest directory for atomic rename
|
|
destDir := filepath.Dir(dest)
|
|
if err := os.MkdirAll(destDir, 0755); err != nil {
|
|
return err
|
|
}
|
|
|
|
tmpFile, err := os.CreateTemp(destDir, ".tmp-")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer os.Remove(tmpFile.Name())
|
|
|
|
// Copy and hash simultaneously
|
|
srcFile, err := os.Open(blobPath)
|
|
if err != nil {
|
|
tmpFile.Close()
|
|
return err
|
|
}
|
|
defer srcFile.Close()
|
|
|
|
hasher := sha256.New()
|
|
teeReader := io.TeeReader(srcFile, hasher)
|
|
|
|
_, err = io.Copy(tmpFile, teeReader)
|
|
tmpFile.Close()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Check hash
|
|
gotHash := fmt.Sprintf("%x", hasher.Sum(nil))
|
|
if gotHash != sha {
|
|
os.Remove(tmpFile.Name())
|
|
return fmt.Errorf("hash mismatch: expected %s, got %s", sha, gotHash)
|
|
}
|
|
|
|
// Atomic rename
|
|
return os.Rename(tmpFile.Name(), dest)
|
|
}
|