access to file attributes via inode

Discussion in 'C Programming' started by cartercc@gmail.com, Jan 30, 2013.

  1. Guest

    This is a listing named testinode.c:
    1 #include <stdio.h>
    2 #include <sys/stat.h>
    3 int main()
    4 {
    5 char filename[256] = "test.txt";
    6 struct stat filestat;
    7 stat(filename, &filestat);
    8 printf("st_ino is %d\n", filestat.st_ino);
    9 return 0;
    10 }

    When I run it, it prints out the inode of the file, like this:
    $ ./testinode.exe => st_ino is 102832

    I can reverse this to go the other way, like this:
    $ find -inum 102832 => ./test.txt

    QUESTION: Is there any built in function, library function, or system call that allows me to use the inode directly? That is, change line 7 above to something like this:
    stat(inode, &filestat);
    where 'inode' is a number like 102832?

    Thanks much, CC.
     
    , Jan 30, 2013
    #1
    1. Advertising

  2. Mark Bluemel Guest

    On 30/01/2013 13:50, wrote:
    > This is a listing named testinode.c:
    > 1 #include <stdio.h>
    > 2 #include <sys/stat.h>
    > 3 int main()
    > 4 {
    > 5 char filename[256] = "test.txt";
    > 6 struct stat filestat;
    > 7 stat(filename, &filestat);
    > 8 printf("st_ino is %d\n", filestat.st_ino);
    > 9 return 0;
    > 10 }
    >
    > When I run it, it prints out the inode of the file, like this:
    > $ ./testinode.exe => st_ino is 102832
    >
    > I can reverse this to go the other way, like this:
    > $ find -inum 102832 => ./test.txt
    >
    > QUESTION: Is there any built in function, library function, or system call that allows me to use the inode directly? That is, change line 7 above to something like this:
    > stat(inode, &filestat);
    > where 'inode' is a number like 102832?


    You'd do better asking in comp.unix.programmer - inodes are a unix
    concept, not a C one.

    <http://stackoverflow.com/questions/4606774/why-cant-files-be-manipulated-by-inode>
    may be relevant, however. A simple Google search for "access file by
    inode" found that as the first result, you may wish to view some others.
     
    Mark Bluemel, Jan 30, 2013
    #2
    1. Advertising

  3. Guest

    On Wednesday, January 30, 2013 9:35:48 AM UTC-5, Rutles - China Blue Suede Schubert wrote:

    > Not in C nor POSIX. Also inode and device numbers can change after an unmount
    > and remount. You should be asking yourself why do you need this.


    Why? Just curious, that's all. I like knowing whether things are symmetrical or not, and this does not appear to be symmetrical.
    >
    > The closest thing in POSIX is an open file descriptor which remains linked to
    > the same inode as long as it openned and the device is not forcibly removed.
    > Many of the kernel functions like stat have an fd variant like fstat.


    Yeah. I can use an FD, but was trying to use the inode.

    Thanks for your reply, CC.
     
    , Jan 30, 2013
    #3
  4. wrote:
    > On Wednesday, January 30, 2013 9:35:48 AM UTC-5, Rutles - China Blue Suede Schubert wrote:


    >> Not in C nor POSIX. Also inode and device numbers can
    >> change after an unmount and remount. You should be asking
    >> yourself why do you need this.


    > Why? Just curious, that's all. I like knowing whether things are
    > symmetrical or not, and this does not appear to be symmetrical.


    As others have noted, this isn't really a C question, but, no,
    it isn't symmetrical.

    (snip)

    > Yeah. I can use an FD, but was trying to use the inode.


    I am not sure how much this is still applicable, given all the different
    types of file systems available on current unix-like systems.

    The reason that find is used is because it is not symmetrical.
    Directory entries point to i-nodes, but there is no back pointer.
    (Especially as more than one directory entry can point to the
    same i-node.) The only way to find the name(s) of such files
    is to search through all the directory entries on the disk and
    compare the i-node value, which is what find does.

    (You can also look inside the file and see if you recognize the
    data.)

    Even more, there doesn't have to be even one.

    A common unix convention for temporary files is to open the file,
    and then immediately delete (unlink) it. That removes the directory
    entry, but the actual files stays around until close.

    Not all file systems can implement the above, which has complications
    for their use with unix. One that it complicates is NFS.

    -- glen
     
    glen herrmannsfeldt, Jan 30, 2013
    #4
  5. Nobody Guest

    On Wed, 30 Jan 2013 05:50:42 -0800, cartercc wrote:

    > QUESTION: Is there any built in function, library function, or system call
    > that allows me to use the inode directly? That is, change line 7 above to
    > something like this: stat(inode, &filestat);
    > where 'inode' is a number like 102832?


    Not in POSIX (and certainly not in the C standards, which don't even
    have the concept of directories).

    For a start, an inode number doesn't uniquely identify a file; you also
    need the device major and minor numbers (an inode number is only unique
    per device, not globally unique).

    Beyond that, being able to access a file by inode number would bypass the
    permission mechanism, which requires that the process has execute
    permission on each directory in the path leading to the file. It would
    also allow you to reference unlinked files.

    If you really need this functionality, you'll probably need to access the
    underlying block device directly, and decode its inode table. There may be
    libraries to facilitate this (e.g. libext2fs for Linux' ex2fs filesystem).
    But it's prone to race conditions, and you certainly shouldn't try to
    modify a mounted filesystem in this manner.
     
    Nobody, Jan 31, 2013
    #5
    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.
Similar Threads
  1. Jan Stap
    Replies:
    16
    Views:
    549
    E. Robert Tisdale
    Nov 12, 2003
  2. removing file by inode

    , Mar 23, 2006, in forum: Python
    Replies:
    5
    Views:
    592
    Arne Ludwig
    Mar 23, 2006
  3. kerb
    Replies:
    4
    Views:
    354
  4. asit

    inode number in windows XP

    asit, Jan 25, 2008, in forum: Python
    Replies:
    2
    Views:
    1,353
    Gabriel Genellina
    Jan 25, 2008
  5. open file by inode?

    , May 28, 2006, in forum: Perl Misc
    Replies:
    6
    Views:
    338
Loading...

Share This Page