handling filesystems

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
User avatar
mathematician
Member
Member
Posts: 437
Joined: Fri Dec 15, 2006 5:26 pm
Location: Church Stretton Uk

Post by mathematician »

Once your OS has booted the first thing you are ever likely to read from disk is the root directory, which on a FAT system will be in a fixed location, and in UNIX style systems there would presumably be a cluster number stored in the super block which would enable you to locate it. Any directory further down in the directory tree will then be read or written to in much the same way as any other file (which is what directories are). When, say, somebody wants to open a file in the root directory the file system will need to allocate a structure for that file. What needs to go in that structure is something you will soon find out when you begin to write the FS driver, but would be likely to include, for example, the current offset into the file a pointer to the files i/o buffer, and a pointer to any loaded inodes related to the file. Having done that it would need to go to the directory, look up the files entry, locate the first inode (in a Unix type system), and call the device driver to read that from disk. The inode will have pointers to other inodes if it is anything other that a very small file, and also a list of the clusters where the first part of the file is to be found. When the user wants to read the file it will then use that list of clusters to locate the file on disk, and then call the device driver to read those clusters into the buffer whose address you have stored in the FILE structure.
User avatar
AndrewAPrice
Member
Member
Posts: 2309
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Post by AndrewAPrice »

okay, thanks. I think I've got it :)

I have another question. If a program requests a list of directories in a directory, what do I give it? Do I take a pointer to a 2d array of chars?

I was thinking, a program doesn't know how many directories there will be, and the file system driver can't allocate the memory, because it would be
a) unaccessible from the program, since the memory will be in the fs's space
b) the memory would be un-freeable

So.. would I have to write a fs_getnumberofentries(char *directory), and then do a loop through fs_getentryname(int num, char *directory, char *name) for each entry?
My OS is Perception.
User avatar
mathematician
Member
Member
Posts: 437
Joined: Fri Dec 15, 2006 5:26 pm
Location: Church Stretton Uk

Post by mathematician »

Do the same as every other OS does - give them a findfirst() function, and then subsequent calls to a findnext() function. It will be for the caller to allocate memory for the returned info.
Post Reply