From 8b37e6e04035f9f8a3d01701dae121cdc67cbf43 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Mon, 27 Apr 2026 08:27:28 +0300 Subject: 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. --- internal/processor/processor.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'internal/processor/processor.go') 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) } } } -- cgit v1.2.3