7

I am working with the disk file directly. since the size of a directory is 0 in the directory structure, I wonder how do I detect the end of a directory file on the disk.

DIR_Name[0] == 0x00

The above way to detect end of directory doesn't seem reliable. I found on wiki that the size of the root directory in FAT32 is fixed to 512 entries, but what about other subdirectories. I might need to traverse down directories using the FAT and the cluster number.

alk
  • 69,737
  • 10
  • 105
  • 255
Louis Kuang
  • 727
  • 1
  • 14
  • 30

2 Answers2

4

From the first Google search result for "fat32 on disk format", page 24:

When a directory is created, a file with the ATTR_DIRECTORY bit set in its DIR_Attr field, you set its DIR_FileSize to 0. DIR_FileSize is not used and is always 0 on a file with the ATTR_DIRECTORY attribute (directories are sized by simply following their cluster chains to the EOC mark).

Also: The FAT32 root directory size is not fixed at 512 entries; its size is determined in exactly the same way as any other directory.

From another reliable source:

Reading Directories

The first step in reading directories is finding and reading the root directory. On a FAT 12 or FAT 16 volumes the root directory is at a fixed position immediately after the File Allocation Tables:

first_root_dir_sector = first_data_sector - root_dir_sectors;

In FAT32, root directory appears in data area on given cluster and can be a cluster chain.

root_cluster_32 = extBS_32->root_cluster;

Emphasis added.

keithmo
  • 4,893
  • 1
  • 21
  • 17
  • 1
    I do know all of these, but I still have no clue how to automatically stop reading when the end of a directory is reached. For example if I want to find a file against the files under a certain directory – Louis Kuang Nov 15 '15 at 11:52
  • `DIR_Name[0] == 0x00` A directory entry is 32 bytes in size. The first 11 bytes is a `char` array. The last directory entry has a zero in the first position to mark it as the last unused entry. This is not representative of the directory size. – Mike Gonta Jan 23 '17 at 23:03
3

A non-root directory is just a file.

The root directory starts in a fixed place on the disk (following the FAT). An entry in the root directory contains a cluster number. That cluster contains the data of the file or directory. The entry of that cluster number in the FAT, i.e. FAT[cluster_number] contains the number of the next cluster that belongs to the file or directory. That cluster contains more data of the file or directory and the FAT entry contains the number of the next cluster of the file or directory, etcetera, until you encounter the end-of-cluste mark, a value equal to or greater than 0xFFFFFFF8.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • 1
    Does that mean I can use the cluster size divide by the entry size and then if that much entry has been read, go to the FAT for the next block? – Louis Kuang Nov 15 '15 at 11:55
  • In FAT32 the root directory is of course not in a fixed place as in FAT12/FAT16. – Mike Gonta Jan 23 '17 at 23:11