Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Is it possible to get a file name from the file descriptor returnedby Low-Level I/O API open?

Reply
Thread Tools

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

 
 
Bill David
Guest
Posts: n/a
 
      09-09-2008
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, ).
 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      09-09-2008
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?


On 9 Sep, 08:17, Bill David <(E-Mail Removed)> wrote:

> 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
 
Reply With Quote
 
 
 
 
Richard
Guest
Posts: n/a
 
      09-09-2008
Nick Keighley <(E-Mail Removed)> writes:

> 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.

>
>
> On 9 Sep, 08:17, Bill David <(E-Mail Removed)> wrote:
>
>> 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...


--
 
Reply With Quote
 
Wolfgang Draxinger
Guest
Posts: n/a
 
      09-09-2008
Gordon Burditt wrote:

> [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
--
E-Mail address works, Jabber: http://www.velocityreviews.com/forums/(E-Mail Removed), ICQ: 134682867

 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      09-09-2008
In article <(E-Mail Removed)>,
Nick Keighley <(E-Mail Removed)> wrote:

>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
--
Please remember to mention me / in tapes you leave behind.
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      09-09-2008
On 9 Sep, 11:27, Richard<(E-Mail Removed)> wrote:
> Nick Keighley <(E-Mail Removed)> writes:


> > 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.


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


> > On 9 Sep, 08:17, Bill David <(E-Mail Removed)> wrote:

>
> >> 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

> Infinitely many bits doesn't give you "100% accuracy". You will
> only be able to represent the algebraic numbers.



 
Reply With Quote
 
vippstar@gmail.com
Guest
Posts: n/a
 
      09-09-2008
On Sep 9, 3:04 pm, Nick Keighley <(E-Mail Removed)>
wrote:
> On 9 Sep, 11:27, Richard the fool <(E-Mail Removed)> wrote:
>
> > Nick Keighley <(E-Mail Removed)> writes:
> > > 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.

>
> 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.
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      09-09-2008
Gordon Burditt wrote, On 09/09/08 09:42:

<snip>

> 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.
--
Flash Gordon
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      09-10-2008
Bill David wrote:
>
> 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/>


--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get the list of all my open file(descriptor)s and locks? Ismael Farfán Python 0 09-19-2012 05:34 PM
How to get a directory file descriptor? Cong Ma Python 19 11-27-2008 12:03 AM
Re: How to get a directory file descriptor? Baptiste Carvello Python 0 11-25-2008 09:59 AM
How can I get a file descriptor from a c++ i/o stream? Thorsten Knopel C++ 2 04-14-2005 06:39 AM
How to get file descriptor permissions? Mike C++ 2 02-27-2005 11:58 AM



Advertisments