summaryrefslogtreecommitdiffstats
path: root/usr.bin
diff options
context:
space:
mode:
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/cvs/buf.c82
-rw-r--r--usr.bin/cvs/buf.h30
-rw-r--r--usr.bin/cvs/diff.c17
3 files changed, 95 insertions, 34 deletions
diff --git a/usr.bin/cvs/buf.c b/usr.bin/cvs/buf.c
index d9ae11b87f4..49a6443626f 100644
--- a/usr.bin/cvs/buf.c
+++ b/usr.bin/cvs/buf.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: buf.c,v 1.6 2004/12/07 17:10:56 tedu Exp $ */
+/* $OpenBSD: buf.c,v 1.7 2004/12/08 21:11:07 djm Exp $ */
/*
* Copyright (c) 2003 Jean-Francois Brousseau <jfb@openbsd.org>
* All rights reserved.
@@ -34,6 +34,7 @@
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
+#include <errno.h>
#include "buf.h"
#include "log.h"
@@ -370,34 +371,25 @@ cvs_buf_peek(BUF *b, size_t off)
/*
- * cvs_buf_write()
+ * cvs_buf_write_fd()
*
- * Write the contents of the buffer <b> to the file whose path is given in
- * <path>. If the file does not exist, it is created with mode <mode>.
+ * Write the contents of the buffer <b> to the specified <fd>
*/
int
-cvs_buf_write(BUF *b, const char *path, mode_t mode)
+cvs_buf_write_fd(BUF *b, int fd)
{
- int fd;
u_char *bp;
size_t len;
ssize_t ret;
- fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, mode);
- if (fd == -1) {
- cvs_log(LP_ERRNO, "failed to open file `%s'", path);
- return (-1);
- }
-
len = b->cb_len;
bp = b->cb_cur;
do {
ret = write(fd, bp, MIN(len, 8192));
if (ret == -1) {
- cvs_log(LP_ERRNO, "failed to write to file `%s'", path);
- (void)close(fd);
- (void)unlink(path);
+ if (errno == EINTR || errno == EAGAIN)
+ continue;
return (-1);
}
@@ -405,11 +397,69 @@ cvs_buf_write(BUF *b, const char *path, mode_t mode)
bp += (size_t)ret;
} while (len > 0);
+ return (0);
+}
+
+/*
+ * cvs_buf_write()
+ *
+ * Write the contents of the buffer <b> to the file whose path is given in
+ * <path>. If the file does not exist, it is created with mode <mode>.
+ */
+
+int
+cvs_buf_write(BUF *b, const char *path, mode_t mode)
+{
+ int ret, fd;
+
+ fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, mode);
+ if (fd == -1) {
+ cvs_log(LP_ERRNO, "failed to open file `%s': %s",
+ path, strerror(errno));
+ return (-1);
+ }
+
+ ret = cvs_buf_write_fd(b, fd);
+ if (ret == -1) {
+ cvs_log(LP_ERRNO, "failed to write to file `%s': %s",
+ path, strerror(errno));
+ (void)unlink(path);
+ }
(void)close(fd);
- return (0);
+ return (ret);
}
+/*
+ * cvs_buf_write_stmp()
+ *
+ * Write the contents of the buffer <b> to a temporary file whose path is
+ * specified using <template> (see mkstemp.3). NB. This function will modify
+ * <template>, as per mkstemp
+ */
+
+int
+cvs_buf_write_stmp(BUF *b, char *template, mode_t mode)
+{
+ int ret, fd;
+
+ fd = mkstemp(template);
+ if (fd == -1) {
+ cvs_log(LP_ERRNO, "failed to mkstemp file `%s': %s",
+ template, strerror(errno));
+ return (-1);
+ }
+
+ ret = cvs_buf_write_fd(b, fd);
+ if (ret == -1) {
+ cvs_log(LP_ERRNO, "failed to write to temp file `%s': %s",
+ template, strerror(errno));
+ (void)unlink(template);
+ }
+ (void)close(fd);
+
+ return (ret);
+}
/*
* cvs_buf_grow()
diff --git a/usr.bin/cvs/buf.h b/usr.bin/cvs/buf.h
index f857cfac04a..d2041399d90 100644
--- a/usr.bin/cvs/buf.h
+++ b/usr.bin/cvs/buf.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: buf.h,v 1.3 2004/07/14 03:33:53 vincent Exp $ */
+/* $OpenBSD: buf.h,v 1.4 2004/12/08 21:11:07 djm Exp $ */
/*
* Copyright (c) 2003 Jean-Francois Brousseau <jfb@openbsd.org>
* All rights reserved.
@@ -48,19 +48,21 @@
typedef struct cvs_buf BUF;
-BUF* cvs_buf_alloc (size_t, u_int);
-BUF* cvs_buf_load (const char *, u_int);
-void cvs_buf_free (BUF *);
-void* cvs_buf_release (BUF *);
-void cvs_buf_empty (BUF *);
-ssize_t cvs_buf_copy (BUF *, size_t, void *, size_t);
-int cvs_buf_set (BUF *, const void *, size_t, size_t);
-ssize_t cvs_buf_append (BUF *, const void *, size_t);
-int cvs_buf_fappend (BUF *, const char *, ...);
-int cvs_buf_putc (BUF *, int);
-size_t cvs_buf_size (BUF *);
-const void* cvs_buf_peek (BUF *, size_t);
-int cvs_buf_write (BUF *, const char *, mode_t);
+BUF* cvs_buf_alloc (size_t, u_int);
+BUF* cvs_buf_load (const char *, u_int);
+void cvs_buf_free (BUF *);
+void* cvs_buf_release (BUF *);
+void cvs_buf_empty (BUF *);
+ssize_t cvs_buf_copy (BUF *, size_t, void *, size_t);
+int cvs_buf_set (BUF *, const void *, size_t, size_t);
+ssize_t cvs_buf_append (BUF *, const void *, size_t);
+int cvs_buf_fappend (BUF *, const char *, ...);
+int cvs_buf_putc (BUF *, int);
+size_t cvs_buf_size (BUF *);
+const void* cvs_buf_peek (BUF *, size_t);
+int cvs_buf_write_fd (BUF *, int);
+int cvs_buf_write (BUF *, const char *, mode_t);
+int cvs_buf_write_stmp(BUF *, char *, mode_t);
#define cvs_buf_get(b) cvs_buf_peek(b, 0)
diff --git a/usr.bin/cvs/diff.c b/usr.bin/cvs/diff.c
index a2beaac108a..f5b40e832e5 100644
--- a/usr.bin/cvs/diff.c
+++ b/usr.bin/cvs/diff.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: diff.c,v 1.11 2004/12/07 17:10:56 tedu Exp $ */
+/* $OpenBSD: diff.c,v 1.12 2004/12/08 21:11:07 djm Exp $ */
/*
* Copyright (C) Caldera International Inc. 2001-2002.
* All rights reserved.
@@ -472,6 +472,7 @@ cvs_diff_file(struct cvs_file *cfp, void *arg)
{
char *dir, *repo, buf[64];
char fpath[MAXPATHLEN], dfpath[MAXPATHLEN], rcspath[MAXPATHLEN];
+ char path_tmp1[MAXPATHLEN], path_tmp2[MAXPATHLEN];
BUF *b1, *b2;
RCSNUM *r1, *r2;
RCSFILE *rf;
@@ -585,9 +586,17 @@ cvs_diff_file(struct cvs_file *cfp, void *arg)
if (dap->rev2 != NULL)
printf(" -r%s", dap->rev2);
printf(" %s\n", diff_file);
- cvs_buf_write(b1, "/tmp/diff1", 0600);
- cvs_buf_write(b2, "/tmp/diff2", 0600);
- cvs_diffreg("/tmp/diff1", "/tmp/diff2");
+ strlcpy(path_tmp1, "/tmp/diff1.XXXXXXXXXX", sizeof(path_tmp1));
+ if (cvs_buf_write_stmp(b1, path_tmp1, 0600) == -1)
+ return (-1);
+ strlcpy(path_tmp2, "/tmp/diff2.XXXXXXXXXX", sizeof(path_tmp1));
+ if (cvs_buf_write_stmp(b2, path_tmp2, 0600) == -1) {
+ (void)unlink(path_tmp1);
+ return (-1);
+ }
+ cvs_diffreg(path_tmp1, path_tmp2);
+ (void)unlink(path_tmp1);
+ (void)unlink(path_tmp2);
}
cvs_ent_free(entp);