From 4fcd64ea813abeb0684714ab90f9ce8e41c0b1bb Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Fri, 25 Nov 2022 18:03:45 -0600 Subject: [PATCH] Setup FS structure and set fs to point to struct --- include/fuse.h | 33 ++++++++++++++++++++++++++++++++- include/fuseactions.h | 8 ++++---- src/fuse.c | 15 +++++++++++++-- src/fuseactions.c | 12 +++++------- test/test.c | 10 ++++++++++ 5 files changed, 64 insertions(+), 14 deletions(-) diff --git a/include/fuse.h b/include/fuse.h index 16efffe..47f1ef5 100644 --- a/include/fuse.h +++ b/include/fuse.h @@ -1,6 +1,9 @@ #ifndef FUSE_H #define FUSE_H +#define DEFAULTINODEMAX 128 +#define DEFAULTBLOCKSIZE 512 // Size in bytes + typedef struct fuseArgStruct { int create; @@ -17,12 +20,40 @@ typedef struct fuseArgStruct int filefsname; } fuseArgStruct; +typedef struct BlockStruct +{ + char byte[DEFAULTBLOCKSIZE]; +} BlockStruct; + +typedef struct InodeStruct +{ + BlockStruct* dataBlock; +} InodeStruct; + +typedef struct SuperBlockStruct +{ + int blockCount; + int blockSize; +} SuperBlockStruct; + +typedef struct FBLStruct +{ + int freeList[DEFAULTINODEMAX/32]; +} FBLStruct; + +typedef struct FileSystemStruct +{ + SuperBlockStruct superBlock; + FBLStruct freeList; + InodeStruct inodes[DEFAULTINODEMAX]; +} FileSystemStruct; + void Fuse(int argc, char* argv[]); void FuseGetArgs(int argc, char* argv[], fuseArgStruct* fuseArgs); -void FuseCheckArgs(int argc, char* argv[], fuseArgStruct* fuseArgs); void FuseGivenTest(fuseArgStruct* fuseArgs, char* programPath); void FuseStructInit(fuseArgStruct* fuseStruct); void FuseUsageError(char* programPath); int zerosize(int fd); +int FindEmptyBitPosition(int number); #endif diff --git a/include/fuseactions.h b/include/fuseactions.h index c08e84d..c8e2c10 100644 --- a/include/fuseactions.h +++ b/include/fuseactions.h @@ -1,12 +1,12 @@ #ifndef FUSEACTIONS_H #define FUSEACTIONS_H +#include "fuse.h" + #define FSSIZE 10000000 -extern unsigned char* fs; - -void MapFS(int fd); -void UnmapFS(); +void MapFS(FileSystemStruct* fs, int fd); +void UnmapFS(FileSystemStruct* fs); void FormatFS(); void LoadFS(); void ListFS(); diff --git a/src/fuse.c b/src/fuse.c index b891fbc..924116c 100644 --- a/src/fuse.c +++ b/src/fuse.c @@ -51,6 +51,7 @@ void FuseGetArgs(int argc, char* argv[], fuseArgStruct* fuseArgs) // Given code for default functionality void FuseGivenTest(fuseArgStruct* fuseArgs, char* programPath) { + FileSystemStruct fileSystem; if (!fuseArgs->filefsname) FuseUsageError(programPath); fuseArgs->fd = open(fuseArgs->fsname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR); @@ -74,7 +75,7 @@ void FuseGivenTest(fuseArgStruct* fuseArgs, char* programPath) exit(EXIT_FAILURE); } } - MapFS(fuseArgs->fd); + MapFS(&fileSystem, fuseArgs->fd); if (fuseArgs->newfs) FormatFS(); LoadFS(); @@ -86,7 +87,7 @@ void FuseGivenTest(fuseArgStruct* fuseArgs, char* programPath) ExtractFileFromFS(fuseArgs->toExtract); if(fuseArgs->list) ListFS(); - UnmapFS(); + UnmapFS(&fileSystem); } // Initialize entire fuseStruct @@ -123,3 +124,13 @@ int zerosize(int fd) return 1; return 0; } + +// Finds next empty bit in Free Block List +int FindEmptyBitPosition(int number) +{ + for (int i = 0; i < 32; i++) + if ((~number & (1 << i)) == (1 << i)) + return i; + return -1; +} + diff --git a/src/fuseactions.c b/src/fuseactions.c index 425fc7b..0dd6a12 100644 --- a/src/fuseactions.c +++ b/src/fuseactions.c @@ -4,9 +4,7 @@ #include #include "fuseactions.h" -unsigned char* fs; - -void MapFS(int fd) +void MapFS(FileSystemStruct* fs, int fd) { fs = mmap(NULL, FSSIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (fs == NULL) @@ -16,22 +14,22 @@ void MapFS(int fd) } } -void UnmapFS() +void UnmapFS(FileSystemStruct* fs) { munmap(fs, FSSIZE); } -void FormatFS() +void FormatFS(void) { } -void LoadFS() +void LoadFS(void) { } -void ListFS() +void ListFS(void) { } diff --git a/test/test.c b/test/test.c index 1836051..3db81e7 100644 --- a/test/test.c +++ b/test/test.c @@ -39,9 +39,19 @@ void Test_FuseGetArgs_Should_SetDummyArgs(void) TEST_ASSERT_EQUAL(1, dummyFuse.extract); } +void Test_FindEmptyBitPosition_Should_ReturnPosition(void) +{ + TEST_ASSERT_EQUAL(3, FindEmptyBitPosition(7)); + TEST_ASSERT_EQUAL(1, FindEmptyBitPosition(29)); + TEST_ASSERT_EQUAL(5, FindEmptyBitPosition(95)); + TEST_ASSERT_EQUAL(31, FindEmptyBitPosition(2147483647)); + TEST_ASSERT_EQUAL(-1, FindEmptyBitPosition(4294967295)); +} + int main(void) { UNITY_BEGIN(); RUN_TEST(Test_FuseGetArgs_Should_SetDummyArgs); + RUN_TEST(Test_FindEmptyBitPosition_Should_ReturnPosition); return UNITY_END(); }