Compare commits
4 commits
epitar-dem
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ca10e8aac8 | ||
|
|
2c07b3f2f9 | ||
|
|
c6408716d5 | ||
|
|
8ec0d9a4ee |
11 changed files with 93 additions and 64 deletions
|
|
@ -22,7 +22,7 @@ $(TARGET): $(OBJS)
|
||||||
@echo $(OBJS)
|
@echo $(OBJS)
|
||||||
|
|
||||||
all: $(OBJS)
|
all: $(OBJS)
|
||||||
$(CC) -o $@ $^ $(LDFLAGS) $(LDLIBS)
|
$(CC) -o $(TARGET) $^ $(LDFLAGS) $(LDLIBS)
|
||||||
|
|
||||||
debug: CFLAGS += $(DBG_CFLAGS)
|
debug: CFLAGS += $(DBG_CFLAGS)
|
||||||
debug: LDFLAGS += $(DBG_LDFLAGS)
|
debug: LDFLAGS += $(DBG_LDFLAGS)
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,57 @@
|
||||||
#include "utils/config.h"
|
#include "utils/config.h"
|
||||||
#include "utils/errors.h"
|
#include "utils/errors.h"
|
||||||
|
|
||||||
|
static int handle_err_code(enum error_code err, struct config *config)
|
||||||
|
{
|
||||||
|
int ret_code = 0;
|
||||||
|
switch (err)
|
||||||
|
{
|
||||||
|
case SUCCESS:
|
||||||
|
ret_code = 0;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case BAD_CHECKSUM:
|
||||||
|
puts("epitar: bad checksum");
|
||||||
|
ret_code = 2;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case NOT_IMPLEMENTED:
|
||||||
|
puts("epitar: Function not implemented");
|
||||||
|
ret_code = 2;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FILE_NOT_FOUND:
|
||||||
|
if (config->mode == ARCHIVE)
|
||||||
|
{
|
||||||
|
// Rest of code should have printed the first part of the line
|
||||||
|
printf(" to tarball %s\n", config->archive_file);
|
||||||
|
ret_code = 3;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
__attribute__((fallthrough));
|
||||||
|
|
||||||
|
case EMPTY_ARCHIVE:
|
||||||
|
if (config->mode == ARCHIVE)
|
||||||
|
{
|
||||||
|
puts("epitar: cowardly refusing to create an empty archive");
|
||||||
|
ret_code = 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("epitar: error extracting tarball %s\n",
|
||||||
|
config->archive_file);
|
||||||
|
ret_code = 3;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
printf("epitar: error extracting tarball %s\n", config->archive_file);
|
||||||
|
ret_code = 3;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return ret_code;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
// Handle args
|
// Handle args
|
||||||
|
|
@ -32,48 +83,7 @@ int main(int argc, char **argv)
|
||||||
err = archive(config.archive_file, config.input_files, config.verbose);
|
err = archive(config.archive_file, config.input_files, config.verbose);
|
||||||
}
|
}
|
||||||
|
|
||||||
int ret_code = 0;
|
int ret_code = handle_err_code(err, &config);
|
||||||
switch (err)
|
|
||||||
{
|
|
||||||
case SUCCESS:
|
|
||||||
ret_code = 0;
|
|
||||||
break;
|
|
||||||
|
|
||||||
// case UNKNOWN_ERROR:
|
|
||||||
// printf("epitar: error extracting tarball %s\n",
|
|
||||||
// config.archive_file); ret_code = 3; break;
|
|
||||||
|
|
||||||
case BAD_CHECKSUM:
|
|
||||||
puts("epitar: bad checksum");
|
|
||||||
ret_code = 2;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case NOT_IMPLEMENTED:
|
|
||||||
puts("epitar: Function not implemented");
|
|
||||||
ret_code = 2;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case FILE_NOT_FOUND:
|
|
||||||
if (config.mode == ARCHIVE)
|
|
||||||
{
|
|
||||||
// Rest of code should have printed the first part of the line
|
|
||||||
printf(" to tarball %s\n", config.archive_file);
|
|
||||||
ret_code = 3;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
__attribute__((fallthrough));
|
|
||||||
|
|
||||||
case EMPTY_ARCHIVE:
|
|
||||||
puts("epitar: cowardly refusing to create an empty archive");
|
|
||||||
ret_code = 1;
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
printf("epitar: error extracting tarball %s\n", config.archive_file);
|
|
||||||
ret_code = 3;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
destroy_config(&config);
|
destroy_config(&config);
|
||||||
return ret_code;
|
return ret_code;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -100,7 +100,7 @@ static enum error_code archivefile(FILE *archive, char *file_path, bool verbose)
|
||||||
FILE *file = fopen(file_path, "r");
|
FILE *file = fopen(file_path, "r");
|
||||||
if (file == NULL)
|
if (file == NULL)
|
||||||
{
|
{
|
||||||
printf("Unable to add file %s", file_path);
|
printf("unable to add file %s", file_path);
|
||||||
return FILE_NOT_FOUND;
|
return FILE_NOT_FOUND;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -162,7 +162,7 @@ static enum error_code archivedir(FILE *archive, char *dir_path, bool verbose)
|
||||||
if (isdir == FILE_NOT_FOUND)
|
if (isdir == FILE_NOT_FOUND)
|
||||||
{
|
{
|
||||||
// Does not exists
|
// Does not exists
|
||||||
printf("Unable to add file %s", dir_path);
|
printf("unable to add file %s", dir_path);
|
||||||
err = FILE_NOT_FOUND;
|
err = FILE_NOT_FOUND;
|
||||||
}
|
}
|
||||||
else if (isdir == SUCCESS)
|
else if (isdir == SUCCESS)
|
||||||
|
|
@ -204,7 +204,7 @@ enum error_code archive(char *archive_name, char **files, bool verbose)
|
||||||
if (isdir == FILE_NOT_FOUND)
|
if (isdir == FILE_NOT_FOUND)
|
||||||
{
|
{
|
||||||
// Does not exists
|
// Does not exists
|
||||||
printf("Unable to add file %s", *current_file);
|
printf("unable to add file %s", *current_file);
|
||||||
err = FILE_NOT_FOUND;
|
err = FILE_NOT_FOUND;
|
||||||
}
|
}
|
||||||
else if (isdir == SUCCESS)
|
else if (isdir == SUCCESS)
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,10 @@
|
||||||
#ifndef ARCHIVE_H
|
#ifndef ARCHIVE_H
|
||||||
#define ARCHIVE_H
|
#define ARCHIVE_H
|
||||||
|
|
||||||
#include "../utils/errors.h"
|
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#include "../utils/errors.h"
|
||||||
|
|
||||||
/* @brief Archives the listed `files` into `archive_name`
|
/* @brief Archives the listed `files` into `archive_name`
|
||||||
* @return 0 on success, the corresponding error code otherwise
|
* @return 0 on success, the corresponding error code otherwise
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "../utils/filesystem.h"
|
#include "../utils/filesystem.h"
|
||||||
|
#include "tar.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
|
@ -49,9 +50,14 @@ static enum error_code extract_ustar(FILE *stream, struct ustar_header *header)
|
||||||
static enum error_code extract_unixtar(FILE *stream,
|
static enum error_code extract_unixtar(FILE *stream,
|
||||||
struct unixtar_header *header)
|
struct unixtar_header *header)
|
||||||
{
|
{
|
||||||
(void)stream;
|
struct file_stats stats = { .name = header->name,
|
||||||
(void)header;
|
.length = strtol(header->size, NULL, 8),
|
||||||
return NOT_IMPLEMENTED;
|
.content_stream = stream,
|
||||||
|
.mode = header->mode,
|
||||||
|
.uid = header->uid,
|
||||||
|
.gid = header->gid,
|
||||||
|
.mtime = header->mtime };
|
||||||
|
return create_file(&stats);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -130,6 +136,12 @@ enum error_code extract(char *archive_name, bool verbose)
|
||||||
got_empty_block = false;
|
got_empty_block = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!check_checksum(&header))
|
||||||
|
{
|
||||||
|
fclose(stream);
|
||||||
|
return BAD_CHECKSUM;
|
||||||
|
}
|
||||||
|
|
||||||
// Extract file
|
// Extract file
|
||||||
enum error_code err = extract_file(stream, &header, verbose);
|
enum error_code err = extract_file(stream, &header, verbose);
|
||||||
if (err != SUCCESS)
|
if (err != SUCCESS)
|
||||||
|
|
|
||||||
|
|
@ -81,7 +81,7 @@ enum arg_error_code args_handler(int argc, char **argv, struct config *config)
|
||||||
|
|
||||||
// Parse option flags
|
// Parse option flags
|
||||||
int opt;
|
int opt;
|
||||||
while ((opt = getopt(argc, argv, "cxvh")) != -1)
|
while ((opt = getopt(argc, argv, ":cvxh")) != -1)
|
||||||
{
|
{
|
||||||
int err = handle_opt(opt, config);
|
int err = handle_opt(opt, config);
|
||||||
if (err == ARG_HELP)
|
if (err == ARG_HELP)
|
||||||
|
|
|
||||||
|
|
@ -180,7 +180,7 @@ char *listdirectory(char *path)
|
||||||
return de->d_name;
|
return de->d_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
void freedirectory()
|
void freedirectory(void)
|
||||||
{
|
{
|
||||||
if (current_dir != NULL)
|
if (current_dir != NULL)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -59,6 +59,6 @@ char *listdirectory(char *path);
|
||||||
* related allocated memory
|
* related allocated memory
|
||||||
* @warn Use this function if you only partially listed a directory previously.
|
* @warn Use this function if you only partially listed a directory previously.
|
||||||
*/
|
*/
|
||||||
void freedirectory();
|
void freedirectory(void);
|
||||||
|
|
||||||
#endif // FILESYSTEM_H
|
#endif // FILESYSTEM_H
|
||||||
|
|
|
||||||
|
|
@ -1,3 +0,0 @@
|
||||||
#include "utils.h"
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
@ -1,6 +0,0 @@
|
||||||
#ifndef MISC_H
|
|
||||||
#define MISC_H
|
|
||||||
|
|
||||||
void print_str_array(const char **input_files);
|
|
||||||
|
|
||||||
#endif /* MISC_H */
|
|
||||||
|
|
@ -282,7 +282,23 @@ echo -e "\n$BBlue=== Help and Invalid Arguments ===$Color_Off"
|
||||||
test_help "Help flag"
|
test_help "Help flag"
|
||||||
test_invalid_args "No arguments" ""
|
test_invalid_args "No arguments" ""
|
||||||
test_invalid_args "Invalid flag" "-z"
|
test_invalid_args "Invalid flag" "-z"
|
||||||
test_invalid_args "Missing archive" "-c"
|
test_invalid_args "Invalid flagS" "-zbk"
|
||||||
|
test_invalid_args "Invalid position" "test.tar -h"
|
||||||
|
test_invalid_args "Invalid flags and position" "test.tar -z"
|
||||||
|
test_invalid_args "Invalid flags and position mix" "test.tar -zhx"
|
||||||
|
test_invalid_args "Invalid position" "test.tar -z"
|
||||||
|
test_invalid_args "Invalid flag mix" "-zh"
|
||||||
|
test_invalid_args "Invalid flag mix 2" "-hnh"
|
||||||
|
test_invalid_args "Invalid flag mix 3" "-cnc"
|
||||||
|
test_invalid_args "Invalid flag mix 3" "-hhhz"
|
||||||
|
test_invalid_args "Invalid flag mix 3" "-hnc"
|
||||||
|
test_invalid_args "Invalid flag mix 3" "-cch"
|
||||||
|
test_invalid_args "Invalid flag mix 3" "-cnc"
|
||||||
|
test_invalid_args "Valid flags multiple" "-cc"
|
||||||
|
test_invalid_args "Incompatible flag" "-cx"
|
||||||
|
test_invalid_args "Missing op flag" "-v"
|
||||||
|
test_invalid_args "Missing archive (c)" "-c"
|
||||||
|
test_invalid_args "Missing archive (x)" "-x"
|
||||||
test_invalid_args "Extract with files" "-x archive.tar file.txt"
|
test_invalid_args "Extract with files" "-x archive.tar file.txt"
|
||||||
|
|
||||||
echo -e "\n$BBlue=== Edge Cases ===$Color_Off"
|
echo -e "\n$BBlue=== Edge Cases ===$Color_Off"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue