Implemented adding to archive
This commit is contained in:
parent
1146623d33
commit
ea2669da23
5
Makefile
5
Makefile
@ -8,10 +8,7 @@ compile:
|
||||
gcc $(INC) -c -o build/stuffy.o src/stuffy.c
|
||||
|
||||
link:
|
||||
gcc -o bin/stuffy.out build/*.o
|
||||
|
||||
exec: compile link
|
||||
./bin/stuffy.out -a test/example.tar test/sample.txt
|
||||
gcc -o bin/stuffy.out build/stuffy.o build/main.o
|
||||
|
||||
test: testCompile testLink testExec
|
||||
|
||||
|
@ -1,11 +1,20 @@
|
||||
#ifndef STUFFY_H
|
||||
#define STUFFY_H
|
||||
#include <sys/stat.h>
|
||||
|
||||
typedef struct HeaderStruct
|
||||
{
|
||||
char moduleName[50];
|
||||
struct stat moduleInfo;
|
||||
} HeaderStruct;
|
||||
|
||||
void Stuffy(int argc, char* argv[]);
|
||||
int StuffyArgument(int argc, char** argv);
|
||||
void StuffyAction(char* argv[], int archiveAction);
|
||||
int IsFileArchived(int archiveFile, char* fileName);
|
||||
void AddToArchive(char* archiveName, char* fileName);
|
||||
void RemoveFromArchive(char* archiveName, char* fileName);
|
||||
void ListArchive(char* archiveName);
|
||||
void ExtractArchive(char* argv[]);
|
||||
|
||||
#endif
|
||||
|
70
src/stuffy.c
70
src/stuffy.c
@ -1,6 +1,9 @@
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include "stuffy.h"
|
||||
|
||||
void Stuffy(int argc, char* argv[])
|
||||
@ -37,12 +40,58 @@ int StuffyArgument(int argc, char* argv[])
|
||||
|
||||
void StuffyAction(char* argv[], int archiveAction)
|
||||
{
|
||||
;
|
||||
if (archiveAction == 0)
|
||||
AddToArchive(argv[2], argv[3]);
|
||||
if (archiveAction == 1)
|
||||
RemoveFromArchive(argv[2], argv[3]);
|
||||
if (archiveAction == 2)
|
||||
ListArchive(argv[2]);
|
||||
if (archiveAction == 3)
|
||||
ExtractArchive(argv);
|
||||
}
|
||||
|
||||
int IsFileArchived(int archiveFile, char* fileName)
|
||||
{
|
||||
ssize_t readSize;
|
||||
long long int moduleSize;
|
||||
HeaderStruct moduleHeader;
|
||||
readSize = read(archiveFile, &moduleHeader, sizeof(HeaderStruct));
|
||||
if (!strcmp(moduleHeader.moduleName, fileName))
|
||||
return 1;
|
||||
while (readSize > 0)
|
||||
{
|
||||
moduleSize = (long long) moduleHeader.moduleInfo.st_size;
|
||||
char moduleData[moduleSize];
|
||||
readSize = read(archiveFile, &moduleData, moduleSize);
|
||||
readSize = read(archiveFile, &moduleHeader, sizeof(HeaderStruct));
|
||||
if (!strcmp(moduleHeader.moduleName, fileName))
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void AddToArchive(char* archiveName, char* fileName)
|
||||
{
|
||||
;
|
||||
int archiveFile = open(archiveName, O_RDWR | O_CREAT, 0644);
|
||||
if (archiveFile < 0)
|
||||
{
|
||||
fprintf(stderr, "%s failed to open.\n", archiveName);
|
||||
exit(1);
|
||||
}
|
||||
if (IsFileArchived(archiveFile, fileName))
|
||||
{
|
||||
fprintf(stderr, "File already exists\n");
|
||||
exit(1);
|
||||
}
|
||||
HeaderStruct newModuleHeader;
|
||||
strcpy(newModuleHeader.moduleName, fileName);
|
||||
stat(fileName, &(newModuleHeader.moduleInfo));
|
||||
write(archiveFile, &newModuleHeader, sizeof(newModuleHeader));
|
||||
long long int newModuleSize = (long long) newModuleHeader.moduleInfo.st_size;
|
||||
char newModuleData[newModuleSize];
|
||||
write(archiveFile, newModuleData, newModuleSize);
|
||||
close(archiveFile);
|
||||
return;
|
||||
}
|
||||
|
||||
void RemoveFromArchive(char* archiveName, char* fileName)
|
||||
@ -52,10 +101,23 @@ void RemoveFromArchive(char* archiveName, char* fileName)
|
||||
|
||||
void ListArchive(char* archiveName)
|
||||
{
|
||||
;
|
||||
int archiveFile = open(archiveName, O_RDONLY);
|
||||
ssize_t readSize;
|
||||
long long int moduleSize;
|
||||
if (archiveFile < 0)
|
||||
{
|
||||
fprintf(stderr, "%s does not exist.\n", archiveName);
|
||||
exit(1);
|
||||
}
|
||||
HeaderStruct moduleHeader;
|
||||
while (readSize > 0)
|
||||
{
|
||||
moduleSize = (long long) moduleHeader.moduleInfo.st_size;
|
||||
char moduleData[moduleSize];
|
||||
}
|
||||
}
|
||||
|
||||
void ExtractArchive(char*)
|
||||
void ExtractArchive(char* argv[])
|
||||
{
|
||||
;
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ char* argv[6];
|
||||
|
||||
void setUp(void)
|
||||
{
|
||||
argv[0] = "./stuffy";
|
||||
argv[0] = "./stuffy.out";
|
||||
argv[1] = "-a";
|
||||
argv[2] = "mystuffyarchive";
|
||||
argv[3] = "somefile";
|
||||
|
Loading…
Reference in New Issue
Block a user