diff options
| author | Paul Buetow <paul@buetow.org> | 2026-04-27 08:27:28 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-04-27 08:27:28 +0300 |
| commit | 8b37e6e04035f9f8a3d01701dae121cdc67cbf43 (patch) | |
| tree | cc9b7ddbadbb401eb957c38cac96b5819e503dda /internal/processor/processor.go | |
| parent | f8f17653e66bd2cd90c6a2cf7970afee54c643dc (diff) | |
processor: fix uniqueID to return error instead of infinite loop on Stat errors
Previously, uniqueID(postsDir, t) returned only a string and looped
forever when os.Stat returned an error other than IsNotExist
(e.g. permission denied). Change the signature to (string, error),
propagate the error, and update the caller in processFile to handle
it. Add positive and negative tests covering new ID generation,
suffix collision, and permission-based stat failure.
Diffstat (limited to 'internal/processor/processor.go')
| -rw-r--r-- | internal/processor/processor.go | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/internal/processor/processor.go b/internal/processor/processor.go index 43f5023..d781a8b 100644 --- a/internal/processor/processor.go +++ b/internal/processor/processor.go @@ -103,7 +103,10 @@ func claimedByMarkdown(entries []os.DirEntry, inputDir string) (map[string]bool, // The source file is removed from the input dir on success. func processFile(srcPath, postsDir string) error { now := time.Now().UTC() - id := uniqueID(postsDir, now) + id, err := uniqueID(postsDir, now) + if err != nil { + return fmt.Errorf("generate unique ID: %w", err) + } postDir := filepath.Join(postsDir, id) if err := os.MkdirAll(postDir, 0o755); err != nil { @@ -260,11 +263,15 @@ func copyLocalImages(filenames []string, sourceDir, postDir string) ([]string, e // uniqueID generates a post ID for the given time that does not already exist // as a directory under postsDir. Appends a numeric suffix if needed. -func uniqueID(postsDir string, t time.Time) string { +func uniqueID(postsDir string, t time.Time) (string, error) { for i := 0; ; i++ { id := post.NewID(t, i) - if _, err := os.Stat(filepath.Join(postsDir, id)); os.IsNotExist(err) { - return id + _, err := os.Stat(filepath.Join(postsDir, id)) + if err != nil { + if os.IsNotExist(err) { + return id, nil + } + return "", fmt.Errorf("stat post dir %s: %w", id, err) } } } |
