Cleaned program code

This commit is contained in:
TriantaTV 2022-11-12 01:25:08 -06:00
parent 074fb0507b
commit 36c4d61c9f
3 changed files with 94 additions and 71 deletions

View File

@ -14,8 +14,10 @@ typedef struct ModuleStruct
void* moduleData;
} ModuleStruct;
int ReadSingleModule(int fd, ModuleStruct* module);
int WriteSingleModule(int fd, ModuleStruct* module);
int ReadSingleModuleFromArchive(int fd, ModuleStruct* module);
int WriteSingleModuleToArchive(int fd, ModuleStruct* module);
int OpenArchive(char* archiveName, int flags);
void PrintModuleHeader(ModuleStruct* module);
void SafetyCheck(int status, char* message);
char* StripFilename(char* filename);
void Stuffy(int argc, char* argv[]);

View File

@ -9,7 +9,7 @@
// Put a module header into a HeaderStruct and read (skip) module data
// Returns size of data read when reading a module header
int ReadSingleModule(int fd, ModuleStruct* module)
int ReadSingleModuleFromArchive(int fd, ModuleStruct* module)
{
int readSize;
long long int moduleSize;
@ -21,7 +21,7 @@ int ReadSingleModule(int fd, ModuleStruct* module)
}
// Write a single module into file
int WriteSingleModule(int fd, ModuleStruct* module)
int WriteSingleModuleToArchive(int fd, ModuleStruct* module)
{
int writeSize;
long long int moduleSize;
@ -31,6 +31,37 @@ int WriteSingleModule(int fd, ModuleStruct* module)
return writeSize;
}
void PrintModuleHeader(ModuleStruct* module)
{
printf("Name: %s | ", module->moduleHeader.moduleName);
printf("Size: %ld\n", module->moduleHeader.moduleInfo.st_size);
return;
}
int OpenArchive(char* archiveName, int flags)
{
int archiveFD = open(archiveName, flags, 0644);
if (archiveFD < 0)
{
perror("Archive");
return -1;
}
return archiveFD;
}
void LoadModuleFromFile(char* filename, ModuleStruct* module)
{
stat(filename, &(module->moduleHeader.moduleInfo));
long long int moduleSize;
moduleSize = (long long) module->moduleHeader.moduleInfo.st_size;
module->moduleData = malloc(moduleSize);
int fdInput = open(filename, O_RDONLY);
read(fdInput, module->moduleData, moduleSize);
close(fdInput);
return;
}
// Checks if operation went ok, if not then prints message and exits
void SafetyCheck(int status, char* message)
{
@ -109,21 +140,20 @@ int IsFileArchived(char* archiveName, char* filename)
ModuleStruct module;
ssize_t readSize;
char* newFilename = StripFilename(filename);
int archiveFile = open(archiveName, O_RDONLY | O_CREAT, 0644);
SafetyCheck((archiveFile < 0), "Archive failed to open.");
int archiveFD = OpenArchive(archiveName, O_RDONLY | O_CREAT);
do
{
readSize = ReadSingleModule(archiveFile, &(module));
readSize = ReadSingleModuleFromArchive(archiveFD, &(module));
free(module.moduleData);
if (strcmp(module.moduleHeader.moduleName, filename) == 0)
{
free(newFilename);
close(archiveFile);
close(archiveFD);
return 1;
}
} while (readSize > 0);
free(newFilename);
close(archiveFile);
close(archiveFD);
return 0;
}
@ -133,6 +163,8 @@ int IsFileArchived(char* archiveName, char* filename)
// file already inside the archive
void AddToArchive(char* archiveName, char* filename)
{
ModuleStruct module;
ssize_t readSize;
char* filenameCleaned = StripFilename(filename);
if (IsFileArchived(archiveName, filenameCleaned))
{
@ -144,27 +176,18 @@ void AddToArchive(char* archiveName, char* filename)
fprintf(stderr, "%s not found.\n", filename);
return;
}
int archiveFile = open(archiveName, O_RDWR | O_CREAT, 0644);
SafetyCheck((archiveFile < 0), "Archive failed to open.");
ssize_t readSize;
ModuleStruct module;
int archiveFD = OpenArchive(archiveName, O_RDWR | O_CREAT);
do
{
readSize = ReadSingleModule(archiveFile, &(module));
readSize = ReadSingleModuleFromArchive(archiveFD, &(module));
free(module.moduleData);
}
while (readSize > 0);
} while (readSize > 0);
LoadModuleFromFile(filename, &module);
strcpy(module.moduleHeader.moduleName, filenameCleaned);
stat(filename, &(module.moduleHeader.moduleInfo));
long long int moduleSize;
moduleSize = (long long) module.moduleHeader.moduleInfo.st_size;
module.moduleData = malloc(moduleSize);
int fdInput = open(filename, O_RDONLY);
read(fdInput, module.moduleData, moduleSize);
close(fdInput);
WriteSingleModule(archiveFile, &module);
WriteSingleModuleToArchive(archiveFD, &module);
free(filenameCleaned);
free(module.moduleData);
close(archiveFD);
return;
}
@ -173,7 +196,6 @@ void AddToArchive(char* archiveName, char* filename)
void RemoveFromArchive(char* archiveName, char* filename)
{
ssize_t readSize;
long long int moduleSize;
char* filenameCleaned = StripFilename(filename);
char tempArchiveName[strlen(archiveName)+5];
strcpy(tempArchiveName, archiveName);
@ -186,18 +208,17 @@ void RemoveFromArchive(char* archiveName, char* filename)
return;
}
ModuleStruct module;
int newArchiveFile = open(tempArchiveName, O_WRONLY | O_CREAT, 0644);
SafetyCheck((newArchiveFile < 0), "New archive failed to open.");
readSize = ReadSingleModule(archiveFile, &(module));
int newArchiveFD = OpenArchive(tempArchiveName, O_WRONLY | O_CREAT);
readSize = ReadSingleModuleFromArchive(archiveFile, &(module));
while (readSize > 0)
{
if (strcmp(module.moduleHeader.moduleName, filenameCleaned) != 0)
WriteSingleModule(newArchiveFile, &module);
WriteSingleModuleToArchive(newArchiveFD, &module);
free(module.moduleData);
readSize = ReadSingleModule(archiveFile, &(module));
readSize = ReadSingleModuleFromArchive(archiveFile, &(module));
}
close(archiveFile);
close(newArchiveFile);
close(newArchiveFD);
SafetyCheck(remove(archiveName), "Old archive failed removal.");
SafetyCheck(rename(tempArchiveName, archiveName),
"New archive failed name change.");
@ -213,38 +234,38 @@ void ListArchive(char* archiveName)
ssize_t readSize;
SafetyCheck((archiveFile < 0), "Archive failed to open.");
ModuleStruct module;
readSize = ReadSingleModule(archiveFile, &module);
while (readSize > 0)
do
{
printf("Name: %s | Size: %ld\n", module.moduleHeader.moduleName,
module.moduleHeader.moduleInfo.st_size);
free(module.moduleData);
readSize = ReadSingleModule(archiveFile, &module);
}
readSize = ReadSingleModuleFromArchive(archiveFile, &module);
if (readSize > 0)
{
PrintModuleHeader(&module);
free(module.moduleData);
}
} while (readSize > 0);
return;
}
int Test_ListArchive(char* archiveName)
{
struct stat statArchive;
int archiveFile = open(archiveName, O_RDONLY);
int archiveFD = OpenArchive(archiveName, O_RDONLY);
ssize_t readSize;
long int sizeTotal = 0;
if (archiveFile < 0)
if (archiveFD < 0)
return -1;
ModuleStruct module;
stat(archiveName, &statArchive);
do
{
readSize = ReadSingleModule(archiveFile, &module);
readSize = ReadSingleModuleFromArchive(archiveFD, &module);
if (readSize > 0)
{
sizeTotal += sizeof(module.moduleHeader);
sizeTotal += module.moduleHeader.moduleInfo.st_size;
}
free(module.moduleData);
}
while (readSize > 0);
} while (readSize > 0);
return sizeTotal;
}
@ -263,13 +284,13 @@ int _ExtractArchive(char* archiveName, char* filenameIn)
return -1;
}
ModuleStruct module;
readSize = ReadSingleModule(archiveFile, &(module));
readSize = ReadSingleModuleFromArchive(archiveFile, &(module));
while (readSize > 0)
{
if (strcmp(module.moduleHeader.moduleName, filenameCleaned) == 0)
writeSize += write(STDOUT_FILENO, module.moduleData, module.moduleHeader.moduleInfo.st_size);
free(module.moduleData);
readSize = ReadSingleModule(archiveFile, &(module));
readSize = ReadSingleModuleFromArchive(archiveFile, &(module));
}
close(archiveFile);
free(filenameCleaned);

