2022-11-14 21:46:44 -06:00
|
|
|
#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 "fuseactions.h"
|
|
|
|
|
|
|
|
// Main handler for Fuse
|
|
|
|
void Fuse(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
fuseArgStruct fuseArgs;
|
|
|
|
FuseCheckArgs(argc, argv, &fuseArgs);
|
|
|
|
FuseGivenTest(&fuseArgs, argv[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Originally given method for getting arguments
|
|
|
|
void FuseCheckArgs(int argc, char* argv[], fuseArgStruct* fuseArgs)
|
|
|
|
{
|
|
|
|
int opt;
|
|
|
|
while ((opt = getopt(argc, argv, "la:r:e:")) != -1)
|
|
|
|
{
|
|
|
|
switch (opt)
|
|
|
|
{
|
|
|
|
case 'l':
|
|
|
|
fuseArgs->list = 1;
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
fuseArgs->add = 1;
|
|
|
|
fuseArgs->toAdd = strdup(optarg);
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
fuseArgs->remove = 1;
|
|
|
|
fuseArgs->toRemove = strdup(optarg);
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
fuseArgs->extract = 1;
|
|
|
|
fuseArgs->toExtract = strdup(optarg);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
FuseUsageError(argv[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Given code for default functionality
|
|
|
|
void FuseGivenTest(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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mapfs(fuseArgs->fd);
|
|
|
|
if (fuseArgs->newfs)
|
|
|
|
formatfs();
|
|
|
|
loadfs();
|
|
|
|
if (fuseArgs->add)
|
|
|
|
addfilefs(fuseArgs->toAdd);
|
|
|
|
if (fuseArgs->remove)
|
|
|
|
removefilefs(fuseArgs->toRemove);
|
|
|
|
if (fuseArgs->extract)
|
|
|
|
extractfilefs(fuseArgs->toExtract);
|
|
|
|
if(fuseArgs->list)
|
|
|
|
lsfs();
|
|
|
|
unmapfs();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize entire fuseStruct
|
|
|
|
void FuseStructInit(fuseArgStruct* fuseStruct)
|
|
|
|
{
|
|
|
|
fuseStruct->create = 0;
|
|
|
|
fuseStruct->list = 0;
|
|
|
|
fuseStruct->add = 0;
|
|
|
|
fuseStruct->remove = 0;
|
|
|
|
fuseStruct->extract = 0;
|
|
|
|
fuseStruct->toAdd = NULL;
|
|
|
|
fuseStruct->toRemove = NULL;
|
|
|
|
fuseStruct->toExtract = NULL;
|
|
|
|
fuseStruct->fsname = NULL;
|
|
|
|
fuseStruct->fd = -1;
|
|
|
|
fuseStruct->newfs = 0;
|
|
|
|
fuseStruct->filefsname = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print error if usage is wrong
|
|
|
|
void FuseUsageError(char* programPath)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Usage %s [-l] [-a path] [-e path] [-r path] -f name\n",
|
|
|
|
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;
|
|
|
|
}
|