Files
aego/asset/pipeline/source_fingerprint.go
sanspie 50be5c04bc
ci-platforms / darwin amd64 smoke (push) Skipped
GitHub / linux amd64 native GitHub-hosted native CI: success
ci-platforms / windows amd64 smoke (push) Skipped
GitHub / windows amd64 native GitHub-hosted native CI: success
GitHub / darwin arm64 native GitHub-hosted native CI: success
GitHub / darwin amd64 native GitHub-hosted native CI: success
ci / checks (push) Failing after 1h21m34s
ci / native-authoring (push) Skipped
ci-platforms / darwin arm64 smoke (push) Skipped
github-native / gitea-pending (push) Skipped
github-native / darwin amd64 native (push) Skipped
github-native / linux amd64 native (push) Skipped
github-native / windows amd64 native (push) Skipped
github-native / darwin arm64 native (push) Skipped
github-native / gitea-result (push) Skipped
ci / build (amd64, linux, ubuntu-24.04) (push) Successful in 12m4s
ci / release (push) Skipped
Stage 12.5: Art Workspace and asset authoring
Add dockable Art tools, animation and previews, guarded asset workflows,
and the Web Asset Studio built on the shared Art Core. Expand English and
Russian documentation, interactive Try scenes, examples and editor icons.

Add audio waveform navigation, source analysis, guarded previews and a
browser Worker mixer. Preserve source changes and recovery with bounded
checks, and strengthen native, browser and interoperability CI.

Prepare version 0.12.5 and manually built native artifact candidates.
Fix the production site image demo build dependency.
Reuse immutable scene indices when capturing keyframes.
Wait for real Persist save/load completion in component tests.
Stabilize browser Game View captures outside pointer hover.
Aseprite interchange and persistent Audio EDL recovery remain limited;
the artifact workflow does not publish releases or change stable.
2026-11-15 10:37:50 +03:00

226 lines
7.2 KiB
Go

