access to file attributes via inode

C

cartercc

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.
 
M

Mark Bluemel

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.
 
C

cartercc

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.
 
G

glen herrmannsfeldt

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
 
N

Nobody

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top