<i>[file delete -force] fails with "file name too long" on trees deeper than PATH_MAX (Unix)</i>.
by
.
D 2026-10-17T10:51:11.957
J cmimetype text/x-fossil-wiki
J comment On\sUnix,\s<nowiki>[file\sdelete\s-force]</nowiki>\scannot\sremove\sa\sdirectory\stree\sthat\sis\sdeeper\sthan\s<tt>PATH_MAX</tt>\s(4096\sbytes\son\sLinux).\s<tt>rm\s-rf</tt>\sremoves\sit\swithout\strouble.\sSuch\strees\scan\sbe\screated\sby\sTcl\sitself.\sFor\sexample,\s<nowiki>[file\scopy\s1\s1/2]</nowiki>\s(ticket\s[82049f96f8])\srecurses\suntil\s"file\sname\stoo\slong"\sand\sleaves\sa\sdeep\s<tt>1/2/1/2/...</tt>\stree\sbehind,\swhich\s<nowiki>[file\sdelete\s-force]</nowiki>\sthen\scannot\sremove.\n\n<verbatim>\nset\stop\s[file\sjoin\s[pwd]\sdeep]\n#\s2100\snested\sdirectories\s"d",\sbuilt\swith\srelative\smkdir/cd\nexec\ssh\s-c\s{mkdir\s"$1"\s&&\scd\s"$1"\s&&\si=0\s&&\n\s\s\s\swhile\s[\s$i\s-lt\s2100\s];\sdo\smkdir\sd\s&&\scd\s-P\sd\s||\sexit\s1;\si=$((i+1));\sdone}\ssh\s$top\ncatch\s{file\sdelete\s-force\s$top}\smsg\nputs\s$msg\nputs\s[file\sexists\s$top]\nexec\srm\s-rf\s$top\n</verbatim>\n\nActual:\s<tt>error\sdeleting\s"/.../deep/d/d/.../d":\sfile\sname\stoo\slong</tt>\sand\sthe\stree\sis\sstill\sthere\s(<tt>1</tt>).\n\nExpected:\sthe\stree\sis\sremoved\s(<tt>0</tt>),\sas\swith\s<tt>rm\s-rf</tt>.\n\nCause:\s<tt>TraverseUnixTree()</tt>\sin\sunix/tclUnixFCmd.c\sbuilds\sfull\spath\snames.\sWith\s<tt>HAVE_FTS</tt>\sit\sruns\s<tt>fts_open(...,\sFTS_PHYSICAL\s|\sFTS_NOCHDIR\s|\s...)</tt>,\sso\sfts\saccesses\seach\sentry\sby\sits\sfull\spath.\sThe\snon-fts\sbranch\sdoes\sthe\ssame\swith\s<tt>opendir()</tt>/<tt>lstat()</tt>.\s<tt>TraversalDelete()</tt>\salso\sremoves\sentries\sby\stheir\sfull\spath.\sOnce\sthat\spath\sexceeds\s<tt>PATH_MAX</tt>,\s<tt>lstat()</tt>\sfails\swith\s<tt>ENAMETOOLONG</tt>\sand\sthe\sdeletion\sstops.\sA\spossible\sfix\sis\sa\straversal\srelative\sto\sdirectory\sfile\sdescriptors\s(<tt>openat()</tt>/<tt>fstatat()</tt>/<tt>unlinkat()</tt>),\sas\s<tt>rm</tt>\sdoes,\sat\sleast\sfor\sdeletion.\n\nReproduced\son\sLinux\swith\s8.4.20,\s8.5.0,\s8.5.19,\s8.6.0,\s8.6.18,\s9.0.5\sand\smain.\sSo\sit\sis\san\sold\slimitation,\snot\sa\sregression.
J foundin 8.6.18,\s9.0.5,\s9.1.0
J login serhiy.storchaka
J priority 5\sMedium
J resolution None
J severity Minor
J status Open
J subsystem 37.\sFile\sSystem
J title [file\sdelete\s-force]\sfails\swith\s"file\sname\stoo\slong"\son\strees\sdeeper\sthan\sPATH_MAX\s(Unix)
J type Bug
K daba77ae1d24afbd93bf09efd3e0ed3fb4eaaab8
U serhiy.storchaka
Z 3d9090c646f3a5aa396d6b6986b31972