]> mj.ucw.cz Git - umpf.git/commitdiff
fix saving bodies
authorAnicka Bernathova <anicka@anicka.net>
Fri, 17 Jul 2009 20:00:05 +0000 (22:00 +0200)
committerAnicka Bernathova <anicka@anicka.net>
Fri, 17 Jul 2009 20:00:05 +0000 (22:00 +0200)
ham.c
int.c
umpf.h

diff --git a/ham.c b/ham.c
index 6b68404fd1c0efe79253856d7d81e5b0e9c3f5df..f8f96bd007fb7994af99b4d5fc2779fac2700f84 100644 (file)
--- a/ham.c
+++ b/ham.c
@@ -1,6 +1,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sysexits.h>
 
 #include <unistd.h>
 
 
 #include <unistd.h>
 
@@ -79,10 +80,9 @@ get_body(void)
 {
        char* buf;
        struct email* b;
 {
        char* buf;
        struct email* b;
-       int c, fd, cannot_save = 0;
+       int c, fd;
        int will_save = 0;
        int i = 0;
        int will_save = 0;
        int i = 0;
-       int j = 0;
        int curbufsize = MAIL_LEN;
        char* tmpfile;
        int res;
        int curbufsize = MAIL_LEN;
        char* tmpfile;
        int res;
@@ -97,23 +97,21 @@ get_body(void)
        while ((c = getchar()) != EOF){
                buf[i++] = c;
                if (i >= curbufsize - 1) {
        while ((c = getchar()) != EOF){
                buf[i++] = c;
                if (i >= curbufsize - 1) {
-                       if (cannot_save) {
-                               buf = xrealloc(buf, curbufsize *= 2);
-                               continue;
-                       }
                        tmpfile = xstrdup("/tmp/umpf.XXXXXX");
                        fd = mkstemp(tmpfile);
                        /* cannot create tmpfile, try to continue reading */
                        tmpfile = xstrdup("/tmp/umpf.XXXXXX");
                        fd = mkstemp(tmpfile);
                        /* cannot create tmpfile, try to continue reading */
-                       if (fd < 0) {
-                               buf = xrealloc(buf, curbufsize *= 2);
-                               cannot_save = 1;
-                               free_string(tmpfile);
-                               continue;
-                       } else {
+                       if (fd < 0)
+                               bye(EX_TEMPFAIL, "%m");
+                       else {
                                will_save = 1;
                                b->body = NULL;
                                b->tmpfile = tmpfile;
                                b->fd = fd;
                                will_save = 1;
                                b->body = NULL;
                                b->tmpfile = tmpfile;
                                b->fd = fd;
+                               res = write(fd, buf, MAIL_LEN);
+                               if (res < MAIL_LEN) {
+                                       unlink(b->tmpfile);
+                                       bye(EX_TEMPFAIL, "%m");
+                               }
                                break;  
                        }
                }
                                break;  
                        }
                }
@@ -121,6 +119,7 @@ get_body(void)
        b->body_len = i;
        /* save rest of the body to the tmpfile */
        if (will_save) {
        b->body_len = i;
        /* save rest of the body to the tmpfile */
        if (will_save) {
+               int j = 0;
                while ((c = getchar()) != EOF){
                        buf[j++] = c;
                        b->body_len++;
                while ((c = getchar()) != EOF){
                        buf[j++] = c;
                        b->body_len++;
@@ -128,32 +127,17 @@ get_body(void)
                                j = 0;
                                res = write(fd, buf, MAIL_LEN);
                                if (res < MAIL_LEN) {
                                j = 0;
                                res = write(fd, buf, MAIL_LEN);
                                if (res < MAIL_LEN) {
-                                       int missing = MAIL_LEN - res;
-                                       curbufsize = 2*b->body_len;
-                                       buf = xrealloc(buf, curbufsize);
-                                       // no point to check length here
-                                       read(fd, buf, b->body_len - missing);
                                        unlink(b->tmpfile);
                                        unlink(b->tmpfile);
-                                       b->tmpfile = NULL;
-                                       b->fd = -1;
-                                       b->body = buf;
-                                       break;
+                                       bye(EX_TEMPFAIL, "%m");
                                }
                        }
                }
                                }
                        }
                }
-               /* could not write all the body to the tmpfile, try to read
-                       the rest */
-               if (b->body) {
-                       while ((c = getchar()) != EOF){
-                               buf[j++] = c;
-                               b->body_len++;
-                               if (i >= curbufsize - 1) {
-                                       buf = xrealloc(buf, curbufsize *= 2);
-                               }
-                       }
+               res = write(fd, buf, j);
+               if (res < j) {
+                       unlink(b->tmpfile);
+                       bye(EX_TEMPFAIL, "%m");
                }
        }
                }
        }
-
        return b; 
 }
 
        return b; 
 }
 
