23 lines
598 B
Go
23 lines
598 B
Go
package state
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
)
|
|
|
|
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
|
|
}
|