Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Issue with [file normalize] on Linux with symlink wtih ".."

Open3577801fffffffffffffffffffffffffffffffffCreated 14 years agoUpdated 44 days ago
Descriptionplain2012-10-17 09:28:35
Happend using latest Tcl 8.6b3 and 8.5.12. Code to reproduce (on Ubuntu Linux, ext3, but I doubt it matters).

set d [file join [file dirname [info script]] test]
catch {file delete -force $d}
file mkdir $d/d

close [open $d/f w]
close [open $d/d/f2 w]

exec ln -s f2 $d/d/l2
exec ln -s ../f $d/d/l

puts [vfs::filesystem fullynormalize $d/f]
puts [vfs::filesystem fullynormalize $d/d/l]
puts [vfs::filesystem fullynormalize $d/d/l2]
Referenced by3
  • 616cef19e5Ticket [3183016fff] cd .. from link in Windows confuses Tcl status still Open with 4 other changes ticket2026-10-19 18:44:30
  • 7c2e96e549Ticket [3570244fff] "cd .." behaves wrong if last element in pwd is a symlink status still Open with 4 other changes ticket2026-10-19 18:44:30
  • 854be12c0aFix [3577801fff], [3570244fff], [3183016fff] and [3099612fff]: on Unix, [file normalize] and [cd ..] used the target of a link literally when ".." followed it, so "..", "." or a trailing "/" in the target or a chain of links gave a wrong path. Normalize the target first, as realpath() does. On Windows, resolve ".." lexically, as the system does, so that [pwd], [glob] and native paths agree after [cd ..] from a junction or a symbolic link. Potential incompatibility on Windows: [file normalize junction/..] is now the parent of the junction, not of its target. check-in2026-10-19 18:37:17
zoro2plain2012-10-17 18:50:04
I have updated the script to show that the issue is with [file normalize] itself:

set d [file join [file dirname [info script]] test]
catch {file delete -force $d}
file mkdir $d/d

close [open $d/f w]
close [open $d/d/f2 w]

exec ln -s f2 $d/d/l2
exec ln -s ../f $d/d/l

puts [vfs::filesystem fullynormalize $d/f]
puts [file normalize $d/f]

puts ""
puts [vfs::filesystem fullynormalize $d/d/l]
# the first one returns test/d/f; second reports test/f
puts [file normalize $d/d/l/../../f]
puts [file normalize $d/d/l2/../../f]

puts ""
puts [vfs::filesystem fullynormalize $d/d/l2]
# the first one returns test/f2; second reports test/d/f2
puts [file normalize $d/d/l/../f2]
puts [file normalize $d/d/l2/../f2]
dgp Ticket [3577801fff]: 1 change [History diff]2012-10-17 19:31:25
serhiy.storchakawiki2026-10-19 18:44:30

Still present in 8.6, 9.0 and 9.1: when ".." follows a link, the link target is used literally, so ".." or "." in the target, a trailing "/" or a chain of links give a wrong result (d/l/.. with l -> ../x gives d/..).

Proposed fix in branch normalize-dotdot-after-link, together with [3570244fff], [3183016fff] and [3099612fff]: on Unix the link target is normalized and further links are followed before backing up, so the results agree with realpath(). Tests filesystem-1.55 to 1.58. The last component of a path is still not resolved, as documented.

Log in (anonymous is fine) to add a comment.