@@ -277,13 +261,13 @@ write_email_to_fd(int fd, struct email* email)
        struct hlist* ph;
        LIST_FOREACH(ph, email->headers){
                written = write(fd, ph->name, strlen(ph->name));
        struct hlist* ph;
        LIST_FOREACH(ph, email->headers){
                written = write(fd, ph->name, strlen(ph->name));
-               if (written < strlen(ph->name))
+               if (written < (ssize_t) strlen(ph->name))
                        return 1;
                written = write(fd, ":", 1);
                if (written < 1)
                        return 1;
                written = write(fd, ph->value, strlen(ph->value));
                        return 1;
                written = write(fd, ":", 1);
                if (written < 1)
                        return 1;
                written = write(fd, ph->value, strlen(ph->value));
-               if (written < strlen(ph->value))
+               if (written < (ssize_t) strlen(ph->value))
                        return 1;
                written = write(fd, "\n", 1);
                if (written < 1)
                        return 1;
                written = write(fd, "\n", 1);
                if (written < 1)
diff --git a/int.c b/int.c
index d4360c6696cb60a5eae84f49c6d4dd7f56a87492..e1c906f058342ada6c8d264876d5f4fcc48bed35 100644 (file)
--- a/int.c
+++ b/int.c
@@ -7,6 +7,7 @@
 #include <fcntl.h>
 #include <stdarg.h>
 #include <sys/wait.h>
 #include <fcntl.h>
 #include <stdarg.h>
 #include <sys/wait.h>
+#include <sysexits.h>
 
 #include "cond.tab.h"
 #include "umpf.h"
 
 #include "cond.tab.h"
 #include "umpf.h"
@@ -17,7 +18,7 @@
 
 #define INT_TO_STRING_LEN ((sizeof(int)*4*CHAR_BIT)/10 + 6)
 
 
 #define INT_TO_STRING_LEN ((sizeof(int)*4*CHAR_BIT)/10 + 6)
 
-static void __attribute__ ((noreturn)) 
+void __attribute__ ((noreturn)) 
 bye(int code, char* msg, ...)
 {
         va_list args;
 bye(int code, char* msg, ...)
 {
         va_list args;
@@ -391,7 +392,7 @@ deliver(char* where, int copy, struct list* hash)
 
        if (!copy) {
                if (res)
 
        if (!copy) {
                if (res)
-                       bye(-res, "%m");
+                       bye(EX_TEMPFAIL, "%m");
                else
                        bye(0, NULL);
        }
                else
                        bye(0, NULL);
        }
@@ -426,7 +427,7 @@ end:
        destroy_email(em);
        if (!copy) {
                if (res)
        destroy_email(em);
        if (!copy) {
                if (res)
-                       bye(-res, "%m");
+                       bye(EX_TEMPFAIL, "%m");
                else
                        bye(0, NULL);
        }
                else
                        bye(0, NULL);
        }
diff --git a/umpf.h b/umpf.h
index 87f5477a9c0d82f3faef23746a2902346f34157a..7947ef37224a5424882aede606a26f92e72ddf65 100644 (file)
--- a/umpf.h
+++ b/umpf.h
@@ -204,6 +204,7 @@ void save_current_headers(struct list* hash);
 void print_vars(struct list* hash);
 void interp(struct list* ins, struct list* hash);
 void free_string(char* c);
 void print_vars(struct list* hash);
 void interp(struct list* ins, struct list* hash);
 void free_string(char* c);
+void __attribute__ ((noreturn)) bye(int code, char* msg, ...);
 
 /* ham.c */
 char* default_mailbox;
 
 /* ham.c */
 char* default_mailbox;