For example
set ofh [open foo w]
# What is the real code corresponding to the code below?
puts "The name of the open file is [chan configure $ofh -name]"
Thanks,
Simon
Not from within Tcl. Though it's a good idea and one I've thought of
before, nobody ever got around to adding it. :-)
You can hack around it on a Unix-based system with lsof:
set info [exec lsof -a -p [pid] -d [regexp -inline {\d+} $chan] -F]
regexp -line {^n(.*)} $info -> filename
Yes, that's really ugly. Works for me though. :-) (Except for stdin,
stdout and stderr; those you have to know correspond to descriptors 0,
1 and 2 respectively.)
Donal (weird-hacks-Я-us).
Out of curiosity, what's the use case ?
-Alex
>Is there a way of enquiring the name of a file from the channel it's
>connected to?
I have a vague recollection of a discussion of exactly this,
some while ago, whose conclusion was that there's no safe
or reliable way to do it, because of all the other things
that might have happened to the file since it was opened.
I'm nowhere near enough of a Unix wonk to recall any of
the gory details, though. And, of course, I could be
spectacularly wrong yet again.
--
Jonathan Bromley
In Unix filesystems, the "name" of a file is not associated with the
file, but rather exists only in a directory. The name is simply a
pointer to the file contents. The result of this is that plural,
different, names in multiple different directories can all "point" to
the exact same file contents on disk. This would be the source of a
"no reliable way to do this" because of this many -> one possibility.
There is no way to know, by knowing which file contents is open, which
one of potentially plural different names was the one utilized to open
the contents. That and the fact that most all Unix filesystems are
optimized to perform name -> contents lookups without a whole lot of
effort, but performing contents -> names usually requires a complete
scan of the disk volume.
Now, to make this Tcl specific, you could substitute your own helper
proc that wraps around open and remembers which name was utilized to
open which file descriptor. Something like (note, this has not been
tested, it is just to show the idea):
rename open tcl-open
proc open { name {access {}} {perms {}} } {
set fd [ tcl-open $name $access $perms ]
set ::file_name_map($fd) $name
return $fd
}
Then a lookup of the fd in the ::file_name_map would tell you what
name was used to open the fd. Creating a helper for close to remove
the relevant mapping upon close of the fd is left an an exercise.
Open file for writing
pass file handle to the processing routine which writes the file
on successful exit write a line in a different file stating the name of
the file just created - this is the bit I thought it would be nice to do
by querying the channel rather than keeping an array or dict to track
this information
close channels
Simon
No need to keep info: the caller of the processing routine, having
just done the [open], and knowing about the success, is in a perfect
position to write the log line.
-Alex