Fixed structure of inodes, setup testing for opening and adding fs

This commit is contained in:
TriantaTV 2022-12-03 18:35:51 -06:00
parent 370c5bd505
commit 0e6af896c3
6 changed files with 179 additions and 69 deletions

View File

@ -28,7 +28,6 @@ testLink:
testExec:
./bin/test.out
test/testfs.sh
clean:
rm build/*.o

View File

@ -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

View File

@ -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

View File

@ -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;
}

View File

@ -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);
// 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;
}
read(fd, fs->inodes[inodeNumber].dataBlock.byte, sizeof(BlockStruct));
close(fd);
// 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)
{
}

View File

@ -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();
}