FAT32 Write Function?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
pcmattman
Member
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:

FAT32 Write Function?

Post 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.
pcmattman
Member
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:

Post by pcmattman »

Never mind, I found a good code example which cleared things up for me...
User avatar
xyjamepa
Member
Member
Posts: 397
Joined: Fri Sep 29, 2006 8:59 am

Post 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.
pcmattman
Member
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:

Post by pcmattman »

User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Post 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.
User avatar
Combuster
Member
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:

Post 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?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
pcmattman
Member
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:

Post 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.
User avatar
Combuster
Member
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:

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Post 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.
Post Reply