in reply to Re: Can you delete a file by descriptor?
in thread Can you delete a file by descriptor?

It's possible, at least on linux, if you don't mind cheating a little.

Find /proc/pid/fd/n, and readlink it. That will give you /a/ (not neccessarly the only) name of the file opened on that fd. (Careful, though, because it will sometimes give things that are not filenames, like socket:n and /foo/bar (deleted).

However, the other ideas on this thread are almost undoubtedly better.

(I know several other unixes have similar proc (or proclike) filesystems, and many of them can probably be used similarly. Win32 doesn't, but you can do internals things to get the names from the handles. I wouldn't recommend it.)


We are using here a powerful strategy of synthesis: wishful thinking. -- The Wizard Book

  • Comment on Re: Re: Can you delete a file by descriptor?

Replies are listed 'Best First'.
Re3: Can you delete a file by descriptor?
by blakem (Monsignor) on Mar 14, 2002 at 20:05 UTC
    Thats pretty slick... I like it. You can get the same info from /dev/fd/n as well.
    % perl -e'open($fh,"/tmp/file"); print readlink("/dev/fd/" . fileno($f +h))' /tmp/file

    -Blake

      Nod. (BTW, this works because /dev/fd/ is a symlink to /proc/self/fd/, and /proc/self is (magicly) a symlink to /proc/(pid) for whatever process is following the link.


      We are using here a powerful strategy of synthesis: wishful thinking. -- The Wizard Book