changeset 64:72d87920de94

Direntries are no longer cached separately from everything else Main thing: direntries are no longer cached separately from everything else, this was done to make future changes with creating/deleting a file much easier There is currently a TON of code duplication in listFiles() and lookupFile(fname), I am still trying to think of a better way to do this...
author Jonathan Pevarnek <pevarnj@gmail.com>
date Thu, 21 Apr 2011 21:59:51 -0400
parents f0d58047305c
children cd108c0a9030
files include/fs.h src/fs.c src/testFS.c
diffstat 3 files changed, 50 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/include/fs.h	Thu Apr 21 21:23:16 2011 -0400
+++ b/include/fs.h	Thu Apr 21 21:59:51 2011 -0400
@@ -8,6 +8,7 @@
 #define FSBLKSIZE 1024
 #define DSKBLKSIZE 512
 #define MAXBLOCKS 248
+#define DEPBLK 32 //director entries per block
 
 struct SUPERBLOCK {
 	u32 magic;              // == 0x42420374
@@ -46,7 +47,7 @@
 typedef struct DIRENTRY Direntry;
 
 int init_fs(u32 devnum, u64 __memsize);
-void listFiles();
+u32 listFiles();
 u32 lookupFile(char *fname);
 int getFileSize(u32 fid, u32 *size);
 int getFileData(u32 fid, void *ptr);
--- a/src/fs.c	Thu Apr 21 21:23:16 2011 -0400
+++ b/src/fs.c	Thu Apr 21 21:59:51 2011 -0400
@@ -13,14 +13,12 @@
 static int sync(u32 blkno);
 
 static u32 DevID;
-static Direntry *Direntries = NULL;
-static int NFiles;
-
 static u32 CacheCap; //how many blocks the cache can hold
 static u32 CacheSize; //the current number of elts 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
+static u32 rootLoc;
 
 int init_fs(u32 devnum, u64 __memsize)
 {
@@ -39,35 +37,66 @@
 	sb = readFSBlock(1);
 	if(!sb) return -1;
 	if(sb->magic != 0x42420374) return -1;
-	Inode *rootINode;
-	u32 rootLoc = sb->root_inode;
-	rootINode = readFSBlock(rootLoc);
-	if(!rootINode) return -1;
-	NFiles = rootINode->size/sizeof(Direntry);
-	Direntries = malloc(rootINode->size); //get array of the files
-	if(!Direntries) return -1;
-	if(getFileData(rootLoc, Direntries)) return -1;
+	rootLoc = sb->root_inode;
 
 	return 0;
 }
 
 //this lists all the files
-void listFiles()
+u32 listFiles()
 {
-	Direntry *dp;
-	for(dp = Direntries; dp - Direntries < NFiles; dp++)
+	size_t i;
+	Inode *rootINode = malloc(sizeof(Inode));
+	if(!rootINode) return -1;
+	if(getBlockData(rootLoc, rootINode)) {
+		free(rootINode);
+		return -1;
+	}
+
+	u32 NFiles = rootINode->size/sizeof(Direntry);
+
+	Direntry *dp = NULL;
+	for(i = 0; i < NFiles; i++, dp++) {
+		if(!(i%DEPBLK)) {
+			dp = readFSBlock(rootINode->blocks[i/DEPBLK]);
+			if(!dp) {
+				free(rootINode);
+				return -1;
+			}
+		}
 		printFname(dp->fname), putline("\n", 1);
+	}
+	free(rootINode);
+	return 0;
 }
 
 //This returns the "file ID" of a specific file
 u32 lookupFile(char *fname)
 {
-	Direntry *dp;
-	for(dp = Direntries; dp - Direntries < NFiles; dp++) {
-		if(!fnameCmp(fname, dp->fname)) {
-			return dp->inode;
+	//XXX XXX XXX TODO TODO XXX XXX XXX
+	//XXX MASSIVE CODE DUPLICATION XXX
+	size_t i;
+	Inode *rootINode = malloc(sizeof(Inode));
+	if(!rootINode) return -1;
+	if(getBlockData(rootLoc, rootINode)) {
+		free(rootINode);
+		return -1;
+	}
+
+	u32 NFiles = rootINode->size/sizeof(Direntry);
+
+	Direntry *dp = NULL;
+	for(i = 0; i < NFiles; i++, dp++) {
+		if(!(i%DEPBLK)) {
+			dp = readFSBlock(rootINode->blocks[i/DEPBLK]);
+			if(!dp) {
+				free(rootINode);
+				return -1;
+			}
 		}
+		if(!fnameCmp(fname, dp->fname)) return dp->inode; //XXX THIS IS THE ONLY NON DUPLICATED LINE XXX
 	}
+	free(rootINode);
 	return 0;
 }
 
--- a/src/testFS.c	Thu Apr 21 21:23:16 2011 -0400
+++ b/src/testFS.c	Thu Apr 21 21:59:51 2011 -0400
@@ -17,8 +17,7 @@
 	if(init_fs(0x100, __memsize)) goto END;
 
 	while(1) {
-		//Prints off the name of each file
-		listFiles();
+		if(listFiles()) sPrint("WARNING: ERROR IN READING FILE NAMES\n");
 		char fname[28];
 		sPrint("Please enter the file to read: ");
 		getFname(fname);