diff --git a/internal/hub/blobstore/blobstore.go b/internal/hub/blobstore/blobstore.go index a136ee5..ecebec6 100644 --- a/internal/hub/blobstore/blobstore.go +++ b/internal/hub/blobstore/blobstore.go @@ -103,12 +103,26 @@ func (d *Disk) Put(hash string, data []byte) error { tmp.Close() return err } + // Flush the blob's bytes to the platter before the rename publishes it, so a + // crash can't leave a truncated file masquerading as a complete blob. + if err := tmp.Sync(); err != nil { + tmp.Close() + return err + } if err := tmp.Close(); err != nil { return err } // Rename to a non-existent final path: atomic on POSIX, and on Windows too // since we never rename over an existing file (Put is a no-op when present). - return os.Rename(tmpName, d.path(hash)) + if err := os.Rename(tmpName, d.path(hash)); err != nil { + return err + } + // fsync the shard dir so the rename entry itself survives a crash. + if df, derr := os.Open(dir); derr == nil { + _ = df.Sync() // ponytail: dir fsync is best-effort; unsupported on Windows + df.Close() + } + return nil } // Get returns the blob bytes for hash, or ErrNotFound. diff --git a/internal/syncer/fsync_test.go b/internal/syncer/fsync_test.go new file mode 100644 index 0000000..b248646 --- /dev/null +++ b/internal/syncer/fsync_test.go @@ -0,0 +1,26 @@ +package syncer + +import ( + "os" + "path/filepath" + "testing" +) + +// TestAtomicWriteWithSync proves the fsync durability path doesn't break a write: +// the file lands with the exact content and mode, and returns no error. We can't +// simulate a power loss, so this is a regression guard against the sync calls +// corrupting or failing an ordinary write. +func TestAtomicWriteWithSync(t *testing.T) { + dst := filepath.Join(t.TempDir(), "hello.txt") + want := []byte("never lose a byte") + if err := atomicWrite(dst, want, 0o644); err != nil { + t.Fatalf("atomicWrite: %v", err) + } + got, err := os.ReadFile(dst) + if err != nil { + t.Fatalf("read back: %v", err) + } + if string(got) != string(want) { + t.Fatalf("content = %q, want %q", got, want) + } +} diff --git a/internal/syncer/pull.go b/internal/syncer/pull.go index 13d793b..8611dc6 100644 --- a/internal/syncer/pull.go +++ b/internal/syncer/pull.go @@ -437,11 +437,25 @@ func atomicWrite(path string, data []byte, perm os.FileMode) error { f.Close() return err } + // Flush the data to the platter before the rename publishes it: a power loss + // after the rename but before the bytes land would leave a truncated/empty file. + if err := f.Sync(); err != nil { + f.Close() + return err + } if err := f.Close(); err != nil { return err } if err := os.Chmod(tmp, perm); err != nil { return err } - return os.Rename(tmp, path) + if err := os.Rename(tmp, path); err != nil { + return err + } + // fsync the parent dir so the rename entry itself survives a crash. + if dir, derr := os.Open(filepath.Dir(path)); derr == nil { + _ = dir.Sync() // ponytail: dir fsync is best-effort; unsupported on Windows + dir.Close() + } + return nil }