changeset 76:6538d7c45ad8

I may have append working, got rid of setFileData I am going to work on another function soon to write to some offset in the file For now, I am going to work on making testFS be a bit more featureful (so I can actually test stuff)
author Jonathan Pevarnek <pevarnj@gmail.com>
date Wed, 27 Apr 2011 09:24:44 -0400
parents c10465be6160
children 771320cf8c80
files include/fs.h src/fs.c
diffstat 2 files changed, 34 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/include/fs.h	Tue Apr 26 23:17:35 2011 -0400
+++ b/include/fs.h	Wed Apr 27 09:24:44 2011 -0400
@@ -54,9 +54,9 @@
 ErrCode getFInfo(u32 n, void* de);
 ErrCode lookupFile(char *fname, u32 *fid);
 ErrCode makeFile(char *fname, u32 *fid);
+ErrCode fileAppend(u32 fid, void *data, u32 length);
 ErrCode getFileSize(u32 fid, u32 *size);
 ErrCode getFileData(u32 fid, void *ptr);
-ErrCode setFileData(u32 fid, const void *ptr, size_t size);
 void printFname(char *name);
 void getFname(char *fname);
 int fnameCmp(const char *a, const char *b);
--- a/src/fs.c	Tue Apr 26 23:17:35 2011 -0400
+++ b/src/fs.c	Wed Apr 27 09:24:44 2011 -0400
@@ -96,6 +96,7 @@
 
 	//the block is now on the disk, time to add it to the root inode
 	Inode *root = readFSBlock(rootLoc);
+	if(!root) return ERR_FSBLKREADFAIL;
 	if(isError(err = setFileSize(rootLoc, root->size + sizeof(Direntry)))) return err;
 	root = readFSBlock(rootLoc); //just in case the cache get messed up
 	u32 fileno = root->size/sizeof(Direntry) - 1;
@@ -109,6 +110,38 @@
 	return 0;
 }
 
+ErrCode fileAppend(u32 fid, void *data, u32 length)
+{
+	ErrCode err;
+	ErrCode ret = 0;
+	Inode *iptr = readFSBlock(fid);
+	if(!iptr) return ERR_FSBLKREADFAIL;
+	u32 oldSize = iptr->size;
+	if(isError(err = setFileSize(fid, oldSize + length))) return err;
+	Inode in;
+	if(isError(err = getBlockData(fid, &in))) return err;
+
+	//Each iteration here should copy all the data that needs to go into one block...
+	u32 maxSize = in.size; //the final size the file should hold
+	u32 currSize = oldSize;
+	while(currSize < maxSize) {
+		u32 currBlock = in.blocks[currSize/FSBLKSIZE];
+		void *ptr = readFSBlock(currBlock);
+		if(!ptr) return ERR_FSBLKREADFAIL;
+		u32 tcOS = currSize%FSBLKSIZE; //the current offset for the block
+		u32 tcSize = Min_u32(maxSize - currSize, FSBLKSIZE) - tcOS;
+		memcpy(ptr + tcOS, data, tcSize);
+		if(isError(err = sync(currBlock))) return err;
+		data += tcSize;
+		currSize += tcSize;
+	}
+	
+	if(isError(err = setTod(&in))) ret = makeWarn(err);
+	//everything else should still work fine, just a bad time
+	if(isError(err = writeFSBlock(fid, &in))) return err;
+	return ret;
+}
+
 //PRECON:  fid is a valid file id
 //This sets *size equal to the size of the file
 ErrCode getFileSize(u32 fid, u32 *size)
@@ -147,47 +180,6 @@
 	return ret;
 }
 
-ErrCode setFileData(u32 fid, const void *ptr, size_t size)
-{
-	ErrCode ret = 0, err;
-	unsigned int i;
-	if(isError(err = setFileSize(fid, size))) {
-		ret = err;
-		goto end; 
-	}
-	Inode in;
-	if(isError(err = getBlockData(fid, &in))) {
-		ret = err;
-		goto end;
-	}
-
-	for(i = 0; i < in.nblocks; i++) {
-		Inode *filePtr = readFSBlock(in.blocks[i]);
-		if(!filePtr) {
-			ret = ERR_FSBLKREADFAIL;
-			goto end;
-		}
-		memcpy(filePtr, ptr, Min_u32(size, FSBLKSIZE));
-		if(isError(err = sync(in.blocks[i]))) {
-			ret = err;
-			goto end;
-		}
-		ptr += FSBLKSIZE;
-		size -= FSBLKSIZE;
-	}
-
-	if(isError(err = setTod(&in))) { //sadly, this needs to be updated...
-		ret = makeWarn(err); //everything else should still be good
-	}
-	if(isError(err = writeFSBlock(fid, &in))) {
-		ret = err;
-		goto end;
-	}
-
-end:
-	return ret;
-}
-
 void printFname(char *name)
 {
 	putline(name, 28);