Explanation of Inodes / path

Hello,
I have the solution for this but unfortunately the tutor was too fast to understand the steps.

To get the the block 407, 635 and 321. The inode number would be 9, 34 and 6

Now my question is how I get to the file "testfile"?

tutor solution was : root/tmp/ida/testfile

I dont understand the steps how it comes to this solution.

If you look at testfile in block 635, the inode number of its directory (.) is 34, which is is ida's directory (see block 407). The directory "ida" is in the directory with inode number 9 (again look at the entry with the dot), which is the directory "tmp"..

Your tutor is correct (mostly).

In Unix filesystems all data files are made up of allocated blocks of disk but also all directories are also 'files'.

If the operating system needs to read the file:

/tmp/ida/testfile

it would go to inode 1 (well actually inode 2 on most operating systems because inode 1 is the bad block table), look up which block the root directory index is located on, and go to that block and look for 'tmp's inode number. It now knows that the 'tmp' directory inode is 9.

It goes to inode 9 and reads that the 'tmp' directory index starts at block 407. It goes to block 407, reads the index looking for 'ida' and then knows that 'ida's inode number is 34.

It goes to inode 34 and reads that the 'ida' directory index starts at block 635. It goes to block 635, reads the index looking for 'testfile' and then knows that 'testfile's inode number is 76.

It goes to inode 76 and reads the starting block number for testfile and it can now go to that block and start reading testfile's actual data content.

Additionally, if the O/S wanted to read /home then in the root directory index it would find that /home inode number is 6. Going to inode 6 is would read that /home directory index starts at block 321.

See your updated graphic attached.

Hope that helps. If you still don't get it, post back your questions.

Hey thanks for the replys so far.

I think I understood a part of the explanation.

Absolute path for otto 53 would be:

root/home/otto 

The reason for this is that the block 321 starts at inode 6 which we also have listed in our root directory. so there is no long way.

Same goes for oswald inode 19:

root/tmp/oswald

If this is correct so far then the edited screenshot with description helped me so much too understand it.

The last question would be just how do I do it with a relative path?
I know that the absolute path starts from the root and the relative path to the current working directory. But how I find the current working directory? Should I just pick one of those blocks?

../tmp/ida/testfile

might be this correct?

You can search for files or directories using /usr/bin/find command
Sample:

$ /usr/bin/find -maxdepth 1 -inum 4222124650820299
.
$ /usr/bin/find -maxdepth 1 -inum 4222124650820299 -exec /bin/ls -lid {} \;
4222124650820299 drwx------+ 1 murugesandins TES 0 Jun 21 09:06 .
$ /bin/ls -lid .
4222124650820299 drwx------+ 1 murugesandins TES 0 Jun 21 09:06 .
$ # Commented line
$ # Following command used to take more time and errors are redirected to /dev/null
$ /usr/bin/find -inum 4222124650820299 -exec /bin/ls -lid {} \; 2>/dev/null
4222124650820299 drwx------+ 1 murugesandins TES 0 Jun 21 09:06 .
$ cd /root/tmp
$ /usr/bin/find ./ -type f -inum 2814749767312557 -exec /bin/ls -lid {} \;
2814749767312557 -rw-r--r--+ 1 murugesandins TES 0 Jun 22 16:11 ./ida/testfile

When you want to walk an absolute pathname starting from the process' root directory, a C program can use rootdir = opendir("/"); to open a directory stream to read the root directory and it can use currentdir = opendir("."); to open a directory stream to read the current working directory.

If you want the pathname of the current working directory in a process, a C program can use the getcwd() function to get an absolute pathname of the current working directory:

#include <unistd.h>
#include <limits.h>
#include <stdio.h>

char buf[PATHMAX];
char *p;

int
main() {
	p = getcwd(buf, PATHMAX);
	if(p != NULL) {
		printf("The current working directory is \"%s\".", buf);
	} else {
		perror("getcwd() failed:");
	}
}
// Written comment for new employees/joinees  and for me too :) => Better initialize
#include <string.h>    // strerror
#include <sys/errno.h> // errno
#include <unistd.h>    // getcwd
#include <stdio.h>     // printf
// PATH_LENGTH variaes on OS
// Change PATH_LENGTH based on macro, OS and requirement
// Example: What is the longest file path that Windows can handle? - Super User
// Written CYGWIN_NT since .exe can run outside bash.exe or sh.exe or mksh.exe or ...
// => gecwd error => Numerical result out of range
// getcwd will fail when length varies above expected lenth at windows
#ifdef CYGWIN_NT
        #define PATH_LENGTH 195
#else
        #define PATH_LENGTH 256
#endif
char cwd[PATH_LENGTH] = {};
char *PWD = NULL;
int main()
{
        PWD = getcwd(cwd, PATH_LENGTH);
        // value != variable to remove initialization by typewriting mistakes
        // Example: if( p = NULL )
        if( NULL != PWD )
        {
                printf("The current working directory is \"%s\"\n", cwd);
        }
        else
        {
                printf( "getcwd() failed.\n%s\n", strerror(errno));
                return 1;
        }
        return 0;
}