From: Jonathan Dieter Date: Thu, 3 May 2018 19:52:11 +0000 (+0300) Subject: Make zck a proper utility with arguments and help. Still needs some work X-Git-Tag: archive/raspbian/1.1.9+ds1-1+rpi1~1^2~292 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=75ac5c3b29cb01564a53f13030f6a9fe05ccf1e0;p=zchunk.git Make zck a proper utility with arguments and help. Still needs some work on splitting strings. Signed-off-by: Jonathan Dieter --- diff --git a/src/zck.c b/src/zck.c index 3d2a7f0..19fed17 100644 --- a/src/zck.c +++ b/src/zck.c @@ -33,27 +33,105 @@ #include #include #include +#include #include "buzhash/buzhash.h" #include "memmem.h" +static char doc[] = "zck - Create a new zchunk file"; + +static char args_doc[] = ""; + +void version() { + printf(ZCK_NAME " " ZCK_VERSION "\nCopyright (c) 2018 Jonathan Dieter\n"); + exit(0); +} + +static struct argp_option options[] = { + {"verbose", 'v', 0, 0, + "Increase verbosity (can be specified more than once for debugging)"}, + {"quiet", 'q', 0, 0, "Only show errors"}, + {"split", 's', "STRING", 0, "Split chunks at beginning of STRING"}, + {"dict", 'D', "FILE", 0, "Set zstd compression dictionary to FILE"}, + {"version", 'V', 0, 0, "Show program version"}, + { 0 } +}; + +struct arguments { + char *args[1]; + zck_log_type log_level; + char *split_string; + char *dict; +}; + +static error_t parse_opt (int key, char *arg, struct argp_state *state) { + struct arguments *arguments = state->input; + + switch (key) { + case 'v': + arguments->log_level -= 1; + if(arguments->log_level < ZCK_LOG_DEBUG) + arguments->log_level = ZCK_LOG_DEBUG; + break; + case 'q': + arguments->log_level = ZCK_LOG_ERROR; + break; + case 's': + arguments->split_string = arg; + break; + case 'D': + arguments->dict = arg; + break; + case 'V': + version(); + break; + + case ARGP_KEY_ARG: + if (state->arg_num >= 1) { + argp_usage (state); + return EINVAL; + } + arguments->args[state->arg_num] = arg; + + break; + + case ARGP_KEY_END: + if (state->arg_num < 1) { + argp_usage (state); + return EINVAL; + } + break; + + default: + return ARGP_ERR_UNKNOWN; + } + return 0; +} + +static struct argp argp = {options, parse_opt, args_doc, doc}; + int main (int argc, char *argv[]) { + struct arguments arguments = {0}; + + /* Defaults */ + arguments.log_level = ZCK_LOG_WARNING; + + argp_parse (&argp, argc, argv, 0, 0, &arguments); + + zck_set_log_level(arguments.log_level); + char *out_name; + out_name = malloc(strlen(arguments.args[0]) + 5); + snprintf(out_name, strlen(arguments.args[0]) + 5, "%s.zck", + arguments.args[0]); + + /* Set dictionary if available */ char *dict = NULL; size_t dict_size = 0; - - zck_set_log_level(ZCK_LOG_DEBUG); - - if(argc < 3 || argc > 4) { - printf("Usage: %s [optional dictionary]\n", argv[0]); - exit(1); - } - out_name = malloc(strlen(argv[1]) + 5); - snprintf(out_name, strlen(argv[1]) + 5, "%s.zck", argv[1]); - if(argc == 4) { - int dict_fd = open(argv[3], O_RDONLY); + if(arguments.dict != NULL) { + int dict_fd = open(arguments.dict, O_RDONLY); if(dict_fd < 0) { - printf("Unable to open dictionary %s for reading", argv[3]); + printf("Unable to open dictionary %s for reading", arguments.dict); perror(""); exit(1); } @@ -75,7 +153,7 @@ int main (int argc, char *argv[]) { close(dict_fd); } - int dst_fd = open(out_name, O_EXCL | O_WRONLY | O_CREAT, 0644); + int dst_fd = open(out_name, O_TRUNC | O_WRONLY | O_CREAT, 0644); if(dst_fd < 0) { printf("Unable to open %s", out_name); perror(""); @@ -107,8 +185,8 @@ int main (int argc, char *argv[]) { free(dict); char *data; - int in_fd = open(argv[1], O_RDONLY); - int in_size = 0; + int in_fd = open(arguments.args[0], O_RDONLY); + off_t in_size = 0; if(in_fd < 0) { printf("Unable to open %s for reading", argv[1]); perror(""); @@ -131,8 +209,7 @@ int main (int argc, char *argv[]) { } close(in_fd); - /* Chunk based on string in argv[2] (Currently with ugly hack to group srpms together) */ - if(False) { + if(arguments.split_string) { char *found = data; char *search = found; char *prev_srpm = memmem(search, in_size - (search-data), "count); + } + zck_free(&zck); close(dst_fd); }