package pipeline
import (
"context"
"crypto/sha256"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path"
"path/filepath"
"strings"
"aego/core/ids"
)
// MaxSourceFingerprintBytes bounds a single optional streaming fingerprint.
// It does not raise the existing source transaction, importer or recovery
// limits, nor does it permit a caller to write the fingerprinted source.
const MaxSourceFingerprintBytes int64 = 256 << 20
// SourceFingerprint distinguishes a missing path from an existing empty file.
// It describes one observed byte sequence, not a lock or an atomic CAS.
type SourceFingerprint struct {
Exists bool
Size int64
SHA256 [sha256.Size]byte
}
// FingerprintSource streams a bounded project source without loading it into
// memory. It refuses pending recovery and symlinks, and makes best-effort
// identity and metadata checks for changes during the read. Those checks cannot
// detect every ABA or same-size rewrite with restored timestamps. Other
// processes do not share this mutex: a caller must still arrange a guarded
// publication and recheck all related inputs. Cancellation is cooperative and
// cannot interrupt a file read or mutex wait.
func (p *Pipeline) FingerprintSource(ctx context.Context, rel string, limit int64) (SourceFingerprint, error) {
if p == nil || ctx == nil || limit < 0 || limit > MaxSourceFingerprintBytes {
return SourceFingerprint{}, errors.New("source fingerprint needs a context and a bounded limit")
}
p.mu.RLock()
defer p.mu.RUnlock()
if err := p.requireNoPendingSourceRecovery(); err != nil {
return SourceFingerprint{}, err
}
root, err := os.OpenRoot(p.root)
if err != nil {
return SourceFingerprint{}, err
}
defer root.Close()
return p.fingerprintSource(ctx, root, rel, limit, nil)
}
// VerifyGuardedPreviewSource rechecks one private preview's indexed source,
// metadata and original project root after background preparation. It does not
// publish or modify anything. The caller must still check its document and
// playback generations; external file checks are best-effort, not atomic CAS.
func (p *Pipeline) VerifyGuardedPreviewSource(ctx context.Context, id ids.AssetID, rel string, expected SourceFingerprint) error {
if p == nil || ctx == nil || id.IsZero() || !expected.Exists || expected.Size < 0 || expected.Size > MaxSourceFingerprintBytes {
return errors.New("guarded preview needs a bounded indexed source fingerprint")
}
if err := ctx.Err(); err != nil {
return err
}
p.mu.RLock()
defer p.mu.RUnlock()
if p.unlock == nil {
return errors.New("guarded preview pipeline is closed")
}
r := p.byID[id]
if r == nil || r.path != rel || p.byPath[rel] != r {
return errors.New("guarded preview source identity changed")
}
if err := p.requireNoPendingSourceRecovery(); err != nil {
return err
}
root, err := os.OpenRoot(p.root)
if err != nil {
return err
}
defer root.Close()
opened, err := root.Stat(".")
if err != nil {
return err
}
checkRoot := func() error {
current, err := os.Stat(p.root)
if err != nil || p.rootInfo == nil || !opened.IsDir() || !current.IsDir() ||
!os.SameFile(p.rootInfo, opened) || !os.SameFile(opened, current) {
return errors.New("guarded preview project root changed")
}
return nil
}
if err := checkRoot(); err != nil {
return err
}
if err := p.checkMetadataSnapshot(r); err != nil {
return err
}
got, err := p.fingerprintSource(ctx, root, rel, expected.Size, nil)
if err != nil {
return err
}
if got != expected {
return errors.New("guarded preview source changed")
}
if err := p.requireNoPendingSourceRecovery(); err != nil {
return err
}
if err := p.checkMetadataSnapshot(r); err != nil {
return err
}
if err := checkRoot(); err != nil {
return err
}
return ctx.Err()
}
// The test checkpoint runs only after the stream has been read, allowing tests
// to exercise replacement detection deterministically; production passes nil.
func (p *Pipeline) fingerprintSource(ctx context.Context, root *os.Root, rel string, limit int64, checkpoint func()) (SourceFingerprint, error) {
if ctx == nil || root == nil || limit < 0 || limit > MaxSourceFingerprintBytes {
return SourceFingerprint{}, errors.New("source fingerprint needs a context and a bounded limit")
}
if err := ctx.Err(); err != nil {
return SourceFingerprint{}, err
}
if !fs.ValidPath(rel) || !filepath.IsLocal(rel) || strings.ContainsAny(rel, "\\:#") || !p.inRoots(rel) {
return SourceFingerprint{}, fmt.Errorf("not a project source path: %s", rel)
}
parts := strings.Split(rel, "/")
infos := make([]os.FileInfo, 0, len(parts))
current := ""
for i, part := range parts {
if strings.HasPrefix(part, ".") {
return SourceFingerprint{}, fmt.Errorf("hidden asset path: %s", rel)
}
current = path.Join(current, part)
info, err := root.Lstat(current)
if errors.Is(err, os.ErrNotExist) {
return SourceFingerprint{}, nil
}
if err != nil {
return SourceFingerprint{}, err
}
if info.Mode()&os.ModeSymlink != 0 || (i < len(parts)-1 && !info.IsDir()) || (i == len(parts)-1 && !info.Mode().IsRegular()) {
return SourceFingerprint{}, fmt.Errorf("unsafe source path: %s", rel)
}
infos = append(infos, info)
}
initial := infos[len(infos)-1]
if initial.Size() > limit {
return SourceFingerprint{}, fmt.Errorf("%s exceeds %d bytes", rel, limit)
}
f, err := root.Open(rel)
if err != nil {
return SourceFingerprint{}, err
}
defer f.Close()
opened, err := f.Stat()
if err != nil || !sameFingerprintFile(initial, opened) {
return SourceFingerprint{}, fmt.Errorf("%s changed before fingerprinting", rel)
}
h := sha256.New()
buf := make([]byte, 64<<10)
var size int64
for {
if err := ctx.Err(); err != nil {
return SourceFingerprint{}, err
}
n, readErr := f.Read(buf)
if n > 0 {
size += int64(n)
if size > limit {
return SourceFingerprint{}, fmt.Errorf("%s exceeds %d bytes", rel, limit)
}
_, _ = h.Write(buf[:n])
}
if errors.Is(readErr, io.EOF) {
break
}
if readErr != nil {
return SourceFingerprint{}, readErr
}
if n == 0 {
return SourceFingerprint{}, io.ErrNoProgress
}
}
if checkpoint != nil {
checkpoint()
}
if err := ctx.Err(); err != nil {
return SourceFingerprint{}, err
}
after, err := f.Stat()
if err != nil || size != initial.Size() || !sameFingerprintFile(initial, after) {
return SourceFingerprint{}, fmt.Errorf("%s changed while fingerprinting", rel)
}
current = ""
for i, part := range parts {
current = path.Join(current, part)
info, err := root.Lstat(current)
if err != nil || info.Mode()&os.ModeSymlink != 0 || !os.SameFile(infos[i], info) || infos[i].Mode() != info.Mode() {
return SourceFingerprint{}, fmt.Errorf("%s changed while fingerprinting", rel)
}
if i == len(parts)-1 && !sameFingerprintFile(initial, info) {
return SourceFingerprint{}, fmt.Errorf("%s changed while fingerprinting", rel)
}
}
if err := ctx.Err(); err != nil {
return SourceFingerprint{}, err
}
if err := f.Close(); err != nil {
return SourceFingerprint{}, err
}
result := SourceFingerprint{Exists: true, Size: size}
copy(result.SHA256[:], h.Sum(nil))
return result, nil
}
func sameFingerprintFile(a, b os.FileInfo) bool {
return a != nil && b != nil && a.Mode().IsRegular() && b.Mode().IsRegular() && os.SameFile(a, b) &&
a.Mode() == b.Mode() && a.Size() == b.Size() && a.ModTime().Equal(b.ModTime())
}