Does anyone know where I can find a good FAT32 file write implementation? The problem I have at the moment is that I can write into the file if it exists and if its clusters are valid, but I need to handle the cases where I need to increase the file size by extending the cluster chain and I have no idea how to do that.
I don't want to copy code, but I do want to know how it's done properly. Thanks in advance.
FAT32 Write Function?
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
I take you are doing that to prevent corruption on a crash? you could also write the new version of the file, then change the starting cluster before removing the old cluster chain. It would significantly reduce the timespan that the disk will contain an illogical state - you'd only have a free chain of FAT entries to take care of, which would also be bogus in most cases (partially written new version or partially deleted original) no?Dex wrote:As a side note, i find it much better, if a file has increased in size, to rename the file to a temp name, then rewrite it as a new file by the old name, then if no errors delete the temp named file.
-
- Member
- Posts: 2566
- Joined: Sun Jan 14, 2007 9:15 pm
- Libera.chat IRC: miselin
- Location: Sydney, Australia (I come from a land down under!)
- Contact:
Actually, at the end of the write I look at how many bytes were written (the offset into the written buffer). I then calculate a difference between the file size and the last byte written. If it's not negative then I add the difference to the file size and write it back to the directory entry. The only thing that could cause problems is the cluster chain.Dex wrote:As a side note, i find it much better, if a file has increased in size, to rename the file to a temp name, then rewrite it as a new file by the old name, then if no errors delete the temp named file.
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
The issue with that is that when a file is partially written when the system crashes is that you end up with a file that is partly new and partly old, which is potentially catastrophic for all data contained in that file.pcmattman wrote:Actually, at the end of the write I look at how many bytes were written (the offset into the written buffer). I then calculate a difference between the file size and the last byte written. If it's not negative then I add the difference to the file size and write it back to the directory entry. The only thing that could cause problems is the cluster chain.
Thats right Combuster, and your way is a good idea, but it was a simple case of reuse the write new file function.Combuster wrote:I take you are doing that to prevent corruption on a crash? you could also write the new version of the file, then change the starting cluster before removing the old cluster chain. It would significantly reduce the timespan that the disk will contain an illogical state - you'd only have a free chain of FAT entries to take care of, which would also be bogus in most cases (partially written new version or partially deleted original) no?Dex wrote:As a side note, i find it much better, if a file has increased in size, to rename the file to a temp name, then rewrite it as a new file by the old name, then if no errors delete the temp named file.