Page 1 of 1

Expanding Files?

Posted: Tue Mar 27, 2007 1:07 am
by pcmattman
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.

Posted: Tue Mar 27, 2007 1:12 am
by os64dev
There are 10 people in this world that understand binary...
Then i will be the 11 th :twisted:

Posted: Tue Mar 27, 2007 1:25 am
by pcmattman
Wouldn't that make you the 3rd person?

Anyways, I have still not figured out how to get around this problem. For the time being, and for the first alpha, I think it might pass.

Posted: Tue Mar 27, 2007 2:22 am
by AJ
[OT] Shouldn't it be:
"There are 10 types of people in this world, those who understand binary and those who don't...". [/OT]

Posted: Tue Mar 27, 2007 2:47 am
by pcmattman
Ohhhh... you've heard it before :(

Posted: Tue Mar 27, 2007 2:55 am
by jhawthorn
os64dev wrote:Then i will be the 11 th :twisted:
I would have written it 11rd.

Posted: Tue Mar 27, 2007 11:25 am
by Dex
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 :cry: .

Posted: Tue Mar 27, 2007 11:54 am
by urxae
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.
To me, it sounded more like appending to a file failed once an extra sector was needed.
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.
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...
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 :cry: .
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?

Posted: Tue Mar 27, 2007 12:45 pm
by Dex
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...
I am not too sure, if we are talking hdd then its ok.
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.
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?
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.

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.

Posted: Tue Mar 27, 2007 3:18 pm
by pcmattman
My problem is when I call my write function with an offset + count that would require the file to be expanded to cover x more sectors.

I tried allocating more sectors for the file but that failed miserably and corrupted the filesystem.

I can post code if you want.

Posted: Tue Mar 27, 2007 3:43 pm
by Dex
Code would be helpfull .

Posted: Tue Mar 27, 2007 3:47 pm
by pcmattman
Here you go:

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;
	}
Basically, count is the size of the data to be written, offset is the offset in the file to write to.