how may i direct your call?

I put in a marathon day of code yesterday - perhaps six hours by the end - and finally got directory creations working. The process is actually quite complicated as you have to handle all the weirdness that makes FAT so wonderful. First you have to take the name, and figure out how many directory entries are needed (FAT stores its long file names across multiple entries). Then you search the current dir for a gap of that many entries (gaps happen when files are deleted) or move the end-of-directory marker to make room at the end.

Having found space, you then generate the short name, comprised of what FAT calls the “basis name” and the “numeric tail”. You’ve probably seen this if you’ve used disks created in Windows on older system like DOS - a file called “mars attacks.html” gets converted to “MARSAT~1.HTM”. The conversion process is non-trivial. After storing the short name, you then cut the long name up and store it across multiple directory entries.

At this point the name exists, and will turn up in a directory listing, but the job isn’t done yet. Next we have to allocate space on the disk to store the directory contents, and put three entries within it - the “dot” (.) entry, pointing at the new directory (ie pointing to itself), the “dotdot” (..) entry, pointing to its parent, and the “end of directory” marker. Once this is done, we report success back to DOS and the calling application.

My code isn’t perfect yet. Most significantly it doesn’t do all its error checking and its possible for the filesystem to get into an inconsistent state if some lowlevel error occured (like a hardware error). It also hasn’t been well tested - its undoubtedly trashing my filesystems in every interesting way. But it appears to work, and thats the most important thing. Creating directories is also the hardest bit of doing write support - the rest shouldn’t take long to implement!