Add a signal handler for SIGINT which removes output file when necessary.

While I'm there, move unlink_input() slightly down to after closing the
output file, in uncompression path.

MFC after:	2 weeks
This commit is contained in:
Xin LI 2010-04-26 20:05:48 +00:00
parent e2f198273c
commit 18333f544e
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=207247

View File

@ -43,7 +43,6 @@ __RCSID("$FreeBSD$");
* *
* TODO: * TODO:
* - use mmap where possible * - use mmap where possible
* - handle some signals better (remove outfile?)
* - make bzip2/compress -v/-t/-l support work as well as possible * - make bzip2/compress -v/-t/-l support work as well as possible
*/ */
@ -194,6 +193,7 @@ static int qflag; /* quiet mode */
static int rflag; /* recursive mode */ static int rflag; /* recursive mode */
static int tflag; /* test */ static int tflag; /* test */
static int vflag; /* verbose mode */ static int vflag; /* verbose mode */
static const char *remove_file = NULL; /* file to be removed upon SIGINT */
#else #else
#define qflag 0 #define qflag 0
#define tflag 0 #define tflag 0
@ -231,6 +231,7 @@ static void usage(void);
static void display_version(void); static void display_version(void);
#ifndef SMALL #ifndef SMALL
static void display_license(void); static void display_license(void);
static void sigint_handler(int);
#endif #endif
static const suffixes_t *check_suffix(char *, int); static const suffixes_t *check_suffix(char *, int);
static ssize_t read_retry(int, void *, size_t); static ssize_t read_retry(int, void *, size_t);
@ -300,11 +301,10 @@ main(int argc, char **argv)
#endif #endif
int ch; int ch;
/* XXX set up signals */
#ifndef SMALL #ifndef SMALL
if ((gzip = getenv("GZIP")) != NULL) if ((gzip = getenv("GZIP")) != NULL)
prepend_gzip(gzip, &argc, &argv); prepend_gzip(gzip, &argc, &argv);
signal(SIGINT, sigint_handler);
#endif #endif
/* /*
@ -1171,6 +1171,15 @@ unlink_input(const char *file, const struct stat *sb)
return; return;
unlink(file); unlink(file);
} }
static void
sigint_handler(int signo __unused)
{
if (remove_file != NULL)
unlink(remove_file);
exit(2);
}
#endif #endif
static const suffixes_t * static const suffixes_t *
@ -1257,6 +1266,9 @@ file_compress(char *file, char *outfile, size_t outsize)
fclose(stdin); fclose(stdin);
return -1; return -1;
} }
#ifndef SMALL
remove_file = outfile;
#endif
} else } else
out = STDOUT_FILENO; out = STDOUT_FILENO;
@ -1288,6 +1300,7 @@ file_compress(char *file, char *outfile, size_t outsize)
} }
copymodes(out, &isb, outfile); copymodes(out, &isb, outfile);
remove_file = NULL;
#endif #endif
if (close(out) == -1) if (close(out) == -1)
maybe_warn("couldn't close output"); maybe_warn("couldn't close output");
@ -1424,6 +1437,9 @@ file_uncompress(char *file, char *outfile, size_t outsize)
maybe_warn("can't open %s", outfile); maybe_warn("can't open %s", outfile);
goto lose; goto lose;
} }
#ifndef SMALL
remove_file = outfile;
#endif
} else } else
zfd = STDOUT_FILENO; zfd = STDOUT_FILENO;
@ -1555,11 +1571,12 @@ file_uncompress(char *file, char *outfile, size_t outsize)
unlink(outfile); unlink(outfile);
return -1; return -1;
} }
unlink_input(file, &isb);
#ifndef SMALL #ifndef SMALL
copymodes(ofd, &isb, outfile); copymodes(ofd, &isb, outfile);
remove_file = NULL;
#endif #endif
close(ofd); close(ofd);
unlink_input(file, &isb);
return size; return size;
unexpected_EOF: unexpected_EOF: