changeset 69:19000e354e36

Start working on redoing the error codes
author Jonathan Pevarnek <pevarnj@gmail.com>
date Sun, 24 Apr 2011 18:28:12 -0400
parents 406b6e8ec54f
children 3b73044b740f
files include/error.h include/fs.h src/fs.c src/testFS.c
diffstat 4 files changed, 76 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/include/error.h	Sun Apr 24 18:28:12 2011 -0400
@@ -0,0 +1,28 @@
+#ifndef __ERROR_H
+#define __ERROR_H
+
+#define mkError(mod,code,sev) (((mod) << 12) | ((code) << 2) | (sev))
+#define notError(mod) (((mod) << 12) | (NOERROR << 2) | INFO)
+#define isError(code) (((code) & 3) == ERROR)
+#define errCode(code) (((code) >> 2) & 1023)
+
+//Severities
+#define INFO 0
+#define WARN 1
+#define ERROR 2
+
+//Modules
+#define MODFS 1 //stuff in the file system
+
+//Codes
+#define NOERROR 0 //there was no issue
+#define ALLOCFAIL 1 //a general code for a malloc failing
+#define NOMAGIC 2 //The magic code in the superblock was not set (filesystem)
+#define BLKREADFAIL 3 //failure for reading a filesystem block
+#define OUTOFRANGE 4 //
+#define NOTFILE 5
+
+typedef u32 ErrCode;
+
+#endif //__ERROR_H
+
--- a/include/fs.h	Fri Apr 22 00:19:18 2011 -0400
+++ b/include/fs.h	Sun Apr 24 18:28:12 2011 -0400
@@ -1,4 +1,5 @@
 #include <std.h>
+#include <error.h>
 
 #ifndef __FS_H
 #define __FS_H
@@ -46,10 +47,10 @@
 };
 typedef struct DIRENTRY Direntry;
 
-int init_fs(u32 devnum, u64 __memsize);
-u32 getFInfo(u32 n, void* de);
-u32 lookupFile(char *fname);
-int getFileSize(u32 fid, u32 *size);
+ErrCode init_fs(u32 devnum, u64 __memsize);
+ErrCode getFInfo(u32 n, void* de);
+ErrCode lookupFile(char *fname, u32 *fid);
+ErrCode getFileSize(u32 fid, u32 *size);
 int getFileData(u32 fid, void *ptr);
 int setFileData(u32 fid, const void *ptr, size_t size);
 short isBlockAt(u32 block);
--- a/src/fs.c	Fri Apr 22 00:19:18 2011 -0400
+++ b/src/fs.c	Sun Apr 24 18:28:12 2011 -0400
@@ -1,6 +1,7 @@
 #include <fs.h>
 #include <std.h>
 #include <tod.h>
+#include <error.h>
 
 static u32 setTod(Inode *inPtr);
 static u32 getCacheLoc(u32 blkno);
@@ -20,15 +21,15 @@
 static u32 CacheNext; //the next location to toss a cached block into
 static u32 rootLoc;
 
-int init_fs(u32 devnum, u64 __memsize)
+ErrCode init_fs(u32 devnum, u64 __memsize)
 {
 	//Create the cache
 	CacheCap = ((__memsize/100)/FSBLKSIZE)*CACHESPACE;
 	//number of blocks to store in the cache
 	FS_Cache = malloc(CacheCap*FSBLKSIZE);
-	if(!FS_Cache) return -1;
+	if(!FS_Cache) return mkError(MODFS, ALLOCFAIL, ERROR);
 	CacheLocs = malloc(CacheCap);
-	if(!CacheLocs) return -1;
+	if(!CacheLocs) return mkError(MODFS, ALLOCFAIL, ERROR);
 	CacheSize = 0;
 	CacheNext = 0;
 
@@ -36,43 +37,48 @@
 	Superblock *sb;
 	sb = readFSBlock(1);
 	if(!sb) return -1;
-	if(sb->magic != 0x42420374) return -1;
+	if(sb->magic != 0x42420374) return mkError(MODFS, NOMAGIC, ERROR);
 	rootLoc = sb->root_inode;
 
-	return 0;
+	return notError(MODFS);
 }
 
