# HG changeset patch # User Jonathan Pevarnek # Date 1303437591 14400 # Node ID 72d87920de945dbda545b5b7b4252030d9688789 # Parent f0d58047305c2050346d72e76c80f538a85dc662 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... diff -r f0d58047305c -r 72d87920de94 include/fs.h --- 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); diff -r f0d58047305c -r 72d87920de94 src/fs.c --- 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; } diff -r f0d58047305c -r 72d87920de94 src/testFS.c --- 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);