]> diplodocus.org Git - mdeliver/commitdiff
Reorganize the deliver function, splitting out logical units into the
authorepg <>
Sun, 25 Aug 2002 07:05:50 +0000 (07:05 +0000)
committerepg <>
Sun, 25 Aug 2002 07:05:50 +0000 (07:05 +0000)
separate functions deliver, get_filename, strip_from, and
copy_message.

(get_filename): Move deliver's filename-getting code to this new
function.

(strip_from): Move deliver's busted-from-line stripping code to this
new function.

(copy_message): Move deliver's stdin-to-new-file-copying code to this
new function.

(deliver): Call the above new functions appropriately.

mdeliver.c

index 03f5c99..6d31a82 100644 (file)
@@ -70,21 +70,15 @@ full_write(int fd, const char *buf, size_t nbytes)
 }
 
 static void
 }
 
 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;
     time_t now;
     struct tm *t;
-    pid_t pid;
+    char *buildfn;              /* used to build up the fn */
     struct stat statbuf;
     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));
 
     pid = getpid();
     gethostname(host, sizeof(host));
@@ -110,41 +104,45 @@ deliver(char *maildir, char *newfn)
 
         sleep(2);
     }
 
         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");
         }
             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)) {
 
     while ((r = read(STDIN_FILENO, buf, BUFSIZ)) > 0
            || (r == -1 && errno == EINTR)) {
@@ -163,10 +161,37 @@ deliver(char *maildir, char *newfn)
         unlink(fn);
         err(EX_TEMPFAIL, "Failed fsync(%s)", fn);
     }
         unlink(fn);
         err(EX_TEMPFAIL, "Failed fsync(%s)", fn);
     }
+
     if (close(fd) < 0) {
         unlink(fn);
         err(EX_TEMPFAIL, "Failed close(%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);
 
     strcpy(newfn, fn);
     memcpy(newfn, "new", 3);