For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > April 2006 > filename of the file to which a filedescriptor refers.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author filename of the file to which a filedescriptor refers.
Sticky Bit

2006-04-29, 4:02 am

Hello,
Is it possible to know the filename that a filedescriptor refers to?
Other than by reading the code and finding the argument of open().

On Linux it is possible to get this information from /proc/PID/fd/NN, but I
will need to port the code to AIX.

fstat() only gives the inode of the filedescriptor, not the name.

Thank you for your insights.

Måns Rullgård

2006-04-29, 4:02 am

Sticky Bit <sticky@yahoo.com> writes:

> Hello,
> Is it possible to know the filename that a filedescriptor refers to?


No, not easily in any portable way. There is not even such a thing as
"the filename" of a file descriptor. An file can have zero, one, or
more names associated with it. How would you know which one to
return? Recording the name used to open it is no use either, since
that name may be deleted or renamed. The original name could even be
referring to a different file than the one that was opened.

--
Måns Rullgård
mru@inprovide.com
Casper H.S. Dik

2006-04-29, 8:01 am

Sticky Bit <sticky@yahoo.com> writes:

>Is it possible to know the filename that a filedescriptor refers to?
>Other than by reading the code and finding the argument of open().


There's no portable way; there's generally not even a reliable
non-portable way (last time I tried, the Linux /proc filesystem
can give wrong answers for files which had undergone some link/unlink
steps)

>On Linux it is possible to get this information from /proc/PID/fd/NN, but I
>will need to port the code to AIX.


>fstat() only gives the inode of the filedescriptor, not the name.



The only vagualy portable way is:

use st_dev to locate the device the file is on and the
mounpoint (say, /its/inhere)
use st_ino:

find /its/inhere -ino st_ino -xdev -print


No, there's of course no guarantee that the file still exists in
the filesystem or that you can access the directory the file is in.

Casper
Sticky Bit

2006-04-30, 8:05 am

Thanks, Casper and Måns
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com