]> diplodocus.org Git - nmh/blobdiff - uip/dropsbr.c
lock_file.c: close(2) file descriptor on failure, avoiding leak.
[nmh] / uip / dropsbr.c
index a02e29c48fda1a28ac3ee6ed55682bf56c64056c..40ec9b806451feb55949738fbb41eb98a38f0e5e 100644 (file)
@@ -6,20 +6,13 @@
  */
 
 #include <h/nmh.h>
  */
 
 #include <h/nmh.h>
-#include <h/utils.h>
-
 #include <h/mh.h>
 #include <h/mh.h>
+#include <h/utils.h>
 #include <h/dropsbr.h>
 #include <h/mts.h>
 #include <h/tws.h>
 #include <h/dropsbr.h>
 #include <h/mts.h>
 #include <h/tws.h>
-
-#ifdef NTOHLSWAP
-# include <netinet/in.h>
-#else
-# undef ntohl
-# define ntohl(n) (n)
-#endif
-
+#include "sbr/lock_file.h"
+#include "sbr/m_mktemp.h"
 #include <fcntl.h>
 
 /*
 #include <fcntl.h>
 
 /*
@@ -27,7 +20,6 @@
  */
 static int mbx_chk_mbox (int);
 static int mbx_chk_mmdf (int);
  */
 static int mbx_chk_mbox (int);
 static int mbx_chk_mmdf (int);
-static int map_open (char *, int);
 
 
 /*
 
 
 /*
@@ -46,19 +38,16 @@ mbx_open (char *file, int mbx_style, uid_t uid, gid_t gid, mode_t mode)
     /* attempt to open and lock file */
     for (count = 4; count > 0; count--) {
         int failed_to_lock = 0;
     /* attempt to open and lock file */
     for (count = 4; count > 0; count--) {
         int failed_to_lock = 0;
-       if ((fd = lkopenspool (file, O_RDWR | O_CREAT |
-                              O_NONBLOCK, mode, &failed_to_lock)) == NOTOK) {
-            if (failed_to_lock) {
-                j = errno;
-                sleep (5);
-                continue;
-            } else {
-                return NOTOK;
-            }
-       }
 
 
-       /* good file descriptor */
-       break;
+       if ((fd = lkopenspool (file, O_RDWR | O_CREAT | O_NONBLOCK,
+            mode, &failed_to_lock)) != NOTOK)
+            break;
+
+        if (!failed_to_lock)
+            return NOTOK;
+
+        j = errno;
+        sleep (5);
     }
 
     errno = j;
     }
 
     errno = j;
@@ -84,7 +73,7 @@ mbx_open (char *file, int mbx_style, uid_t uid, gid_t gid, mode_t mode)
        if (chmod (file, mode) < 0) {
            advise (file, "chmod");
        }
        if (chmod (file, mode) < 0) {
            advise (file, "chmod");
        }
