organize module
[apps/madmutt.git] / lib-mx / compress.c
index 1684dd3..ec4db2b 100644 (file)
@@ -12,7 +12,7 @@
 #include <lib-sys/mutt_signal.h>
 #include <lib-sys/unix.h>
 
-#include <lib-ui/curses.h>
+#include <lib-ui/lib-ui.h>
 
 #include "mutt.h"
 
@@ -38,7 +38,7 @@ static int mbox_lock_compressed (CONTEXT * ctx, FILE * fp, int excl, int retry)
 {
   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,22 +52,36 @@ static void mbox_unlock_compressed (CONTEXT * ctx, FILE * fp)
 {
   if (ctx->locked) {
     fflush (fp);
-
-    mx_unlock_file (ctx->realpath, fileno (fp), 1);
+    mx_unlock_file(ctx->realpath, fileno(fp));
     ctx->locked = 0;
   }
 }
 
 static int is_new (const char *path)
 {
-  return (access (path, W_OK) && errno == ENOENT);
+  return access (path, W_OK) && errno == ENOENT;
 }
 
 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)
@@ -88,7 +102,7 @@ int mutt_can_append_compressed (const char *path)
   int magic;
 
   if (is_new (path))
-    return (find_compress_hook (M_CLOSEHOOK, path) ? 1 : 0);
+    return find_compress_hook (M_CLOSEHOOK, path) ? 1 : 0;
 
   magic = mx_get_magic (path);
 
@@ -196,11 +210,11 @@ int mutt_open_read_compressed(CONTEXT * ctx)
   ctx->realpath = ctx->path;
 
   /* Uncompress to /tmp */
-  tmpfd = m_tempfd(tmppath, sizeof(tmppath), NONULL(Tempdir), NULL);
+  tmpfd = m_tempfd(tmppath, sizeof(tmppath), NONULL(mod_core.tmpdir), NULL);
   /* If we cannot open tempfile, that means the file already exists (!?)
    * or we are following a symlink, which is bad and insecure.
    */
-  if(!tmpfd) {
+  if(tmpfd < 0) {
       return -1;
   }
   close(tmpfd);
@@ -266,7 +280,7 @@ static void restore_path (CONTEXT * ctx)
 /* remove the temporary mailbox */
 static void remove_file (CONTEXT * ctx)
 {
-  if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
+  if (ctx->magic == M_MBOX)
     remove (ctx->path);
 }
 
@@ -289,20 +303,15 @@ int mutt_open_append_compressed (CONTEXT * ctx)
   ctx->realpath = ctx->path;
 
   /* Uncompress to /tmp */
-  fh = m_tempfile(tmppath, sizeof(tmppath), NONULL(Tempdir), NULL);
+  fh = m_tempfile(tmppath, sizeof(tmppath), NONULL(mod_core.tmpdir), NULL);
   m_fclose(&fh);
 
   ctx->path = p_dupstr(tmppath, m_strlen(tmppath));
-
-  ctx->magic = DefaultMagic;
-
-  if (is_new (ctx->realpath) || 
-      (ctx->magic != M_MBOX &&
-       ctx->magic != M_MMDF))
+  ctx->magic = M_MBOX;
+  if (is_new (ctx->realpath) || ctx->magic != M_MBOX)
       unlink(tmppath);
 
   /* No error checking - the parent function will catch it */
-
   return 0;
 }
 
@@ -313,11 +322,7 @@ void mutt_fast_close_compressed (CONTEXT * ctx)
     m_fclose(&ctx->fp);
 
     /* if the folder was removed, remove the gzipped folder too */
-    if (access (ctx->path, F_OK) != 0 && !option (OPTSAVEEMPTY))
-      remove (ctx->realpath);
-    else
-      remove_file (ctx);
-
+    remove_file (ctx);
     restore_path (ctx);
     p_delete(&ctx->cinfo);
   }
@@ -465,4 +470,5 @@ mx_t const compress_mx = {
     NULL,
     NULL,
     NULL,
+    NULL,
 };