Skip to content

Commit

Permalink
Add --rm option
Browse files Browse the repository at this point in the history
This is an option to remove input files after successful compression or
decompression. This is silently ignored if output is stdout.
  • Loading branch information
sorairolake committed Jan 10, 2024
1 parent afc9d3c commit 384eda8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
4 changes: 4 additions & 0 deletions bzip3.1.in
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ Display a help message and exit.
.B \-j --jobs N
Set the amount of parallel worker threads that process one block each.
.TP
.B \--rm
Remove the input files after successful compression or decompression. This is
silently ignored if output is stdout.
.TP
.B \-k --keep
Keep (don't delete) the input files. Set by default, provided only
for compatibility with other compressors.
Expand Down
32 changes: 31 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -63,6 +64,7 @@ static void help() {
" -t, --test verify validity of compressed data\n"
" -h, --help display an usage overview\n"
" -f, --force force overwriting output if it already exists\n"
" --rm remove input files after successful (de)compression\n"
" -k, --keep keep (don't delete) input files (default)\n"
" -v, --verbose verbose mode (display more information)\n"
" -V, --version display version information\n"
Expand Down Expand Up @@ -144,6 +146,19 @@ static void close_out_file(FILE * des) {
}
}

static void remove_in_file(char * file_name, FILE * output_des) {
if (file_name == NULL) {
return;
}
if (output_des == stdout) {
return;
}
if (remove(file_name)) {
fprintf(stderr, "Error: failed to remove input file `%s': %s\n", file_name, strerror(errno));
exit(1);
}
}

static int process(FILE * input_des, FILE * output_des, int mode, int block_size, int workers, int verbose,
char * file_name) {
uint64_t bytes_read = 0, bytes_written = 0;
Expand Down Expand Up @@ -537,7 +552,7 @@ int main(int argc, char * argv[]) {
int force = 0;

// command line arguments
int force_stdstreams = 0, workers = 0, batch = 0, verbose = 0;
int force_stdstreams = 0, workers = 0, batch = 0, verbose = 0, remove_input_file = 0;

// the block size
u32 block_size = MiB(16);
Expand All @@ -548,13 +563,16 @@ int main(int argc, char * argv[]) {
const char * short_options = "Bb:cdefhkrtvVz";
#endif

enum { RM_OPTION = CHAR_MAX + 1 };

static struct option long_options[] = { { "encode", no_argument, 0, 'e' },
{ "decode", no_argument, 0, 'd' },
{ "test", no_argument, 0, 't' },
{ "stdout", no_argument, 0, 'c' },
{ "force", no_argument, 0, 'f' },
{ "recover", no_argument, 0, 'r' },
{ "help", no_argument, 0, 'h' },
{ "rm", no_argument, 0, RM_OPTION },
{ "keep", no_argument, 0, 'k' },
{ "version", no_argument, 0, 'V' },
{ "verbose", no_argument, 0, 'v' },
Expand Down Expand Up @@ -593,6 +611,9 @@ int main(int argc, char * argv[]) {
case 'f':
force = 1;
break;
case RM_OPTION:
remove_input_file = 1;
break;
case 'k':
break;
case 'h':
Expand Down Expand Up @@ -659,6 +680,9 @@ int main(int argc, char * argv[]) {
fclose(input_des);
close_out_file(output_des);
if (!force_stdstreams) free(output_name);
if (remove_input_file) {
remove_in_file(arg, output_des);
}
}
break;
case MODE_RECOVER:
Expand Down Expand Up @@ -688,6 +712,9 @@ int main(int argc, char * argv[]) {
fclose(input_des);
close_out_file(output_des);
if (!force_stdstreams) free(output_name);
if (remove_input_file) {
remove_in_file(arg, output_des);
}
}
break;
case MODE_TEST:
Expand Down Expand Up @@ -785,6 +812,9 @@ int main(int argc, char * argv[]) {
fprintf(stderr, "Error: Failed on fclose(stdout): %s\n", strerror(errno));
return 1;
}
if (remove_input_file) {
remove_in_file(input, output_des);
}

return r;
}

0 comments on commit 384eda8

Please sign in to comment.