-    } else if (st.st_size > (off_t) 0) {
+    } else if (st.st_size > 0) {
        int status;
 
        /* check the maildrop */
        int status;
 
        /* check the maildrop */
@@ -118,7 +107,7 @@ static int
 mbx_chk_mbox (int fd)
 {
     /* just seek to the end */
 mbx_chk_mbox (int fd)
 {
     /* just seek to the end */
-    if (lseek (fd, (off_t) 0, SEEK_END) == (off_t) NOTOK)
+    if (lseek(fd, 0, SEEK_END) == (off_t) NOTOK)
        return NOTOK;
 
     return OK;
        return NOTOK;
 
     return OK;
@@ -135,7 +124,7 @@ mbx_chk_mmdf (int fd)
     ssize_t count;
     char ldelim[BUFSIZ];
 
     ssize_t count;
     char ldelim[BUFSIZ];
 
-    count = strlen (mmdlm2);
+    count = LEN(MMDF_DELIM);
 
     if (lseek (fd, -count, SEEK_END) == (off_t) NOTOK)
        return NOTOK;
 
     if (lseek (fd, -count, SEEK_END) == (off_t) NOTOK)
        return NOTOK;
@@ -144,116 +133,10 @@ mbx_chk_mmdf (int fd)
 
     ldelim[count] = 0;
 
 
     ldelim[count] = 0;
 
-    if (strcmp (ldelim, mmdlm2)
+    if (strcmp (ldelim, MMDF_DELIM)
            && write (fd, "\n", 1) != 1
            && write (fd, "\n", 1) != 1
-           && write (fd, mmdlm2, count) != count)
-       return NOTOK;
-
-    return OK;
-}
-
-
-int
-mbx_read (FILE *fp, long pos, struct drop **drops)
-{
-    int len, size;
-    long ld1, ld2;
-    char *bp;
-    char buffer[BUFSIZ];
-    struct drop *cp, *dp, *ep, *pp;
-
-    len = MAXFOLDER;
-    pp = mh_xcalloc(len, sizeof *pp);
-
-    ld1 = (long) strlen (mmdlm1);
-    ld2 = (long) strlen (mmdlm2);
-
-    fseek (fp, pos, SEEK_SET);
-    for (ep = (dp = pp) + len - 1; fgets (buffer, sizeof(buffer), fp);) {
-       size = 0;
-       if (strcmp (buffer, mmdlm1) == 0)
-           pos += ld1, dp->d_start = (long) pos;
-       else {
-           dp->d_start = (long)pos , pos += (long) strlen (buffer);
-           for (bp = buffer; *bp; bp++, size++)
-               if (*bp == '\n')
-                   size++;
-       }
-
-       while (fgets (buffer, sizeof(buffer), fp) != NULL)
-           if (strcmp (buffer, mmdlm2) == 0)
-               break;
-           else {
-               pos += (long) strlen (buffer);
-               for (bp = buffer; *bp; bp++, size++)
-                   if (*bp == '\n')
-                       size++;
-           }
-
-       if (dp->d_start != (long) pos) {
-           dp->d_id = 0;
-           dp->d_size = (long) size;
-           dp->d_stop = pos;
-           dp++;
-       }
-       pos += ld2;
-
-       if (dp >= ep) {
-           int    curlen = dp - pp;
-
-           cp = (struct drop *) mh_xrealloc ((char *) pp,
-                                   (size_t) (len += MAXFOLDER) * sizeof(*pp));
-           dp = cp + curlen, ep = (pp = cp) + len - 1;
-       }
-    }
-
-    if (dp == pp)
-       free(pp);
-    else
-       *drops = pp;
-    return (dp - pp);
-}
-
-
-int
-mbx_write(char *mailbox, int md, FILE *fp, int id, long last,
-           long pos, off_t stop, int mapping, int noisy)
-{
-    int i, j, size;
-    off_t start;
-    long off;
-    char *cp;
-    char buffer[BUFSIZ];
-
-    off = (long) lseek (md, (off_t) 0, SEEK_CUR);
-    j = strlen (mmdlm1);
-    if (write (md, mmdlm1, j) != j)
+           && write (fd, MMDF_DELIM, count) != count)
        return NOTOK;
        return NOTOK;
-    start = lseek (md, (off_t) 0, SEEK_CUR);
-    size = 0;
-
-    fseek (fp, pos, SEEK_SET);
-    while (fgets (buffer, sizeof(buffer), fp) && (pos < stop)) {
-       i = strlen (buffer);
-       for ( ; (j = stringdex (mmdlm1, buffer)) >= 0; buffer[j]++)
-           continue;
-       for ( ; (j = stringdex (mmdlm2, buffer)) >= 0; buffer[j]++)
-           continue;
-       if (write (md, buffer, i) != i)
-           return NOTOK;
-       pos += (long) i;
-       if (mapping)
-           for (cp = buffer; i-- > 0; size++)
-               if (*cp++ == '\n')
-                   size++;
-    }
-
-    stop = lseek (md, (off_t) 0, SEEK_CUR);
-    j = strlen (mmdlm2);
-    if (write (md, mmdlm2, j) != j)
-       return NOTOK;
-    if (mapping)
-       map_write (mailbox, md, id, last, start, stop, off, size, noisy);
 
     return OK;
 }
 
     return OK;
 }
@@ -265,24 +148,20 @@ mbx_write(char *mailbox, int md, FILE *fp, int id, long last,
 
 int
 mbx_copy (char *mailbox, int mbx_style, int md, int fd,
 
 int
 mbx_copy (char *mailbox, int mbx_style, int md, int fd,
-          int mapping, char *text, int noisy)
+          char *text)
 {
     int i, j, size;
 {
     int i, j, size;
-    off_t start, stop;
-    long pos;
     char *cp, buffer[BUFSIZ + 1];   /* Space for NUL. */
     FILE *fp;
 
     char *cp, buffer[BUFSIZ + 1];   /* Space for NUL. */
     FILE *fp;
 
-    pos = (long) lseek (md, (off_t) 0, SEEK_CUR);
     size = 0;
 
     switch (mbx_style) {
        case MMDF_FORMAT: 
        default: 
     size = 0;
 
     switch (mbx_style) {
        case MMDF_FORMAT: 
        default: 
-           j = strlen (mmdlm1);
-           if (write (md, mmdlm1, j) != j)
+           j = LEN(MMDF_DELIM);
+           if (write (md, MMDF_DELIM, j) != j)
                return NOTOK;
                return NOTOK;
-           start = lseek (md, (off_t) 0, SEEK_CUR);
 
            if (text) {
                i = strlen (text);
 
            if (text) {
                i = strlen (text);
@@ -296,24 +175,17 @@ mbx_copy (char *mailbox, int mbx_style, int md, int fd,
            while ((i = read (fd, buffer, sizeof buffer - 1)) > 0) {
                 buffer[i] = '\0';   /* Terminate for stringdex(). */
 
            while ((i = read (fd, buffer, sizeof buffer - 1)) > 0) {
                 buffer[i] = '\0';   /* Terminate for stringdex(). */
 
-               for ( ; (j = stringdex (mmdlm1, buffer)) >= 0; buffer[j]++)
+               for ( ; (j = stringdex (MMDF_DELIM, buffer)) >= 0; buffer[j]++)
                    continue;
                    continue;
-               for ( ; (j = stringdex (mmdlm2, buffer)) >= 0; buffer[j]++)
+               for ( ; (j = stringdex (MMDF_DELIM, buffer)) >= 0; buffer[j]++)
                    continue;
                if (write (md, buffer, i) != i)
                    return NOTOK;
                    continue;
                if (write (md, buffer, i) != i)
                    return NOTOK;
-               if (mapping)
-                   for (cp = buffer; i-- > 0; size++)
-                       if (*cp++ == '\n')
-                           size++;
            }
 
            }
 
-           stop = lseek (md, (off_t) 0, SEEK_CUR);
-           j = strlen (mmdlm2);
-           if (write (md, mmdlm2, j) != j)
+           j = LEN(MMDF_DELIM);
+           if (write (md, MMDF_DELIM, j) != j)
                return NOTOK;
                return NOTOK;
-           if (mapping)
-               map_write (mailbox, md, 0, (long) 0, start, stop, pos, size, noisy);
 
            return (i != NOTOK ? OK : NOTOK);
 
 
            return (i != NOTOK ? OK : NOTOK);
 
@@ -324,7 +196,6 @@ mbx_copy (char *mailbox, int mbx_style, int md, int fd,
                close (j);
                return NOTOK;
            }
                close (j);
                return NOTOK;
            }
-           start = lseek (md, (off_t) 0, SEEK_CUR);
 
            /* If text is given, we add it to top of message */
            if (text) {
 
            /* If text is given, we add it to top of message */
            if (text) {
@@ -398,52 +269,20 @@ mbx_copy (char *mailbox, int mbx_style, int md, int fd,
                    fclose (fp);
                    return NOTOK;
                }
                    fclose (fp);
                    return NOTOK;
                }
-               if (mapping)
-                   for (cp = buffer; i-- > 0; size++)
-                       if (*cp++ == '\n')
-                           size++;
            }
            if (write (md, "\n", 1) != 1) {
                fclose (fp);
                return NOTOK;
            }
            }
            if (write (md, "\n", 1) != 1) {
                fclose (fp);
                return NOTOK;
            }
-           if (mapping)
-               size += 2;
 
            fclose (fp);
 
            fclose (fp);
-           lseek (fd, (off_t) 0, SEEK_END);
-           stop = lseek (md, (off_t) 0, SEEK_CUR);
-           if (mapping)
-               map_write (mailbox, md, 0, (long) 0, start, stop, pos, size, noisy);
+           lseek(fd, 0, SEEK_END);
 
            return OK;
     }
 }
 
 
 
            return OK;
     }
 }
 
 
-int
-mbx_size (int md, off_t start, off_t stop)
-{
-    int i, fd;
-    long pos;
-    FILE *fp;
-
-    if ((fd = dup (md)) == NOTOK || (fp = fdopen (fd, "r")) == NULL) {
-       if (fd != NOTOK)
-           close (fd);
-       return NOTOK;
-    }
-
-    fseek (fp, start, SEEK_SET);
-    for (i = 0, pos = stop - start; pos-- > 0; i++)
-       if (fgetc (fp) == '\n')
-           i++;
-
-    fclose (fp);
-    return i;
-}
-
-
 /*
  * Close and unlock file/maildrop.
  */
 /*
  * Close and unlock file/maildrop.
  */
@@ -455,242 +294,3 @@ mbx_close (char *mailbox, int md)
         return OK;
     return NOTOK;
 }
         return OK;
     return NOTOK;
 }
-
-
-/*
- * This function is performed implicitly by getbbent.c:
- *     bb->bb_map = map_name (bb->bb_file);
- */
-
-char *
-map_name (char *file)
-{
-    char *cp, *dp;
-    static char buffer[BUFSIZ];
-
-    if ((dp = strchr(cp = r1bindex (file, '/'), '.')) == NULL)
-       dp = cp + strlen (cp);
-    if (cp == file)
-       snprintf (buffer, sizeof(buffer), ".%.*s%s", (int)(dp - cp), cp, ".map");
-    else
-       snprintf (buffer, sizeof(buffer), "%.*s.%.*s%s",
-               (int)(cp - file), file, (int)(dp - cp), cp, ".map");
-
-    return buffer;
-}
-
-
-int
-map_read (char *file, long pos, struct drop **drops, int noisy)
-{
-    int i, md, msgp;
-    char *cp;
-    struct drop d;
-    struct drop *mp, *dp;
-
-    if ((md = open (cp = map_name (file), O_RDONLY)) == NOTOK
-           || map_chk (cp, md, mp = &d, pos, noisy)) {
-       if (md != NOTOK)
-           close (md);
-       return 0;
-    }
-
-    msgp = mp->d_id;
-    dp = mh_xcalloc(msgp + 1, sizeof *dp);
-    memcpy((char *) dp, (char *) mp, sizeof(*dp));
-
-    lseek (md, (off_t) sizeof(*mp), SEEK_SET);
-    if ((i = read (md, (char *) (dp + 1), msgp * sizeof(*dp))) <
-        (int) sizeof(*dp)) {
-       i = 0;
-       free(dp);
-    } else {
-#ifdef NTOHLSWAP
-       struct drop *tdp;
-       int j;
-
-       for (j = 0, tdp = dp; j < i / sizeof(*dp); j++, tdp++) {
-           tdp->d_id = ntohl(tdp->d_id);
-           tdp->d_size = ntohl(tdp->d_size);
-           tdp->d_start = ntohl(tdp->d_start);
-           tdp->d_stop = ntohl(tdp->d_stop);
-       }
-#endif
-       *drops = dp;
-    }
-
-    close (md);
-
-    return (i / sizeof(*dp));
-}
-
-
-int
-map_write (char *mailbox, int md, int id, long last, off_t start,
-           off_t stop, long pos, int size, int noisy)
-{
-    int i;
-    int clear, fd, td;
-    char *file;
-    struct drop *dp;
-    struct drop d1, d2, *rp;
-    FILE *fp;
-    struct stat st;
-
-    if ((fd = map_open (file = map_name (mailbox), md)) == NOTOK)
-       return NOTOK;
-
-    if ((fstat (fd, &st) == OK) && (st.st_size > 0))
-       clear = 0;
-    else
-       clear = 1;
-
-    if (!clear && map_chk (file, fd, &d1, pos, noisy)) {
-       (void) m_unlink (file);
-       mbx_close (file, fd);
-       if ((fd = map_open (file, md)) == NOTOK)
-           return NOTOK;
-       clear++;
-    }
-
-    if (clear) {
-       if ((td = dup (md)) == NOTOK || (fp = fdopen (td, "r")) == NULL) {
-           if (noisy)
-               admonish (file, "unable to %s", td != NOTOK ? "fdopen" : "dup");
-           if (td != NOTOK)
-               close (td);
-           mbx_close (file, fd);
-           return NOTOK;
-       }
-
-       switch (i = mbx_read (fp, 0, &rp)) {
-           case NOTOK:
-               fclose (fp);
-               mbx_close (file, fd);
-               return NOTOK;
-
-           case OK:
-               fclose (fp);
-               break;
-
-           default:
-               d1.d_id = 0;
-               for (dp = rp; i-- >0; dp++) {
-                   if (dp->d_start == start)
-                       dp->d_id = id;
-                   lseek (fd, (off_t) (++d1.d_id * sizeof(*dp)), SEEK_SET);
-                   if (write (fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
-                       if (noisy)
-                           admonish (file, "write error");
-                       mbx_close (file, fd);
-                       fclose (fp);
-                       return NOTOK;
-                   }
-               }
-               free(rp);
-               fclose (fp);
-               break;
-       }
-    }
-    else {
-       if (last == 0)
-           last = d1.d_start;
-       dp = &d2;
-       dp->d_id = id;
-       dp->d_size = (long) (size ? size : mbx_size (fd, start, stop));
-       dp->d_start = start;
-       dp->d_stop = stop;
-       lseek (fd, (off_t) (++d1.d_id * sizeof(*dp)), SEEK_SET);
-       if (write (fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
-           if (noisy)
-               admonish (file, "write error");
-           mbx_close (file, fd);
-           return NOTOK;
-       }
-    }
-
-    dp = &d1;
-    dp->d_size = DRVRSN;
-    dp->d_start = (long) last;
-    dp->d_stop = lseek (md, (off_t) 0, SEEK_CUR);
-
-    lseek (fd, (off_t) 0, SEEK_SET);
-    if (write (fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
-       if (noisy)
-           admonish (file, "write error");
-       mbx_close (file, fd);
-       return NOTOK;
-    }
-
-    mbx_close (file, fd);
-
-    return OK;
-}
-
-
-static int
-map_open (char *file, int md)
-{
-    mode_t mode;
-    struct stat st;
-
-    mode = fstat (md, &st) != NOTOK ? (int) (st.st_mode & 0777) : m_gmprot ();
-    return mbx_open (file, OTHER_FORMAT, st.st_uid, st.st_gid, mode);
-}
-
-
-int
-map_chk (char *file, int fd, struct drop *dp, long pos, int noisy)
-{
-    ssize_t count;
-    struct drop d, tmpd;
-    struct drop *dl;
-
-    if (read (fd, (char *) &tmpd, sizeof(*dp)) != sizeof(*dp)) {
-#ifdef notdef
-       inform("%s: missing or partial index, continuing...", file);
-#endif /* notdef */
-       return NOTOK;
-    }
-#ifndef        NTOHLSWAP
-    *dp = tmpd;                /* if ntohl(n)=(n), can use struct assign */
-#else
-    dp->d_id    = ntohl(tmpd.d_id);
-    dp->d_size  = ntohl(tmpd.d_size);
-    dp->d_start = ntohl(tmpd.d_start);
-    dp->d_stop  = ntohl(tmpd.d_stop);
-#endif
-    
-    if (dp->d_size != DRVRSN) {
-       if (noisy)
-           inform("%s: version mismatch (%d != %d), continuing...", file,
-                               dp->d_size, DRVRSN);
-       return NOTOK;
-    }
-
-    if (dp->d_stop != pos) {
-       if (noisy && pos != (long) 0)
-           inform("%s: pointer mismatch or incomplete index (%ld!=%ld), "
-               "continuing...", file, dp->d_stop, (long) pos);
-       return NOTOK;
-    }
-
-    if ((long) ((dp->d_id + 1) * sizeof(*dp)) != (long) lseek (fd, (off_t) 0, SEEK_END)) {
-       if (noisy)
-           inform("%s: corrupt index(1), continuing...", file);
-       return NOTOK;
-    }
-
-    dl = &d;
-    count = strlen (mmdlm2);
-    lseek (fd, (off_t) (dp->d_id * sizeof(*dp)), SEEK_SET);
-    if (read (fd, (char *) dl, sizeof(*dl)) != sizeof(*dl)
-           || (ntohl(dl->d_stop) != dp->d_stop
-               && ntohl(dl->d_stop) + count != dp->d_stop)) {
-       if (noisy)
-           inform("%s: corrupt index(2), continuing...", file);
-       return NOTOK;
-    }
-
-    return OK;
-}