Continue refactor for consistency

This commit is contained in:
Nathan Fisher 2024-02-11 18:54:38 -05:00
parent f72b120b0c
commit cc8ecc55b7
12 changed files with 166 additions and 39 deletions

127
bin.c Normal file
View file

@ -0,0 +1,127 @@
#include "haggis.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
typedef enum {
action_error,
command_help,
create_archive,
extract_archive,
list_files,
} haggis_action;
struct option main_opts[] = {
{"help", no_argument, &help_flag, 'h'},
{"version", no_argument, &version_flag, 'V'},
{0, 0, 0, 0},
};
struct option create_opts[] = {
{"algorithm", required_argument, NULL, 'a'},
{"quiet", no_argument, &quiet_flag, 'q'},
{"zstd", no_argument, &zstd_flag, 'z'},
{"level", required_argument, NULL, 'l'},
{"help", no_argument, &help_flag, 'h'},
{0, 0, 0, 0},
};
struct option extract_opts[] = {
{"quiet", no_argument, &quiet_flag, 'q'},
{"zstd", no_argument, &zstd_flag, 'z'},
{"change", required_argument, NULL, 'c'},
{"help", no_argument, &help_flag, 'h'},
{0, 0, 0, 0},
};
struct option list_opts[] = {
{"color", no_argument, &color_flag, 'c'},
{"long", no_argument, &long_flag, 'l'},
{"files", no_argument, &files_flag, 'f'},
{"pager", optional_argument, NULL, 'p'},
{"zstd", no_argument, &zstd_flag, 'z'},
{"help", no_argument, &help_flag, 'h'},
{0, 0, 0, 0},
};
haggis_action parse_action(char *spec) {
haggis_action action;
if (memcmp(spec, "help", 4) == 0) {
action = command_help;
} else if (memcmp(spec, "create", 6) == 0 || memcmp(spec, "cr", 2) == 0) {
action = create_archive;
} else if (memcmp(spec, "extract", 7) == 0 || memcmp(spec, "ex", 2) == 0) {
action = extract_archive;
} else if (memcmp(spec, "list", 4) == 0 || memcmp(spec, "ls", 2) == 0) {
action = list_files;
} else {
action = action_error;
}
return action;
}
haggis_algorithm parse_algorithm(char *spec) {
haggis_algorithm alg = skip;
return alg;
}
void print_help() {
fprintf(stderr, "todo\n");
}
void print_version() {
fprintf(stderr, "todo\n");
}
void print_usage() {
fprintf(stderr, "todo\n");
}
int main(int argc, char **argv) {
int c, idx = 0, help_flag = 0, version_flag = 0, quiet_flag = 0,
zstd_flag = 0, long_flag = 0, files_flag = 0, color_flag = 0;
haggis_action action = no_action;
char *archive = NULL;
char *subcommand = NULL;
if (argc < 2) {
print_usage();
exit(1);
}
subcommand = *argv;
if (memcmp(subcommand, "create", 6) == 0) {
action = create_archive;
argv++;
} else if (memcmp(subcommand, "extract", 7) == 0) {
action = extract_archive;
argv++;
} else if (memcmp(subcommand, "list", 4) == 0) {
action == list_files;
argv++;
} else if (memcmp(subcommand, "help", 4) == 0) {
// todo
print_help();
exit(0);
} else {
print_usage();
exit(1);
}
archive = *argv;
/*while (1) {
c = getopt_long(argc, argv, "hvqz", opts, &idx);
if (c == -1)
break;
switch (c) {
case 'h':
print_help();
break;
case 'v':
print_version();
break;
case '?':
break;
default:
break;
}
}*/
return 0;
}

View file

