Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   access to file attributes via inode (http://www.velocityreviews.com/forums/t957087-access-to-file-attributes-via-inode.html)

cartercc@gmail.com 01-30-2013 01:50 PM

access to file attributes via inode
 
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.

Mark Bluemel 01-30-2013 02:19 PM

Re: access to file attributes via inode
 
On 30/01/2013 13:50, cartercc@gmail.com 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.

cartercc@gmail.com 01-30-2013 03:43 PM

Re: access to file attributes via inode
 
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.

glen herrmannsfeldt 01-30-2013 08:52 PM

Re: access to file attributes via inode
 
cartercc@gmail.com 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

Nobody 01-31-2013 02:04 AM

Re: access to file attributes via inode
 
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.




All times are GMT. The time now is 04:44 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.