Preflight report output filenames

This commit is contained in:
2026-08-13 02:33:32 +00:00
parent 44ee389334
commit f4e3a6f26c
6 changed files with 76 additions and 3 deletions

View File

@@ -8,11 +8,28 @@ import (
"path/filepath"
)
const (
maxFileNameBytes = 255
temporaryFilePattern = ".weatherreporter-*.tmp"
)
// ValidateAtomicPath verifies that the final path can be safely used with this
// package's same-directory atomic-write implementation.
func ValidateAtomicPath(path string) error {
if len(filepath.Base(path)) > maxFileNameBytes {
return fmt.Errorf("final file name exceeds the %d-byte limit", maxFileNameBytes)
}
return nil
}
func WriteFileAtomic(path string, data []byte) error {
if err := ValidateAtomicPath(path); err != nil {
return err
}
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return fmt.Errorf("create directory %q: %w", filepath.Dir(path), err)
}
tmp, err := os.CreateTemp(filepath.Dir(path), "."+filepath.Base(path)+".*.tmp")
tmp, err := os.CreateTemp(filepath.Dir(path), temporaryFilePattern)
if err != nil {
return fmt.Errorf("create temporary file for %q: %w", path, err)
}