1

For educational purposes, I'm rolling my own FAT reader (allows you to browse a drive, etc). My current issue is determining the current working directory (such as for the prompt in a typical command prompt). As far as I can tell, the directory table gives no info on the path you would have taken to get there. (I've been working off the standard I found here) Thus, my current approach is just keep track of each directory you pass through (i.e. essentially each time cd <dir> is used, put that value in a list, and remove the last one when cd .. is used)

Here's where the issue arises. Lets say two different paths bring you to the same directory. If you then follow the .. directory upwards, the issue becomes more complicated than just removing the last directory name from your list. If .. takes you up the path you didn't come down, you'd actually have to determine a whole new working directory.

This issue becomes irrelevant if FAT doesn't allow undirected cycles. (I believe I read that certain file systems disallow this sort of complexity for exactly this reason of simplifying traversal, but I can't find specific info for FAT) Do I need to worry about this? In other words, is FAT described by a tree or by a general graph?

For reference, I'm dealing with FAT16 and FAT32 (incidentally in C on Linux, but I assume that's irrelevant)

Cannoliopsida
  • 3,044
  • 5
  • 36
  • 61

3 Answers3

1

There's nothing in the FATx format that disallows cycles. However, there are other issues if you allow cycles:

  • As pointed out, ".." is ambiguous when two directory entries point to the same directory. Not usually a big deal as DOS/WINDOWS maintain the full text of the directory and doing "cd .." is a name operation, not a directory traversal. *NIX, on the other hand will have problems.
  • There are no reference counts, meaning RMDIR X cannot effectively free disk space unless it does an entire tree walk to see if there are any other references to that directory.
  • Worse, it could be possible to "disconnect" the cycle from the root, leaving unreachable space which CHKDSK/fsck would need to clean up.

(N.B. these are some of the reasons *NIX doesn't allow hard links to directories).

MJZ
  • 1,074
  • 6
  • 12
0

It doesn't matter it there are two parent directories pointing to the same subdirectory as you don't have that duality in your directory stack. When the user issues a cd .. command, just pop the stack and that's it.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Let's say a/foo/b brings us to the same directory as a/bar/b. We traverse down a/foo/b and are now in the b directory. If the .. file in b points bar, are you saying I should still return to foo? In that case, I would have no idea where in the directory structure to go to, because I'm not using the pointer given by .. in the directory file, so I'd either have to hold every pointer I'd ever used or do a tree search every time I used cd .. . – Cannoliopsida Oct 30 '12 at 14:56
  • @Akroy You said yourself in your question that you have a list that you push and pop directories (in reality a stack) when you use the `cd` command. Therefore in your program you don't have a conflict if you use that stack instead of the actual parent directory pointer. – Some programmer dude Oct 30 '12 at 16:18
  • No, not a traditional stack, because I want to know the entire path, not just the most recent directory. (stacks are LIFO, so you only have access to the top, unless you essentially empty your stack) And using that list wouldn't create a conflict, but it would still be wrong. If .. refers to one directory, and I go somewhere else, that's not right. – Cannoliopsida Oct 30 '12 at 16:34
  • Also, knowing the name of a directory doesn't tell where on the drive it exists. The point of using an actual directory entry is that it has a pointer to where on disk you should be going. – Cannoliopsida Oct 30 '12 at 16:35
0

Asking around, experienced operating systems guys have told me that FAT doesn't explicitly ban cycles, but it would add such complexity that all implementations ignore it. After all, standard OSes don't give a way to create such a cycle without bit-level hacking. Therefore, the problem can be ignored.

Cannoliopsida
  • 3,044
  • 5
  • 36
  • 61