Add and List fully functional with testing

This commit is contained in:
TriantaTV 2022-11-10 21:36:19 -06:00
parent ff37c5e208
commit df97ff36d9
3 changed files with 70 additions and 52 deletions

View File

@ -25,6 +25,7 @@ int IsFileArchived(char* archiveName, char* filename);
void AddToArchive(char* archiveName, char* filename);
void RemoveFromArchive(char* archiveName, char* filename);
void ListArchive(char* archiveName);
int Test_ListArchive(char* archiveName);
void ExtractArchive(char* argv[]);
#endif

View File

@ -23,24 +23,18 @@ int ReadSingleModule(int fd, ModuleStruct* module)
// Returns size of data read when reading a module header
int WriteSingleModule(int fd, ModuleStruct* module, char* filename)
{
int readSize;
int writeSize;
long long int moduleSize;
readSize = read(fd, &(module->moduleHeader), sizeof(module->moduleHeader));
moduleSize = (long long) module->moduleHeader.moduleInfo.st_size;
module->moduleData = malloc(moduleSize);
readSize = read(fd, module->moduleData, moduleSize);
// strcpy(module.moduleHeader.moduleName, newFilename);
// stat(filename, &(module.moduleHeader.moduleInfo));
// write(archiveFile, &module.moduleHeader, sizeof(module.moduleHeader));
// moduleSize = (long long) module.moduleHeader.moduleInfo.st_size;
// char moduleData[moduleSize];
// write(archiveFile, moduleData, moduleSize);
// close(archiveFile);
return readSize;
int fdInput = open(filename, O_RDONLY);
if (fdInput < 0)
return 0;
read(fdInput, module->moduleData, moduleSize);
close(fdInput);
writeSize = write(fd, &(module->moduleHeader), sizeof(module->moduleHeader));
writeSize = write(fd, module->moduleData, moduleSize);
return writeSize;
}
// Checks if operation went ok, if not then prints message and exits
@ -118,7 +112,6 @@ void StuffyAction(char* argv[], int archiveAction)
int IsFileArchived(char* archiveName, char* filename)
{
ModuleStruct module;
long long int moduleSize;
ssize_t readSize;
char* newFilename = StripFilename(filename);
int archiveFile = open(archiveName, O_RDONLY | O_CREAT, 0644);
@ -127,9 +120,7 @@ int IsFileArchived(char* archiveName, char* filename)
{
readSize = ReadSingleModule(archiveFile, &(module));
free(module.moduleData);
module.moduleData = NULL;
printf("%s\n", newFilename);
if (!strcmp(module.moduleHeader.moduleName, filename))
if (strcmp(module.moduleHeader.moduleName, filename) == 0)
{
free(newFilename);
close(archiveFile);
@ -147,31 +138,28 @@ int IsFileArchived(char* archiveName, char* filename)
// file already inside the archive
void AddToArchive(char* archiveName, char* filename)
{
// SafetyCheck(IsFileArchived(archiveName, filename),
// "File already exists in archive, try removing file first.");
SafetyCheck(IsFileArchived(archiveName, filename),
"File already exists in archive, try removing file first.");
int archiveFile = open(archiveName, O_RDWR | O_CREAT, 0644);
SafetyCheck((archiveFile < 0), "Archive failed to open.");
ssize_t readSize;
// long long int moduleSize;
ModuleStruct module;
// readSize = read(archiveFile, &module.moduleHeader, sizeof(module.moduleHeader));
do
readSize = ReadSingleModule(archiveFile, &(module));
while (readSize > 0);
char* newFilename = StripFilename(filename);
WriteSingleModule(archiveFile, &module, newFilename);
free(newFilename);
char* filenameCleaned = StripFilename(filename);
strcpy(module.moduleHeader.moduleName, filenameCleaned);
stat(filename, &(module.moduleHeader.moduleInfo));
WriteSingleModule(archiveFile, &module, filename);
free(filenameCleaned);
free(module.moduleData);
// return;
return;
}
// Check through archive for file, if file exists with name, remove it
// If file not in archive, print "somefile was not found."
void RemoveFromArchive(char* archiveName, char* filename)
{
printf("'RemoveFromArchive()' function is still broken.\n");
return;
ssize_t readSize;
long long int moduleSize;
char* newFileName;
@ -205,28 +193,41 @@ void ListArchive(char* archiveName)
{
int archiveFile = open(archiveName, O_RDONLY);
ssize_t readSize;
long long int moduleSize;
SafetyCheck((archiveFile < 0), "Archive failed to open.");
// ModuleStruct module;
// do
// {
// readSize = ReadSingleModule(archiveFile, &module);
// printf("Name: %s | Size: %lld\n", module.moduleHeader.moduleName, moduleSize);
// }
// while (readSize > 0);
HeaderStruct moduleHeader;
readSize = read(archiveFile, &moduleHeader, sizeof(moduleHeader));
while (readSize > 0)
ModuleStruct module;
do
{
moduleSize = (long long) moduleHeader.moduleInfo.st_size;
printf("Name: %s | Size: %lld\n", moduleHeader.moduleName, moduleSize);
char moduleData[moduleSize];
read(archiveFile, &moduleData, moduleSize);
readSize = read(archiveFile, &moduleHeader, sizeof(moduleHeader));
readSize = ReadSingleModule(archiveFile, &module);
printf("Name: %s | Size: %ld\n", module.moduleHeader.moduleName,
module.moduleHeader.moduleInfo.st_size);
}
while (readSize > 0);
return;
}
int Test_ListArchive(char* archiveName)
{
struct stat statArchive;
int archiveFile = open(archiveName, O_RDONLY);
ssize_t readSize;
long int sizeTotal = 0;
if (archiveFile < 0)
return 0;
ModuleStruct module;
stat(archiveName, &statArchive);
do
{
readSize = ReadSingleModule(archiveFile, &module);
if (readSize > 0)
{
sizeTotal += sizeof(module.moduleHeader);
sizeTotal += module.moduleHeader.moduleInfo.st_size;
}
}
while (readSize > 0);
return (statArchive.st_size == sizeTotal);
}
// Extract data of name file from archive to stdout, which is then redirected
// Extracted data remains in archive
void ExtractArchive(char* argv[])

View File

@ -35,6 +35,16 @@ void Test_StuffyArgument_Should_ReturnCorrectly(void)
TEST_ASSERT_EQUAL(-1, StuffyArgument(4, argv));
}
void Test_IsFileArchived_Should_ReturnCorrectly(void)
{
TEST_ASSERT_EQUAL(1, IsFileArchived(argv[2], "test/files/temp.test"));
TEST_ASSERT_EQUAL(1, IsFileArchived(argv[2], "test/files/temp2.test"));
TEST_ASSERT_EQUAL(1, IsFileArchived(argv[2], "test/files/temp3.test"));
TEST_ASSERT_EQUAL(1, IsFileArchived(argv[2], "test/files/temp4.test"));
TEST_ASSERT_EQUAL(0, IsFileArchived(argv[2], "test/files/rewio.test"));
TEST_ASSERT_EQUAL(0, IsFileArchived(argv[2], "test/files/fake.test"));
}
void Test_StripFilename_Should_ChangeName(void)
{
TEST_ASSERT_EQUAL_STRING("mystuffyarchive.test", StripFilename("test/files/mystuffyarchive.test"));
@ -57,16 +67,19 @@ void Test_ReadSingleModule_Should_ReadCorrectly(void)
void Test_ReadSingleModule_Should_ReadNewArchive(void)
{
TEST_IGNORE_MESSAGE("Implementation Required: AddToArchive");
// TEST_IGNORE_MESSAGE("Implementation Required: AddToArchive");
remove("test/files/newstuffyarchive.test");
int fd = open("test/files/newstuffyarchive.test", O_RDONLY | O_CREAT, 0644);
AddToArchive("test/files/newstuffyarchive.test", "temp4.test");
AddToArchive("test/files/newstuffyarchive.test", "test/files/temp4.test");
TEST_ASSERT_EQUAL(27, ReadSingleModule(fd, &moduleTest));
AddToArchive("test/files/newstuffyarchive.test", "temp3.test");
AddToArchive("test/files/newstuffyarchive.test", "test/files/temp3.test");
TEST_ASSERT_EQUAL(27, ReadSingleModule(fd, &moduleTest));
AddToArchive("test/files/newstuffyarchive.test", "temp2.test");
AddToArchive("test/files/newstuffyarchive.test", "test/files/temp2.test");
TEST_ASSERT_EQUAL(25, ReadSingleModule(fd, &moduleTest));
AddToArchive("test/files/newstuffyarchive.test", "temp.test");
AddToArchive("test/files/newstuffyarchive.test", "test/files/temp.test");
TEST_ASSERT_EQUAL(80, ReadSingleModule(fd, &moduleTest));
AddToArchive("test/files/newstuffyarchive.test", "test/files/fake.test");
TEST_ASSERT_EQUAL(0, ReadSingleModule(fd, &moduleTest));
close(fd);
}
@ -77,7 +90,9 @@ void Test_RemoveFromArchive_Should_RemoveCorrectly(void)
void Test_ListArchive_Should_ListCorrectly(void)
{
TEST_IGNORE_MESSAGE("Implementation Required: ListArchive");
// TEST_IGNORE_MESSAGE("Implementation Required: ListArchive");
TEST_ASSERT_TRUE(Test_ListArchive("test/files/newstuffyarchive.test"));
TEST_ASSERT_FALSE(Test_ListArchive("test/files/fakeArchive.test"));
}
void Test_ExtractArchive_Should_ExtractCorrectly(void)
@ -90,6 +105,7 @@ int main(void)
UNITY_BEGIN();
RUN_TEST(Test_StuffyArgument_Should_ReturnCorrectly);
RUN_TEST(Test_StripFilename_Should_ChangeName);
RUN_TEST(Test_IsFileArchived_Should_ReturnCorrectly);
RUN_TEST(Test_ReadSingleModule_Should_ReadCorrectly);
RUN_TEST(Test_ReadSingleModule_Should_ReadNewArchive);
RUN_TEST(Test_RemoveFromArchive_Should_RemoveCorrectly);