From 454698224b4a670e4aa2a1b4baf59b581b62fdc7 Mon Sep 17 00:00:00 2001 From: Damien Miller Date: Wed, 8 Dec 2004 21:11:08 +0000 Subject: use mkstemp for local diff -r r1 -r r2; ok jfb@ --- usr.bin/cvs/buf.c | 82 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 66 insertions(+), 16 deletions(-) (limited to 'usr.bin/cvs/buf.c') 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 * All rights reserved. @@ -34,6 +34,7 @@ #include #include #include +#include #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 to the file whose path is given in - * . If the file does not exist, it is created with mode . + * Write the contents of the buffer to the specified */ 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 to the file whose path is given in + * . If the file does not exist, it is created with 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 to a temporary file whose path is + * specified using