| Ticket UUID: | 678662 | ||
| Title: | Tcl needs 'file normalize -full' and similar C api. | ||
| Type: | RFE | Created on: | 2003-02-01 13:44:10 |
| Submitter: | jcw | Assigned to: | vincentdarley |
| Subsystem: | 37. File System | Severity: | |
| Priority: | 5 Medium | Last modified: | 2026-11-13 11:18:45 |
| Status: | Closed | Closed by: | serhiy.storchaka |
| Resolution: | Rejected | Closed on: | 2026-11-13 11:18:43 |
| Version: | None | ||
| Description: | ||||
Below a transcript which I can not figure out. All based on the latest CVS, Feb 1. FYI, I added a debug puts to tclkit's boot.tcl, showing the mount command it sets up on startup: try $ mkdir foo try $ cd foo/ foo $ ln -s ../tclkit bar foo $ cd .. try $ foo/bar vfs::filesystem mount /home/shared/basekit/tclkit/try/foo/bar ::vfs::mk4::handler exe application-specific initialization failed: unknown encoding "iso8859-1" % vfs::filesystem info /home/shared/basekit/tclkit/tclkit % info nameofexe /home/shared/basekit/tclkit/try/foo/bar % pwd /home/shared/basekit/tclkit/try % The weird part is the output of "vfs::filesystem info". That path was how tclkit was originally built, but I've moved it inside "try/", so it does not exist. Note that the symlink is relative, it has a ".." in it. This is a tricky case. There seems to be something hosed, I'm checking into it. In the same case as above - watch this: try $ ./tclkit vfs::filesystem mount /home/shared/basekit/tclkit/try/tclkit ::vfs::mk4::handler exe % file readlink foo/bar ../tclkit % file size foo/bar 3011296 % open foo/bar file4 % open [file readlink foo/bar] couldn't open "../tclkit": no such file or directory Everything appears to be ok, this shows that open and open/readlink are not identical (logical, though perhaps not expected). Explanation: the ".." in the symlink would need to be resolved against the dir in which the symlink resides. Now compare with running the same tclkit executable through a symlink: try $ foo/bar vfs::filesystem mount /home/shared/basekit/tclkit/try/foo/bar ::vfs::mk4::handler exe application-specific initialization failed: unknown encoding "iso8859-1" % file readlink foo/bar ../tclkit % file size foo/bar 3011296 % open foo/bar file4 % seek file4 0 end % tell file4 3011296 % open [file readlink foo/bar] mk1 % read mk1 % file stat foo/bar sb % array get sb mtime 1044103928 atime 1044103927 gid 100 nlink 1 mode 33261 type file ctime 1044103953 uid 500 ino 273302 size 3011296 dev 778 % seek mk1 0 end error during seek on "mk1": Success % tell mk1 -1 % This second case shows that the original path still looks like a file (the tclkit executable), but opening the dereferenced symlink ends up being inside VFS. I don't understand what it is, the "file" seems to be bogus (rightly so maybe, since the thing *should* be a mountpoint now, i.e. a dir). Looks like a couple of things are not right. I'm not sure exactly *what* the correct results for all these cases should be, but wanted to pass this on. It all came about when I noticed that symlinked starkits did not mount properly. I started adding some code, but it's getting hairy. Would it be better if "file readlink" returned a path which was fully resolved? The way it is now, it looks like translating a symlink to the real file will require some nasty path trickery. What I can think of so far to deal with relative symlinks on Unix is: set f [file join [file dirname $f] [file readlink $f]] Yikes... | ||||
| User Comments: | ||||
jcw added on 2003-02-01 21:23:52:
Logged In: YES user_id=1983 Got it... I think. Same example as before: % file normalize foo/bar /home/shared/basekit/tclkit/try/foo/bar % file readlink foo/bar ../tclkit % file normalize [file readlink foo/bar] /home/shared/basekit/tclkit/tclkit % FWIW, the resolved symlink points to: /home/shared/basekit/tclkit/try/tclkit Nothing in the above is technically wrong. But it's what is causing the "vfs::filesystem info" to report a bad path, somewhere in tclvfs. IMO, the potential confusion is important enough to treat this as a core issue, not just tclvfs. vincentdarley added on 2003-02-05 01:10:26:
Logged In: YES user_id=32170 Isn't this a bug in VfsFullyNormalizePath in tclvfs/vfs.c. What does that function give when passed your try/foo/bar path? I don't believe it is mounting the thing in the right place. In principle it would actually be more helpful if Tcl provided an API to return a fully normalized path, but, currently it doesn't. (In retrospect this was a bit of an oversight of TIP 17). vincentdarley added on 2003-02-05 01:25:23:
Logged In: YES user_id=32170 Oops sorry. Isn't there are clear bug in your 'file normalize [file readlink foo/bar]' Or should that line instead be 'file normalize [file join [pwd] [file readlink foo/bar]]' jcw added on 2003-02-05 01:37:11:
Logged In: YES user_id=1983 Yes, this is indeed (on purpose) naively wrong: - file normalize [file readlink foo/bar]' But the solution you offer does not work, I think: - file normalize [file join [pwd] [file readlink foo/bar]] Explanation: readlink will return "../tclkit". Join that to pwd, which is try, and you end up with /home/shared/tclkit/tclkit This is not rigth either: the point is that symlinks resolve relatively to where *they* are, not to what my pwd happens to be. This is the essence of the whole problem, IMO. Methinks we may need a better file normalize (or perhaps "file normalize -full") which does symlink expansion. This is all solvable, but I'm afraid the current situation will trip people. Well, at least one person... me. Heh. vincentdarley added on 2003-02-05 01:46:09:
Logged In: YES user_id=32170 So in fact you want: set me "foo/bar" file normalize [file join [file dirname $me] [file readlink $me]] I agree 'file normalize -full' and an equivalent C API are needed. Vince. vincentdarley added on 2003-02-05 02:01:24:
Logged In: YES user_id=32170 I've committed what I think is a fix to vfs.c -- please test. jcw added on 2003-02-05 02:23:55:
Logged In: YES user_id=1983 Error gone (running tclkit through a symlink). Flesystem mount listed ok now. BINGO! dgp added on 2003-02-05 02:49:08:
Logged In: YES user_id=80530 Isn't this just a bug in [file normalize] ? Why add a "-full" switch? Why not just fix the bug? jcw added on 2003-02-05 03:00:50:
Logged In: YES user_id=1983 Looks like there are different ways to interpret "normalization": - convert relative path to absolute path (based on curr dir) - simplify "a/b/../c" to "a/c" - expand symlinks - a combination of the above (first two can be done on non-existent paths, btw, rest not) I'll leave it to others as to how this translates to features and options in Tcl. vincentdarley added on 2003-02-05 19:51:23:
Logged In: YES user_id=32170 Well, 2 comments: 'normalization' is defined in Tcl's man pages to be a unique representation of the filesystem from Tcl's point of view. This actually goes beyond jcw's list to include the unique case-sensitive name (even on case- insensitive systems like Win/Mac). BUT, it does not include expansion of a symlink in the final element of the path, because Tcl's view is that it wants to see the symlink (for 'file delete', etc). This is why an additional 'file normalize -full' is needed, and why it isn't a bug in [file normalize]. Otherwise I could never ask Tcl for the full normalized path to a symlink which would be a curious omission. dgp added on 2003-02-06 22:45:49:
Logged In: YES
user_id=80530
Thanks, Vince. I understand better now.
(Looking at the docs always helps.)
So, it seems clear that the [file] command
does not hide links from the caller. The
caller is expected to know that links exist
and behave somewhat differently from
normal files.
Given that, it seems the correct answer
has already been identified. If the caller
does want to follow a symlink reference,
that caller should evaluate:
file join [file dirname $link] [file readlink $link]
and then [file normalize] the result, if normalized
form is required. All the tools are in place, and
it's up to the caller to use them correctly.
hobbs added on 2003-02-07 02:14:08:
Logged In: YES user_id=72656 I agree with dgp's analysis. The tools are all there. We could stack more up for Tcl to auto-handle it, but if the user is going to add the flag, they already know a different "path" is needed. The default behavior that file normalize works on non- existent files/dirs is important. serhiy.storchaka added on 2026-11-13 11:18:45:
Closing per dgp's and hobbs' comments: the existing commands are sufficient, and neither file normalize -full nor a separate C API was added. | ||||
