Is it possible to get a file name from the file descriptor returnedby Low-Level I/O API open?

B

Bill David

I know it's very strange to do that since we have the file name when
we call:
int open(const char *pathname, int oflag,...);
And we can store the file name for later usage.

But I just wonder if we can get a file name from the file descriptor
(on Windows/Linux), then the solution may looks simpler (if it's not
so hard to get the file name, :) ).
 
N

Nick Keighley

not all news readers display the subject in an easily accessible
manner.
Please include the subject in the body of your post

Subject: Is it possible to get a file name from the file descriptor
returned by Low-Level I/O API open?


I know it's very strange to do that since we have the file name when
we call:
int open(const char *pathname, int oflag,...);

this isn't standard C

And we can store the file name for later usage.

But I just wonder if we can get a file name from the file descriptor
(on Windows/Linux), then the solution may looks simpler (if it's not
so hard to get the file name, :) ).

You need to ask in a ng specific to the platform(s) you are interested
in.

This is a a comp.lang.c FAQ

FAQ 19.15 "How can I recover the file name given an open stream or
file
descriptor?"

It doesn't give good news...


--
Nick Keighley

The beginning of wisdom for a [software engineer] is to recognize the
difference between getting a program to work, and getting it right.
-- M A Jackson, 1975
 
R

Richard

Nick Keighley said:
not all news readers display the subject in an easily accessible
manner.

Utter nonsense. The subject is the subject of the contents. If your news
reader does not properly display the Subject then I suggest you find one
that does.
Please include the subject in the body of your post

Subject: Is it possible to get a file name from the file descriptor
returned by Low-Level I/O API open?

Please do no top post.
this isn't standard C



You need to ask in a ng specific to the platform(s) you are interested
in.

This is a a comp.lang.c FAQ

FAQ 19.15 "How can I recover the file name given an open stream or
file
descriptor?"

It doesn't give good news...

--
 
W

Wolfgang Draxinger

Gordon said:
[some scheme relying on POSIX only]

I'd like to add a scheme, that works for Linux:
The directory /proc/$PID/fd/ contains symlinks with numeric
names, which are the open file descriptors. And the symlinks
point to the filename that was originally opened with open. Of
course if the file has been unlinked in the meantime, the
symlink is broken. But you can still retrieve a name.

BTW /proc/self is a shortcut for /proc/$PID, so that you don't
have to find out the current process' PID.

Wolfgang Draxinger
 
R

Richard Tobin

Nick Keighley said:
not all news readers display the subject in an easily accessible
manner.

This seems to fall into the same category as implementations of malloc()
that delete all your files.

If your newsreader doesn't display the subject clearly, get another
newsreader!

-- Richard
 
N

Nick Keighley

Utter nonsense. The subject is the subject of the contents. If your news
reader does not properly display the Subject then I suggest you find one
that does.



Please do no top post.

I top-post information that is addressed to the post itself
rather than its content.
 
V

vippstar

I top-post information that is addressed to the post itself
rather than its content.

You did not top post. Your post was fine, and your observation
regarding the subject field accurate. Richard was trolling once more.
 
F

Flash Gordon

Gordon Burditt wrote, On 09/09/08 09:42:

fstat() the open file. Note the st_dev and st_ino fields returned.
Treewalk the entire file system. Find names with the same st_dev
and st_ino fields. These are the names for the file, or at least
were when they were found. Note that there are some shortcuts (but
system-dependent, even for POSIX) you can use with mount points and
the mount table to only treewalk the relevant filesystem, rather
than the whole system.

That shortcut could prevent you from finding valid names for a file
under Linux. In fact, I administer two servers where it is guaranteed
that you would not find all of the names for a file using that method.
 
C

CBFalconer

Bill said:
I know it's very strange to do that since we have the file name
when we call:
int open(const char *pathname, int oflag,...);
And we can store the file name for later usage.

But I just wonder if we can get a file name from the file
descriptor (on Windows/Linux), then the solution may looks
simpler (if it's not so hard to get the file name, :) ).

No you can't. Unix/Linux filesystems allow files to have multiple
names, and the name by which selected is abandoned once the file is
open. Read up on the structure of those file systems. If you want
the name, remember it when you open the file. But remember that
name may no longer be valid; someone could rename the file while
you have it open!!!

If you want to post a followup via groups.google.com, ensure
you quote enough for the article to make sense. Google is only
an interface to Usenet; it's not Usenet itself. Don't assume
your readers can, or ever will, see any previous articles.

More details at: <http://cfaj.freeshell.org/google/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top