Add and List fully functional with testing
This commit is contained in:
parent
ff37c5e208
commit
df97ff36d9
@ -25,6 +25,7 @@ int IsFileArchived(char* archiveName, char* filename);
|
|||||||
void AddToArchive(char* archiveName, char* filename);
|
void AddToArchive(char* archiveName, char* filename);
|
||||||
void RemoveFromArchive(char* archiveName, char* filename);
|
void RemoveFromArchive(char* archiveName, char* filename);
|
||||||
void ListArchive(char* archiveName);
|
void ListArchive(char* archiveName);
|
||||||
|
int Test_ListArchive(char* archiveName);
|
||||||
void ExtractArchive(char* argv[]);
|
void ExtractArchive(char* argv[]);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
93
src/stuffy.c
93
src/stuffy.c
@ -23,24 +23,18 @@ int ReadSingleModule(int fd, ModuleStruct* module)
|
|||||||
// Returns size of data read when reading a module header
|
// Returns size of data read when reading a module header
|
||||||
int WriteSingleModule(int fd, ModuleStruct* module, char* filename)
|
int WriteSingleModule(int fd, ModuleStruct* module, char* filename)
|
||||||
{
|
{
|
||||||
int readSize;
|
int writeSize;
|
||||||
long long int moduleSize;
|
long long int moduleSize;
|
||||||
readSize = read(fd, &(module->moduleHeader), sizeof(module->moduleHeader));
|
|
||||||
moduleSize = (long long) module->moduleHeader.moduleInfo.st_size;
|
moduleSize = (long long) module->moduleHeader.moduleInfo.st_size;
|
||||||
module->moduleData = malloc(moduleSize);
|
module->moduleData = malloc(moduleSize);
|
||||||
readSize = read(fd, module->moduleData, moduleSize);
|
int fdInput = open(filename, O_RDONLY);
|
||||||
|
if (fdInput < 0)
|
||||||
|
return 0;
|
||||||
// strcpy(module.moduleHeader.moduleName, newFilename);
|
read(fdInput, module->moduleData, moduleSize);
|
||||||
// stat(filename, &(module.moduleHeader.moduleInfo));
|
close(fdInput);
|
||||||
// write(archiveFile, &module.moduleHeader, sizeof(module.moduleHeader));
|
writeSize = write(fd, &(module->moduleHeader), sizeof(module->moduleHeader));
|
||||||
// moduleSize = (long long) module.moduleHeader.moduleInfo.st_size;
|
writeSize = write(fd, module->moduleData, moduleSize);
|
||||||
// char moduleData[moduleSize];
|
return writeSize;
|
||||||
// write(archiveFile, moduleData, moduleSize);
|
|
||||||
// close(archiveFile);
|
|
||||||
|
|
||||||
|
|
||||||
return readSize;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Checks if operation went ok, if not then prints message and exits
|
// 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)
|
int IsFileArchived(char* archiveName, char* filename)
|
||||||
{
|
{
|
||||||
ModuleStruct module;
|
ModuleStruct module;
|
||||||
long long int moduleSize;
|
|
||||||
ssize_t readSize;
|
ssize_t readSize;
|
||||||
char* newFilename = StripFilename(filename);
|
char* newFilename = StripFilename(filename);
|
||||||
int archiveFile = open(archiveName, O_RDONLY | O_CREAT, 0644);
|
int archiveFile = open(archiveName, O_RDONLY | O_CREAT, 0644);
|
||||||
@ -127,9 +120,7 @@ int IsFileArchived(char* archiveName, char* filename)
|
|||||||
{
|
{
|
||||||
readSize = ReadSingleModule(archiveFile, &(module));
|
readSize = ReadSingleModule(archiveFile, &(module));
|
||||||
free(module.moduleData);
|
free(module.moduleData);
|
||||||
module.moduleData = NULL;
|
if (strcmp(module.moduleHeader.moduleName, filename) == 0)
|
||||||
printf("%s\n", newFilename);
|
|
||||||
if (!strcmp(module.moduleHeader.moduleName, filename))
|
|
||||||
{
|
{
|
||||||
free(newFilename);
|
free(newFilename);
|
||||||
close(archiveFile);
|
close(archiveFile);
|
||||||
@ -147,31 +138,28 @@ 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)
|
||||||
{
|
{
|
||||||
// SafetyCheck(IsFileArchived(archiveName, filename),
|
SafetyCheck(IsFileArchived(archiveName, filename),
|
||||||
// "File already exists in archive, try removing file first.");
|
"File already exists in archive, try removing file first.");
|
||||||
int archiveFile = open(archiveName, O_RDWR | O_CREAT, 0644);
|
int archiveFile = open(archiveName, O_RDWR | O_CREAT, 0644);
|
||||||
SafetyCheck((archiveFile < 0), "Archive failed to open.");
|
SafetyCheck((archiveFile < 0), "Archive failed to open.");
|
||||||
ssize_t readSize;
|
ssize_t readSize;
|
||||||
// long long int moduleSize;
|
|
||||||
ModuleStruct module;
|
ModuleStruct module;
|
||||||
// readSize = read(archiveFile, &module.moduleHeader, sizeof(module.moduleHeader));
|
|
||||||
do
|
do
|
||||||
readSize = ReadSingleModule(archiveFile, &(module));
|
readSize = ReadSingleModule(archiveFile, &(module));
|
||||||
while (readSize > 0);
|
while (readSize > 0);
|
||||||
char* newFilename = StripFilename(filename);
|
char* filenameCleaned = StripFilename(filename);
|
||||||
WriteSingleModule(archiveFile, &module, newFilename);
|
strcpy(module.moduleHeader.moduleName, filenameCleaned);
|
||||||
free(newFilename);
|
stat(filename, &(module.moduleHeader.moduleInfo));
|
||||||
|
WriteSingleModule(archiveFile, &module, filename);
|
||||||
|
free(filenameCleaned);
|
||||||
free(module.moduleData);
|
free(module.moduleData);
|
||||||
// return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check through archive for file, if file exists with name, remove it
|
// Check through archive for file, if file exists with name, remove it
|
||||||
// If file not in archive, print "somefile was not found."
|
// If file not in archive, print "somefile was not found."
|
||||||
void RemoveFromArchive(char* archiveName, char* filename)
|
void RemoveFromArchive(char* archiveName, char* filename)
|
||||||
{
|
{
|
||||||
printf("'RemoveFromArchive()' function is still broken.\n");
|
|
||||||
return;
|
|
||||||
|
|
||||||
ssize_t readSize;
|
ssize_t readSize;
|
||||||
long long int moduleSize;
|
long long int moduleSize;
|
||||||
char* newFileName;
|
char* newFileName;
|
||||||
@ -205,28 +193,41 @@ void ListArchive(char* archiveName)
|
|||||||
{
|
{
|
||||||
int archiveFile = open(archiveName, O_RDONLY);
|
int archiveFile = open(archiveName, O_RDONLY);
|
||||||
ssize_t readSize;
|
ssize_t readSize;
|
||||||
long long int moduleSize;
|
|
||||||
SafetyCheck((archiveFile < 0), "Archive failed to open.");
|
SafetyCheck((archiveFile < 0), "Archive failed to open.");
|
||||||
// ModuleStruct module;
|
ModuleStruct module;
|
||||||
// do
|
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)
|
|
||||||
{
|
{
|
||||||
moduleSize = (long long) moduleHeader.moduleInfo.st_size;
|
readSize = ReadSingleModule(archiveFile, &module);
|
||||||
printf("Name: %s | Size: %lld\n", moduleHeader.moduleName, moduleSize);
|
printf("Name: %s | Size: %ld\n", module.moduleHeader.moduleName,
|
||||||
char moduleData[moduleSize];
|
module.moduleHeader.moduleInfo.st_size);
|
||||||
read(archiveFile, &moduleData, moduleSize);
|
|
||||||
readSize = read(archiveFile, &moduleHeader, sizeof(moduleHeader));
|
|
||||||
}
|
}
|
||||||
|
while (readSize > 0);
|
||||||
return;
|
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
|
// Extract data of name file from archive to stdout, which is then redirected
|
||||||
// Extracted data remains in archive
|
// Extracted data remains in archive
|
||||||
void ExtractArchive(char* argv[])
|
void ExtractArchive(char* argv[])
|
||||||
|
28
test/test.c
28
test/test.c
@ -35,6 +35,16 @@ void Test_StuffyArgument_Should_ReturnCorrectly(void)
|
|||||||
TEST_ASSERT_EQUAL(-1, StuffyArgument(4, argv));
|
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)
|
void Test_StripFilename_Should_ChangeName(void)
|
||||||
{
|
{
|
||||||
TEST_ASSERT_EQUAL_STRING("mystuffyarchive.test", StripFilename("test/files/mystuffyarchive.test"));
|
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)
|
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);
|
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));
|
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));
|
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));
|
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));
|
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);
|
close(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,7 +90,9 @@ void Test_RemoveFromArchive_Should_RemoveCorrectly(void)
|
|||||||
|
|
||||||
void Test_ListArchive_Should_ListCorrectly(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)
|
void Test_ExtractArchive_Should_ExtractCorrectly(void)
|
||||||
@ -90,6 +105,7 @@ 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_ReadSingleModule_Should_ReadCorrectly);
|
RUN_TEST(Test_ReadSingleModule_Should_ReadCorrectly);
|
||||||
RUN_TEST(Test_ReadSingleModule_Should_ReadNewArchive);
|
RUN_TEST(Test_ReadSingleModule_Should_ReadNewArchive);
|
||||||
RUN_TEST(Test_RemoveFromArchive_Should_RemoveCorrectly);
|
RUN_TEST(Test_RemoveFromArchive_Should_RemoveCorrectly);
|
||||||
|
Loading…
Reference in New Issue
Block a user