| Author |
file name & file descriptor
|
|
|
| Hi all,
How to get full file name (dir_from_root/file_name) having file descriptor for this file ?
sugar
| |
| Ulrich Eckhardt 2004-01-23, 5:23 pm |
| sugar wrote:quote:
> How to get full file name (dir_from_root/file_name) having
> file descriptor for this file ?
A file descriptor doesn't necessarily have a directory-entry and a name
associated with it or could have more than one (consider hardlinks..).
The answer to your problem is therefore that you need to save the filename
when you open the file. Other ways might exist (i.e. going after the inode
and seaching a corresponding entry), but neither are guaranteed to
succeed.
Uli
| |
|
| > A file descriptor doesn't necessarily have a directory-entry and a namequote:
> associated with it or could have more than one (consider hardlinks..).
>
> The answer to your problem is therefore that you need to save the filename
> when you open the file. Other ways might exist (i.e. going after the inode
> and seaching a corresponding entry), but neither are guaranteed to
> succeed.
The problem is something like i've opened file with relative dir (example: `./../dir/file')
then i open the one, the get file file descriptor and want to know full file name (dir + name).
Possibly is a method to get full dir name having relative dir name (example: ../../.././.. ).
sugar
| |
| Jens.Toerring@physik.fu-berlin.de 2004-01-23, 5:23 pm |
| sugar <pestl@o2.pl> wrote:quote:
[QUOTE][color=darkred]
> The problem is something like i've opened file with relative dir (example: `./../dir/file')
> then i open the one, the get file file descriptor and want to know full file name (dir + name).
> Possibly is a method to get full dir name having relative dir name (example: ../../.././.. ).
You can't do this using the file descriptor - there might be several
ways that could get you to the current directory (just think symlinks
for directories). I would guess the simplest possible method would be
to call getcwd(3) function and use the result to replace the starting
"./" bit. If there are some "../" instead it you have to work your way
up the absolute path (by looking for the last '/' character in that
string)...
Regards, Jens
--
\ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.physik.fu-berlin.de/~toerring
| |
|
| > You can't do this using the file descriptor - there might be severalquote:
> ways that could get you to the current directory (just think symlinks
> for directories). I would guess the simplest possible method would be
> to call getcwd(3) function and use the result to replace the starting
> "./" bit. If there are some "../" instead it you have to work your way
> up the absolute path (by looking for the last '/' character in that
> string)...
> Regards, Jens
Thanks Jens & Urlich,
I thought that there is any func that do it for me (i'm so lazy man...)!
But your algorithm Jens is no such trivial, think about dir something
like ./../dir1/../dir2/./../file - no last `/' in that string ! Additionally i hate
doing txt parsing manually.
But meantime i guess simpler method: just chdir() to the dir. If i get EACCESS
i probably can't also get this dir name other way, then getcwd(), and
back to orginal dir!
sugar
| |
| Robert Harris 2004-01-23, 5:23 pm |
| sugar wrote:quote:
> Hi all,
> How to get full file name (dir_from_root/file_name) having file descriptor for this file ?
>
> sugar
>
>
You can't (generally). The file may have been unlinked from its
directory entries in its filesystem! You need to get its file name
before you open it.
Robert
| |
| Jeff Schwab 2004-01-23, 5:23 pm |
| Jens.Toerring@physik.fu-berlin.de wrote:quote:
> sugar <pestl@o2.pl> wrote:
>
>
>
>
>
> You can't do this using the file descriptor - there might be several
> ways that could get you to the current directory (just think symlinks
> for directories). I would guess the simplest possible method would be
> to call getcwd(3) function and use the result to replace the starting
> "./" bit. If there are some "../" instead it you have to work your way
> up the absolute path (by looking for the last '/' character in that
> string)...
> Regards, Jens
There are plenty of existing, open-source functions that do exactly what
Jens has recommended. Google for "getCanonicalPath" or "getAbsolutePath."
| |
| Stephane CHAZELAS 2004-01-23, 5:23 pm |
| 2004-01-20, 13:41(+01), sugar:quote:
> How to get full file name (dir_from_root/file_name) having
> file descriptor for this file ?
On a Linux system with a /proc fs, you can use:
char file[PATH_MAX], buf[64];
sprintf(buf, "/proc/self/fd/%d", fd);
readlink(buf, &file[0], sizeof(file));
--
Stéphane ["Stephane.Chazelas" at "free.fr"]
| |
| Mohun Biswas 2004-01-23, 5:23 pm |
| sugar wrote:quote:
> I thought that there is any func that do it for me (i'm so lazy man...)!
No way to do it from the descriptor but from the relative path there's a
standard function "realpath".
MB
|
|
|
|