Skip to content

Commit b0a1ca3

Browse files
committed
Use os.DirFS + fs.WalkDir in listTempFiles to confine listing to .entire/tmp/
Replaces os.ReadDir with os.DirFS + fs.WalkDir so directory listing is scoped to the temp directory, consistent with deleteTempFiles. Signed-off-by: Paulo Gomes <paulo@entire.io> Assisted-by: Claude Opus 4.6 <noreply@anthropic.com> Entire-Checkpoint: fd7de86795d7
1 parent 1848ba8 commit b0a1ca3

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

cmd/entire/cli/clean.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"io"
8+
"io/fs"
89
"os"
910
"strings"
1011

@@ -425,19 +426,13 @@ func runCleanAllWithItems(ctx context.Context, w io.Writer, force, dryRun bool,
425426

426427
// listTempFiles returns files in .entire/tmp/ that are safe to delete,
427428
// excluding files belonging to active sessions.
429+
// Uses os.DirFS + fs.WalkDir to confine listing to the temp directory.
428430
func listTempFiles(ctx context.Context) ([]string, error) {
429-
tmpDir, err := paths.AbsPath(ctx, paths.EntireTmpDir)
430-
if err != nil {
431-
return nil, fmt.Errorf("failed to get temp dir path: %w", err)
432-
}
433-
434-
entries, err := os.ReadDir(tmpDir)
435-
if os.IsNotExist(err) {
436-
return nil, nil
437-
}
431+
root, err := os.OpenRoot(paths.EntireTmpDir)
438432
if err != nil {
439-
return nil, fmt.Errorf("failed to read temp dir: %w", err)
433+
return nil, fmt.Errorf("failed to open root: %w", err)
440434
}
435+
defer root.Close()
441436

442437
// Build set of active session IDs to protect their temp files
443438
activeSessionIDs := make(map[string]bool)
@@ -448,17 +443,27 @@ func listTempFiles(ctx context.Context) ([]string, error) {
448443
}
449444

450445
var files []string
451-
for _, entry := range entries {
452-
if entry.IsDir() {
453-
continue
446+
err = fs.WalkDir(root.FS(), ".", func(_ string, d fs.DirEntry, err error) error {
447+
if err != nil {
448+
return err
449+
}
450+
if d.IsDir() {
451+
return nil
454452
}
455453
// Skip temp files belonging to active sessions (e.g., "session-id.json")
456-
name := entry.Name()
454+
name := d.Name()
457455
sessionID := strings.TrimSuffix(name, ".json")
458456
if sessionID != name && activeSessionIDs[sessionID] {
459-
continue
457+
return nil
460458
}
461459
files = append(files, name)
460+
return nil
461+
})
462+
if os.IsNotExist(err) {
463+
return nil, nil
464+
}
465+
if err != nil {
466+
return nil, fmt.Errorf("failed to list temp dir: %w", err)
462467
}
463468
return files, nil
464469
}

0 commit comments

Comments
 (0)