@ -302,7 +302,7 @@ int haggis_validate_cksum(haggis_file *file) {
return 0; return 0;
} }
int haggis_file_init(char *path, haggis_file *hf, haggis_algorithm a) { int haggis_file_init(haggis_file *self, char *path, haggis_algorithm a) {
FILE *f; FILE *f;
long len; long len;
@ -318,16 +318,16 @@ int haggis_file_init(char *path, haggis_file *hf, haggis_algorithm a) {
fclose(f); fclose(f);
return 2; return 2;
} }
hf->len.val = (uint64_t)len; self->len.val = (uint64_t)len;
rewind(f); rewind(f);
hf->data = calloc(1, (size_t)len); self->data = calloc(1, (size_t)len);
if (fread(hf->data, 1, (size_t)len, f) != (size_t)len) { if (fread(self->data, 1, (size_t)len, f) != (size_t)len) {
free(hf->data); free(self->data);
fclose(f); fclose(f);
return 1; return 1;
} }
fclose(f); fclose(f);
haggis_init_cksum(hf, a); haggis_init_cksum(self, a);
return 0; return 0;
} }
@ -336,32 +336,32 @@ void haggis_file_deinit(haggis_file *f) {
free(f->data); free(f->data);
} }
int haggis_store_file(FILE *stream, haggis_file *file) { int haggis_store_file(haggis_file *self, FILE *stream) {
if (store_u64(stream, &file->len) != 8) if (store_u64(stream, &self->len) != 8)
return 1; return 1;
if (haggis_store_cksum(&file->cksum, stream) != 0) if (haggis_store_cksum(&self->cksum, stream) != 0)
return 1; return 1;
int res = fwrite(file->data, 1, (size_t)file->len.val, stream); int res = fwrite(self->data, 1, (size_t)self->len.val, stream);
if (res != (size_t)file->len.val) if (res != (size_t)self->len.val)
return 1; return 1;
return 0; return 0;
} }
int haggis_load_file(FILE *stream, haggis_file *f) { int haggis_load_file(haggis_file *self, FILE *stream) {
if (load_u64(stream, &f->len) != 8) if (load_u64(stream, &self->len) != 8)
return 1; return 1;
if (haggis_load_cksum(&f->cksum, stream) != 0) if (haggis_load_cksum(&self->cksum, stream) != 0)
return 1; return 1;
f->data = calloc(1, (size_t)f->len.val); self->data = calloc(1, (size_t)self->len.val);
if (f->data == NULL) if (self->data == NULL)
return -1; return -1;
int res = fread(f->data, 1, (size_t)f->len.val, stream); int res = fread(self->data, 1, (size_t)self->len.val, stream);
if (res != (size_t)f->len.val) { if (res != (size_t)self->len.val) {
free(f->data); free(self->data);
return 1; return 1;
} }
if (haggis_validate_cksum(f)) { if (haggis_validate_cksum(self)) {
free(f->data); free(self->data);
return 1; return 1;
} }
return 0; return 0;
@ -415,7 +415,7 @@ int haggis_load_filetype(
switch (tag) { switch (tag) {
case normal: case normal:
file->tag = 0; file->tag = 0;
return haggis_load_file(stream, &file->file); return haggis_load_file(&file->file, stream);
case hardlink: case hardlink:
return haggis_load_filename(stream, &file->target); return haggis_load_filename(stream, &file->target);
file->tag = 1; file->tag = 1;
@ -449,7 +449,7 @@ int haggis_store_filetype(FILE *stream, haggis_filetype *filetype) {
flag = 0; flag = 0;
if (fwrite(&flag, 1, 1, stream) != 1) if (fwrite(&flag, 1, 1, stream) != 1)
return 1; return 1;
if (haggis_store_file(stream, &filetype->file) != 0) if (haggis_store_file(&filetype->file, stream) != 0)
return 1; return 1;
break; break;
case hardlink: case hardlink:
@ -573,7 +573,7 @@ int haggis_init_file_node(
} }
} }
node->filetype.tag = normal; node->filetype.tag = normal;
res = haggis_file_init(node->name.name, &node->filetype.file, a); res = haggis_file_init(&node->filetype.file, node->name.name, a);
if (res != 0) { if (res != 0) {
haggis_node_deinit(node); haggis_node_deinit(node);
return 1; return 1;

View file

@ -54,9 +54,9 @@ int validate_md5 (haggis_file *file);
int validate_sha1 (haggis_file *file); int validate_sha1 (haggis_file *file);
int validate_sha256 (haggis_file *file); int validate_sha256 (haggis_file *file);
int haggis_validate_cksum (haggis_file *file); int haggis_validate_cksum (haggis_file *file);
int haggis_file_init (char *path, haggis_file *hf, haggis_algorithm a); int haggis_file_init (haggis_file *self, char *path, haggis_algorithm a);
int haggis_store_file (FILE *stream, haggis_file *file); int haggis_store_file (haggis_file *self, FILE *stream);
int haggis_load_file (FILE *stream, haggis_file *f); int haggis_load_file (haggis_file *self, FILE *stream);
void haggis_filename_init (char *target, haggis_filename *fname); void haggis_filename_init (char *target, haggis_filename *fname);
void haggis_filename_deinit(haggis_filename *fname); void haggis_filename_deinit(haggis_filename *fname);
int haggis_load_filename (FILE *stream, haggis_filename *n); int haggis_load_filename (FILE *stream, haggis_filename *n);

View file

@ -8,7 +8,7 @@ int main() {
char *f = "check_header.c"; char *f = "check_header.c";
const u8 buf[16] = { 127, 161, 170, 134, 33, 218, 100, 29, 250, 255, 15, 125, 109, 5, 216, 9 }; const u8 buf[16] = { 127, 161, 170, 134, 33, 218, 100, 29, 250, 255, 15, 125, 109, 5, 216, 9 };
assert(haggis_file_init(f, &hf, md5) == 0); assert(haggis_file_init(&hf, f, md5) == 0);
assert(hf.cksum.tag == md5); assert(hf.cksum.tag == md5);
assert(memcmp(buf, hf.cksum.md5, 16) == 0); assert(memcmp(buf, hf.cksum.md5, 16) == 0);
} }

View file

@ -9,7 +9,7 @@ int main() {
const u8 buf[20] = { 175, 45, 7, 226, 140, 184, 187, 69, 59, 208, 246, 108, const u8 buf[20] = { 175, 45, 7, 226, 140, 184, 187, 69, 59, 208, 246, 108,
187, 249, 63, 111, 56, 65, 1, 58 }; 187, 249, 63, 111, 56, 65, 1, 58 };
assert(haggis_file_init(f, &hf, sha1) == 0); assert(haggis_file_init(&hf, f, sha1) == 0);
assert(hf.cksum.tag == sha1); assert(hf.cksum.tag == sha1);
assert(memcmp(buf, hf.cksum.sha1, 20) == 0); assert(memcmp(buf, hf.cksum.sha1, 20) == 0);
} }

View file

@ -10,7 +10,7 @@ int main() {
250, 203, 77, 108, 230, 7, 159, 124, 6, 237, 169, 239, 70, 218, 173, 188, 250, 203, 77, 108, 230, 7, 159, 124, 6, 237, 169, 239, 70, 218, 173, 188,
153, 219, 121, 82 }; 153, 219, 121, 82 };
assert(haggis_file_init(f, &hf, sha256) == 0); assert(haggis_file_init(&hf, f, sha256) == 0);
assert(hf.cksum.tag == sha256); assert(hf.cksum.tag == sha256);
assert(memcmp(buf, hf.cksum.sha256, 32) == 0); assert(memcmp(buf, hf.cksum.sha256, 32) == 0);
} }

