version 1.6, 2004/12/07 17:10:56 |
version 1.7, 2004/12/08 21:11:07 |
|
|
#include <unistd.h> |
#include <unistd.h> |
#include <string.h> |
#include <string.h> |
#include <stdlib.h> |
#include <stdlib.h> |
|
#include <errno.h> |
|
|
#include "buf.h" |
#include "buf.h" |
#include "log.h" |
#include "log.h" |
|
|
|
|
|
|
/* |
/* |
* cvs_buf_write() |
* cvs_buf_write_fd() |
* |
* |
* Write the contents of the buffer <b> to the file whose path is given in |
* Write the contents of the buffer <b> to the specified <fd> |
* <path>. If the file does not exist, it is created with mode <mode>. |
|
*/ |
*/ |
int |
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; |
u_char *bp; |
size_t len; |
size_t len; |
ssize_t ret; |
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; |
len = b->cb_len; |
bp = b->cb_cur; |
bp = b->cb_cur; |
|
|
do { |
do { |
ret = write(fd, bp, MIN(len, 8192)); |
ret = write(fd, bp, MIN(len, 8192)); |
if (ret == -1) { |
if (ret == -1) { |
cvs_log(LP_ERRNO, "failed to write to file `%s'", path); |
if (errno == EINTR || errno == EAGAIN) |
(void)close(fd); |
continue; |
(void)unlink(path); |
|
return (-1); |
return (-1); |
} |
} |
|
|
|
|
bp += (size_t)ret; |
bp += (size_t)ret; |
} while (len > 0); |
} 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); |
(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() |
* cvs_buf_grow() |