changeset 71:a435ee2c668a

Set more of the code to properly utilize error codes
author Jonathan Pevarnek <pevarnj@gmail.com>
date Mon, 25 Apr 2011 13:35:43 -0400
parents 3b73044b740f
children 7f312eb75ec0
files include/fs.h src/fs.c
diffstat 2 files changed, 21 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/include/fs.h	Mon Apr 25 10:15:56 2011 -0400
+++ b/include/fs.h	Mon Apr 25 13:35:43 2011 -0400
@@ -56,7 +56,7 @@
 ErrCode getFileSize(u32 fid, u32 *size);
 ErrCode getFileData(u32 fid, void *ptr);
 ErrCode setFileData(u32 fid, const void *ptr, size_t size);
-short isBlockAt(u32 block);
+ErrCode isBlockAt(u32 block, short *b);
 void printFname(char *name);
 void getFname(char *fname);
 int fnameCmp(const char *a, const char *b);
--- a/src/fs.c	Mon Apr 25 10:15:56 2011 -0400
+++ b/src/fs.c	Mon Apr 25 13:35:43 2011 -0400
@@ -15,7 +15,7 @@
 
 static u32 DevID;
 static u32 CacheCap; //how many blocks the cache can hold
-static u32 CacheSize; //the current number of elts in the cache
+static u32 CacheSize; //the current number of items in the cache
 static FSBlk *FS_Cache = NULL; //A pointer to the actual cache
 static u32 *CacheLocs; //what location each block points to
 static u32 CacheNext; //the next location to toss a cached block into
@@ -37,7 +37,7 @@
 	Superblock *sb;
 	sb = readFSBlock(1);
 	if(!sb) return ERR_FSBLKREADFAIL;
-	if(sb->magic != 0x42420374) return mkError(MODFS, MUGGLE, ERROR);
+	if(sb->magic != FSMAGICNUM) return mkError(MODFS, MUGGLE, ERROR);
 	rootLoc = sb->root_inode;
 
 	return 0;
@@ -113,7 +113,7 @@
 {
 	ErrCode ret = 0, err;
 	unsigned int i;
-	if(isError(err = setFileSize(fid, size))) { //TODO efficiency (talk to Jeff)
+	if(isError(err = setFileSize(fid, size))) {
 		ret = err;
 		goto end; 
 	}
@@ -178,8 +178,14 @@
 	} else { //there are fewer blocks
 		for(i = in.nblocks; i < neededBlocks; i++) {
 			in.blocks[i] = 0;
-			for(j = in.blocks[i - 1]; in.blocks[i] == 0; j++)
-				if(!isBlockAt(j)) in.blocks[i] = j;
+			for(j = in.blocks[i - 1]; in.blocks[i] == 0; j++) {
+				short ib;
+				if(isError(err = isBlockAt(j, &ib))) {
+					ret = err;
+					goto end;
+				}
+				if(!ib) in.blocks[i] = j;
+			}
 			if(isError(err = setBlockAt(in.blocks[i], 1))) {
 				ret = err;
 				goto end;
@@ -200,14 +206,15 @@
 	return ret;
 }
 
-short isBlockAt(u32 block)
-{ //TODO ErrCode
+ErrCode isBlockAt(u32 block, short *b)
+{
 	u32 inBlock = block/(8*FSBLKSIZE) + 2; //which block the bit is in
 	u32 blockSpot = (block%(8*FSBLKSIZE))/8; //which u8 the bit is in
 	u32 spotSpot = 7 - block%8; //which bit the bit is
 	u8 *ptr = readFSBlock(inBlock);
-//	if(!ptr) return -1; //TODO return value
-	return (ptr[blockSpot] >> spotSpot) & 1;
+	if(!ptr) return ERR_FSBLKREADFAIL;
+	*b = (ptr[blockSpot] >> spotSpot) & 1;
+	return 0;
 }
 
 //sets the block table to indicate whether there is a block at block
@@ -271,7 +278,7 @@
 }
 
 //PRECON:   block blkno is not already in the cache
-//Returns the locaion in the cache where the data is
+//Returns the location in the cache where the data is
 //sets aside space in the cache for some item.  NOTE: this does not read the
 //current contents of the disk to the cache
 static u32 cacheAdd(u32 blkno)
@@ -320,7 +327,9 @@
 	if(isError(err = fba_write_blk(DevID, blkno*2, nextDSKB))) return err;
 	//load from the cache to the disk
 	if(isError(err = fba_write_blk(DevID, blkno*2 + 1, nextDSKB + 1))) return err;
-	if(!isBlockAt(blkno)) {
+	short ib;
+	if(isError(err = isBlockAt(blkno, &ib))) return err;
+	if(!ib) {
 		if(isError(err = setBlockAt(blkno, 1))) return makeWarn(err);
 	}
 	return 0;