changeset 74:36e6fc4a0487

Added the ability to create a new file The function makeFile will create a file with a given name TODO: redo the testing program (actually, I will probably do this after adding append)
author Jonathan Pevarnek <pevarnj@gmail.com>
date Tue, 26 Apr 2011 22:38:45 -0400
parents 4c47b80ad9ac
children c10465be6160
files .hgignore Makefile include/error.h include/fs.h src/fs.c
diffstat 5 files changed, 40 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Tue Apr 26 21:32:20 2011 -0400
+++ b/.hgignore	Tue Apr 26 22:38:45 2011 -0400
@@ -8,3 +8,5 @@
 ^loader\.bin$
 ^cscope\.out$
 \.swp$
+
+^changes
--- a/Makefile	Tue Apr 26 21:32:20 2011 -0400
+++ b/Makefile	Tue Apr 26 22:38:45 2011 -0400
@@ -95,10 +95,11 @@
 
 src/dynamic.o: include/std.h include/die.h include/operations.h
 src/dynamic.o: include/stack.h
-src/fs.o: include/fs.h include/std.h include/die.h
+src/fs.o: include/fs.h include/std.h include/die.h include/error.h
+src/fs.o: include/tod.h
 src/operations.o: include/operations.h include/std.h include/die.h
 src/operations.o: include/stack.h include/math.h
 src/sarpn.o: include/std.h include/die.h include/operations.h include/stack.h
 src/stack.o: include/stack.h include/std.h include/die.h
 src/std.o: include/std.h include/die.h
-src/testFS.o: include/std.h include/die.h include/fs.h
+src/testFS.o: include/std.h include/die.h include/fs.h include/error.h
--- a/include/error.h	Tue Apr 26 21:32:20 2011 -0400
+++ b/include/error.h	Tue Apr 26 22:38:45 2011 -0400
@@ -27,6 +27,7 @@
 #define SIZETOOLARGE 7 //the user requested a size that is too large to be set for a file
 #define NOTINCACHE 8
 #define DISKFULL 9 //every block on the disk is currently set to be in use
+#define EXISTS 10 //the user tried to create a file that already exists
 
 //Codes: Time of date
 #define UPTIME 1 //the actual time of date was not set in the system
--- a/include/fs.h	Tue Apr 26 21:32:20 2011 -0400
+++ b/include/fs.h	Tue Apr 26 22:38:45 2011 -0400
@@ -53,6 +53,7 @@
 ErrCode init_fs(u32 devnum, u64 __memsize);
 ErrCode getFInfo(u32 n, void* de);
 ErrCode lookupFile(char *fname, u32 *fid);
+ErrCode makeFile(char *fname, u32 *fid);
 ErrCode getFileSize(u32 fid, u32 *size);
 ErrCode getFileData(u32 fid, void *ptr);
 ErrCode setFileData(u32 fid, const void *ptr, size_t size);
--- a/src/fs.c	Tue Apr 26 21:32:20 2011 -0400
+++ b/src/fs.c	Tue Apr 26 22:38:45 2011 -0400
@@ -75,6 +75,39 @@
 	return mkError(MODFS, NOTFILE, ERROR);
 }
 
+//makes an empty file with name fname, sets fid to the fid for the new file
+//If the file already exists, an error is returned but fid will be set to the
+//fid of that file
+ErrCode makeFile(char *fname, u32 *fid)
+{
+	ErrCode err;
+	if(lookupFile(fname, fid) != mkError(MODFS, NOTFILE, ERROR))
+		return mkError(MODFS, EXISTS, ERROR); //the file should not exist
+	if(isError(err = nextFreeBlock(0, fid))) return err;
+
+	Inode nin;
+	nin.size = 0;
+	nin.nblocks = 0;
+	if(isError(err = setTod(&nin))) return err;
+	nin.ctime = nin.mtime;
+	nin._pad0 = nin._pad1 = nin._pad2 = 0; //I am not sure if this is needed...
+	if(isError(err = writeFSBlock(*fid, &nin))) return err;
+
+	//the block is now on the disk, time to add it to the root inode
+	Inode *root = readFSBlock(rootLoc);
+	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;
+	u32 blkno = root->blocks[fileno/DEPBLK]; //block where direntry is
+	Direntry *dp = readFSBlock(blkno);
+	if(!dp) return ERR_FSBLKREADFAIL;
+	dp += fileno%DEPBLK; //dp is set to the current de
+	dp->inode = *fid; //set the file location
+	memcpy(dp->fname, fname, FNAMELEN); //set the file name
+	if(isError(err = sync(blkno))) return err; //update the block
+	return 0;
+}
+
 //PRECON:  fid is a valid file id
 //This sets *size equal to the size of the file
 ErrCode getFileSize(u32 fid, u32 *size)