}
static void
-deliver(char *maildir, char *newfn)
+get_filename(char *fn)
{
- char fn[TMPNAMLEN], host[HOST_NAME_MAX + 1];
- char *buildfn; /* used to build up the fn */
+ pid_t pid;
+ char host[HOST_NAME_MAX + 1];
+ int try;
time_t now;
struct tm *t;
- pid_t pid;
+ char *buildfn; /* used to build up the fn */
struct stat statbuf;
- int try;
- int fd, r;
- char buf[BUFSIZ];
-
- if (chdir(maildir) != 0) {
- err(EX_TEMPFAIL, "Failed chdir(%s)", maildir);
- }
pid = getpid();
gethostname(host, sizeof(host));
sleep(2);
}
+}
- fd = open(fn, O_WRONLY | O_EXCL | O_CREAT, 0600);
- if (fd < 0) {
- err(EX_TEMPFAIL, "Failed open(%s)", fn);
+/* buf is passed only as an (early, evil) space optimization). */
+static void
+strip_from(int fd, char *buf, char *fn)
+{
+ ssize_t r;
+
+ r = read(STDIN_FILENO, buf, 5);
+ if (r != 5) {
+ unlink(fn);
+ err(EX_TEMPFAIL, "Failed read from stdin");
}
- /* From this point, we have a tmp file, so if someting fails we
- * must try to unlink it before bombing. */
+ if (strncmp(buf, "From ", 5)) {
+ while ((r = read(STDIN_FILENO, buf, 1)) > 0
+ || (r == -1 && errno == EINTR)) {
+ if (buf[0] == '\n') {
+ break;
+ }
+ }
- {
- r = read(STDIN_FILENO, buf, 5);
- if (r != 5) {
+ if (r == -1) {
unlink(fn);
err(EX_TEMPFAIL, "Failed read from stdin");
}
-
- if (strncmp(buf, "From ", 5)) {
- while ((r = read(STDIN_FILENO, buf, 1)) > 0
- || (r == -1 && errno == EINTR)) {
- if (buf[0] == '\n') {
- break;
- }
- }
-
- if (r == -1) {
- unlink(fn);
- err(EX_TEMPFAIL, "Failed read from stdin");
- }
- } else {
- if (full_write(fd, buf, r) != r) {
- unlink(fn);
- err(EX_TEMPFAIL, "Failed write(%s)", fn);
- }
+ } else {
+ if (full_write(fd, buf, r) != r) {
+ unlink(fn);
+ err(EX_TEMPFAIL, "Failed write(%s)", fn);
}
}
+}
+
+/* buf is passed only as an (early, evil) space optimization). */
+static void
+copy_message(int fd, char *buf, char *fn)
+{
+ ssize_t r;
while ((r = read(STDIN_FILENO, buf, BUFSIZ)) > 0
|| (r == -1 && errno == EINTR)) {
unlink(fn);
err(EX_TEMPFAIL, "Failed fsync(%s)", fn);
}
+
if (close(fd) < 0) {
unlink(fn);
err(EX_TEMPFAIL, "Failed close(%s)", fn);
}
+}
+
+static void
+deliver(char *maildir, char *newfn)
+{
+ char fn[TMPNAMLEN];
+ int fd;
+ char buf[BUFSIZ]; /* Allocated here only as an (early,
+ * evil) space optimization. */
+
+ if (chdir(maildir) != 0) {
+ err(EX_TEMPFAIL, "Failed chdir(%s)", maildir);
+ }
+
+ get_filename(fn);
+
+ fd = open(fn, O_WRONLY | O_EXCL | O_CREAT, 0600);
+ if (fd < 0) {
+ err(EX_TEMPFAIL, "Failed open(%s)", fn);
+ }
+
+ /* From this point, we have a tmp file, so if someting fails we
+ * must try to unlink it before bombing. */
+
+ strip_from(fd, buf, fn);
+ copy_message(fd, buf, fn);
strcpy(newfn, fn);
memcpy(newfn, "new", 3);