Fixed structure of inodes, setup testing for opening and adding fs
This commit is contained in:
parent
370c5bd505
commit
0e6af896c3
1
Makefile
1
Makefile
@ -28,7 +28,6 @@ testLink:
|
|||||||
|
|
||||||
testExec:
|
testExec:
|
||||||
./bin/test.out
|
./bin/test.out
|
||||||
test/testfs.sh
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm build/*.o
|
rm build/*.o
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
#define DEFAULTINODEMAX 128
|
#define DEFAULTINODEMAX 128
|
||||||
#define DEFAULTBLOCKSIZE 512 // Size in bytes
|
#define DEFAULTBLOCKSIZE 512 // Size in bytes
|
||||||
|
#define DEFAULTBLOCKMAX 16384
|
||||||
|
|
||||||
typedef struct fuseArgStruct
|
typedef struct fuseArgStruct
|
||||||
{
|
{
|
||||||
@ -22,49 +23,74 @@ typedef struct fuseArgStruct
|
|||||||
int filefsname;
|
int filefsname;
|
||||||
} fuseArgStruct;
|
} fuseArgStruct;
|
||||||
|
|
||||||
typedef struct SuperBlockStruct
|
typedef struct SuperBlock
|
||||||
{
|
{
|
||||||
blksize_t blockSize;
|
blksize_t blockSize;
|
||||||
blkcnt_t blockCount;
|
blkcnt_t blockCount;
|
||||||
} SuperBlockStruct;
|
unsigned long inodeCount;
|
||||||
|
} SuperBlock;
|
||||||
|
|
||||||
typedef struct FBLStruct
|
typedef struct FreeBlockList
|
||||||
{
|
{
|
||||||
int freeList[DEFAULTINODEMAX/32];
|
int freeList[DEFAULTINODEMAX/32];
|
||||||
} FBLStruct;
|
} FreeBlockList;
|
||||||
|
|
||||||
typedef struct BlockStruct
|
// 128 Blocks
|
||||||
|
typedef struct DataBlock
|
||||||
{
|
{
|
||||||
char byte[DEFAULTBLOCKSIZE];
|
char byte[DEFAULTBLOCKSIZE];
|
||||||
} BlockStruct;
|
} DataBlock;
|
||||||
|
|
||||||
// TODO: Adjust Inodes to be 128 inodes with 128 direct block references *each*
|
// typedef struct InodeInfo
|
||||||
// Slides exist for inode structure
|
// {
|
||||||
typedef struct InodeStruct
|
// // char filePath[256];
|
||||||
{
|
// // off_t fileSize;
|
||||||
char filePath[256];
|
// ino_t inode;
|
||||||
off_t fileSize;
|
// unsigned int isDirectory : 1;
|
||||||
BlockStruct dataBlock;
|
// unsigned int isPrinted : 1;
|
||||||
} InodeStruct;
|
// unsigned int isValid : 1;
|
||||||
|
// } InodeInfo;
|
||||||
|
|
||||||
typedef struct FileSystemStruct
|
// Inode * 128 + number
|
||||||
|
// Directblock == Files/Directories inside an inode?
|
||||||
|
typedef struct DirectBlock
|
||||||
{
|
{
|
||||||
SuperBlockStruct superBlock;
|
unsigned int isValid : 1;
|
||||||
FBLStruct fbl;
|
unsigned int isDirectory : 1;
|
||||||
InodeStruct inodes[DEFAULTINODEMAX];
|
char name[256];
|
||||||
} FileSystemStruct;
|
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 Fuse(int argc, char* argv[]);
|
||||||
void GetArguments(int argc, char* argv[], fuseArgStruct* fuseArgs);
|
void GetArguments(int argc, char* argv[], fuseArgStruct* fuseArgs);
|
||||||
void OpenFS(fuseArgStruct* fuseArgs, char* programPath);
|
void OpenFS(fuseArgStruct* fuseArgs, char* programPath);
|
||||||
FileSystemStruct* SetupFS(fuseArgStruct* fuseArgs);
|
FileSystem* SetupFS(fuseArgStruct* fuseArgs);
|
||||||
void RunFuse(FileSystemStruct* fs, fuseArgStruct* fuseArgs);
|
void RunFuse(FileSystem* fs, fuseArgStruct* fuseArgs);
|
||||||
void TearDownFS(void);
|
void TearDownFS(void);
|
||||||
void FuseStructInit(fuseArgStruct* fuseStruct);
|
void FuseStructInit(fuseArgStruct* fuseStruct);
|
||||||
void FuseUsageError(char* programPath);
|
void FuseUsageError(char* programPath);
|
||||||
int zerosize(int fd);
|
int zerosize(int fd);
|
||||||
int FindEmptyBitPosition(int number);
|
int FindEmptyBitPosition(int number);
|
||||||
ino_t GetFreeInodeNumber(int fbl[]);
|
ino_t GetFreeInodeNumber(Inode inodes[]);
|
||||||
void SetFileSystemDefaults(FileSystemStruct* fs);
|
ino_t GetFreeBlockNumber(int fbl[]);
|
||||||
|
void SetFileSystemDefaults(FileSystem* fs);
|
||||||
|
void SetupRootDirectory(FileSystem* fs);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -9,11 +9,14 @@ extern unsigned char* tempfs;
|
|||||||
|
|
||||||
void MapFS(int fd);
|
void MapFS(int fd);
|
||||||
void UnmapFS(void);
|
void UnmapFS(void);
|
||||||
void FormatFS(FileSystemStruct* fs);
|
void FormatFS(FileSystem* fs);
|
||||||
void LoadFS(FileSystemStruct** fs);
|
void LoadFS(FileSystem** fs);
|
||||||
void ListFS(FileSystemStruct* fs);
|
void ListFS(FileSystem* fs);
|
||||||
void AddFileToFS(FileSystemStruct* fs, char* fname);
|
void _RecursivePrintFS_(FileSystem* fs, ino_t inodePosition);
|
||||||
void RemoveFileFromFS(FileSystemStruct* fs, char* fname);
|
int FindNextDirectory(char* fname);
|
||||||
void ExtractFileFromFS(FileSystemStruct* fs, 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
|
#endif
|
||||||
|
48
src/fuse.c
48
src/fuse.c
@ -10,10 +10,21 @@
|
|||||||
#include "fuse.h"
|
#include "fuse.h"
|
||||||
#include "fuseactions.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
|
// Main handler for Fuse
|
||||||
void Fuse(int argc, char* argv[])
|
void Fuse(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
FileSystemStruct* fsptr;
|
FileSystem* fsptr;
|
||||||
fuseArgStruct fuseArgs;
|
fuseArgStruct fuseArgs;
|
||||||
GetArguments(argc, argv, &fuseArgs);
|
GetArguments(argc, argv, &fuseArgs);
|
||||||
OpenFS(&fuseArgs, argv[0]);
|
OpenFS(&fuseArgs, argv[0]);
|
||||||
@ -23,7 +34,6 @@ void Fuse(int argc, char* argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Gets command line arguments and puts information into fuseArgs
|
// Gets command line arguments and puts information into fuseArgs
|
||||||
// New method for getting arguments
|
|
||||||
void GetArguments(int argc, char* argv[], fuseArgStruct* fuseArgs)
|
void GetArguments(int argc, char* argv[], fuseArgStruct* fuseArgs)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < argc; i++)
|
for (int i = 0; i < argc; i++)
|
||||||
@ -81,9 +91,9 @@ void OpenFS(fuseArgStruct* fuseArgs, char* programPath)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
FileSystemStruct* SetupFS(fuseArgStruct* fuseArgs)
|
FileSystem* SetupFS(fuseArgStruct* fuseArgs)
|
||||||
{
|
{
|
||||||
FileSystemStruct* fs;
|
FileSystem* fs;
|
||||||
MapFS(fuseArgs->fd);
|
MapFS(fuseArgs->fd);
|
||||||
LoadFS(&fs);
|
LoadFS(&fs);
|
||||||
if (fuseArgs->newfs)
|
if (fuseArgs->newfs)
|
||||||
@ -91,7 +101,7 @@ FileSystemStruct* SetupFS(fuseArgStruct* fuseArgs)
|
|||||||
return fs;
|
return fs;
|
||||||
}
|
}
|
||||||
|
|
||||||
void RunFuse(FileSystemStruct* fs, fuseArgStruct* fuseArgs)
|
void RunFuse(FileSystem* fs, fuseArgStruct* fuseArgs)
|
||||||
{
|
{
|
||||||
if (fuseArgs->add)
|
if (fuseArgs->add)
|
||||||
AddFileToFS(fs, fuseArgs->toAdd);
|
AddFileToFS(fs, fuseArgs->toAdd);
|
||||||
@ -155,7 +165,7 @@ int FindEmptyBitPosition(int number)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Gets next free inode number and returns it
|
// Gets next free inode number and returns it
|
||||||
ino_t GetFreeInodeNumber(int fbl[])
|
ino_t GetFreeBlockNumber(int fbl[])
|
||||||
{
|
{
|
||||||
int bitPosition;
|
int bitPosition;
|
||||||
for (int i = 0; i < (DEFAULTINODEMAX/32); i++)
|
for (int i = 0; i < (DEFAULTINODEMAX/32); i++)
|
||||||
@ -170,18 +180,30 @@ ino_t GetFreeInodeNumber(int fbl[])
|
|||||||
return -1;
|
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
|
// Function for setting all defaults of filesystem
|
||||||
void SetFileSystemDefaults(FileSystemStruct* fs)
|
void SetFileSystemDefaults(FileSystem* fs)
|
||||||
{
|
{
|
||||||
fs->superBlock.blockSize = DEFAULTBLOCKSIZE;
|
fs->superBlock.blockSize = DEFAULTBLOCKSIZE;
|
||||||
fs->superBlock.blockCount = DEFAULTINODEMAX;
|
fs->superBlock.blockCount = DEFAULTBLOCKMAX;
|
||||||
|
fs->superBlock.inodeCount = DEFAULTINODEMAX;
|
||||||
for (int i = 0; i < 4; i++)
|
for (int i = 0; i < 4; i++)
|
||||||
fs->fbl.freeList[i] = 0;
|
fs->fbl.freeList[i] = 0;
|
||||||
for (int i = 0; i < DEFAULTINODEMAX; i++)
|
for (int i = 0; i < DEFAULTINODEMAX; i++)
|
||||||
{
|
fs->inodes[i].isValid = 0;
|
||||||
strcpy(fs->inodes[i].filePath, "\0");
|
|
||||||
fs->inodes[i].fileSize = 0;
|
|
||||||
strcpy(fs->inodes[i].dataBlock.byte, "\0");
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SetupRootDirectory(FileSystem* fs)
|
||||||
|
{
|
||||||
|
fs->inodes[0].isValid = 1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -31,56 +31,97 @@ void UnmapFS(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Sets defaults of a new filesystem
|
// Sets defaults of a new filesystem
|
||||||
void FormatFS(FileSystemStruct* fs)
|
void FormatFS(FileSystem* fs)
|
||||||
{
|
{
|
||||||
SetFileSystemDefaults(fs);
|
SetFileSystemDefaults(fs);
|
||||||
|
SetupRootDirectory(fs);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load existing filesystem
|
// Redirect global pointer to structured local pointer
|
||||||
// Depreciated Function
|
void LoadFS(FileSystem** fs)
|
||||||
void LoadFS(FileSystemStruct** fs)
|
|
||||||
{
|
{
|
||||||
*fs = (FileSystemStruct*) (tempfs);
|
*fs = (FileSystem*) (tempfs);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// List path names of all used inodes
|
// 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++)
|
Inode* fsRoot = &(fs->inodes[0]);
|
||||||
for (int j = 0; j < 32; j++)
|
printf("/\n");
|
||||||
if ((fs->fbl.freeList[i] & (1 << j)) == (1 << j))
|
for (int i = 0; i < fs->superBlock.inodeCount; i++)
|
||||||
printf("%s\n", fs->inodes[i*32 + j].filePath);
|
{
|
||||||
|
if (!(fsRoot->inodeBlocks[i].isValid))
|
||||||
|
continue;
|
||||||
|
printf("%s\n", fsRoot->inodeBlocks[i].name);
|
||||||
|
}
|
||||||
return;
|
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
|
// 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;
|
struct stat statBuffer;
|
||||||
ino_t inodeNumber = GetFreeInodeNumber(fs->fbl.freeList);
|
ino_t inodeNumber = GetFreeInodeNumber(fs->fbl.freeList);
|
||||||
strcpy(fs->inodes[inodeNumber].filePath, fname);
|
// strcpy(fs->inodes[inodeNumber].info.filePath, fname);
|
||||||
stat(fname, &statBuffer);
|
// stat(fname, &statBuffer);
|
||||||
fs->inodes[inodeNumber].fileSize = statBuffer.st_size;
|
// fs->inodes[inodeNumber].info.fileSize = statBuffer.st_size;
|
||||||
int fd = open(fname, O_RDONLY);
|
// int fd = open(fname, O_RDONLY);
|
||||||
if (fd < 0)
|
// if (fd < 0)
|
||||||
{
|
// {
|
||||||
perror(fname);
|
// perror(fname);
|
||||||
exit(1);
|
// exit(1);
|
||||||
}
|
// }
|
||||||
read(fd, fs->inodes[inodeNumber].dataBlock.byte, sizeof(BlockStruct));
|
// read(fd, fs->inodes[inodeNumber].dataBlocks.byte, sizeof(BlockStruct));
|
||||||
close(fd);
|
// close(fd);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Depreciated due to new structure method
|
||||||
|
void _RecursiveAddFS_(FileSystem* fs, char* fname)
|
||||||
|
{
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove a file from the filesystem
|
// 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
|
// 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)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
23
test/test.c
23
test/test.c
@ -5,6 +5,7 @@
|
|||||||
#include "fuse.h"
|
#include "fuse.h"
|
||||||
#include "fuseactions.h"
|
#include "fuseactions.h"
|
||||||
|
|
||||||
|
FileSystem* fakefs;
|
||||||
fuseArgStruct dummyFuse;
|
fuseArgStruct dummyFuse;
|
||||||
int argc = 5;
|
int argc = 5;
|
||||||
char* argv[5];
|
char* argv[5];
|
||||||
@ -14,9 +15,9 @@ void setUp(void)
|
|||||||
FuseStructInit(&dummyFuse);
|
FuseStructInit(&dummyFuse);
|
||||||
argv[0] = "bin/fuse.out";
|
argv[0] = "bin/fuse.out";
|
||||||
argv[1] = "-l";
|
argv[1] = "-l";
|
||||||
argv[2] = "test/test.txt";
|
argv[2] = "test/files/test1.txt";
|
||||||
argv[3] = "-f";
|
argv[3] = "-f";
|
||||||
argv[4] = "test/fakefs";
|
argv[4] = "test/fakefs.test";
|
||||||
}
|
}
|
||||||
|
|
||||||
void tearDown(void)
|
void tearDown(void)
|
||||||
@ -48,10 +49,28 @@ void Test_FindEmptyBitPosition_Should_ReturnPosition(void)
|
|||||||
TEST_ASSERT_EQUAL(-1, FindEmptyBitPosition(4294967295));
|
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)
|
int main(void)
|
||||||
{
|
{
|
||||||
UNITY_BEGIN();
|
UNITY_BEGIN();
|
||||||
RUN_TEST(Test_FuseGetArgs_Should_SetDummyArgs);
|
RUN_TEST(Test_FuseGetArgs_Should_SetDummyArgs);
|
||||||
RUN_TEST(Test_FindEmptyBitPosition_Should_ReturnPosition);
|
RUN_TEST(Test_FindEmptyBitPosition_Should_ReturnPosition);
|
||||||
|
RUN_TEST(Test_FileSystem_Should_SuccessfullyOpen);
|
||||||
|
RUN_TEST(Test_FileSystem_Should_AddFile);
|
||||||
return UNITY_END();
|
return UNITY_END();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user