From c93b0bdb25b3cda223132f5b4ab07593ce83e17d Mon Sep 17 00:00:00 2001 From: Matteo Paonessa Date: Mon, 13 Mar 2017 10:13:03 +0100 Subject: [PATCH] Exit on error --- INSTALL | 2 +- README.md | 2 +- src/error.c | 6 ++++++ src/helper.c | 7 ++++--- src/main.c | 12 ++++++++---- src/utils.c | 5 +---- 6 files changed, 21 insertions(+), 13 deletions(-) diff --git a/INSTALL b/INSTALL index 98c647c..4fe1b38 100644 --- a/INSTALL +++ b/INSTALL @@ -14,7 +14,7 @@ Unpack the archive (if not cloned from GIT), enter the directory and compile the program. The sequence should be something like: - $ tar xfv caesium-clt-*.tar.gz + $ tar xfv caesiumclt-*.tar.gz $ cd caesium-clt-* $ cmake . $ make diff --git a/README.md b/README.md index 0c72c7f..ac63ed5 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ $ caesiumclt -q 0 -R -o ~/output/ ~/Pictures Losslessly compress ```Pictures``` folder and subfolders, located in the ```home``` directory, into a folder called ```output``` retaining the input folder structure ``` -$ caesiumclt -q 0 -RS-o ~/output/ ~/Pictures +$ caesiumclt -q 0 -RS -o ~/output/ ~/Pictures ``` ---------- diff --git a/src/error.c b/src/error.c index 68348d0..cc1b898 100644 --- a/src/error.c +++ b/src/error.c @@ -1,5 +1,6 @@ #include #include +#include #include "error.h" @@ -10,6 +11,9 @@ void display_error(error_level level, int code) error_level, code, get_error_message(code)); + if (level == ERROR) { + exit(-code); + } } const char *get_error_message(int code) @@ -38,6 +42,8 @@ const char *get_error_message(int code) return "-R is useless on files."; case 11: return "-S is useless without -R."; + case 12: + return "Cannot set output folder inside the input one"; default: return "Unrecognized error."; diff --git a/src/helper.c b/src/helper.c index 7c48495..9bb1b6e 100644 --- a/src/helper.c +++ b/src/helper.c @@ -33,7 +33,6 @@ cclt_options parse_arguments(char **argv, cs_image_pars *options) options->jpeg.quality = atoi(opts.optarg); if (options->jpeg.quality < 0 || options->jpeg.quality > 100) { display_error(ERROR, 1); - exit(EXIT_FAILURE); } break; case 'e': @@ -64,7 +63,6 @@ cclt_options parse_arguments(char **argv, cs_image_pars *options) default: fprintf(stderr, "%s: %s\n", argv[0], opts.errmsg); display_error(ERROR, 2); - exit(EXIT_FAILURE); } } @@ -100,6 +98,10 @@ cclt_options parse_arguments(char **argv, cs_image_pars *options) } //If there're files and folders, we cannot keep the structure + if (strstr(parameters.output_folder, parameters.input_folder) != NULL) { + display_error(ERROR, 12); + } + if (parameters.recursive && !folders_flag) { display_error(WARNING, 10); parameters.recursive = false; @@ -125,7 +127,6 @@ int start_compression(cclt_options *options, cs_image_pars *parameters) //Create the output folder if does not exists if (mkpath(options->output_folder, 0777) == -1) { display_error(ERROR, 5); - exit(EXIT_FAILURE); } for (int i = 0; i < options->files_count; i++) { diff --git a/src/main.c b/src/main.c index 46e3e92..eb3a242 100755 --- a/src/main.c +++ b/src/main.c @@ -30,11 +30,15 @@ int main(int argc, char *argv[]) diff = clock() - start; compression_time = diff * 1000 / CLOCKS_PER_SEC; + //Output the compression results - fprintf(stdout, "-------------------------------\n" - "Compression completed in " - "%lum%lus%lums\n%s -> %s [%.2f%% | %s]\n", - compression_time / 1000 / 60, compression_time / 1000 % 60, compression_time % 1000, + fprintf(stdout, "-------------------------------\n"); + compression_time / 1000 % 60 >= 1 ? + fprintf(stdout, "Compression completed in %lum%lus\n", + compression_time / 1000 / 60, compression_time / 1000 % 60) : + fprintf(stdout, "Compression completed in %lum%lus%lums\n", + compression_time / 1000 / 60, compression_time / 1000 % 60, compression_time % 1000); + fprintf(stdout, "%s -> %s [%.2f%% | %s]\n", get_human_size(options.input_total_size), get_human_size(options.output_total_size), ((float) options.output_total_size - options.input_total_size) * 100 / options.input_total_size, get_human_size((options.output_total_size - options.input_total_size))); diff --git a/src/utils.c b/src/utils.c index e3c40d2..29d7c87 100644 --- a/src/utils.c +++ b/src/utils.c @@ -21,8 +21,7 @@ void print_help() "\t-e, --exif\t\tkeeps EXIF info during compression\n" "\t-o, --output\t\toutput folder\n" "\t-R, --recursive\t\tif input is a folder, scan subfolders too\n" - //TODO Remove this warning - "\t-S, --keep-structure\tkeep the folder structure [Not active yet], use with -R\n" + "\t-S, --keep-structure\tkeep the folder structure, use with -R\n" "\t-h, --help\t\tdisplay this help and exit\n" "\t-v, --version\t\toutput version information and exit\n\n"); exit(EXIT_SUCCESS); @@ -34,7 +33,6 @@ bool is_directory(const char *path) if (tinydir_file_open(&file, path) == -1) { display_error(ERROR, 6); - exit(EXIT_FAILURE); } return (bool) file.is_dir; @@ -118,7 +116,6 @@ off_t get_file_size(const char *path) if (tinydir_file_open(&file, path) == -1) { display_error(ERROR, 7); - exit(EXIT_FAILURE); } return file._s.st_size;