Tcl Source Code

View Ticket
Login
Ticket UUID: daba77ae1d24afbd93bf09efd3e0ed3fb4eaaab8
Title: [file delete -force] fails with "file name too long" on trees deeper than PATH_MAX (Unix)
Type: Bug Created on: 2026-10-17 10:51:11
Submitter: serhiy.storchaka Assigned to:
Subsystem: 37. File System Severity: Minor
Priority: 5 Medium Last modified: 2026-10-20 18:11:29
Status: Open Closed by:
Resolution: None Closed on:
Version: 8.6.18, 9.0.5, 9.1.0
Description:

On Unix, [file delete -force] cannot remove a directory tree that is deeper than PATH_MAX (4096 bytes on Linux). rm -rf removes it without trouble. Such trees can be created by Tcl itself. For example, [file copy 1 1/2] (ticket [82049f96f8]) recurses until "file name too long" and leaves a deep 1/2/1/2/... tree behind, which [file delete -force] then cannot remove.

set top [file join [pwd] deep]
# 2100 nested directories "d", built with relative mkdir/cd
exec sh -c {mkdir "$1" && cd "$1" && i=0 &&
    while [ $i -lt 2100 ]; do mkdir d && cd -P d || exit 1; i=$((i+1)); done} sh $top
catch {file delete -force $top} msg
puts $msg
puts [file exists $top]
exec rm -rf $top

Actual: error deleting "/.../deep/d/d/.../d": file name too long and the tree is still there (1).

Expected: the tree is removed (0), as with rm -rf.

Cause: TraverseUnixTree() in unix/tclUnixFCmd.c builds full path names. With HAVE_FTS it runs fts_open(..., FTS_PHYSICAL | FTS_NOCHDIR | ...), so fts accesses each entry by its full path. The non-fts branch does the same with opendir()/lstat(). TraversalDelete() also removes entries by their full path. Once that path exceeds PATH_MAX, lstat() fails with ENAMETOOLONG and the deletion stops. A possible fix is a traversal relative to directory file descriptors (openat()/fstatat()/unlinkat()), as rm does, at least for deletion.

Reproduced on Linux with 8.4.20, 8.5.0, 8.5.19, 8.6.0, 8.6.18, 9.0.5 and main. So it is an old limitation, not a regression.

User Comments:
serhiy.storchaka added on 2026-10-17 13:17:31:

Proposed fix in branch unix-delete-deep-tree: the recursive delete works relative to directory descriptors (openat()/unlinkat(), no chdir()), as rm does, and does not follow symlinks. Test unixFCmd-bug-daba77ae1d. Copying such a tree ([82049f96f8]) still fails.


serhiy.storchaka added on 2026-10-17 14:15:15:

The branch unix-delete-deep-tree now also skips entries that someone else deletes meanwhile. Before, the deletion stopped at such an entry and left the rest of the tree behind, while [file delete -force] still returned OK. Test unixFCmd-22.2 (the deep tree test is now unixFCmd-22.1).