Extracted fixed and working

This commit is contained in:
TriantaTV 2022-11-11 00:29:37 -06:00
parent 22fadea36c
commit 074fb0507b
3 changed files with 13 additions and 22 deletions

View File

@ -27,7 +27,6 @@ void RemoveFromArchive(char* archiveName, char* filename);
void ListArchive(char* archiveName);
int Test_ListArchive(char* archiveName);
void ExtractArchive(int argc, char* argv[]);
int _ExtractArchive(char* archiveName, char* filenameIn, int fd);
void _ExtractArchiveToFile(char* filenameOut);
int _ExtractArchive(char* archiveName, char* filenameIn);
#endif

View File

@ -4,6 +4,7 @@
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "stuffy.h"
// Put a module header into a HeaderStruct and read (skip) module data
@ -148,7 +149,10 @@ void AddToArchive(char* archiveName, char* filename)
ssize_t readSize;
ModuleStruct module;
do
{
readSize = ReadSingleModule(archiveFile, &(module));
free(module.moduleData);
}
while (readSize > 0);
strcpy(module.moduleHeader.moduleName, filenameCleaned);
stat(filename, &(module.moduleHeader.moduleInfo));
@ -189,6 +193,7 @@ void RemoveFromArchive(char* archiveName, char* filename)
{
if (strcmp(module.moduleHeader.moduleName, filenameCleaned) != 0)
WriteSingleModule(newArchiveFile, &module);
free(module.moduleData);
readSize = ReadSingleModule(archiveFile, &(module));
}
close(archiveFile);
@ -213,6 +218,7 @@ void ListArchive(char* archiveName)
{
printf("Name: %s | Size: %ld\n", module.moduleHeader.moduleName,
module.moduleHeader.moduleInfo.st_size);
free(module.moduleData);
readSize = ReadSingleModule(archiveFile, &module);
}
return;
@ -236,6 +242,7 @@ int Test_ListArchive(char* archiveName)
sizeTotal += sizeof(module.moduleHeader);
sizeTotal += module.moduleHeader.moduleInfo.st_size;
}
free(module.moduleData);
}
while (readSize > 0);
return sizeTotal;
@ -243,9 +250,8 @@ int Test_ListArchive(char* archiveName)
// Extract data of name file from archive to stdout, which is then redirected
// Extracted data remains in archive
int _ExtractArchive(char* archiveName, char* filenameIn, int fd)
int _ExtractArchive(char* archiveName, char* filenameIn)
{
ssize_t readSize;
ssize_t writeSize = 0;
long long int moduleSize;
@ -261,7 +267,8 @@ int _ExtractArchive(char* archiveName, char* filenameIn, int fd)
while (readSize > 0)
{
if (strcmp(module.moduleHeader.moduleName, filenameCleaned) == 0)
writeSize += write(1, module.moduleData, sizeof(module.moduleData));
writeSize += write(STDOUT_FILENO, module.moduleData, module.moduleHeader.moduleInfo.st_size);
free(module.moduleData);
readSize = ReadSingleModule(archiveFile, &(module));
}
close(archiveFile);
@ -269,24 +276,9 @@ int _ExtractArchive(char* archiveName, char* filenameIn, int fd)
return writeSize;
}
void _ExtractArchiveToFile(char* filenameOut)
{
int fd = open(filenameOut, O_WRONLY | O_CREAT, 0644);
char buffer[1024];
while (read(1, &buffer, sizeof(buffer)) > 0)
{
write(fd, &buffer, sizeof(buffer));
}
close(fd);
}
void ExtractArchive(int argc, char* argv[])
{
int writeSize;
writeSize = _ExtractArchive(argv[2], argv[3], 0);
if (writeSize < 0)
return;
if (argc == 6)
_ExtractArchiveToFile(argv[5]);
writeSize = _ExtractArchive(argv[2], argv[3]);
return;
}

View File

@ -105,7 +105,7 @@ void Test_ListArchive_Should_ListCorrectly(void)
void Test_ExtractArchive_Should_ExtractCorrectly(void)
{
// TEST_IGNORE_MESSAGE("Implementation Required: ExtractArchive");
int outputFileSize = _ExtractArchive("test/files/newstuffyarchive.test", argv[3], 0);
int outputFileSize = _ExtractArchive("test/files/newstuffyarchive.test", argv[3]);
TEST_ASSERT_GREATER_THAN(0, outputFileSize);
}