-u32 getFinfo(u32 n, void* de)
+ErrCode getFInfo(u32 n, void* de)
 {
 	Inode *root = readFSBlock(rootLoc);
-	if(!root) return -1;
-	if(n >= root->size/DEPBLK) return -1; //block does not exist
+	if(!root) return mkError(MODFS, BLKREADFAIL, ERROR);
+	if(n >= root->size/DEPBLK) return mkError(MODFS, OUTOFRANGE, WARN);
 	Direntry *dp = readFSBlock(root->blocks[n/DEPBLK]);
-	if(!dp) return -1;
+	if(!dp) return mkError(MODFS, BLKREADFAIL, ERROR);
 	memcpy(de, dp + n%DEPBLK, sizeof(Direntry));
-	return 0;
+	return notError(MODFS);
 }
 
-//This returns the "file ID" of a specific file
-u32 lookupFile(char *fname)
+//This sets *fid to be equal to the fileid of the file with name fname
+ErrCode lookupFile(char *fname, u32 *fid)
 {
 	size_t i;
 	for(i = 0; 1; i++) {
 		Direntry de;
-		if(getFinfo(i, &de)) break;
-		if(!fnameCmp(fname, de.fname)) return de.inode;
+		ErrCode err = getFInfo(i, &de);
+		if(err == mkError(MODFS, OUTOFRANGE, WARN)) break; //have iterated through all files
+		else if(isError(err)) return err; //blah
+		if(!fnameCmp(fname, de.fname)) {
+			*fid = de.inode;
+			return notError(MODFS);
+		}
 	}
-	return 0;
+	return mkError(MODFS, NOTFILE, ERROR);
 }
 
 //PRECON:  fid is a valid file id
-//This sets *size equal to the size of the file.  Will return 0 an success
-int getFileSize(u32 fid, u32 *size)
+//This sets *size equal to the size of the file
+ErrCode getFileSize(u32 fid, u32 *size)
 {
 	Inode *ptr = readFSBlock(fid);
-	if(!ptr) return -1;
+	if(!ptr) return mkError(MODFS, BLKREADFAIL, ERROR);
 	*size = ptr->size;
-	return 0;
+	return notError(MODFS);
 }
 
 //PRECON:  ptr points to a large enough location to hold all the data in file
--- a/src/testFS.c	Fri Apr 22 00:19:18 2011 -0400
+++ b/src/testFS.c	Sun Apr 24 18:28:12 2011 -0400
@@ -1,5 +1,6 @@
 #include <std.h>
 #include <fs.h>
+#include <error.h>
 
 void dumpText(char *text, int size)
 {
@@ -15,30 +16,39 @@
 	size_t i;
 	for(i = 0; 1; i++) {
 		Direntry de;
-		if(getFinfo(i, &de)) break;
+		ErrCode err = getFInfo(i, &de);
+		if(err == mkError(MODFS, OUTOFRANGE, WARN)) break;
+		else if(isError(err)) return err;
 		printFname(de.fname), sPrint("\n");
 	}
-	return 0;
+	return notError(0);
 }
 
 void start(u64 __memsize)
 {
 	init_all(__memsize);
 	char buffer[256];
-	if(init_fs(0x100, __memsize)) goto END;
+	if(isError(init_fs(0x100, __memsize))) goto END;
 
 	while(1) {
-		if(listFiles()) sPrint("WARNING: ERROR IN READING FILE NAMES\n");
+		if(isError(listFiles())) sPrint("WARNING: ERROR IN READING FILE NAMES\n");
 		char fname[28];
 		sPrint("Please enter the file to read: ");
 		getFname(fname);
-		u32 fid = lookupFile(fname);
-		if(!fid) continue; //if fid is 0, the file was not found
+		u32 fid;
+		ErrCode err = lookupFile(fname, &fid);
+		if(isError(err)) {
+			if(errCode(err) == NOTFILE) sPrint("ERROR: Invalid file name\n");
+			continue;
+		}
 		u32 fileSize;
-		if(getFileSize(fid, &fileSize)) continue;
+		if(isError(getFileSize(fid, &fileSize))) continue;
 		char *text = malloc(fileSize);
 		if(!text) continue;
-		if(getFileData(fid, text)) continue;
+		if(getFileData(fid, text)) {
+			free(text);
+			continue;
+		}
 		dumpText(text, fileSize);
 		free(text);
 	}