Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > access to file attributes via inode

Reply
Thread Tools

access to file attributes via inode

 
 
cartercc@gmail.com
Guest
Posts: n/a
 
      01-30-2013
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.
 
Reply With Quote
 
 
 
 
Mark Bluemel
Guest
Posts: n/a
 
      01-30-2013
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.
 
Reply With Quote
 
 
 
 
cartercc@gmail.com
Guest
Posts: n/a
 
      01-30-2013
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.
 
Reply With Quote
 
glen herrmannsfeldt
Guest
Posts: n/a
 
      01-30-2013
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
 
Reply With Quote
 
Nobody
Guest
Posts: n/a
 
      01-31-2013
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.


 
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
inode number in windows XP asit Python 2 01-25-2008 05:50 PM
open file by inode? ivowel@gmail.com Perl Misc 6 05-29-2006 08:28 AM
removing file by inode s99999999s2003@yahoo.com Python 5 03-23-2006 09:44 PM
freebsd (ufs) direct access to Hard drive using inode kerb C Programming 4 01-06-2005 09:22 AM
Linking to an opened inode w/o links left: how? Jan Stap C Programming 16 11-12-2003 07:11 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57