Files
Anya Tokugawa d5d21b7841
Build and publish container images / images (push) Successful in 9m34s
Release container images / release (push) Successful in 36s
Refactor architecture into eight components and consolidate reliability work
Extract Grammaratur, Dregarnuhr, Geduldh, Mestionora, Ordoschnellic, Schutzaria, Kunstzeal and Seheweit behind compatible application contracts.

Include accumulated editor resilience, navigation and theme UX, remote CLI, storage/schema and DR groundwork, tests and design/operations documentation.

Verified: uncached full Go suite; go vet ./...; race tests for all eight components; 11 targeted Chromium tests; Node suites; 19 Python CLI tests; reproducible editor build. No push, release tag or deployment in this checkpoint.
2026-11-01 15:23:31 +09:00

265 lines
6.3 KiB
Go

package geduldh
import (
"context"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"io"
"math"
"os"
"path"
"path/filepath"
"strings"
)
// Local requires hard-link no-replace publication and directory fsync support.
// A mounted NFS/SMB/SSHFS share is not certified just because it has a path.
// Failed persistence primitives return errors; there is no unsafe rename fallback.
type Local struct{ root string }
var _ Store = (*Local)(nil)
// NewLocal has no I/O side effects. The root must be provisioned by the caller.
func NewLocal(root string) (*Local, error) {
if strings.TrimSpace(root) == "" {
return nil, errors.New("Document root is required")
}
absolute, err := filepath.Abs(root)
if err != nil {
return nil, err
}
return &Local{root: absolute}, nil
}
func (s *Local) Open(ctx context.Context, hash string) (Reader, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
key, err := Key(hash)
if err != nil {
return nil, err
}
root, err := os.OpenRoot(s.root)
if err != nil {
return nil, err
}
defer root.Close()
file, err := root.Open(key)
if err != nil {
return nil, err
}
info, err := file.Stat()
if err != nil || !info.Mode().IsRegular() {
file.Close()
if err != nil {
return nil, err
}
return nil, errors.New("Document is not a regular file")
}
return &localReader{file: file, ctx: ctx}, nil
}
func (s *Local) Stat(ctx context.Context, hash string) (Info, error) {
reader, err := s.Open(ctx, hash)
if err != nil {
return Info{}, err
}
defer reader.Close()
info, err := reader.(*localReader).file.Stat()
if err != nil {
return Info{}, err
}
return Info{Hash: strings.ToLower(strings.TrimSpace(hash)), Size: info.Size()}, nil
}
func verify(ctx context.Context, hash string, reader io.Reader) error {
sum := sha256.New()
if _, err := io.CopyBuffer(sum, contextReader{ctx, reader}, make([]byte, 64<<10)); err != nil {
return err
}
if hex.EncodeToString(sum.Sum(nil)) != strings.ToLower(strings.TrimSpace(hash)) {
return ErrHashMismatch
}
return ctx.Err()
}
func (s *Local) Verify(ctx context.Context, hash string) error {
reader, err := s.Open(ctx, hash)
if err != nil {
return err
}
defer reader.Close()
return verify(ctx, hash, reader)
}
func (s *Local) PutIfAbsent(ctx context.Context, hash string, size int64, input io.Reader) error {
if err := ctx.Err(); err != nil {
return err
}
key, err := Key(hash)
if err != nil {
return err
}
if size < 0 || size == math.MaxInt64 {
return ErrSizeMismatch
}
if input == nil {
return errors.New("Document reader is required")
}
root, err := os.OpenRoot(s.root)
if err != nil {
return err
}
defer root.Close()
// The common retry/backfill path must not rewrite or stage existing blobs.
// Still validate the supplied bytes, then verify the stored content.
if existing, err := root.Open(key); err == nil {
defer existing.Close()
info, err := existing.Stat()
if err != nil {
return err
}
if !info.Mode().IsRegular() {
return errors.New("Document is not a regular file")
}
if err := copyValidated(ctx, hash, size, input, io.Discard); err != nil {
return err
}
if err := verify(ctx, hash, existing); err != nil {
return err
}
return syncPublishedDirectories(root, path.Dir(key))
} else if !errors.Is(err, os.ErrNotExist) {
return err
}
dir := path.Dir(key)
if err := root.MkdirAll(dir, 0755); err != nil {
return err
}
var nonce [16]byte
if _, err := rand.Read(nonce[:]); err != nil {
return err
}
tmp := dir + "/.document-" + hex.EncodeToString(nonce[:])
file, err := root.OpenFile(tmp, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0600)
if err != nil {
return err
}
defer root.Remove(tmp)
defer file.Close()
if err := copyValidated(ctx, hash, size, input, file); err != nil {
return err
}
if err := file.Chmod(0644); err != nil {
return err
}
if err := file.Sync(); err != nil {
return err
}
if err := file.Close(); err != nil {
return err
}
// Link is atomic and fails if the destination exists. Rename would silently
// replace an existing payload between an existence check and publication.
if err := root.Link(tmp, key); err != nil {
if !errors.Is(err, os.ErrExist) {
return err
}
existing, err := root.Open(key)
if err != nil {
return err
}
defer existing.Close()
info, err := existing.Stat()
if err != nil {
return err
}
if !info.Mode().IsRegular() {
return errors.New("Document is not a regular file")
}
if err := verify(ctx, hash, existing); err != nil {
return fmt.Errorf("existing Document: %w", err)
}
}
// Persist the new entry and newly created shard directories before allowing
// a metadata transaction to reference the payload. Errors are not swallowed.
if err := syncPublishedDirectories(root, dir); err != nil {
return err
}
return ctx.Err()
}
func syncPublishedDirectories(root *os.Root, shard string) error {
for _, name := range []string{shard, "sha256", "."} {
directory, err := root.Open(name)
if err != nil {
return err
}
err = directory.Sync()
closeErr := directory.Close()
if err != nil {
return err
}
if closeErr != nil {
return closeErr
}
}
return nil
}
func copyValidated(ctx context.Context, hash string, size int64, input io.Reader, output io.Writer) error {
sum := sha256.New()
n, err := io.CopyBuffer(io.MultiWriter(output, sum), contextReader{ctx, io.LimitReader(input, size+1)}, make([]byte, 64<<10))
if err != nil {
return err
}
if n != size {
return ErrSizeMismatch
}
if hex.EncodeToString(sum.Sum(nil)) != strings.ToLower(strings.TrimSpace(hash)) {
return ErrHashMismatch
}
return ctx.Err()
}
type contextReader struct {
ctx context.Context
reader io.Reader
}
func (r contextReader) Read(p []byte) (int, error) {
if err := r.ctx.Err(); err != nil {
return 0, err
}
return r.reader.Read(p)
}
type localReader struct {
file *os.File
ctx context.Context
}
func (r *localReader) Close() error { return r.file.Close() }
func (r *localReader) Read(p []byte) (int, error) {
if err := r.ctx.Err(); err != nil {
return 0, err
}
return r.file.Read(p)
}
func (r *localReader) ReadAt(p []byte, offset int64) (int, error) {
if err := r.ctx.Err(); err != nil {
return 0, err
}
return r.file.ReadAt(p, offset)
}
func (r *localReader) Seek(offset int64, whence int) (int64, error) {
if err := r.ctx.Err(); err != nil {
return 0, err
}
return r.file.Seek(offset, whence)
}