Expanding Files?

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:

Expanding Files?

Post 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.
User avatar
os64dev
Member
Member
Posts: 553
Joined: Sat Jan 27, 2007 3:21 pm
Location: Best, Netherlands

Post by os64dev »

There are 10 people in this world that understand binary...
Then i will be the 11 th :twisted:
Author of COBOS
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 »

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.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post 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]
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 »

Ohhhh... you've heard it before :(
User avatar
jhawthorn
Member
Member
Posts: 58
Joined: Sun Nov 26, 2006 4:06 pm
Location: Victoria, BC, Canada
Contact:

Post by jhawthorn »

os64dev wrote:Then i will be the 11 th :twisted:
I would have written it 11rd.
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Post 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: .
urxae
Member
Member
Posts: 149
Joined: Sun Jul 30, 2006 8:16 am
Location: The Netherlands

Post 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?
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

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

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.
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Post by Dex »

Code would be helpfull .
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 »

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