diff options
author | Joris Vink <joris@cvs.openbsd.org> | 2006-03-08 20:19:40 +0000 |
---|---|---|
committer | Joris Vink <joris@cvs.openbsd.org> | 2006-03-08 20:19:40 +0000 |
commit | 93f41b22c88cc93558ab60707cd82891a30afe32 (patch) | |
tree | a6e03d2a0a59503f55941547182b3b286ad46497 /usr.bin | |
parent | 14f25c918fb2a9d31887f4a36846cfb225576c38 (diff) |
make openrcs use the worklist framework to keep track of temporary files
and remove them in case it gets interrupted.
suggested by deraadt@, ok niallo@
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/cvs/Makefile | 4 | ||||
-rw-r--r-- | usr.bin/rcs/Makefile | 4 | ||||
-rw-r--r-- | usr.bin/rcs/ci.c | 47 | ||||
-rw-r--r-- | usr.bin/rcs/rcsdiff.c | 95 | ||||
-rw-r--r-- | usr.bin/rcs/rcsprog.c | 20 | ||||
-rw-r--r-- | usr.bin/rcs/rcsprog.h | 4 |
6 files changed, 113 insertions, 61 deletions
diff --git a/usr.bin/cvs/Makefile b/usr.bin/cvs/Makefile index e5108024a89..28a75b7d6fa 100644 --- a/usr.bin/cvs/Makefile +++ b/usr.bin/cvs/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.18 2006/01/03 17:04:53 xsa Exp $ +# $OpenBSD: Makefile,v 1.19 2006/03/08 20:19:39 joris Exp $ PROG= cvs MAN= cvs.1 cvsignore.5 cvsrc.5 cvswrappers.5 cvsintro.7 @@ -8,7 +8,7 @@ SRCS= cvs.c add.c admin.c annotate.c buf.c checkout.c cmd.c commit.c \ compress.c date.y diff.c diff3.c edit.c entries.c fatal.c file.c \ getlog.c history.c hist.c import.c init.c log.c logmsg.c proto.c \ rcs.c rcsnum.c release.c remove.c req.c resp.c root.c server.c \ - status.c tag.c update.c util.c version.c watch.c xmalloc.c + status.c tag.c update.c util.c version.c watch.c worklist.c xmalloc.c CFLAGS= -g -ggdb CFLAGS+= -Wall diff --git a/usr.bin/rcs/Makefile b/usr.bin/rcs/Makefile index 4116d2b3f96..8882f7e0c01 100644 --- a/usr.bin/rcs/Makefile +++ b/usr.bin/rcs/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.27 2006/03/07 11:19:08 jmc Exp $ +# $OpenBSD: Makefile,v 1.28 2006/03/08 20:19:39 joris Exp $ .PATH: ${.CURDIR}/../cvs @@ -7,7 +7,7 @@ MAN= ci.1 co.1 ident.1 rcs.1 rcsclean.1 rcsdiff.1 rcsmerge.1 rlog.1 SRCS= ci.c co.c ident.c rcsclean.c rcsdiff.c rcsmerge.c rcsprog.c rlog.c \ buf.c date.y diff.c diff3.c fatal.c log.c rcs.c rcsnum.c util.c \ - xmalloc.c + worklist.c xmalloc.c CPPFLAGS+=-I${.CURDIR}/../cvs -DRCSPROG diff --git a/usr.bin/rcs/ci.c b/usr.bin/rcs/ci.c index 2225a0cbf6e..e6894808a54 100644 --- a/usr.bin/rcs/ci.c +++ b/usr.bin/rcs/ci.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ci.c,v 1.111 2006/03/07 01:47:42 joris Exp $ */ +/* $OpenBSD: ci.c,v 1.112 2006/03/08 20:19:39 joris Exp $ */ /* * Copyright (c) 2005, 2006 Niall O'Higgins <niallo@openbsd.org> * All rights reserved. @@ -292,52 +292,59 @@ checkin_diff_file(struct checkin_params *pb) BUF *b1, *b2, *b3; char rbuf[64], *deltatext; + b1 = b2 = b3 = NULL; + deltatext = NULL; rcsnum_tostr(pb->frev, rbuf, sizeof(rbuf)); if ((b1 = cvs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL) { cvs_log(LP_ERR, "failed to load file: '%s'", pb->filename); - return (NULL); + goto out; } if ((b2 = rcs_getrev(pb->file, pb->frev)) == NULL) { cvs_log(LP_ERR, "failed to load revision"); - cvs_buf_free(b1); - return (NULL); + goto out; } if ((b3 = cvs_buf_alloc((size_t)128, BUF_AUTOEXT)) == NULL) { cvs_log(LP_ERR, "failed to allocated buffer for diff"); - cvs_buf_free(b1); - cvs_buf_free(b2); - return (NULL); + goto out; } strlcpy(path1, rcs_tmpdir, sizeof(path1)); strlcat(path1, "/diff1.XXXXXXXXXX", sizeof(path1)); - if (cvs_buf_write_stmp(b1, path1, 0600) == -1) { - cvs_log(LP_ERRNO, "could not write temporary file"); - cvs_buf_free(b1); - cvs_buf_free(b2); - return (NULL); - } + if (cvs_buf_write_stmp(b1, path1, 0600) == -1) + goto out; + + cvs_worklist_add(path1, &rcs_temp_files); cvs_buf_free(b1); + b1 = NULL; strlcpy(path2, rcs_tmpdir, sizeof(path2)); strlcat(path2, "/diff2.XXXXXXXXXX", sizeof(path2)); - if (cvs_buf_write_stmp(b2, path2, 0600) == -1) { - cvs_buf_free(b2); - (void)unlink(path1); - return (NULL); - } + if (cvs_buf_write_stmp(b2, path2, 0600) == -1) + goto out; + + cvs_worklist_add(path2, &rcs_temp_files); cvs_buf_free(b2); + b2 = NULL; diff_format = D_RCSDIFF; cvs_diffreg(path1, path2, b3); - (void)unlink(path1); - (void)unlink(path2); cvs_buf_putc(b3, '\0'); deltatext = (char *)cvs_buf_release(b3); + b3 = NULL; + +out: + cvs_worklist_run(&rcs_temp_files, cvs_worklist_unlink); + + if (b1 != NULL) + cvs_buf_free(b1); + if (b2 != NULL) + cvs_buf_free(b2); + if (b3 != NULL) + cvs_buf_free(b3); return (deltatext); } diff --git a/usr.bin/rcs/rcsdiff.c b/usr.bin/rcs/rcsdiff.c index b4c157315b2..310fd6b6f4c 100644 --- a/usr.bin/rcs/rcsdiff.c +++ b/usr.bin/rcs/rcsdiff.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rcsdiff.c,v 1.36 2006/03/07 01:40:52 joris Exp $ */ +/* $OpenBSD: rcsdiff.c,v 1.37 2006/03/08 20:19:39 joris Exp $ */ /* * Copyright (c) 2005 Joris Vink <joris@openbsd.org> * All rights reserved. @@ -165,6 +165,7 @@ rcsdiff_usage(void) static int rcsdiff_file(RCSFILE *file, RCSNUM *rev, const char *filename) { + int ret; char path1[MAXPATHLEN], path2[MAXPATHLEN]; BUF *b1, *b2; char rbuf[64]; @@ -173,9 +174,12 @@ rcsdiff_file(RCSFILE *file, RCSNUM *rev, const char *filename) memset(&tv, 0, sizeof(tv)); memset(&tv2, 0, sizeof(tv2)); + ret = -1; + b1 = b2 = NULL; + if (stat(filename, &st) == -1) { cvs_log(LP_ERRNO, "%s", filename); - return (-1); + goto out; } rcsnum_tostr(rev, rbuf, sizeof(rbuf)); @@ -186,58 +190,70 @@ rcsdiff_file(RCSFILE *file, RCSNUM *rev, const char *filename) if ((b1 = rcs_getrev(file, rev)) == NULL) { cvs_log(LP_ERR, "failed to retrieve revision %s", rbuf); - return (-1); + goto out; } + b1 = rcs_kwexp_buf(b1, file, rev); tv[0].tv_sec = (long)rcs_rev_getdate(file, rev); tv[1].tv_sec = tv[0].tv_sec; if ((b2 = cvs_buf_load(filename, BUF_AUTOEXT)) == NULL) { cvs_log(LP_ERR, "failed to load file: '%s'", filename); - cvs_buf_free(b1); - return (-1); + goto out; } + tv2[0].tv_sec = st.st_mtime; tv2[1].tv_sec = st.st_mtime; strlcpy(path1, rcs_tmpdir, sizeof(path1)); strlcat(path1, "/diff1.XXXXXXXXXX", sizeof(path1)); - if (cvs_buf_write_stmp(b1, path1, 0600) == -1) { - cvs_log(LP_ERRNO, "could not write temporary file"); - cvs_buf_free(b1); - cvs_buf_free(b2); - return (-1); - } + if (cvs_buf_write_stmp(b1, path1, 0600) == -1) + goto out; + + cvs_worklist_add(path1, &rcs_temp_files); cvs_buf_free(b1); + b1 = NULL; + if (utimes(path1, (const struct timeval *)&tv) < 0) cvs_log(LP_ERRNO, "error setting utimes"); strlcpy(path2, rcs_tmpdir, sizeof(path2)); strlcat(path2, "/diff2.XXXXXXXXXX", sizeof(path2)); - if (cvs_buf_write_stmp(b2, path2, 0600) == -1) { - cvs_buf_free(b2); - (void)unlink(path1); - return (-1); - } + if (cvs_buf_write_stmp(b2, path2, 0600) == -1) + goto out; + + cvs_worklist_add(path2, &rcs_temp_files); cvs_buf_free(b2); + b2 = NULL; + if (utimes(path2, (const struct timeval *)&tv2) < 0) cvs_log(LP_ERRNO, "error setting utimes"); cvs_diffreg(path1, path2, NULL); - (void)unlink(path1); - (void)unlink(path2); + ret = 0; + +out: + cvs_worklist_run(&rcs_temp_files, cvs_worklist_unlink); - return (0); + if (b1 != NULL) + cvs_buf_free(b1); + if (b2 != NULL) + cvs_buf_free(b2); + + return (ret); } static int rcsdiff_rev(RCSFILE *file, RCSNUM *rev1, RCSNUM *rev2, const char *filename) { + int ret; char path1[MAXPATHLEN], path2[MAXPATHLEN]; BUF *b1, *b2; char rbuf1[64], rbuf2[64]; struct timeval tv[2], tv2[2]; + ret = -1; + b1 = b2 = NULL; memset(&tv, 0, sizeof(tv)); memset(&tv2, 0, sizeof(tv2)); @@ -247,8 +263,9 @@ rcsdiff_rev(RCSFILE *file, RCSNUM *rev1, RCSNUM *rev2, const char *filename) if ((b1 = rcs_getrev(file, rev1)) == NULL) { cvs_log(LP_ERR, "failed to retrieve revision %s", rbuf1); - return (-1); + goto out; } + b1 = rcs_kwexp_buf(b1, file, rev1); tv[0].tv_sec = (long)rcs_rev_getdate(file, rev1); tv[1].tv_sec = tv[0].tv_sec; @@ -259,8 +276,9 @@ rcsdiff_rev(RCSFILE *file, RCSNUM *rev1, RCSNUM *rev2, const char *filename) if ((b2 = rcs_getrev(file, rev2)) == NULL) { cvs_log(LP_ERR, "failed to retrieve revision %s", rbuf2); - return (-1); + goto out; } + b2 = rcs_kwexp_buf(b2, file, rev2); tv2[0].tv_sec = (long)rcs_rev_getdate(file, rev2); tv2[1].tv_sec = tv2[0].tv_sec; @@ -270,31 +288,38 @@ rcsdiff_rev(RCSFILE *file, RCSNUM *rev1, RCSNUM *rev2, const char *filename) strlcpy(path1, rcs_tmpdir, sizeof(path1)); strlcat(path1, "/diff1.XXXXXXXXXX", sizeof(path1)); - if (cvs_buf_write_stmp(b1, path1, 0600) == -1) { - cvs_log(LP_ERRNO, "could not write temporary file"); - cvs_buf_free(b1); - cvs_buf_free(b2); - return (-1); - } + if (cvs_buf_write_stmp(b1, path1, 0600) == -1) + goto out; + + cvs_worklist_add(path1, &rcs_temp_files); cvs_buf_free(b1); + b1 = NULL; + if (utimes(path1, (const struct timeval *)&tv) < 0) cvs_log(LP_ERRNO, "error setting utimes"); strlcpy(path2, rcs_tmpdir, sizeof(path2)); strlcat(path2, "/diff2.XXXXXXXXXX", sizeof(path2)); - if (cvs_buf_write_stmp(b2, path2, 0600) == -1) { - cvs_buf_free(b2); - (void)unlink(path1); - return (-1); - } + if (cvs_buf_write_stmp(b2, path2, 0600) == -1) + goto out; + + cvs_worklist_add(path2, &rcs_temp_files); cvs_buf_free(b2); + b2 = NULL; if (utimes(path2, (const struct timeval *)&tv2) < 0) cvs_log(LP_ERRNO, "error setting utimes"); cvs_diffreg(path1, path2, NULL); - (void)unlink(path1); - (void)unlink(path2); + ret = 0; + +out: + cvs_worklist_run(&rcs_temp_files, cvs_worklist_unlink); + + if (b1 != NULL) + cvs_buf_free(b1); + if (b2 != NULL) + cvs_buf_free(b2); - return (0); + return (ret); } diff --git a/usr.bin/rcs/rcsprog.c b/usr.bin/rcs/rcsprog.c index 26e78a78ef8..1e6d9c7ed2b 100644 --- a/usr.bin/rcs/rcsprog.c +++ b/usr.bin/rcs/rcsprog.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rcsprog.c,v 1.67 2006/03/08 12:25:34 xsa Exp $ */ +/* $OpenBSD: rcsprog.c,v 1.68 2006/03/08 20:19:39 joris Exp $ */ /* * Copyright (c) 2005 Jean-Francois Brousseau <jfb@openbsd.org> * All rights reserved. @@ -64,10 +64,20 @@ struct rcs_prog { { "ident", ident_main, ident_usage }, }; +struct cvs_wklhead rcs_temp_files; + +void sighdlr(int); static void rcs_set_description(RCSFILE *, const char *); static void rcs_attach_symbol(RCSFILE *, const char *); void +sighdlr(int sig) +{ + cvs_worklist_clean(&rcs_temp_files, cvs_worklist_unlink); + _exit(1); +} + +void rcs_set_rev(const char *str, RCSNUM **rev) { RCSNUM *t; @@ -330,6 +340,7 @@ main(int argc, char **argv) ret = -1; rcs_optind = 1; cvs_log_init(LD_STD, 0); + SLIST_INIT(&rcs_temp_files); cmd_argc = 0; cmd_argv[cmd_argc++] = argv[0]; @@ -350,6 +361,13 @@ main(int argc, char **argv) for (ret = 1; ret < argc; ret++) cmd_argv[cmd_argc++] = argv[ret]; + signal(SIGHUP, sighdlr); + signal(SIGINT, sighdlr); + signal(SIGQUIT, sighdlr); + signal(SIGABRT, sighdlr); + signal(SIGALRM, sighdlr); + signal(SIGTERM, sighdlr); + for (i = 0; i < (sizeof(programs)/sizeof(programs[0])); i++) if (strcmp(__progname, programs[i].prog_name) == 0) { usage = programs[i].prog_usage; diff --git a/usr.bin/rcs/rcsprog.h b/usr.bin/rcs/rcsprog.h index 3147bdc6822..67da6bfa377 100644 --- a/usr.bin/rcs/rcsprog.h +++ b/usr.bin/rcs/rcsprog.h @@ -1,4 +1,4 @@ -/* $OpenBSD: rcsprog.h,v 1.35 2006/02/16 17:44:53 niallo Exp $ */ +/* $OpenBSD: rcsprog.h,v 1.36 2006/03/08 20:19:39 joris Exp $ */ /* * Copyright (c) 2005 Joris Vink <joris@openbsd.org> * All rights reserved. @@ -29,6 +29,7 @@ #include "log.h" #include "rcs.h" +#include "worklist.h" #include "xmalloc.h" #define RCS_TMPDIR_DEFAULT "/tmp" @@ -62,6 +63,7 @@ extern int rcs_optind; extern char *rcs_optarg; extern char *rcs_suffixes; extern char *rcs_tmpdir; +extern struct cvs_wklhead rcs_temp_files; /* date.y */ time_t cvs_date_parse(const char *); |