feat: handle Ctrl+C gracefully, cleaning up partial output
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
#include "fpk.h"
|
||||
#include <dirent.h>
|
||||
#include <getopt.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -100,6 +101,13 @@ static void format_duration(double secs, char *buf, size_t bufsize)
|
||||
static unsigned char flags;
|
||||
static uint64_t entry_count;
|
||||
static uint64_t total_bytes;
|
||||
static volatile sig_atomic_t interrupted;
|
||||
|
||||
static void handle_sigint(int sig)
|
||||
{
|
||||
(void)sig;
|
||||
interrupted = 1;
|
||||
}
|
||||
|
||||
static struct option long_opts[] = {
|
||||
{"output", required_argument, NULL, 'o'},
|
||||
@@ -229,6 +237,9 @@ static int add_file(struct fpk_writer *w, const char *path, size_t base_len)
|
||||
size_t size;
|
||||
char *norm;
|
||||
|
||||
if (interrupted)
|
||||
return 0;
|
||||
|
||||
while (*rel_path == '/')
|
||||
rel_path++;
|
||||
|
||||
@@ -439,8 +450,16 @@ int main(int argc, char **argv)
|
||||
return 1;
|
||||
}
|
||||
|
||||
signal(SIGINT, handle_sigint);
|
||||
|
||||
if (!add_dir(w, input_dir, base_len)) {
|
||||
fpk_writer_close(w);
|
||||
if (interrupted) {
|
||||
remove(output);
|
||||
fprintf(stderr, "\nfpack: interrupted, removed incomplete archive: %s\n", output);
|
||||
free(output);
|
||||
return 130;
|
||||
}
|
||||
free(output);
|
||||
return 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user