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; void* moduleData;
} ModuleStruct; } ModuleStruct;
int ReadSingleModule(int fd, ModuleStruct* module); int ReadSingleModuleFromArchive(int fd, ModuleStruct* module);
int WriteSingleModule(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); void SafetyCheck(int status, char* message);
char* StripFilename(char* filename); char* StripFilename(char* filename);
void Stuffy(int argc, char* argv[]); void Stuffy(int argc, char* argv[]);

View File

@ -9,7 +9,7 @@
// Put a module header into a HeaderStruct and read (skip) module data // Put a module header into a HeaderStruct and read (skip) module data
// Returns size of data read when reading a module header // Returns size of data read when reading a module header
int ReadSingleModule(int fd, ModuleStruct* module) int ReadSingleModuleFromArchive(int fd, ModuleStruct* module)
{ {
int readSize; int readSize;
long long int moduleSize; long long int moduleSize;
@ -21,7 +21,7 @@ int ReadSingleModule(int fd, ModuleStruct* module)
} }
// Write a single module into file // Write a single module into file
int WriteSingleModule(int fd, ModuleStruct* module) int WriteSingleModuleToArchive(int fd, ModuleStruct* module)
{ {
int writeSize; int writeSize;
long long int moduleSize; long long int moduleSize;
@ -31,6 +31,37 @@ int WriteSingleModule(int fd, ModuleStruct* module)
return writeSize; 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 // Checks if operation went ok, if not then prints message and exits
void SafetyCheck(int status, char* message) void SafetyCheck(int status, char* message)
{ {
@ -109,21 +140,20 @@ int IsFileArchived(char* archiveName, char* filename)
ModuleStruct module; ModuleStruct module;
ssize_t readSize; ssize_t readSize;
char* newFilename = StripFilename(filename); char* newFilename = StripFilename(filename);
int archiveFile = open(archiveName, O_RDONLY | O_CREAT, 0644); int archiveFD = OpenArchive(archiveName, O_RDONLY | O_CREAT);
SafetyCheck((archiveFile < 0), "Archive failed to open.");
do do
{ {
readSize = ReadSingleModule(archiveFile, &(module)); readSize = ReadSingleModuleFromArchive(archiveFD, &(module));
free(module.moduleData); free(module.moduleData);
if (strcmp(module.moduleHeader.moduleName, filename) == 0) if (strcmp(module.moduleHeader.moduleName, filename) == 0)
{ {
free(newFilename); free(newFilename);
close(archiveFile); close(archiveFD);
return 1; return 1;
} }
} while (readSize > 0); } while (readSize > 0);
free(newFilename); free(newFilename);
close(archiveFile); close(archiveFD);
return 0; return 0;
} }
@ -133,6 +163,8 @@ int IsFileArchived(char* archiveName, char* filename)
// file already inside the archive // file already inside the archive
void AddToArchive(char* archiveName, char* filename) void AddToArchive(char* archiveName, char* filename)
{ {
ModuleStruct module;
ssize_t readSize;
char* filenameCleaned = StripFilename(filename); char* filenameCleaned = StripFilename(filename);
if (IsFileArchived(archiveName, filenameCleaned)) if (IsFileArchived(archiveName, filenameCleaned))
{ {
@ -144,27 +176,18 @@ void AddToArchive(char* archiveName, char* filename)
fprintf(stderr, "%s not found.\n", filename); fprintf(stderr, "%s not found.\n", filename);
return; return;
} }
int archiveFile = open(archiveName, O_RDWR | O_CREAT, 0644); int archiveFD = OpenArchive(archiveName, O_RDWR | O_CREAT);
SafetyCheck((archiveFile < 0), "Archive failed to open.");
ssize_t readSize;
ModuleStruct module;
do do
{ {
readSize = ReadSingleModule(archiveFile, &(module)); readSize = ReadSingleModuleFromArchive(archiveFD, &(module));
free(module.moduleData); free(module.moduleData);
} } while (readSize > 0);
while (readSize > 0); LoadModuleFromFile(filename, &module);
strcpy(module.moduleHeader.moduleName, filenameCleaned); strcpy(module.moduleHeader.moduleName, filenameCleaned);
stat(filename, &(module.moduleHeader.moduleInfo)); WriteSingleModuleToArchive(archiveFD, &module);
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);
free(filenameCleaned); free(filenameCleaned);
free(module.moduleData); free(module.moduleData);
close(archiveFD);
return; return;
} }
@ -173,7 +196,6 @@ void AddToArchive(char* archiveName, char* filename)
void RemoveFromArchive(char* archiveName, char* filename) void RemoveFromArchive(char* archiveName, char* filename)
{ {
ssize_t readSize; ssize_t readSize;
long long int moduleSize;
char* filenameCleaned = StripFilename(filename); char* filenameCleaned = StripFilename(filename);
char tempArchiveName[strlen(archiveName)+5]; char tempArchiveName[strlen(archiveName)+5];
strcpy(tempArchiveName, archiveName); strcpy(tempArchiveName, archiveName);
@ -186,18 +208,17 @@ void RemoveFromArchive(char* archiveName, char* filename)
return; return;
} }
ModuleStruct module; ModuleStruct module;
int newArchiveFile = open(tempArchiveName, O_WRONLY | O_CREAT, 0644); int newArchiveFD = OpenArchive(tempArchiveName, O_WRONLY | O_CREAT);
SafetyCheck((newArchiveFile < 0), "New archive failed to open."); readSize = ReadSingleModuleFromArchive(archiveFile, &(module));
readSize = ReadSingleModule(archiveFile, &(module));
while (readSize > 0) while (readSize > 0)
{ {
if (strcmp(module.moduleHeader.moduleName, filenameCleaned) != 0) if (strcmp(module.moduleHeader.moduleName, filenameCleaned) != 0)
WriteSingleModule(newArchiveFile, &module); WriteSingleModuleToArchive(newArchiveFD, &module);
free(module.moduleData); free(module.moduleData);
readSize = ReadSingleModule(archiveFile, &(module)); readSize = ReadSingleModuleFromArchive(archiveFile, &(module));
} }
close(archiveFile); close(archiveFile);
close(newArchiveFile); close(newArchiveFD);
SafetyCheck(remove(archiveName), "Old archive failed removal."); SafetyCheck(remove(archiveName), "Old archive failed removal.");
SafetyCheck(rename(tempArchiveName, archiveName), SafetyCheck(rename(tempArchiveName, archiveName),
"New archive failed name change."); "New archive failed name change.");
@ -213,38 +234,38 @@ void ListArchive(char* archiveName)
ssize_t readSize; ssize_t readSize;
SafetyCheck((archiveFile < 0), "Archive failed to open."); SafetyCheck((archiveFile < 0), "Archive failed to open.");
ModuleStruct module; ModuleStruct module;
readSize = ReadSingleModule(archiveFile, &module); do
while (readSize > 0)
{ {
printf("Name: %s | Size: %ld\n", module.moduleHeader.moduleName, readSize = ReadSingleModuleFromArchive(archiveFile, &module);
module.moduleHeader.moduleInfo.st_size); if (readSize > 0)
free(module.moduleData); {
readSize = ReadSingleModule(archiveFile, &module); PrintModuleHeader(&module);
} free(module.moduleData);
}
} while (readSize > 0);
return; return;
} }
int Test_ListArchive(char* archiveName) int Test_ListArchive(char* archiveName)
{ {
struct stat statArchive; struct stat statArchive;
int archiveFile = open(archiveName, O_RDONLY); int archiveFD = OpenArchive(archiveName, O_RDONLY);
ssize_t readSize; ssize_t readSize;
long int sizeTotal = 0; long int sizeTotal = 0;
if (archiveFile < 0) if (archiveFD < 0)
return -1; return -1;
ModuleStruct module; ModuleStruct module;
stat(archiveName, &statArchive); stat(archiveName, &statArchive);
do do
{ {
readSize = ReadSingleModule(archiveFile, &module); readSize = ReadSingleModuleFromArchive(archiveFD, &module);
if (readSize > 0) if (readSize > 0)
{ {
sizeTotal += sizeof(module.moduleHeader); sizeTotal += sizeof(module.moduleHeader);
sizeTotal += module.moduleHeader.moduleInfo.st_size; sizeTotal += module.moduleHeader.moduleInfo.st_size;
} }
free(module.moduleData); free(module.moduleData);
} } while (readSize > 0);
while (readSize > 0);
return sizeTotal; return sizeTotal;
} }
@ -263,13 +284,13 @@ int _ExtractArchive(char* archiveName, char* filenameIn)
return -1; return -1;
} }
ModuleStruct module; ModuleStruct module;
readSize = ReadSingleModule(archiveFile, &(module)); readSize = ReadSingleModuleFromArchive(archiveFile, &(module));
while (readSize > 0) while (readSize > 0)
{ {
if (strcmp(module.moduleHeader.moduleName, filenameCleaned) == 0) if (strcmp(module.moduleHeader.moduleName, filenameCleaned) == 0)
writeSize += write(STDOUT_FILENO, module.moduleData, module.moduleHeader.moduleInfo.st_size); writeSize += write(STDOUT_FILENO, module.moduleData, module.moduleHeader.moduleInfo.st_size);
free(module.moduleData); free(module.moduleData);
readSize = ReadSingleModule(archiveFile, &(module)); readSize = ReadSingleModuleFromArchive(archiveFile, &(module));
} }
close(archiveFile); close(archiveFile);
free(filenameCleaned); 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")); 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); int fd = open(argv[2], O_RDONLY);
TEST_ASSERT_EQUAL(80, ReadSingleModule(fd, &moduleTest)); TEST_ASSERT_EQUAL(80, ReadSingleModuleFromArchive(fd, &moduleTest));
TEST_ASSERT_EQUAL(25, ReadSingleModule(fd, &moduleTest)); TEST_ASSERT_EQUAL(25, ReadSingleModuleFromArchive(fd, &moduleTest));
TEST_ASSERT_EQUAL(27, ReadSingleModule(fd, &moduleTest)); TEST_ASSERT_EQUAL(27, ReadSingleModuleFromArchive(fd, &moduleTest));
TEST_ASSERT_EQUAL(27, ReadSingleModule(fd, &moduleTest)); TEST_ASSERT_EQUAL(27, ReadSingleModuleFromArchive(fd, &moduleTest));
close(fd); close(fd);
} }
void Test_ReadSingleModule_Should_ReadNewArchive(void) void Test_ReadSingleModuleFromArchive_Should_ReadNewArchive(void)
{ {
// TEST_IGNORE_MESSAGE("Implementation Required: AddToArchive"); // TEST_IGNORE_MESSAGE("Implementation Required: AddToArchive");
remove("test/files/newstuffyarchive.test"); 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"); 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"); 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"); 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"); 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 // Fail test removed due to printf breaking test
// AddToArchive("test/files/newstuffyarchive.test", "test/files/fake.test"); // AddToArchive("test/files/newstuffyarchive.test", "test/files/fake.test");
// TEST_ASSERT_EQUAL(0, ReadSingleModule(fd, &moduleTest)); // TEST_ASSERT_EQUAL(0, ReadSingleModuleFromArchive(fd, &moduleTest));
close(fd); close(testFD);
}
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"));
} }
void Test_ListArchive_Should_ListCorrectly(void) void Test_ListArchive_Should_ListCorrectly(void)
@ -109,14 +98,25 @@ void Test_ExtractArchive_Should_ExtractCorrectly(void)
TEST_ASSERT_GREATER_THAN(0, outputFileSize); 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) int main(void)
{ {
UNITY_BEGIN(); UNITY_BEGIN();
RUN_TEST(Test_StuffyArgument_Should_ReturnCorrectly); RUN_TEST(Test_StuffyArgument_Should_ReturnCorrectly);
RUN_TEST(Test_StripFilename_Should_ChangeName); RUN_TEST(Test_StripFilename_Should_ChangeName);
RUN_TEST(Test_IsFileArchived_Should_ReturnCorrectly); RUN_TEST(Test_IsFileArchived_Should_ReturnCorrectly);
RUN_TEST(Test_ReadSingleModule_Should_ReadCorrectly); RUN_TEST(Test_ReadSingleModuleFromArchive_Should_ReadCorrectly);
RUN_TEST(Test_ReadSingleModule_Should_ReadNewArchive); RUN_TEST(Test_ReadSingleModuleFromArchive_Should_ReadNewArchive);
RUN_TEST(Test_ListArchive_Should_ListCorrectly); RUN_TEST(Test_ListArchive_Should_ListCorrectly);
RUN_TEST(Test_ExtractArchive_Should_ExtractCorrectly); RUN_TEST(Test_ExtractArchive_Should_ExtractCorrectly);
RUN_TEST(Test_RemoveFromArchive_Should_RemoveCorrectly); RUN_TEST(Test_RemoveFromArchive_Should_RemoveCorrectly);