davelog Wherein, I write.

unix inode fun!

If you have talked to me about school recently, you probably know that I am working on writing a “mickey mouse” file system for my Operating Systems Programming class (CSCI 340). We are supposed to allot a section of memory on the computer and turn it in to a working file system we can manipulate with all of our favorite UNIX commands like ls, mkdir, touch, rmdir, rm, etc… It’s pretty involvled, and there’s a lot of lower-level stuff I’m learning (like managing actual bits instead of variables).

Anyway, I’m working on the structure of this beast, and I started doing research on how much information is stored in a file’s iNode. An iNode is what defines an actual file in a *nix file system (like OSX). It contains information like the permissions, creation date, and file length, as well as pointers to the actual data on the disk. If it is a directory (in *nix, directories are files… weird, huh?), the iNode contains information about its children. So this is really interesting stuff.

The best part about this project is that it’s been done before – in the unix file system! Since I work on a mac, I can easily drop in to a terminal and see the real deal! I quickly learned a few of the FS basics.

Entering -i as a parameter for the ls command shows the normal stuff, as well as a files inode number.

neptune:/ dave$ ls -i /
22 drwxr-xr-x 125 dave dave 4250 Mar 10 11:39 Applications
474198 -rw-r--r-- 1 root admin 54272 Mar 10 09:55 Desktop DB
474199 -rw-r--r-- 1 root admin 138194 Oct 17 20:18 Desktop DF
530660 drwxrwxr-x 16 root admin 544 Jan 8 12:37 Developer
2801 drwxrwxr-t 54 root admin 1836 Dec 7 19:07 Library
3 drwxr-xr-x 1 root wheel 512 Mar 10 15:56 Network
3013 drwxr-xr-x 5 root wheel 170 Dec 30 13:14 System
26753 drwxrwxr-t 6 root admin 204 Oct 30 12:58 Users
1526179 -rw-r--r-- 1 dave dave 0 Mar 2 20:47 Utilities
10985 drwxrwxrwt 9 root admin 306 Mar 10 11:39 Volumes
475038 drwxr-xr-x 4 root admin 136 Jul 28 2006 automount
2755 drwxr-xr-x 40 root wheel 1360 Oct 7 19:59 bin
28483 drwxrwxr-t 2 root admin 68 Jan 13 2006 cores
2 dr-xr-xr-x 2 root wheel 512 Mar 9 23:21 dev

Next, I found that the unix command stat displays all the information you could ever want about a file. I found the -x option the most useful, as it has a verbose output so you can figure out what all the numbers mean.

neptune:/ dave$ stat -x ~
File: "/Users/dave"
Size: 1292 FileType: Directory
Mode: (0755/drwxr-xr-x) Uid: ( 501/ dave) Gid: ( 501/ dave)
Device: 14,2 Inode: 474787 Links: 38
Access: Sat Mar 10 15:32:11 2007
Modify: Sat Mar 10 14:26:33 2007
Change: Sat Mar 10 14:26:33 2007

Another fun one is the find command. I learned that you can find a file based on it’s iNode number. Look at this:

neptune:~ dave$ find / -inum 1337
/Applications/Utilities/Installer.app/Contents/PlugIns/Introduction.bundle/Contents/Resources/zh_TW.lproj/Default.rtf

So now I know what file has iNode number 1337.

There’s tons more I’m learning, but I guess I have to know it all inside and out if I’m going to write my own version of it by May 7th :P