Preflight report output filenames
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user