diff --git a/customfilesystem.c b/customfilesystem.c index 1571140..3780acf 100644 --- a/customfilesystem.c +++ b/customfilesystem.c @@ -3,56 +3,105 @@ #include #include #include -#include "fs_level1.c" #include "fs_level2.c" + +SUPER *sp = NULL; +GD *gp = NULL; +INODE *ip = NULL; +DIR *dp = NULL; + +MINODE minode[NMINODE] = {0}; +PROC proc[NPROC] = {0}, *running = NULL; +MINODE *root = NULL; + +int dev = 0, imap = 0, bmap = 0; +int ninodes = 0, nblocks = 0; +int blk = 0, offset = 0; +int inodes_begin_block = 0, inodes_per_block = 0; +char buf[BLKSIZE] = { 0 }; +char buf2[BLKSIZE] = { 0 }; + +void split_command(char *pathname, char **names) +{ + int i = 0; + //char** names = malloc(64*sizeof(char *)); + const char* delim = " "; + + names[i] = strtok(pathname, delim); + while(names[i] != NULL) + { + i++; + names[i] = strtok(NULL, delim); + } + names[i - 1] = strtok(names[i - 1], "\n"); +} + +typedef struct open_file +{ + int fd; + char *name; +}openFile; + int main(int argc, char *argv[], char *env[]) { - int err = 0, i = 0; - char* input; + int err = 0, i = 0, k = 0; + char input[256], output[256], frankenstring[256], output2[4096]; char* splitInput[256]; + openFile of; - err = mount_root(); + of.name = NULL; + + /*err =*/ mount_root(); - if (!err) + /*if (!err) { printf ("Error mounting root"); return 0; - } + }*/ while (1) { printf ("Please type a command: "); - scanf ("%s", input); + fgets (input, 256, stdin); printf ("\n"); while(i < 256) { splitInput[i] = NULL; + frankenstring[i] = '\0'; i++; } + i = 1; + + splitInput[0] = strtok(input, " "); + while (splitInput[i] = strtok(NULL, " ")) - i = 0; - - while (splitInput[i] = strtok(NULL, " ") { i++; } - if (!strcmp(splitInput[0], "ls")) + if (!strcmp(splitInput[0], "ls") || !strcmp(splitInput[0], "ls\n")) { - ls(splitInput[1]); + if (splitInput[1] != NULL) + { + ls(splitInput[1]); + } + else + { + ls("."); + } } - else if (splitinput[1] && !strcmp(splitInput[0], "cd")) + else if (splitInput[1] && !strcmp(splitInput[0], "cd")) { cd(splitInput[1]); } else if (!strcmp(splitInput[0], "pwd")) { - pwd(); + //pwd(); } else if (splitInput[1] && !strcmp(splitInput[0], "mkdir")) { @@ -62,7 +111,7 @@ int main(int argc, char *argv[], char *env[]) { rmdir_fs(splitInput[1]); } - else if (splitInput1 && !strcmp(splitInput[0], "creat")) + else if (splitInput[1] && !strcmp(splitInput[0], "creat")) { creat_fs(splitInput[1]); } @@ -70,9 +119,9 @@ int main(int argc, char *argv[], char *env[]) { fs_link(splitInput[1], splitInput[2]); } - else if (splitInput[1] && splitInput[2] && !strcmp(splitInput[0], "unlink")) + else if (splitInput[1] && !strcmp(splitInput[0], "unlink")) { - fs_unlink(splitInput[1], splitInput[2]); + fs_unlink(splitInput[1]); } else if (splitInput[1] && splitInput[2] && !strcmp(splitInput[0], "symlink")) { @@ -80,19 +129,89 @@ int main(int argc, char *argv[], char *env[]) } else if (!strcmp(splitInput[0], "readlink")) { - fs_readlink(); + fs_readlink(splitInput[1], output); } - else if (!strcmp(splitInput[0], "open") && splitInput[1]) + else if (!strcmp(splitInput[0], "open") && splitInput[1] +&& splitInput[2]) { - fs_open(splitInput[1]); + if (of.name == NULL) + { + printf(""); + of.fd = fs_open(splitInput[1], atoi(splitInput[2])); + of.name = splitInput[1]; + printf("file descriptor: %d\n", of.fd); + } + else + { + printf("Please close previous file before opening another.\n"); + } } else if (!strcmp(splitInput[0], "close") && splitInput[1]) { - fs_close(splitInput[1]); + fs_close(of.fd); + + of.name = NULL; + } + else if (!strcmp(splitInput[0], "write") && splitInput[1] +&& splitInput[2]) + { + if (*splitInput[2] == '"') + { + for (int j = 2; splitInput[j] != NULL; j++) + { + for (int m = 0; splitInput[j][m] != '\0'; m++) + { + if (splitInput[j][m] == '"'){} + else + { + frankenstring[k] = splitInput[j][m]; + k++; + } + } + + frankenstring[k] = ' '; + + k++; + } + + printf("text: %s\n", frankenstring); + + write_file(of.fd, frankenstring, strlen(frankenstring)); + } + else + { + printf("text: %s\n", splitInput[2]); + + write_file(of.fd, splitInput[2], strlen(splitInput[2])); + } + + } + else if (!strcmp(splitInput[0], "read")) + { + read_file(of.fd, output2, 4096); } else if (!strcmp(splitInput[0], "lseek") && splitInput[1] && splitInput[2]) { - fs_lseek(splitInput[1], splitInput[2]); + if (of.name != NULL) + { + fs_lseek(of.fd, atoi(splitInput[2])); + } + else + { + printf("File is not open."); + } + } + else if (!strcmp(splitInput[0], "chmod") && splitInput[1] && splitInput[2]) + { + //fs_chmod(atoi(splitInput[1], splitInput[2]) + } + else if (!strcmp(splitInput[0], "touch") && splitInput[1]) + { + //fs_touch(splitInput[1]); + } + else if (!strcmp(splitInput[0], "exit\n")) + { + break; } else { @@ -101,100 +220,36 @@ int main(int argc, char *argv[], char *env[]) } } -int mount_root() -{ - dev = open("diskimage", O_RDONLY); +void mount_root() +{ + dev = open("diskimage", O_RDWR); root = iget(dev, 2); - printf("root\n"); - printf("dev: %d\n", root->dev); - printf("ino: %d\n", root->ino); - printf("refCount: %d\n", root->refCount); - printf("dirty: %d\n", root->dirty); - printf("mounted: %d\n", root->mounted); - + //set processes current working directory to root minode proc[0].cwd = iget(root->dev, 2); proc[1].cwd = iget(root->dev, 2); + //set running process to first process running = &proc[0]; - if (!dev && !root && !running) - { - return 0; - } - - return 1; } -int ls(char* pathname) + +void init() { - int ino; - int i_size = 0; - dev = running->cwd->dev; + int i = 0; - MINODE* mip = running->cwd; - - if(pathname) - { - if(pathname[0] == "/") - { - dev = root->dev; - } - else if(pathname[0] == '\n') - { - printf ("No path entered\n"); - pathname[0] = '.'; - pathname[1] = '\0'; - } - + proc[0].uid = 0; + proc[1].uid = 1; + proc[0].cwd = 0; + proc[1].cwd = 0; - printf("getting ino with entered pathname...\n"); - ino = getino(dev, pathname); - printf("got ino!\n", root->dev); - - mip = iget(dev, ino); - } - else + for(i = 0; i < NMINODE; i++) { - printf ("No path entered\n"); - - pathname[0] = '.'; - pathname[1] = '\0'; - - printf("getting current ino ...\n"); - ino = getino(dev, pathname); - printf("got ino!\n", root->dev); - - - mip = iget(3, ino); + minode[i].refCount = 0; } - ip = &mip->inode; - i_size = ip->i_size; - - int i = 0; - for(i = 0; i < 12; i++) - { - - if(ip->i_block[i] != 0) - { - get_block(3, ip->i_block[i], buf); - dp = (DIR *)buf; - i_size -= dp->rec_len; - - - print_dir(); - while(dp != NULL && i_size > 0) - { - dp = (DIR *)((char *)dp + dp->rec_len); - print_dir(); - i_size -= dp->rec_len; - } - - if(i_size <= 0) - break; - } - } } +