Adding files and printing works mostly, issue with first directory not solved

This commit is contained in:
2022-12-05 21:09:12 -06:00
parent 0e6af896c3
commit 88087f6050
12 changed files with 451 additions and 349 deletions
+207
View File
@@ -0,0 +1,207 @@
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include "fsactions.h"
#include "fusestructs.h"
/* Things to set for directory:
* Name, isValid, inode, isDirectory=1
*/
/* Things to set for file:
* Name, size, isValid, inode, isDirectory=0
*/
// Adds a file to the filesystem
// TODO: Fix implementation to add starting at Inode 0
void AddFileToFS(FileSystem* fs, char* fname)
{
struct stat tempstat;
ino_t currentInode = 0;
ino_t newInode;
int newDirectBlock;
int tempfbl[4];
char* tempname;
stat(fname, &tempstat);
// Get first directory, labelled by 0 to splitLocation-1,
// assuming splitLocation not 0;
int splitLocation = FindNextDirectory(fname);
if (splitLocation == 0)
{
fname = fname + 1;
splitLocation = FindNextDirectory(fname);
}
// While there is a '/', make a new inode out of left and keep right side
while (splitLocation > -1)
{
// First: check if directory already exists in current inode
// if yes, then go to that inode and start with next directory
// Second: get a number for the next free direct block
// Third: get a free inode number
// Fourth: set info for direct block
tempname = strndup(fname, splitLocation - 1);
if (IsDirectorySetup(fs->inodes[currentInode], &currentInode, tempname))
continue;
for (int i = 0; i < 4; i++)
tempfbl[i] = (currentInode*4) + i;
newDirectBlock = GetFreeBlockNumber(tempfbl, 4);
newInode = GetFreeInodeNumber(fs->inodes);
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;
currentInode = newInode;
fs->inodes[currentInode].isValid = 1;
// SetDirectBlock(fs->inodes[currentInode].blocks[newDirectBlock]);
// SetNewInode();
// fs->inodes[currentInode].blocks;
// inodeNumber = GetFreeInodeNumber(fs->inodes);
fname = fname + splitLocation + 1;
splitLocation = FindNextDirectory(fname);
}
for (int i = 0; i < 4; i++)
tempfbl[i] = (currentInode*4) + i;
newDirectBlock = GetFreeBlockNumber(tempfbl, 4);
fs->inodes[currentInode].blocks[newDirectBlock].isValid = 1;
fs->inodes[currentInode].blocks[newDirectBlock].size = tempstat.st_size;
strcpy(fs->inodes[currentInode].blocks[newDirectBlock].name, fname);
// SetFileInDirectory();
return;
}
// List path names of all used inodes
// TODO: Fix implementation to print starting at Inode 0
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);
}
return;
}
void _ListFS_(FileSystem* fs, ino_t inodeNumber, short unsigned numtabs)
{
for (int i = 0; i < fs->superBlock.inodeCount; i++)
{
if (!(fs->inodes[inodeNumber].blocks[i].isValid))
continue;
for (int j = 0; j < numtabs; j++)
printf("\t");
printf("%s\n", fs->inodes[inodeNumber].blocks[i].name);
if (fs->inodes[inodeNumber].blocks[i].isDirectory)
_ListFS_(fs, fs->inodes[inodeNumber].blocks[i].inode, numtabs + 1);
}
}
// Remove a file from the filesystem
// TODO: Fix function so that it adds directories, then the file after
void RemoveFileFromFS(FileSystem* fs, char* fname)
{
}
// Extract a file from the filesystem
// TODO: Fix function so that it adds directories, then the file after
void ExtractFileFromFS(FileSystem* fs, char* fname)
{
}
// Get the next free inode number
ino_t GetFreeInodeNumber(Inode inodes[])
{
for (int i = 0; i < DEFAULTINODEMAX; i++)
if (!(inodes[i].isValid))
return i;
return -1;
}
// Gets next free inode number and returns it
ino_t GetFreeBlockNumber(int fbl[], unsigned short size)
{
int bitPosition;
for (int i = 0; i < size; i++)
{
bitPosition = FindEmptyBitPosition(fbl[i]);
if (bitPosition >= 0)
{
fbl[i] = (fbl[i] | (1 << bitPosition));
return ((i*32) + bitPosition);
}
}
return -1;
}
// 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;
}
// Finds the next split character in the path
int FindNextDirectory(char* fname)
{
int i = 0;
while (fname[i] != '\0')
{
if (fname[i] == '/')
return i;
i++;
}
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)
{
for (int i = 0; i < 128; i++)
{
if (!(inode.blocks[i].isValid))
continue;
if (!(inode.blocks[i].isDirectory))
continue;
if (strcmp(inode.blocks[i].name, directoryName) == 0)
{
*inodePosition = i;
return 1;
}
}
return 0;
}
// Function for setting all defaults of filesystem
void SetFileSystemDefaults(FileSystem* fs)
{
fs->superBlock.blockSize = DEFAULTBLOCKSIZE;
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++)
fs->inodes[i].isValid = 0;
return;
}
// Sets first inode as root directory
void SetupRootDirectory(FileSystem* fs)
{
fs->inodes[0].isValid = 1;
return;
}
+107
View File
@@ -0,0 +1,107 @@
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "fuse.h"
#include "fsactions.h"
#include "fsinterface.h"
#include "fusestructs.h"
unsigned char* tempfs;
// Opens the filesystem file for usage
void OpenFS(fuseArgStruct* fuseArgs, char* programPath)
{
if (!fuseArgs->filefsname)
FuseUsageError(programPath);
fuseArgs->fd = open(fuseArgs->fsname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
if (fuseArgs->fd == -1)
{
perror("open failed");
exit(EXIT_FAILURE);
}
if (zerosize(fuseArgs->fd))
fuseArgs->newfs = 1;
if (fuseArgs->newfs)
{
if (lseek(fuseArgs->fd, FSSIZE-1, SEEK_SET) == -1)
{
perror("seek failed");
exit(EXIT_FAILURE);
}
if (write(fuseArgs->fd, "\0", 1) == -1)
{
perror("write failed");
exit(EXIT_FAILURE);
}
}
return;
}
// Connecting filesystem to memory
FileSystem* SetupFS(fuseArgStruct* fuseArgs)
{
FileSystem* fs;
MapFS(fuseArgs->fd);
LoadFS(&fs);
if (fuseArgs->newfs)
FormatFS(fs);
return fs;
}
// Map filesystem to memory
void MapFS(int fd)
{
tempfs = mmap(NULL, FSSIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (tempfs == NULL)
{
perror("mmap failed");
exit(EXIT_FAILURE);
}
close(fd);
return;
}
// Unmap filesystem from memory
void UnmapFS(void)
{
munmap(tempfs, FSSIZE);
return;
}
// Sets defaults of a new filesystem
void FormatFS(FileSystem* fs)
{
SetFileSystemDefaults(fs);
SetupRootDirectory(fs);
return;
}
// Redirect global pointer to structured local pointer
void LoadFS(FileSystem** fs)
{
*fs = (FileSystem*) (tempfs);
return;
}
// Cleanup after using filesystem
void TearDownFS(void)
{
UnmapFS();
return;
}
// Return bool of if the size is 0
int zerosize(int fd)
{
struct stat stats;
fstat(fd, &stats);
if(stats.st_size == 0)
return 1;
return 0;
}
+4 -108
View File
@@ -8,7 +8,8 @@
#include <sys/stat.h>
#include <sys/types.h>
#include "fuse.h"
#include "fuseactions.h"
#include "fsactions.h"
#include "fsinterface.h"
/*
* Steps to formatting inodes:
@@ -63,44 +64,7 @@ void GetArguments(int argc, char* argv[], fuseArgStruct* fuseArgs)
}
}
void OpenFS(fuseArgStruct* fuseArgs, char* programPath)
{
if (!fuseArgs->filefsname)
FuseUsageError(programPath);
fuseArgs->fd = open(fuseArgs->fsname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
if (fuseArgs->fd == -1)
{
perror("open failed");
exit(EXIT_FAILURE);
}
if (zerosize(fuseArgs->fd))
fuseArgs->newfs = 1;
if (fuseArgs->newfs)
{
if (lseek(fuseArgs->fd, FSSIZE-1, SEEK_SET) == -1)
{
perror("seek failed");
exit(EXIT_FAILURE);
}
if (write(fuseArgs->fd, "\0", 1) == -1)
{
perror("write failed");
exit(EXIT_FAILURE);
}
}
return;
}
FileSystem* SetupFS(fuseArgStruct* fuseArgs)
{
FileSystem* fs;
MapFS(fuseArgs->fd);
LoadFS(&fs);
if (fuseArgs->newfs)
FormatFS(fs);
return fs;
}
// Runs the fuse functions that were previously marked
void RunFuse(FileSystem* fs, fuseArgStruct* fuseArgs)
{
if (fuseArgs->add)
@@ -114,12 +78,6 @@ void RunFuse(FileSystem* fs, fuseArgStruct* fuseArgs)
return;
}
void TearDownFS(void)
{
UnmapFS();
return;
}
// Initialize entire fuseStruct
void FuseStructInit(fuseArgStruct* fuseStruct)
{
@@ -135,6 +93,7 @@ void FuseStructInit(fuseArgStruct* fuseStruct)
fuseStruct->fd = -1;
fuseStruct->newfs = 0;
fuseStruct->filefsname = 0;
return;
}
// Print error if usage is wrong
@@ -144,66 +103,3 @@ void FuseUsageError(char* programPath)
programPath);
exit(EXIT_FAILURE);
}
// Return bool of if the size is 0
int zerosize(int fd)
{
struct stat stats;
fstat(fd, &stats);
if(stats.st_size == 0)
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;
}
// Gets next free inode number and returns it
ino_t GetFreeBlockNumber(int fbl[])
{
int bitPosition;
for (int i = 0; i < (DEFAULTINODEMAX/32); i++)
{
bitPosition = FindEmptyBitPosition(fbl[i]);
if (bitPosition >= 0)
{
fbl[i] = (fbl[i] | (1 << bitPosition));
return ((i*32) + bitPosition);
}
}
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(FileSystem* fs)
{
fs->superBlock.blockSize = DEFAULTBLOCKSIZE;
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++)
fs->inodes[i].isValid = 0;
return;
}
void SetupRootDirectory(FileSystem* fs)
{
fs->inodes[0].isValid = 1;
return;
}
-127
View File
@@ -1,127 +0,0 @@
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include "fuseactions.h"
unsigned char* tempfs;
// Map filesystem to memory
void MapFS(int fd)
{
tempfs = mmap(NULL, FSSIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (tempfs == NULL)
{
perror("mmap failed");
exit(EXIT_FAILURE);
}
close(fd);
return;
}
// Unmap filesystem from memory
void UnmapFS(void)
{
munmap(tempfs, FSSIZE);
return;
}
// Sets defaults of a new filesystem
void FormatFS(FileSystem* fs)
{
SetFileSystemDefaults(fs);
SetupRootDirectory(fs);
return;
}
// Redirect global pointer to structured local pointer
void LoadFS(FileSystem** fs)
{
*fs = (FileSystem*) (tempfs);
return;
}
// List path names of all used inodes
// TODO: Fix implementation to print starting at Inode 0
void ListFS(FileSystem* fs)
{
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
// 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].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
// TODO: Fix function so that it adds directories, then the file after
void RemoveFileFromFS(FileSystem* fs, char* fname)
{
}
// Extract a file from the filesystem
// TODO: Fix function so that it adds directories, then the file after
void ExtractFileFromFS(FileSystem* fs, char* fname)
{
}