View File

@ -56,44 +56,33 @@ void Test_StripFilename_Should_ChangeName(void)
TEST_ASSERT_EQUAL_STRING("temp5.test", StripFilename("test/files/temp5.test"));
}
void Test_ReadSingleModule_Should_ReadCorrectly(void)
void Test_ReadSingleModuleFromArchive_Should_ReadCorrectly(void)
{
int fd = open(argv[2], O_RDONLY);
TEST_ASSERT_EQUAL(80, ReadSingleModule(fd, &moduleTest));
TEST_ASSERT_EQUAL(25, ReadSingleModule(fd, &moduleTest));
TEST_ASSERT_EQUAL(27, ReadSingleModule(fd, &moduleTest));
TEST_ASSERT_EQUAL(27, ReadSingleModule(fd, &moduleTest));
TEST_ASSERT_EQUAL(80, ReadSingleModuleFromArchive(fd, &moduleTest));
TEST_ASSERT_EQUAL(25, ReadSingleModuleFromArchive(fd, &moduleTest));
TEST_ASSERT_EQUAL(27, ReadSingleModuleFromArchive(fd, &moduleTest));
TEST_ASSERT_EQUAL(27, ReadSingleModuleFromArchive(fd, &moduleTest));
close(fd);
}
void Test_ReadSingleModule_Should_ReadNewArchive(void)
void Test_ReadSingleModuleFromArchive_Should_ReadNewArchive(void)
{
// TEST_IGNORE_MESSAGE("Implementation Required: AddToArchive");
remove("test/files/newstuffyarchive.test");
int fd = open("test/files/newstuffyarchive.test", O_RDONLY | O_CREAT, 0644);
int testFD = OpenArchive("test/files/newstuffyarchive.test", O_RDONLY | O_CREAT);
AddToArchive("test/files/newstuffyarchive.test", "test/files/temp4.test");
TEST_ASSERT_EQUAL(27, ReadSingleModule(fd, &moduleTest));
TEST_ASSERT_EQUAL(27, ReadSingleModuleFromArchive(testFD, &moduleTest));
AddToArchive("test/files/newstuffyarchive.test", "test/files/temp3.test");
TEST_ASSERT_EQUAL(27, ReadSingleModule(fd, &moduleTest));
TEST_ASSERT_EQUAL(27, ReadSingleModuleFromArchive(testFD, &moduleTest));
AddToArchive("test/files/newstuffyarchive.test", "test/files/temp2.test");
TEST_ASSERT_EQUAL(25, ReadSingleModule(fd, &moduleTest));
TEST_ASSERT_EQUAL(25, ReadSingleModuleFromArchive(testFD, &moduleTest));
AddToArchive("test/files/newstuffyarchive.test", "test/files/temp.test");
TEST_ASSERT_EQUAL(80, ReadSingleModule(fd, &moduleTest));
TEST_ASSERT_EQUAL(80, ReadSingleModuleFromArchive(testFD, &moduleTest));
// Fail test removed due to printf breaking test
// AddToArchive("test/files/newstuffyarchive.test", "test/files/fake.test");
// TEST_ASSERT_EQUAL(0, ReadSingleModule(fd, &moduleTest));
close(fd);
}
void Test_RemoveFromArchive_Should_RemoveCorrectly(void)
{
// TEST_IGNORE_MESSAGE("Implementation Required: RemoveFromArchive");
int oldArchiveSize = Test_ListArchive("test/files/newstuffyarchive.test");
RemoveFromArchive("test/files/newstuffyarchive.test", "test/files/temp.test");
TEST_ASSERT_LESS_THAN(oldArchiveSize, Test_ListArchive("test/files/newstuffyarchive.test"));
oldArchiveSize = Test_ListArchive("test/files/newstuffyarchive.test");
RemoveFromArchive("test/files/newstuffyarchive.test", "test/files/temp2.test");
TEST_ASSERT_LESS_THAN(oldArchiveSize, Test_ListArchive("test/files/newstuffyarchive.test"));
// TEST_ASSERT_EQUAL(0, ReadSingleModuleFromArchive(fd, &moduleTest));
close(testFD);
}
void Test_ListArchive_Should_ListCorrectly(void)
@ -109,14 +98,25 @@ void Test_ExtractArchive_Should_ExtractCorrectly(void)
TEST_ASSERT_GREATER_THAN(0, outputFileSize);
}
void Test_RemoveFromArchive_Should_RemoveCorrectly(void)
{
// TEST_IGNORE_MESSAGE("Implementation Required: RemoveFromArchive");
int oldArchiveSize = Test_ListArchive("test/files/newstuffyarchive.test");
RemoveFromArchive("test/files/newstuffyarchive.test", "test/files/temp.test");
TEST_ASSERT_LESS_THAN(oldArchiveSize, Test_ListArchive("test/files/newstuffyarchive.test"));
oldArchiveSize = Test_ListArchive("test/files/newstuffyarchive.test");
RemoveFromArchive("test/files/newstuffyarchive.test", "test/files/temp2.test");
TEST_ASSERT_LESS_THAN(oldArchiveSize, Test_ListArchive("test/files/newstuffyarchive.test"));
}
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_ReadSingleModuleFromArchive_Should_ReadCorrectly);
RUN_TEST(Test_ReadSingleModuleFromArchive_Should_ReadNewArchive);
RUN_TEST(Test_ListArchive_Should_ListCorrectly);
RUN_TEST(Test_ExtractArchive_Should_ExtractCorrectly);
RUN_TEST(Test_RemoveFromArchive_Should_RemoveCorrectly);