# HG changeset patch # User Jonathan Pevarnek # Date 1303444693 14400 # Node ID 9816d35104670bb3f7083bcc10b5827c10a40c42 # Parent b30006e3fb42dc26ca102161455131e3b41efbcd Redid the interface for handling file data The user can now get the file data for a file n (n has nothing to do with any part of the program other than if a != b then the function will give different results for a and b (this can be used to implement listFiles() diff -r b30006e3fb42 -r 9816d3510467 include/fs.h --- a/include/fs.h Thu Apr 21 23:13:03 2011 -0400 +++ b/include/fs.h Thu Apr 21 23:58:13 2011 -0400 @@ -8,7 +8,7 @@ #define FSBLKSIZE 1024 #define DSKBLKSIZE 512 #define MAXBLOCKS 248 -#define DEPBLK 32 //director entries per block +#define DEPBLK (FSBLKSIZE / sizeof(Direntry)) //director entries per block struct SUPERBLOCK { u32 magic; // == 0x42420374 @@ -47,7 +47,7 @@ typedef struct DIRENTRY Direntry; int init_fs(u32 devnum, u64 __memsize); -u32 listFiles(); +u32 getFInfo(u32 n, void* de); u32 lookupFile(char *fname); int getFileSize(u32 fid, u32 *size); int getFileData(u32 fid, void *ptr); diff -r b30006e3fb42 -r 9816d3510467 src/fs.c --- a/src/fs.c Thu Apr 21 23:13:03 2011 -0400 +++ b/src/fs.c Thu Apr 21 23:58:13 2011 -0400 @@ -42,68 +42,27 @@ return 0; } -//this lists all the files -u32 listFiles() +u32 getFinfo(u32 n, void* de) { - u32 ret = 0; - size_t i; - Inode *rootINode = malloc(sizeof(Inode)); - if(!rootINode) return -1; - if(getBlockData(rootLoc, rootINode)) { - ret = -1; - goto end; - } - - 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) { - ret = -1; - goto end; - } - } - printFname(dp->fname), putline("\n", 1); - } - -end: - free(rootINode); - return ret; + Inode *root = readFSBlock(rootLoc); + if(!root) return -1; + if(n >= root->size/DEPBLK) return -1; //block does not exist + Direntry *dp = readFSBlock(root->blocks[n/DEPBLK]); + if(!dp) return -1; + memcpy(de, dp + n%DEPBLK, sizeof(Direntry)); + return 0; } //This returns the "file ID" of a specific file u32 lookupFile(char *fname) { - //XXX XXX XXX TODO TODO XXX XXX XXX - //XXX MASSIVE CODE DUPLICATION XXX - u32 ret = 0; size_t i; - Inode *rootINode = malloc(sizeof(Inode)); - if(!rootINode) return -1; - if(getBlockData(rootLoc, rootINode)) { - ret = -1; - goto end; + for(i = 0; 1; i++) { + Direntry de; + if(getFinfo(i, &de)) break; + if(!fnameCmp(fname, de.fname)) return de.inode; } - - 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) { - ret = -1; - goto end; - } - } - if(!fnameCmp(fname, dp->fname)) return dp->inode; //XXX THIS IS THE ONLY NON DUPLICATED LINE XXX - } - -end: - free(rootINode); - return ret; + return 0; } //PRECON: fid is a valid file id diff -r b30006e3fb42 -r 9816d3510467 src/testFS.c --- a/src/testFS.c Thu Apr 21 23:13:03 2011 -0400 +++ b/src/testFS.c Thu Apr 21 23:58:13 2011 -0400 @@ -10,6 +10,17 @@ } while(size > 0); } +u32 listFiles() +{ + size_t i; + for(i = 0; 1; i++) { + Direntry de; + if(getFinfo(i, &de)) break; + printFname(de.fname), sPrint("\n"); + } + return 0; +} + void start(u64 __memsize) { init_all(__memsize);