Quote:
- How do i find an 'empty' or free to use cluster on the drive ? Do i need to look in the FAT, and where ?
yes, any FAT entry marked "free" is free, empty, and can be used for storing a file
where doesn't matter (that is a matter of implementation and optimization)
just to note: a 0 value means the cluster is free to be used
Quote:
- How do i create a directory or a file ? I am supposed to create a new entry in the FAT, right ? but how do i do that ? (where to write it, with what data)
a directory is just a file that contains other files (really very simple)
for both these are the things that need to be done:
find a free cluster to use in the FAT table, if you need more than 1 cluster, find more clusters
mark the last cluster in the chain as EOF (iirc that should be 0xFFFFFF8... but check the docs and don't rely on me to be correct)
if you are using more than 1 cluster, make all other clusters point to the next cluster in the list
find an available free slot in the parent directory list, and fill it with the proper name, size, attributes, and starting cluster
Quote:
- How do i allocate a cluster for a file or a directory, if the file data becomes too big for the current cluster ? (that cluster address must then be written in the FAT, right ? so that we can follow the cluster chain)
your directory entry points to the first cluster (this is how you mark which file the cluster is used by), and mark each cluster in the FAT table with the next cluster used by the file, the last cluster should be marked as EOF
if you need to grow a file larger, it is really easy:
just get a new free cluster (any cluster previously marked 0) and mark that as EOF, and point the previous final cluster to the new cluster (don't forget to update the file size in the directory)
Quote:
I tried to understand the concepts behind the file system, but i can't deny that i don't really know what are the data in the FAT itself, as all i've done for now using the fat is following cluster chain.
FAT tables are very simple: its a linked list, where the directory entry points to the start, and each entry then points to the next entry
0 means unused (free, available for use)
any cluster > EOF means bad cluster (probably the disk is damaged in that spot and cannot be reliably read/written)