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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bill David

    Is it possible to get a file name from the file descriptor returnedby 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,...);
    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, :) ).
  • Nick Keighley

    #2
    Re: Is it possible to get a file name from the file descriptorretur ned 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 <billdavi...@gm ail.comwrote:
    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

    Comment

    • Gordon Burditt

      #3
      Re: Is it possible to get a file name from the file descriptor returnedby 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,...);
      >And we can store the file name for later usage.
      The POSIX API is more on-topic in comp.unix.progr ammer. Standard
      C doesn't define an open() call, and not all systems are POSIX.
      >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, :) ).
      First of all, under POSIX, there isn't *THE* name: a given open
      file may have zero or more names. It might have zero if the name
      has been remove()d or rename()d over, but it's still open. It might
      have several names due to hard links. That's not even counting
      symlinks and variant pathnames involving /../ in the path which
      might have been used in the open() call.

      Under POSIX, it is possible, *if* you have the permissions necessary,
      to find some name(s), if any, assuming they are not changing out
      from under you. However, nobody said it would be efficient.
      On a certain large newsserver, treewalking one filesystem looking
      for a particular inode could take *hours*. If it was also handling
      news readers at the time, it might take a whole day.

      The following procedure works on POSIX, but I'm not sure about
      Windows, especially with a FAT or NTFS filesystem:

      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.

      Comment

      • Richard

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

        Nick Keighley <nick_keighley_ nospam@hotmail. comwrites:
        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 <billdavi...@gm ail.comwrote:
        >
        >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...
        --

        Comment

        • Wolfgang Draxinger

          #5
          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: hexarith@jabber .org, ICQ: 134682867

          Comment

          • Richard Tobin

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

            In article <72c70de4-c84f-4535-a722-c6c9b9dac158@p2 5g2000hsf.googl egroups.com>,
            Nick Keighley <nick_keighley_ nospam@hotmail. comwrote:
            >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.

            Comment

            • Nick Keighley

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

              On 9 Sep, 11:27, Richard<rgr...@ gmail.comwrote:
              Nick Keighley <nick_keighley_ nos...@hotmail. comwrites:
              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 <billdavi...@gm ail.comwrote:
              >
              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.

              Comment

              • vippstar@gmail.com

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

                On Sep 9, 3:04 pm, Nick Keighley <nick_keighley_ nos...@hotmail. com>
                wrote:
                On 9 Sep, 11:27, Richard the fool <rgr...@gmail.c omwrote:
                >
                Nick Keighley <nick_keighley_ nos...@hotmail. comwrites:
                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.

                Comment

                • Flash Gordon

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

                  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

                  Comment

                  • CBFalconer

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

                    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.c om, 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.

                    Comment

                    Working...