Merge branch 'master' into nohook
authorPierre Habouzit <madcoder@debian.org>
Tue, 20 Nov 2007 21:34:21 +0000 (22:34 +0100)
committerPierre Habouzit <madcoder@debian.org>
Tue, 20 Nov 2007 21:34:21 +0000 (22:34 +0100)
1  2 
crypt.cpkg
lib-mx/compress.c
lib-mx/mx.c

diff --combined crypt.cpkg
@@@ -470,7 -470,7 +470,7 @@@ static int data_object_to_stream(gpgme_
  static char *data_object_to_tempfile(gpgme_data_t data, FILE **ret_fp)
  {
      int err;
-     char tempfile[_POSIX_PATH_MAX];
+     char tempfile[_POSIX_PATH_MAX], buf[BUFSIZ];
      FILE *fp;
      ssize_t nread = 0;
  
  
      err = ((gpgme_data_seek (data, 0, SEEK_SET) == -1)
             ? gpgme_error_from_errno (errno) : 0);
-     if (!err) {
-         char buf[4096];
-         while ((nread = gpgme_data_read(data, buf, sizeof(buf)))) {
-             if (fwrite (buf, nread, 1, fp) != 1) {
-                 mutt_perror (_("Can't create temporary file"));
-                 m_fclose(&fp);
-                 unlink (tempfile);
-                 return NULL;
-             }
+     if (err) {
+         mutt_perror(_("Can't create temporary file"));
+         goto error;
+     }
+     while ((nread = gpgme_data_read(data, buf, sizeof(buf)))) {
+         if (fwrite (buf, nread, 1, fp) != 1) {
+             mutt_perror (_("Can't create temporary file"));
+             goto error;
          }
      }
  
      if (nread == -1) {
          mutt_error (_("error reading data object: %s\n"), gpgme_strerror (err));
-         unlink (tempfile);
-         m_fclose(&fp);
-         return NULL;
+         goto error;
      }
      if (ret_fp) {
          rewind(fp);
          m_fclose(&fp);
      }
      return m_strdup(tempfile);
+   error:
+     m_fclose(&fp);
+     unlink (tempfile);
+     return NULL;
  }
  
  
@@@ -3400,23 -3402,6 +3402,23 @@@ crypt_ask_for_key(const char *tag, int 
      }
  }
  
 +static char *crypt_hook(address_t *adr)
 +{
 +    char *res = NULL;
 +    lua_State *L = luaM_getruntime();
 +    lua_getglobal(L, "mod_core");             /* push mod_core        1 */
 +    lua_getfield(L, -1, "crypt_hook");        /* push folder_hook()   2 */
 +    if (lua_isfunction(L, -1)) {
 +        lua_pushstring(L, adr->mailbox);
 +        if (!lua_pcall(L, 1, 1, 0)) {
 +            res = m_strdup(lua_tostring(L, -1));
 +        }
 +        lua_pop(L, 1);
 +    }
 +    lua_pop(L, 2);
 +    return res;
 +}
 +
  /* This routine attempts to find the keyids of the recipients of a
     message.  It returns NULL if any of the keys can not be found.  */
  static char *find_keys(ENVELOPE *env, unsigned int app)
      while ((addr = address_list_pop(&lst))) {
          char buf[STRING];
          int forced_valid = 0;
 -        const char *keyID;
 +        char *keyID;
          cryptkey_t *key = NULL;
  
 -        if ((keyID = mutt_crypt_hook(addr))) {
 +        if ((keyID = crypt_hook(addr))) {
              int r;
  
              snprintf(buf, sizeof(buf), _("Use keyID = \"%s\" for %s?"), keyID,
                  address_list_wipe(&lst);
                  address_list_wipe(&addr);
                  buffer_delete(&keylist);
 +                p_delete(&keyID);
                  return NULL;
              }
  
                  }
              }
          }
 +        p_delete(&keyID);
  
          if (!key) {
              key = crypt_getkeybyaddr(addr, KEYFLAG_CANENCRYPT, app, &forced_valid);
diff --combined lib-mx/compress.c
@@@ -38,7 -38,7 +38,7 @@@ static int mbox_lock_compressed (CONTEX
  {
    int r;
  
-   if ((r = mx_lock_file (ctx->realpath, fileno (fp), excl, 1, retry)) == 0)
+   if ((r = mx_lock_file (ctx->realpath, fileno (fp), excl, retry)) == 0)
      ctx->locked = 1;
    else if (retry && !excl) {
      ctx->readonly = 1;
@@@ -52,8 -52,7 +52,7 @@@ static void mbox_unlock_compressed (CON
  {
    if (ctx->locked) {
      fflush (fp);
-     mx_unlock_file (ctx->realpath, fileno (fp), 1);
+     mx_unlock_file(ctx->realpath, fileno(fp));
      ctx->locked = 0;
    }
  }
@@@ -65,24 -64,9 +64,24 @@@ static int is_new (const char *path
  
  static const char *find_compress_hook (int type, const char *path)
  {
 -  const char *c = mutt_find_hook (type, path);
 -
 -  return (!c || !*c) ? NULL : c;
 +  int len = strlen(path);
 +  if (len > 3 && !strcmp(path + len - 3, ".gz")) {
 +      switch (type) {
 +        case M_OPENHOOK:   return "gzip -cd %f > %t";
 +        case M_CLOSEHOOK:  return "gzip -cd %t > %f";
 +        case M_APPENDHOOK: return "gzip -cd %t >> %f";
 +        default: return NULL;
 +      }
 +  }
 +  if (len > 4 && !strcmp(path + len - 4, ".bz2")) {
 +      switch (type) {
 +        case M_OPENHOOK:   return "bzip2 -cd %f > %t";
 +        case M_CLOSEHOOK:  return "bzip2 -cd %t > %f";
 +        case M_APPENDHOOK: return "bzip2 -cd %t >> %f";
 +        default: return NULL;
 +      }
 +  }
 +  return NULL;
  }
  
  int mutt_can_read_compressed (const char *path)
diff --combined lib-mx/mx.c
@@@ -33,6 -33,9 +33,9 @@@
  #include <imap/imap.h>
  #include "pop.h"
  
+ #define MAXLOCKATTEMPT 5
  static mx_t const *mxfmts[] = {
      &mbox_mx,
      &mh_mx,
@@@ -91,132 -94,49 +94,49 @@@ static int mx_get_idx (const char* path
  
  /* Args:
   *    excl            if excl != 0, request an exclusive lock
-  *    dot             if dot != 0, try to dotlock the file
   *    time_out        should retry locking?
   */
- int mx_lock_file (const char *path, int fd, int excl, int dot, int time_out)
+ int mx_lock_file(const char *path, int fd, int excl, int time_out)
  {
- #if defined (USE_FCNTL) || defined (USE_FLOCK)
-     int count;
-     int attempt;
-     struct stat prev_sb;
- #endif
-     int r = 0;
- #ifdef USE_FCNTL
-     struct flock lck;
-     p_clear(&lck, 1);
-     lck.l_type = excl ? F_WRLCK : F_RDLCK;
-     lck.l_whence = SEEK_SET;
-     count = 0;
-     attempt = 0;
-     prev_sb.st_size = 0;
-     while (fcntl (fd, F_SETLK, &lck) == -1) {
-         struct stat sb;
+     int count = 0, attempt = 0;
+     struct flock lck = {
+         .l_type   = excl ? F_WRLCK : F_RDLCK,
+         .l_whence = SEEK_SET,
+     };
  
+     if (dotlock_file(path, time_out) < 0)
+         return -1;
+     while (fcntl(fd, F_SETLK, &lck) == -1) {
          if (errno != EAGAIN && errno != EACCES) {
-             mutt_perror ("fcntl");
-             return (-1);
+             mutt_perror("fcntl");
+             goto error;
          }
  
-         if (fstat (fd, &sb) != 0)
-             sb.st_size = 0;
-         if (count == 0)
-             prev_sb = sb;
-         /* only unlock file if it is unchanged */
-         if (prev_sb.st_size == sb.st_size
-             && ++count >= (time_out ? MAXLOCKATTEMPT : 0)) {
+         if (++count >= (time_out ? MAXLOCKATTEMPT : 0)) {
              if (time_out)
                  mutt_error _("Timeout exceeded while attempting fcntl lock!");
-             return (-1);
-         }
-         prev_sb = sb;
-         mutt_message (_("Waiting for fcntl lock... %d"), ++attempt);
-         mutt_sleep (1);
-     }
- #endif /* USE_FCNTL */
- #ifdef USE_FLOCK
-     count = 0;
-     attempt = 0;
-     while (flock (fd, (excl ? LOCK_EX : LOCK_SH) | LOCK_NB) == -1) {
-         struct stat sb;
-         if (errno != EWOULDBLOCK) {
-             mutt_perror ("flock");
-             r = -1;
-             break;
-         }
-         if (fstat (fd, &sb) != 0)
-             sb.st_size = 0;
-         if (count == 0)
-             prev_sb = sb;
-         /* only unlock file if it is unchanged */
-         if (prev_sb.st_size == sb.st_size
-             && ++count >= (time_out ? MAXLOCKATTEMPT : 0)) {
-             if (time_out)
-                 mutt_error _("Timeout exceeded while attempting flock lock!");
-             r = -1;
-             break;
+             goto error;
          }
-         prev_sb = sb;
-         mutt_message (_("Waiting for flock attempt... %d"), ++attempt);
-         mutt_sleep (1);
+         mutt_message(_("Waiting for fcntl lock... %d"), ++attempt);
+         mutt_sleep(1);
      }
- #endif /* USE_FLOCK */
-     if (r == 0 && dot)
-         r = dotlock_file(path, time_out);
-     if (r == -1) {
-         /* release any other locks obtained in this routine */
- #ifdef USE_FCNTL
-         lck.l_type = F_UNLCK;
-         fcntl (fd, F_SETLK, &lck);
- #endif /* USE_FCNTL */
- #ifdef USE_FLOCK
-         flock (fd, LOCK_UN);
- #endif /* USE_FLOCK */
-         return (-1);
-     }
      return 0;
+   error:
+     undotlock_file(path);
+     return -1;
  }
  
- int mx_unlock_file (const char *path, int fd, int dot)
+ int mx_unlock_file(const char *path, int fd)
  {
- #ifdef USE_FCNTL
      struct flock unlockit;
  
      p_clear(&unlockit, 1);
      unlockit.l_type = F_UNLCK;
      unlockit.l_whence = SEEK_SET;
-     fcntl (fd, F_SETLK, &unlockit);
- #endif
- #ifdef USE_FLOCK
-     flock (fd, LOCK_UN);
- #endif
-     if (dot)
-         undotlock_file (path);
+     fcntl(fd, F_SETLK, &unlockit);
+     undotlock_file(path);
      return 0;
  }
  
@@@ -351,7 -271,7 +271,7 @@@ static int mx_open_mailbox_append (CONT
    case M_MBOX:
      if ((ctx->fp =
           safe_fopen (ctx->path, flags & M_NEWFOLDER ? "w" : "a")) == NULL
-         || mbox_lock_mailbox (ctx, 1, 1) != 0) {
+         || mbox_lock_mailbox(ctx, 1, 1) != 0) {
        if (!ctx->fp)
          mutt_perror (ctx->path);
        else {
@@@ -598,8 -518,16 +518,8 @@@ static int _mx_close_mailbox (CONTEXT 
    }
  
    if (read_msgs && quadoption (OPT_MOVE) != M_NO) {
 -    char *p;
 -
 -    if ((p = mutt_find_hook (M_MBOXHOOK, ctx->path))) {
 -      isSpool = 1;
 -      m_strcpy(mbox, sizeof(mbox), p);
 -    }
 -    else {
 -      m_strcpy(mbox, sizeof(mbox), NONULL(Inbox));
 -      isSpool = mutt_is_spool (ctx->path) && !mutt_is_spool (mbox);
 -    }
 +    m_strcpy(mbox, sizeof(mbox), NONULL(Inbox));
 +    isSpool = mutt_is_spool (ctx->path) && !mutt_is_spool (mbox);
      mutt_expand_path (mbox, sizeof (mbox));
  
      if (isSpool) {