From ee19c23ea8b1cea15d36584bc40c178764f3b88a Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Tue, 6 Dec 2022 20:25:22 -0600 Subject: [PATCH] Found issue relying within the next listing of the program causing issues --- include/fsactions.h | 3 ++- src/fsactions.c | 41 ++++++++++++++++++----------------------- test/test.c | 16 +++++++++++----- 3 files changed, 31 insertions(+), 29 deletions(-) diff --git a/include/fsactions.h b/include/fsactions.h index a6f9c1a..58f4bd6 100644 --- a/include/fsactions.h +++ b/include/fsactions.h @@ -13,8 +13,9 @@ ino_t GetFreeInodeNumber(Inode inodes[]); ino_t GetFreeBlockNumber(int fbl[], unsigned short size); int FindEmptyBitPosition(int number); int FindNextDirectory(char* fname); -int IsDirectorySetup(Inode inode, ino_t* inodePosition, char* directoryName); +int FindDirectory(Inode inode, char* directoryName); void SetFileSystemDefaults(FileSystem* fs); void SetupRootDirectory(FileSystem* fs); +void SetupDirectoryBlock(DirectBlock* newBlock, ino_t inode); #endif diff --git a/src/fsactions.c b/src/fsactions.c index 9bdd448..f988e03 100644 --- a/src/fsactions.c +++ b/src/fsactions.c @@ -45,21 +45,18 @@ void AddFileToFS(FileSystem* fs, char* fname) // Third: get a free inode number // Fourth: set info for direct block tempname = strndup(fname, splitLocation); - if (!IsDirectorySetup(fs->inodes[currentInode], ¤tInode, tempname)) + newInode = FindDirectory(fs->inodes[currentInode], tempname); + if (newInode == -1) { for (int i = 0; i < 4; i++) tempfbl[i] = (currentInode*4) + i; newDirectBlock = GetFreeBlockNumber(tempfbl, 4); newInode = GetFreeInodeNumber(fs->inodes); - printf("Name held: |%s|\n", tempname); - printf("Block: |%i|\n", newDirectBlock); strcpy(fs->inodes[currentInode].blocks[newDirectBlock].name, tempname); free(tempname); - fs->inodes[currentInode].blocks[newDirectBlock].isValid = 1; - fs->inodes[currentInode].blocks[newDirectBlock].isDirectory = 1; - fs->inodes[currentInode].blocks[newDirectBlock].inode = newInode; + SetupDirectoryBlock(&(fs->inodes[currentInode].blocks[newDirectBlock]), newInode); + fs->inodes[newInode].isValid = 1; currentInode = newInode; - fs->inodes[currentInode].isValid = 1; } fname = fname + splitLocation + 1; splitLocation = FindNextDirectory(fname); @@ -70,22 +67,15 @@ void AddFileToFS(FileSystem* fs, char* fname) fs->inodes[currentInode].blocks[newDirectBlock].isValid = 1; fs->inodes[currentInode].blocks[newDirectBlock].size = tempstat.st_size; strcpy(fs->inodes[currentInode].blocks[newDirectBlock].name, fname); + printf("%s\n", fs->inodes[0].blocks[0].name); return; } // List path names of all used inodes void ListFS(FileSystem* fs) { - Inode* fsRoot = &(fs->inodes[0]); printf("/\n"); - for (int i = 0; i < fs->superBlock.inodeCount; i++) - { - if (!(fsRoot->blocks[i].isValid)) - continue; - printf("%s\n", fsRoot->blocks[i].name); - if (fsRoot->blocks[i].isDirectory) - _ListFS_(fs, fsRoot->blocks[i].inode, 1); - } + _ListFS_(fs, 0, 0); return; } @@ -165,8 +155,8 @@ int FindNextDirectory(char* fname) return -1; } -// Bool function checks if directory is setup. If it is, then sets the inode -int IsDirectorySetup(Inode inode, ino_t* inodePosition, char* directoryName) +// Function checks if directory is setup. If it is, then returns the inode +int FindDirectory(Inode inode, char* directoryName) { for (int i = 0; i < 128; i++) { @@ -175,12 +165,9 @@ int IsDirectorySetup(Inode inode, ino_t* inodePosition, char* directoryName) if (!(inode.blocks[i].isDirectory)) continue; if (strcmp(inode.blocks[i].name, directoryName) == 0) - { - *inodePosition = i; - return 1; - } + return i; } - return 0; + return -1; } // Function for setting all defaults of filesystem @@ -202,3 +189,11 @@ void SetupRootDirectory(FileSystem* fs) fs->inodes[0].isValid = 1; return; } + +void SetupDirectoryBlock(DirectBlock* newBlock, ino_t inode) +{ + newBlock->isValid = 1; + newBlock->isDirectory = 1; + newBlock->inode = inode; + return; +} diff --git a/test/test.c b/test/test.c index 549bc63..cdc776d 100644 --- a/test/test.c +++ b/test/test.c @@ -64,18 +64,24 @@ void Test_FileSystem_Should_AddFile(void) argv[1] = "-a"; GetArguments(argc, argv, &dummyFuse); RunFuse(fakefs, &dummyFuse); - argv[2] = "test/files/test2.txt"; - GetArguments(argc, argv, &dummyFuse); - RunFuse(fakefs, &dummyFuse); + TEST_ASSERT_EQUAL(1, fakefs->inodes[0].isValid); + TEST_ASSERT_EQUAL_CHAR_ARRAY("test", fakefs->inodes[0].blocks[0].name, 4); TEST_ASSERT_EQUAL(1, fakefs->inodes[1].isValid); + TEST_ASSERT_EQUAL_CHAR_ARRAY("files", fakefs->inodes[1].blocks[0].name, 5); + TEST_ASSERT_EQUAL(1, fakefs->inodes[2].isValid); TEST_ASSERT_EQUAL_CHAR_ARRAY("test1.txt", fakefs->inodes[2].blocks[0].name, 9); } void Test_FileSystem_Should_ListContents(void) { Fuse(argc, argv); - printf("%s\n", fakefs->inodes[0].blocks[0].name); - TEST_IGNORE_MESSAGE("This only runs the list operation. Check info yourself."); + TEST_ASSERT_EQUAL(1, fakefs->inodes[0].isValid); + TEST_ASSERT_EQUAL_CHAR_ARRAY("test", fakefs->inodes[0].blocks[0].name, 4); + TEST_ASSERT_EQUAL(1, fakefs->inodes[1].isValid); + TEST_ASSERT_EQUAL_CHAR_ARRAY("files", fakefs->inodes[1].blocks[0].name, 5); + TEST_ASSERT_EQUAL(1, fakefs->inodes[2].isValid); + TEST_ASSERT_EQUAL_CHAR_ARRAY("test1.txt", fakefs->inodes[2].blocks[0].name, 9); + TEST_MESSAGE("This only runs the list operation. Check info yourself."); } int main(void)