diff --git a/src/fsactions.c b/src/fsactions.c index 5013f23..7a04656 100644 --- a/src/fsactions.c +++ b/src/fsactions.c @@ -9,19 +9,15 @@ #include "fsactions.h" #include "fusestructs.h" -/* Things to set for directory: - * Name, isValid, inode, isDirectory=1 - */ - -/* Things to set for file: - * Name, size, isValid, inode, isDirectory=0 - */ - // Adds a file to the filesystem -// TODO: Fix implementation to add starting at Inode 0 void AddFileToFS(FileSystem* fs, char* fname) { int inputfd = open(fname, O_RDONLY); + if (inputfd == -1) + { + fprintf(stderr, "Failed opening input file\n"); + exit(1); + } struct stat tempstat; ino_t currentInode = 0; ino_t newInode; @@ -61,6 +57,7 @@ void AddFileToFS(FileSystem* fs, char* fname) strcpy(fs->inodes[currentInode].blocks[newDirectBlock].name, fname); fs->inodes[currentInode].blocks[newDirectBlock].inode = newInode; read(inputfd, &(fs->dataBlocks[newInode].byte), tempstat.st_size); + close(inputfd); return; } @@ -120,10 +117,45 @@ int _RemoveFileFromFS_(FileSystem* fs, ino_t inodeNumber, char* fname) } // Extract a file from the filesystem -// TODO: Fix function so that it adds directories, then the file after void ExtractFileFromFS(FileSystem* fs, char* fname) { - + + ino_t currentInode = 0; + ino_t newInode; + char* tempname; + // Get first directory, labelled by 0 to splitLocation-1, + // assuming splitLocation not 0; + int splitLocation = FindNextDirectory(fname); + if (splitLocation == 0) + { + fname = fname + 1; + splitLocation = FindNextDirectory(fname); + } + // While there is a '/', make a new inode out of left and keep right side + while (splitLocation > -1) + { + tempname = strndup(fname, splitLocation); + newInode = FindDirectory(fs->inodes[currentInode], tempname); + if (newInode == -1) + { + fprintf(stderr, "Error extracting file due to path\n"); + exit(1); + } + currentInode = newInode; + fname = fname + splitLocation + 1; + splitLocation = FindNextDirectory(fname); + } + for (int i = 0; i < DEFAULTINODEMAX; i++) + { + if (strcmp(fs->inodes[currentInode].blocks[i].name, fname) == 0) + { + newInode = fs->inodes[currentInode].blocks[i].inode; + off_t size = fs->inodes[currentInode].blocks[i].size; + write(STDOUT_FILENO, fs->dataBlocks[newInode].byte, size); + return; + } + } + return; } diff --git a/test/files/output.txt b/test/files/output.txt new file mode 100644 index 0000000..1ac88df --- /dev/null +++ b/test/files/output.txt @@ -0,0 +1 @@ +wind