Page 1 of 1
FAT32 Write Function?
Posted: Sun Jul 22, 2007 5:34 pm
by pcmattman
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.
Posted: Sun Jul 22, 2007 9:21 pm
by pcmattman
Never mind, I found a good code example which cleared things up for me...
Posted: Sun Jul 22, 2007 11:59 pm
by xyjamepa
pcmattman wrote:Never mind, I found a good code example which cleared things up for me...
would you please tell us where did you find it.
Posted: Mon Jul 23, 2007 12:02 am
by pcmattman
Posted: Mon Jul 23, 2007 10:06 am
by Dex
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.
Posted: Thu Jul 26, 2007 2:45 pm
by Combuster
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.
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?
Posted: Thu Jul 26, 2007 3:13 pm
by pcmattman
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.
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.
Posted: Thu Jul 26, 2007 3:40 pm
by Combuster
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.
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.
Posted: Thu Jul 26, 2007 7:57 pm
by Dex
Combuster wrote: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.
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?
Thats right Combuster, and your way is a good idea, but it was a simple case of reuse the write new file function.