Fixed issue with adding, adding is now fully functional, added testing for removing
This commit is contained in:
+30
-16
@@ -21,11 +21,11 @@
|
||||
// TODO: Fix implementation to add starting at Inode 0
|
||||
void AddFileToFS(FileSystem* fs, char* fname)
|
||||
{
|
||||
int inputfd = open(fname, O_RDONLY);
|
||||
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,
|
||||
@@ -39,35 +39,28 @@ void AddFileToFS(FileSystem* fs, char* 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);
|
||||
newInode = FindDirectory(fs->inodes[currentInode], tempname);
|
||||
if (newInode == -1)
|
||||
{
|
||||
for (int i = 0; i < 4; i++)
|
||||
tempfbl[i] = (currentInode*4) + i;
|
||||
newDirectBlock = GetFreeBlockNumber(tempfbl, 4);
|
||||
newDirectBlock = GetFreeBlockNumber(fs->fbl.freeList + (currentInode*4), 4);
|
||||
newInode = GetFreeInodeNumber(fs->inodes);
|
||||
strcpy(fs->inodes[currentInode].blocks[newDirectBlock].name, tempname);
|
||||
free(tempname);
|
||||
SetupDirectoryBlock(&(fs->inodes[currentInode].blocks[newDirectBlock]), newInode);
|
||||
fs->inodes[newInode].isValid = 1;
|
||||
currentInode = newInode;
|
||||
}
|
||||
currentInode = newInode;
|
||||
fname = fname + splitLocation + 1;
|
||||
splitLocation = FindNextDirectory(fname);
|
||||
}
|
||||
for (int i = 0; i < 4; i++)
|
||||
tempfbl[i] = (currentInode*4) + i;
|
||||
newDirectBlock = GetFreeBlockNumber(tempfbl, 4);
|
||||
newDirectBlock = GetFreeBlockNumber(fs->fbl.freeList + (currentInode*4), 4);
|
||||
newInode = currentInode*4 + newDirectBlock;
|
||||
fs->inodes[currentInode].blocks[newDirectBlock].isValid = 1;
|
||||
fs->inodes[currentInode].blocks[newDirectBlock].size = tempstat.st_size;
|
||||
strcpy(fs->inodes[currentInode].blocks[newDirectBlock].name, fname);
|
||||
printf("%s\n", fs->inodes[0].blocks[0].name);
|
||||
fs->inodes[currentInode].blocks[newDirectBlock].inode = newInode;
|
||||
read(inputfd, &(fs->dataBlocks[newInode].byte), tempstat.st_size);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -75,7 +68,7 @@ void AddFileToFS(FileSystem* fs, char* fname)
|
||||
void ListFS(FileSystem* fs)
|
||||
{
|
||||
printf("/\n");
|
||||
_ListFS_(fs, 0, 0);
|
||||
_ListFS_(fs, 0, 1);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -97,7 +90,28 @@ void _ListFS_(FileSystem* fs, ino_t inodeNumber, short unsigned numtabs)
|
||||
// TODO: Fix function so that it adds directories, then the file after
|
||||
void RemoveFileFromFS(FileSystem* fs, char* fname)
|
||||
{
|
||||
_RemoveFileFromFS_(fs, 0, fname);
|
||||
}
|
||||
|
||||
// Recursive bool call for checking before removing
|
||||
// If the directory is not empty, returns false
|
||||
int _RemoveFileFromFS_(FileSystem* fs, ino_t inodeNumber, char* fname)
|
||||
{
|
||||
int emptyDir = 1;
|
||||
for (int i = 0; i < DEFAULTINODEMAX; i++)
|
||||
{
|
||||
if (!(fs->inodes[inodeNumber].blocks[i].isValid))
|
||||
continue;
|
||||
if (fs->inodes[inodeNumber].blocks[i].isDirectory)
|
||||
emptyDir =_RemoveFileFromFS_(fs, fs->inodes[inodeNumber].blocks[i].inode, fname + FindNextDirectory(fname) + 1);
|
||||
if (strcmp(fs->inodes[inodeNumber].blocks[i].name, fname) == 0)
|
||||
fs->inodes[inodeNumber].blocks[i].isValid = 0;
|
||||
else
|
||||
emptyDir = 0;
|
||||
}
|
||||
if (!emptyDir)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Extract a file from the filesystem
|
||||
@@ -165,7 +179,7 @@ int FindDirectory(Inode inode, char* directoryName)
|
||||
if (!(inode.blocks[i].isDirectory))
|
||||
continue;
|
||||
if (strcmp(inode.blocks[i].name, directoryName) == 0)
|
||||
return i;
|
||||
return inode.blocks[i].inode;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user