From: epg <> Date: Wed, 11 Sep 2002 02:12:04 +0000 (+0000) Subject: Rename copy_message to copy_file, document it, and make it slightly X-Git-Url: https://diplodocus.org/git/mdeliver/commitdiff_plain/56a5faf785b92d9af66171443bf8b8e5f72cb759 Rename copy_message to copy_file, document it, and make it slightly more generic. Now i can share it with other programs (for example, my portable install(1)). --- diff --git a/mdeliver.c b/mdeliver.c index 39be296..6b4debd 100644 --- a/mdeliver.c +++ b/mdeliver.c @@ -179,16 +179,21 @@ strip_from(int fd, char *buf, char *fn) } } -/* Copy the message from stdin to fd. */ -/* buf is passed only as an (early, evil) space optimization). */ +/* Copy the file from INFD to OUTFD in blocks of BUFSIZE bytes (stored + * in BUF). BUF must point to a buffer of at least BUFSIZE bytes; + * we're not dealing with null-terminated strings here. + * + * Note that OUTFD is closed before returning; this is necessary to + * ensure the file was fully written on some filesystems, such as NFS. + */ static void -copy_message(int fd, char *buf, char *fn) +copy_file(int infd, int outfd, char *buf, size_t bufsize, char *fn) { ssize_t r; - while ((r = read(STDIN_FILENO, buf, BUFSIZ)) > 0 + while ((r = read(infd, buf, bufsize)) > 0 || (r == -1 && errno == EINTR)) { - if (full_write(fd, buf, r) != r) { + if (full_write(outfd, buf, r) != r) { save_errno( unlink(fn) ); err(MDELIVER_EXITCODE, "Failed write(%s)", fn); } @@ -196,15 +201,15 @@ copy_message(int fd, char *buf, char *fn) if (r == -1) { save_errno( unlink(fn) ); - err(MDELIVER_EXITCODE, "Failed read from stdin"); + err(MDELIVER_EXITCODE, "Failed read"); } - if (fsync(fd) < 0) { + if (fsync(outfd) < 0) { save_errno( unlink(fn) ); err(MDELIVER_EXITCODE, "Failed fsync(%s)", fn); } - if (close(fd) < 0) { + if (close(outfd) < 0) { save_errno( unlink(fn) ); err(MDELIVER_EXITCODE, "Failed close(%s)", fn); } @@ -233,7 +238,7 @@ deliver(char *maildir, char *newfn) * must try to unlink it before bombing. */ strip_from(fd, buf, fn); - copy_message(fd, buf, fn); + copy_file(STDIN_FILENO, fd, buf, BUFSIZ, fn); strcpy(newfn, fn); memcpy(newfn, "new", 3);