Expanding Files?
-
- 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:
Expanding Files?
I now have a working FAT driver, but I've run into a small hiccup. I can only write data to files when the data's size does not exceed the number of sectors in the file. I tried to allocate new sectors when data too large to fit in the current file is added but it didn't work properly.
For now, I'm fixing the problem by truncating the data string but it's a hack and I'd like to know how to fix it.
For now, I'm fixing the problem by truncating the data string but it's a hack and I'd like to know how to fix it.
I do not fully understand the ? , if i under your right, your rewrite file to disk, that takes more sectors than the old file did, does not work, if so you should let us know why and maybe we can help you fix it.
One thing that i did, but it maybe only me, that does it this way is:
I first coded a "delete file" function, than a write a "write new file" function.
Then if the file needed more sectors adding i :
1. Check that theres enough room on the disk for the new file size - the old.
2. Then delete the old file and reWrite the new file.
3. This is much simpler, but it maybe better to change it name to a temp name, than add the new file, then delete the temp file, just in case there a problem, with writing the new file to disk .
One thing that i did, but it maybe only me, that does it this way is:
I first coded a "delete file" function, than a write a "write new file" function.
Then if the file needed more sectors adding i :
1. Check that theres enough room on the disk for the new file size - the old.
2. Then delete the old file and reWrite the new file.
3. This is much simpler, but it maybe better to change it name to a temp name, than add the new file, then delete the temp file, just in case there a problem, with writing the new file to disk .
To me, it sounded more like appending to a file failed once an extra sector was needed.Dex wrote:I do not fully understand the ? , if i under your right, your rewrite file to disk, that takes more sectors than the old file did, does not work, if so you should let us know why and maybe we can help you fix it.
It's probably a bad idea to keep deleting and re-writing a file just because 512 bytes got added. Especially if it's a large file...One thing that i did, but it maybe only me, that does it this way is:
I first coded a "delete file" function, than a write a "write new file" function.
Then if the file needed more sectors adding i :
1. Check that theres enough room on the disk for the new file size - the old.
2. Then delete the old file and reWrite the new file.
Wouldn't keeping the old version around and rewriting the new version fail if they don't fit on the disk at the same time?3. This is much simpler, but it maybe better to change it name to a temp name, than add the new file, then delete the temp file, just in case there a problem, with writing the new file to disk .
I am not too sure, if we are talking hdd then its ok.urxae wrote: It's probably a bad idea to keep deleting and re-writing a file just because 512 bytes got added. Especially if it's a large file...
Eg: you open a 1mb text file, you add 512bytes to it, but its not that simple, you may of change bytes all over the file, you can test which parts have been changed or ( the KISS way ) rewrite it in less than a second, with a well tested write new file to disk function.
Sure, but you would not do the -old, and its up to you to deside if it is better to play it safe or fit more on the disk + you could have a popup saying that it does not fit on disk, do you want to delete old file etc.Wouldn't keeping the old version around and rewriting the new version fail if they don't fit on the disk at the same time?
Note: what you think need to be done when thinking about coding a OS, is useally differant, when you need to code it, i thought like you untill i code these functions.
Also remember, linux load the whole floppy, even if you only need to read a 512b 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:
Here you go:
Basically, count is the size of the data to be written, offset is the offset in the file to write to.
Code: Select all
// ok... now tell us the file size ON DISK
int sizeondisk = numsect * 512;
// now see if the offset + the count is > than this
int totwritesize = offset + count;
if( totwritesize > sizeondisk )
{
// find out how much by
int howmuch = totwritesize - sizeondisk;
// find out how many sectors over we are
int scount = ( howmuch > 512 ? ( howmuch / 512 ) + 1 : 1 );
// tell
printf( "Need to allocate %d more sectors, total excess is %d bytes\n", scount, howmuch );
// find scount more sectors
int i;
int ls = 0;
short last = 0;
printf( "sector is at %d...\n", fil->Sector );
for( i = scount; i >= 0; i-- )
{
ls = FindFreeCluster(); // find a free cluster
// check to see if EOF is needed to be written
if( i == scount )
{
SetClusterData( ls, 0x0FFF );
}
else // if( i > 0 )
{
SetClusterData( ls, last );
}
last = ls;
}
// can't forget to set the data for this sector
SetClusterData( fil->Sector - (myFloppy.DataAreaStart), last );
// increment the number of sectors count
numsect += scount;
}