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

Discussion in 'C Programming' started by Bill David, Sep 9, 2008.

  1. Bill David

    Bill David Guest

    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, :) ).
     
    Bill David, Sep 9, 2008
    #1
    1. Advertising

  2. Re: Is it possible to get a file name from the file descriptorreturned by Low-Level I/O API open?

    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 <> 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
     
    Nick Keighley, Sep 9, 2008
    #2
    1. Advertising

  3. Bill David

    Richard Guest

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

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


    --
     
    Richard, Sep 9, 2008
    #3
  4. Re: Is it possible to get a file name from the file descriptor returned by Low-Level I/O API open?

    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: , ICQ: 134682867
     
    Wolfgang Draxinger, Sep 9, 2008
    #4
  5. Re: Is it possible to get a file name from the file descriptorreturned by Low-Level I/O API open?

    In article <>,
    Nick Keighley <> 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.
     
    Richard Tobin, Sep 9, 2008
    #5
  6. Re: Is it possible to get a file name from the file descriptorreturned by Low-Level I/O API open?

    On 9 Sep, 11:27, Richard<> wrote:
    > Nick Keighley <> 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 <> 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.
     
    Nick Keighley, Sep 9, 2008
    #6
  7. Bill David

    Guest

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

    On Sep 9, 3:04 pm, Nick Keighley <>
    wrote:
    > On 9 Sep, 11:27, Richard the fool <> wrote:
    >
    > > Nick Keighley <> 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.
     
    , Sep 9, 2008
    #7
  8. Bill David

    Flash Gordon Guest

    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
     
    Flash Gordon, Sep 9, 2008
    #8
  9. Bill David

    CBFalconer Guest

    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.
     
    CBFalconer, Sep 10, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page