diff options
| author | B. Watson <urchlay@slackware.uk> | 2025-11-26 16:49:33 -0500 |
|---|---|---|
| committer | B. Watson <urchlay@slackware.uk> | 2025-11-26 16:49:33 -0500 |
| commit | 1dc10dd269d180cb33ad31dce597170e5090d502 (patch) | |
| tree | 1f22f90438a9a370d6782755ab3d99402ca7a04e | |
| parent | 0199d360d393c6b9cd58d53dcae9c10d6901d47a (diff) | |
| download | unalf-1dc10dd269d180cb33ad31dce597170e5090d502.tar.gz | |
alf: Don't create output file until the first input file is successfully crunched (avoids 0-byte turds in case of error).
| -rw-r--r-- | src/alf.c | 35 |
1 files changed, 19 insertions, 16 deletions
@@ -256,6 +256,13 @@ void crunch(void) { update_header(); } +void make_backup(void) { + char bak[PATH_MAX + 2]; + strncpy(bak, out_filename, PATH_MAX); + strcat(bak, "~"); + rename(out_filename, bak); +} + void crunch_file(const char *filename) { init_table(); @@ -284,14 +291,19 @@ void crunch_file(const char *filename) { /* crunches the entire input to memory! */ crunch(); - fwrite(output_buf, 1, output_len, out_file); -} + /* don't open the output file until crunch() has succeeded once. + this avoids leaving 0-byte turds */ + if(!out_file) { + if(!opt_overwrite) make_backup(); + out_file = fopen(out_filename, opt_append ? "ab" : "wb"); + if(!out_file) { + fprintf(stderr, "%s: fatal: ", self); + perror(out_filename); + exit(1); + } + } -void make_backup(void) { - char bak[PATH_MAX + 2]; - strncpy(bak, out_filename, PATH_MAX); - strcat(bak, "~"); - rename(out_filename, bak); + fwrite(output_buf, 1, output_len, out_file); } void usage(void) { @@ -339,15 +351,6 @@ int main(int argc, char **argv) { exit(1); } - if(!opt_overwrite) make_backup(); - - out_file = fopen(out_filename, opt_append ? "ab" : "wb"); - if(!out_file) { - fprintf(stderr, "%s: fatal: ", self); - perror(out_filename); - exit(1); - } - while(optind < argc) { crunch_file(argv[optind++]); } |
