package app import ( "context" "crypto/sha256" "encoding/hex" "fmt" "io" "path/filepath" "strings" "gitea.maximumdirect.net/eric/narratio/internal/adapters/storage" "gitea.maximumdirect.net/eric/narratio/internal/artifacts" "gitea.maximumdirect.net/eric/narratio/internal/audio" "gitea.maximumdirect.net/eric/narratio/internal/config" "gitea.maximumdirect.net/eric/narratio/internal/fileops" "gitea.maximumdirect.net/eric/narratio/internal/manifest" ) // RestoreExecutionResult captures concrete file-install results for one restore execution. type RestoreExecutionResult struct { DownloadedCount int } func executeRestorePlan( ctx context.Context, cfg *config.Config, current *RemoteCurrentState, plan *RestorePlan, report *RestoreReport, store storage.ObjectStore, ) (*RestoreExecutionResult, error) { if cfg == nil || cfg.Pipeline == nil || cfg.Session == nil { return nil, fmt.Errorf("resolved config with pipeline/session is required") } if current == nil { return nil, fmt.Errorf("remote current state is required") } if plan == nil { return nil, fmt.Errorf("restore plan is required") } if store == nil { return nil, fmt.Errorf("remote object store is required") } sessionRoot := artifacts.SessionWorkDirForCampaign(cfg.Pipeline.Workspace.Root, cfg.Session.Campaign, cfg.Session.SessionID) manifestActions := make([]RestoreAction, 0, 1) actions := make([]RestoreAction, 0, len(plan.Actions)) for _, action := range plan.Actions { if action.Kind != RestoreActionDownload { continue } if action.LocalRelativePath == config.PathManifestFile { manifestActions = append(manifestActions, action) continue } actions = append(actions, action) } if len(manifestActions) > 1 { return nil, fmt.Errorf("restore plan includes multiple manifest download actions") } if len(manifestActions) == 1 { actions = append(actions, manifestActions[0]) } result := &RestoreExecutionResult{} for _, action := range actions { if err := executeRestoreDownloadAction(ctx, cfg, sessionRoot, current, action, store); err != nil { if report != nil { report.markFailed(action, err) } return nil, fmt.Errorf("install %q from %q: %w", action.LocalRelativePath, action.RemoteKey, err) } if report != nil { report.markDownloaded(action) } result.DownloadedCount++ } return result, nil } func executeRestoreDownloadAction( ctx context.Context, cfg *config.Config, sessionRoot string, current *RemoteCurrentState, action RestoreAction, store storage.ObjectStore, ) error { safeLocalPath, err := joinWithinSessionRoot(sessionRoot, action.LocalRelativePath) if err != nil { return fmt.Errorf("resolve safe local path: %w", err) } if strings.TrimSpace(action.LocalPath) != "" && filepath.Clean(action.LocalPath) != safeLocalPath { return fmt.Errorf("restore plan local path mismatch for %q", action.LocalRelativePath) } if restoreActionIsAudio(action) { return executeRestoreAudioAction(ctx, cfg, safeLocalPath, action, store) } if err := fileops.EnsureWorkspaceDirectory(filepath.Dir(safeLocalPath)); err != nil { return fmt.Errorf("create destination directory: %w", err) } temporary, err := fileops.DownloadToSiblingTemp(safeLocalPath, func(destination io.Writer) error { return storage.DownloadTo(ctx, store, action.RemoteKey, destination) }) if err != nil { return fmt.Errorf("download to temp file: %w", err) } defer func() { _ = temporary.Cleanup() }() if err := verifyRestoredObject(ctx, store, action, temporary); err != nil { return err } if isRestoreManifest(action.LocalRelativePath) { file, err := temporary.Open() if err != nil { return fmt.Errorf("open restored manifest: %w", err) } destinationRoot := sessionRoot requireCurrentIdentity := action.LocalRelativePath == config.PathManifestFile if !requireCurrentIdentity { destinationRoot = filepath.Dir(safeLocalPath) } restored, prepareErr := prepareRestoredManifest(ctx, cfg, current, file, destinationRoot, requireCurrentIdentity) closeErr := file.Close() if prepareErr != nil { return prepareErr } if closeErr != nil { return fmt.Errorf("close restored manifest: %w", closeErr) } if err := (&manifest.LocalStore{}).Save(ctx, safeLocalPath, restored); err != nil { return fmt.Errorf("install rebased manifest atomically: %w", err) } return nil } if err := temporary.Install(filepath.Base(safeLocalPath), fileops.WorkspaceFileMode); err != nil { return fmt.Errorf("install file atomically: %w", err) } return nil } func isRestoreManifest(relativePath string) bool { clean := filepath.ToSlash(filepath.Clean(strings.TrimSpace(relativePath))) return clean == config.PathManifestFile || clean == config.PathPreviousDirSegment+"/"+config.PathManifestFile } func verifyRestoredObject(ctx context.Context, store storage.ObjectStore, action RestoreAction, temporary *fileops.DownloadedTempFile) error { if strings.TrimSpace(action.SHA256) == "" && strings.TrimSpace(action.Generation) == "" { return nil } if strings.TrimSpace(action.SHA256) == "" || strings.TrimSpace(action.Generation) == "" { return fmt.Errorf("committed object identity for %q is incomplete", action.RemoteKey) } file, err := temporary.Open() if err != nil { return fmt.Errorf("open downloaded object for verification: %w", err) } digest := sha256.New() count, copyErr := io.Copy(digest, file) closeErr := file.Close() if copyErr != nil { return fmt.Errorf("checksum downloaded object: %w", copyErr) } if closeErr != nil { return fmt.Errorf("close downloaded object: %w", closeErr) } if count != action.Size { return fmt.Errorf("committed object size mismatch for %q: got %d, want %d", action.RemoteKey, count, action.Size) } if got := hex.EncodeToString(digest.Sum(nil)); got != action.SHA256 { return fmt.Errorf("committed object checksum mismatch for %q: got %s, want %s", action.RemoteKey, got, action.SHA256) } objects, err := store.List(ctx, action.RemoteKey) if err != nil { return fmt.Errorf("read committed object identity for %q: %w", action.RemoteKey, err) } var found *storage.ObjectInfo for _, object := range objects { if normalizeRemoteKey(object.Key) != normalizeRemoteKey(action.RemoteKey) { continue } if found != nil { return fmt.Errorf("committed object %q is ambiguous", action.RemoteKey) } copy := object found = © } if found == nil { return fmt.Errorf("committed object %q is missing", action.RemoteKey) } if found.Size != action.Size || strings.TrimSpace(found.ETag) != action.Generation { return fmt.Errorf("committed object generation mismatch for %q", action.RemoteKey) } return nil } func executeRestoreAudioAction( ctx context.Context, cfg *config.Config, safeLocalPath string, action RestoreAction, store storage.ObjectStore, ) error { if cfg == nil || cfg.Pipeline == nil || cfg.Pipeline.Storage.S3 == nil || cfg.Session == nil { return fmt.Errorf("resolved s3 config and session are required") } spoolDir := artifacts.SessionSpoolRestoreAudioDir(cfg.Pipeline.Spool.Root, cfg.Session.Campaign, cfg.Session.SessionID) spoolPath := filepath.Join(spoolDir, filepath.Base(safeLocalPath)) cacheEnabled := cfg.Pipeline.Cache.S3Audio == nil || *cfg.Pipeline.Cache.S3Audio _, err := audio.MaterializeS3Audio(ctx, audio.S3MaterializeRequest{ Store: store, Object: storage.ObjectInfo{ Key: action.RemoteKey, Size: action.Size, ETag: action.ETag, }, Bucket: strings.TrimSpace(cfg.Pipeline.Storage.S3.Bucket), CacheRoot: strings.TrimSpace(cfg.Pipeline.Cache.Root), CacheEnabled: cacheEnabled, SpoolPath: spoolPath, DestPath: safeLocalPath, }) if err != nil { return fmt.Errorf("materialize audio: %w", err) } return nil }