Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 4cd8cba03101afdb2187a869c7716fde82dc2e1113d15b9a4f856f82f948d403
Ticket: daba77ae1d24afbd93bf09efd3e0ed3fb4eaaab8
[file delete -force] fails with "file name too long" on trees deeper than PATH_MAX (Unix)
User & Date: serhiy.storchaka on 2026-10-17 10:51:11
Changes

  1. cmimetype changed to: "text/x-fossil-wiki"
  2. comment changed to:
    On Unix, <nowiki>[file delete -force]</nowiki> cannot remove a directory tree that is deeper than <tt>PATH_MAX</tt> (4096 bytes on Linux). <tt>rm -rf</tt> removes it without trouble. Such trees can be created by Tcl itself. For example, <nowiki>[file copy 1 1/2]</nowiki> (ticket [82049f96f8]) recurses until "file name too long" and leaves a deep <tt>1/2/1/2/...</tt> tree behind, which <nowiki>[file delete -force]</nowiki> then cannot remove.
    
    <verbatim>
    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
    </verbatim>
    
    Actual: <tt>error deleting "/.../deep/d/d/.../d": file name too long</tt> and the tree is still there (<tt>1</tt>).
    
    Expected: the tree is removed (<tt>0</tt>), as with <tt>rm -rf</tt>.
    
    Cause: <tt>TraverseUnixTree()</tt> in unix/tclUnixFCmd.c builds full path names. With <tt>HAVE_FTS</tt> it runs <tt>fts_open(..., FTS_PHYSICAL | FTS_NOCHDIR | ...)</tt>, so fts accesses each entry by its full path. The non-fts branch does the same with <tt>opendir()</tt>/<tt>lstat()</tt>. <tt>TraversalDelete()</tt> also removes entries by their full path. Once that path exceeds <tt>PATH_MAX</tt>, <tt>lstat()</tt> fails with <tt>ENAMETOOLONG</tt> and the deletion stops. A possible fix is a traversal relative to directory file descriptors (<tt>openat()</tt>/<tt>fstatat()</tt>/<tt>unlinkat()</tt>), as <tt>rm</tt> 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.
    
  3. foundin changed to: "8.6.18, 9.0.5, 9.1.0"
  4. login: "serhiy.storchaka"
  5. priority changed to: "5 Medium"
  6. resolution changed to: "None"
  7. severity changed to: "Minor"
  8. status changed to: "Open"
  9. subsystem changed to: "37. File System"
  10. title changed to:
    [file delete -force] fails with "file name too long" on trees deeper than PATH_MAX (Unix)
    
  11. type changed to: "Bug"