26 lines
871 B
Go

package state
import (
"fmt"
"os"
"path/filepath"
"time"
)
// quarantineCorruptFile runs one orchestration or CLI step.
// Signature: quarantineCorruptFile(path string, payload []byte, replacement []byte, mode os.FileMode) error.
// Why: keeps behavior explicit so startup/shutdown workflows remain maintainable as services evolve.
func quarantineCorruptFile(path string, payload []byte, replacement []byte, mode os.FileMode) error {
if err := os.MkdirAll(filepath.Dir(path), 0o750); err != nil {
return err
}
backup := fmt.Sprintf("%s.corrupt-%s", path, time.Now().UTC().Format("20060102T150405Z"))
if err := os.WriteFile(backup, payload, 0o600); err != nil {
return fmt.Errorf("write backup %s: %w", backup, err)
}
if err := os.WriteFile(path, replacement, mode); err != nil {
return fmt.Errorf("write replacement %s: %w", path, err)
}
return nil
}