View file

@ -10,7 +10,7 @@ int main() {
int res; int res;
fd = fopen(f, "r"); fd = fopen(f, "r");
res = haggis_load_file(fd, &hf); res = haggis_load_file(&hf, fd);
fclose(fd); fclose(fd);
return res; return res;
} }

View file

@ -10,7 +10,7 @@ int main() {
int res; int res;
fd = fopen(f, "r"); fd = fopen(f, "r");
res = haggis_load_file(fd, &hf); res = haggis_load_file(&hf, fd);
fclose(fd); fclose(fd);
return res; return res;
} }

View file

@ -10,7 +10,7 @@ int main() {
int res; int res;
fd = fopen(f, "r"); fd = fopen(f, "r");
res = haggis_load_file(fd, &hf); res = haggis_load_file(&hf, fd);
fclose(fd); fclose(fd);
return res; return res;
} }

View file

@ -8,9 +8,9 @@ int main() {
char *f = "load_device.c"; char *f = "load_device.c";
FILE *fd; FILE *fd;
assert(haggis_file_init(f, &hf, md5) == 0); assert(haggis_file_init(&hf, f, md5) == 0);
fd = fopen("output/store_file_md5", "w"); fd = fopen("output/store_file_md5", "w");
assert(haggis_store_file(fd, &hf) == 0); assert(haggis_store_file(&hf, fd) == 0);
fflush(fd); fflush(fd);
fclose(fd); fclose(fd);
return 0; return 0;

View file

@ -13,9 +13,9 @@ int main() {
char *f = "load_device.c"; char *f = "load_device.c";
FILE *fd; FILE *fd;
assert(haggis_file_init(f, &hf, sha1) == 0); assert(haggis_file_init(&hf, f, sha1) == 0);
fd = fopen("output/store_file_sha1", "w"); fd = fopen("output/store_file_sha1", "w");
assert(haggis_store_file(fd, &hf) == 0); assert(haggis_store_file(&hf, fd) == 0);
fflush(fd); fflush(fd);
fclose(fd); fclose(fd);
return 0; return 0;

View file

@ -13,9 +13,9 @@ int main() {
char *f = "load_device.c"; char *f = "load_device.c";
FILE *fd; FILE *fd;
assert(haggis_file_init(f, &hf, sha1) == 0); assert(haggis_file_init(&hf, f, sha1) == 0);
fd = fopen("output/store_file_sha256", "w"); fd = fopen("output/store_file_sha256", "w");
assert(haggis_store_file(fd, &hf) == 0); assert(haggis_store_file(&hf, fd) == 0);
fflush(fd); fflush(fd);
fclose(fd); fclose(fd);
return 0; return 0;