From 0e6af896c3ee8a937c71cbf0eb12e1d1cdc4510d Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Sat, 3 Dec 2022 18:35:51 -0600 Subject: [PATCH] Fixed structure of inodes, setup testing for opening and adding fs --- Makefile | 1 - include/fuse.h | 72 +++++++++++++++++++++++----------- include/fuseactions.h | 15 +++++--- src/fuse.c | 48 ++++++++++++++++------- src/fuseactions.c | 89 +++++++++++++++++++++++++++++++------------ test/test.c | 23 ++++++++++- 6 files changed, 179 insertions(+), 69 deletions(-) diff --git a/Makefile b/Makefile index 9f2882d..248b499 100644 --- a/Makefile +++ b/Makefile @@ -28,7 +28,6 @@ testLink: testExec: ./bin/test.out - test/testfs.sh clean: rm build/*.o diff --git a/include/fuse.h b/include/fuse.h index 5fafff3..f642f2d 100644 --- a/include/fuse.h +++ b/include/fuse.h @@ -5,6 +5,7 @@ #define DEFAULTINODEMAX 128 #define DEFAULTBLOCKSIZE 512 // Size in bytes +#define DEFAULTBLOCKMAX 16384 typedef struct fuseArgStruct { @@ -22,49 +23,74 @@ typedef struct fuseArgStruct int filefsname; } fuseArgStruct; -typedef struct SuperBlockStruct +typedef struct SuperBlock { blksize_t blockSize; blkcnt_t blockCount; -} SuperBlockStruct; + unsigned long inodeCount; +} SuperBlock; -typedef struct FBLStruct +typedef struct FreeBlockList { int freeList[DEFAULTINODEMAX/32]; -} FBLStruct; +} FreeBlockList; -typedef struct BlockStruct +// 128 Blocks +typedef struct DataBlock { char byte[DEFAULTBLOCKSIZE]; -} BlockStruct; +} DataBlock; -// TODO: Adjust Inodes to be 128 inodes with 128 direct block references *each* -// Slides exist for inode structure -typedef struct InodeStruct -{ - char filePath[256]; - off_t fileSize; - BlockStruct dataBlock; -} InodeStruct; +// typedef struct InodeInfo +// { +// // char filePath[256]; +// // off_t fileSize; +// ino_t inode; +// unsigned int isDirectory : 1; +// unsigned int isPrinted : 1; +// unsigned int isValid : 1; +// } InodeInfo; -typedef struct FileSystemStruct +// Inode * 128 + number +// Directblock == Files/Directories inside an inode? +typedef struct DirectBlock { - SuperBlockStruct superBlock; - FBLStruct fbl; - InodeStruct inodes[DEFAULTINODEMAX]; -} FileSystemStruct; + unsigned int isValid : 1; + unsigned int isDirectory : 1; + char name[256]; + ino_t inode; + off_t size; +} DirectBlock; + +// Inode 0 is root +// Inode * 128 +typedef struct Inode +{ + unsigned int isValid : 1; + DirectBlock inodeBlocks[DEFAULTINODEMAX]; +} Inode; + +typedef struct FileSystem +{ + SuperBlock superBlock; + FreeBlockList fbl; + Inode inodes[DEFAULTINODEMAX]; + DataBlock dataBlocks[DEFAULTBLOCKMAX]; +} FileSystem; void Fuse(int argc, char* argv[]); void GetArguments(int argc, char* argv[], fuseArgStruct* fuseArgs); void OpenFS(fuseArgStruct* fuseArgs, char* programPath); -FileSystemStruct* SetupFS(fuseArgStruct* fuseArgs); -void RunFuse(FileSystemStruct* fs, fuseArgStruct* fuseArgs); +FileSystem* SetupFS(fuseArgStruct* fuseArgs); +void RunFuse(FileSystem* fs, fuseArgStruct* fuseArgs); void TearDownFS(void); void FuseStructInit(fuseArgStruct* fuseStruct); void FuseUsageError(char* programPath); int zerosize(int fd); int FindEmptyBitPosition(int number); -ino_t GetFreeInodeNumber(int fbl[]); -void SetFileSystemDefaults(FileSystemStruct* fs); +ino_t GetFreeInodeNumber(Inode inodes[]); +ino_t GetFreeBlockNumber(int fbl[]); +void SetFileSystemDefaults(FileSystem* fs); +void SetupRootDirectory(FileSystem* fs); #endif diff --git a/include/fuseactions.h b/include/fuseactions.h index 289c8db..d5a3ffc 100644 --- a/include/fuseactions.h +++ b/include/fuseactions.h @@ -9,11 +9,14 @@ extern unsigned char* tempfs; void MapFS(int fd); void UnmapFS(void); -void FormatFS(FileSystemStruct* fs); -void LoadFS(FileSystemStruct** fs); -void ListFS(FileSystemStruct* fs); -void AddFileToFS(FileSystemStruct* fs, char* fname); -void RemoveFileFromFS(FileSystemStruct* fs, char* fname); -void ExtractFileFromFS(FileSystemStruct* fs, char* fname); +void FormatFS(FileSystem* fs); +void LoadFS(FileSystem** fs); +void ListFS(FileSystem* fs); +void _RecursivePrintFS_(FileSystem* fs, ino_t inodePosition); +int FindNextDirectory(char* fname); +void AddFileToFS(FileSystem* fs, char* fname); +void _RecursiveAddFS_(FileSystem* fs, char* fname); +void RemoveFileFromFS(FileSystem* fs, char* fname); +void ExtractFileFromFS(FileSystem* fs, char* fname); #endif diff --git a/src/fuse.c b/src/fuse.c index 0e9f729..b7b63d6 100644 --- a/src/fuse.c +++ b/src/fuse.c @@ -10,10 +10,21 @@ #include "fuse.h" #include "fuseactions.h" +/* + * Steps to formatting inodes: + * 1) Check if file exists already in directory + * 2) If file does not exist in directory, provide new inode + * 3) Inodes are placed inside of inodes that are directories + * + */ + +// TODO: Take BlockStruct out of InodeStruct +// Make a method for turning names into Inodes + // Main handler for Fuse void Fuse(int argc, char* argv[]) { - FileSystemStruct* fsptr; + FileSystem* fsptr; fuseArgStruct fuseArgs; GetArguments(argc, argv, &fuseArgs); OpenFS(&fuseArgs, argv[0]); @@ -23,7 +34,6 @@ void Fuse(int argc, char* argv[]) } // Gets command line arguments and puts information into fuseArgs -// New method for getting arguments void GetArguments(int argc, char* argv[], fuseArgStruct* fuseArgs) { for (int i = 0; i < argc; i++) @@ -81,9 +91,9 @@ void OpenFS(fuseArgStruct* fuseArgs, char* programPath) return; } -FileSystemStruct* SetupFS(fuseArgStruct* fuseArgs) +FileSystem* SetupFS(fuseArgStruct* fuseArgs) { - FileSystemStruct* fs; + FileSystem* fs; MapFS(fuseArgs->fd); LoadFS(&fs); if (fuseArgs->newfs) @@ -91,7 +101,7 @@ FileSystemStruct* SetupFS(fuseArgStruct* fuseArgs) return fs; } -void RunFuse(FileSystemStruct* fs, fuseArgStruct* fuseArgs) +void RunFuse(FileSystem* fs, fuseArgStruct* fuseArgs) { if (fuseArgs->add) AddFileToFS(fs, fuseArgs->toAdd); @@ -155,7 +165,7 @@ int FindEmptyBitPosition(int number) } // Gets next free inode number and returns it -ino_t GetFreeInodeNumber(int fbl[]) +ino_t GetFreeBlockNumber(int fbl[]) { int bitPosition; for (int i = 0; i < (DEFAULTINODEMAX/32); i++) @@ -170,18 +180,30 @@ ino_t GetFreeInodeNumber(int fbl[]) return -1; } +ino_t GetFreeInodeNumber(Inode inodes[]) +{ + for (int i = 0; i < DEFAULTINODEMAX; i++) + if (!(inodes[i].isValid)) + return i; + return -1; +} + // Function for setting all defaults of filesystem -void SetFileSystemDefaults(FileSystemStruct* fs) +void SetFileSystemDefaults(FileSystem* fs) { fs->superBlock.blockSize = DEFAULTBLOCKSIZE; - fs->superBlock.blockCount = DEFAULTINODEMAX; + fs->superBlock.blockCount = DEFAULTBLOCKMAX; + fs->superBlock.inodeCount = DEFAULTINODEMAX; for (int i = 0; i < 4; i++) fs->fbl.freeList[i] = 0; for (int i = 0; i < DEFAULTINODEMAX; i++) - { - strcpy(fs->inodes[i].filePath, "\0"); - fs->inodes[i].fileSize = 0; - strcpy(fs->inodes[i].dataBlock.byte, "\0"); - } + fs->inodes[i].isValid = 0; return; } + +void SetupRootDirectory(FileSystem* fs) +{ + fs->inodes[0].isValid = 1; + return; +} + diff --git a/src/fuseactions.c b/src/fuseactions.c index 92933f7..b0ff6d0 100644 --- a/src/fuseactions.c +++ b/src/fuseactions.c @@ -31,56 +31,97 @@ void UnmapFS(void) } // Sets defaults of a new filesystem -void FormatFS(FileSystemStruct* fs) +void FormatFS(FileSystem* fs) { SetFileSystemDefaults(fs); + SetupRootDirectory(fs); return; } -// Load existing filesystem -// Depreciated Function -void LoadFS(FileSystemStruct** fs) +// Redirect global pointer to structured local pointer +void LoadFS(FileSystem** fs) { - *fs = (FileSystemStruct*) (tempfs); + *fs = (FileSystem*) (tempfs); return; } // List path names of all used inodes -void ListFS(FileSystemStruct* fs) +// TODO: Fix implementation to print starting at Inode 0 +void ListFS(FileSystem* fs) { - for (int i = 0; i < 4; i++) - for (int j = 0; j < 32; j++) - if ((fs->fbl.freeList[i] & (1 << j)) == (1 << j)) - printf("%s\n", fs->inodes[i*32 + j].filePath); + Inode* fsRoot = &(fs->inodes[0]); + printf("/\n"); + for (int i = 0; i < fs->superBlock.inodeCount; i++) + { + if (!(fsRoot->inodeBlocks[i].isValid)) + continue; + printf("%s\n", fsRoot->inodeBlocks[i].name); + } return; } +// Depreciated due to new structure method +void _RecursivePrintFS_(FileSystem* fs, ino_t inodePosition) +{ + // for (int i = 0; i < DEFAULTINODEMAX; i++) + // { + // if (!(fs->inodes[inodePosition].inodeBlocks[i].info.isValid)) + // continue; + // printf("%s\n", fs->inodes[inodePosition].inodeBlocks[i].info.filePath); + // if (fs->inodes[inodePosition].inodeBlocks[i].info.isDirectory) + // _RecursivePrintFS_(fs, fs->inodes[inodePosition].inodeBlocks[i].info.inode); + // } + return; +} + +int FindNextDirectory(char* fname) +{ + int i = 0; + while (fname[i] != '\0') + { + if (fname[i] == '/') + return i; + i++; + } + return -1; +} + // Adds a file to the filesystem -void AddFileToFS(FileSystemStruct* fs, char* fname) +// TODO: Fix implementation to add starting at Inode 0 +void AddFileToFS(FileSystem* fs, char* fname) { struct stat statBuffer; ino_t inodeNumber = GetFreeInodeNumber(fs->fbl.freeList); - strcpy(fs->inodes[inodeNumber].filePath, fname); - stat(fname, &statBuffer); - fs->inodes[inodeNumber].fileSize = statBuffer.st_size; - int fd = open(fname, O_RDONLY); - if (fd < 0) - { - perror(fname); - exit(1); - } - read(fd, fs->inodes[inodeNumber].dataBlock.byte, sizeof(BlockStruct)); - close(fd); + // strcpy(fs->inodes[inodeNumber].info.filePath, fname); + // stat(fname, &statBuffer); + // fs->inodes[inodeNumber].info.fileSize = statBuffer.st_size; + // int fd = open(fname, O_RDONLY); + // if (fd < 0) + // { + // perror(fname); + // exit(1); + // } + // read(fd, fs->inodes[inodeNumber].dataBlocks.byte, sizeof(BlockStruct)); + // close(fd); + return; +} + +// Depreciated due to new structure method +void _RecursiveAddFS_(FileSystem* fs, char* fname) +{ + return; } // Remove a file from the filesystem -void RemoveFileFromFS(FileSystemStruct* fs, char* fname) +// TODO: Fix function so that it adds directories, then the file after +void RemoveFileFromFS(FileSystem* fs, char* fname) { } // Extract a file from the filesystem -void ExtractFileFromFS(FileSystemStruct* fs, char* fname) +// TODO: Fix function so that it adds directories, then the file after +void ExtractFileFromFS(FileSystem* fs, char* fname) { } diff --git a/test/test.c b/test/test.c index b9b84ff..ffa6f9f 100644 --- a/test/test.c +++ b/test/test.c @@ -5,6 +5,7 @@ #include "fuse.h" #include "fuseactions.h" +FileSystem* fakefs; fuseArgStruct dummyFuse; int argc = 5; char* argv[5]; @@ -14,9 +15,9 @@ void setUp(void) FuseStructInit(&dummyFuse); argv[0] = "bin/fuse.out"; argv[1] = "-l"; - argv[2] = "test/test.txt"; + argv[2] = "test/files/test1.txt"; argv[3] = "-f"; - argv[4] = "test/fakefs"; + argv[4] = "test/fakefs.test"; } void tearDown(void) @@ -48,10 +49,28 @@ void Test_FindEmptyBitPosition_Should_ReturnPosition(void) TEST_ASSERT_EQUAL(-1, FindEmptyBitPosition(4294967295)); } +void Test_FileSystem_Should_SuccessfullyOpen(void) +{ + remove(argv[4]); + GetArguments(argc, argv, &dummyFuse); + OpenFS(&dummyFuse, argv[0]); + fakefs = SetupFS(&dummyFuse); +} + +void Test_FileSystem_Should_AddFile(void) +{ + argv[1] = "-a"; + GetArguments(argc, argv, &dummyFuse); + RunFuse(fakefs, &dummyFuse); + TEST_ASSERT_EQUAL(1, fakefs->inodes[1].isValid); +} + int main(void) { UNITY_BEGIN(); RUN_TEST(Test_FuseGetArgs_Should_SetDummyArgs); RUN_TEST(Test_FindEmptyBitPosition_Should_ReturnPosition); + RUN_TEST(Test_FileSystem_Should_SuccessfullyOpen); + RUN_TEST(Test_FileSystem_Should_AddFile); return UNITY_END(); }