rework compress_info type a bit.
[apps/madmutt.git] / compress.c
index 1ac8355..3770fef 100644 (file)
@@ -1,40 +1,31 @@
 /*
+ * Copyright notice from original mutt:
  * Copyright (C) 1997 Alain Penders <Alain@Finale-Dev.com>
  *
- *     This program is free software; you can redistribute it and/or modify
- *     it under the terms of the GNU General Public License as published by
- *     the Free Software Foundation; either version 2 of the License, or
- *     (at your option) any later version.
- *
- *     This program is distributed in the hope that it will be useful,
- *     but WITHOUT ANY WARRANTY; without even the implied warranty of
- *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *     GNU General Public License for more details.
- *
- *     You should have received a copy of the GNU General Public License
- *     along with this program; if not, write to the Free Software
- *     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * This file is part of mutt-ng, see http://www.muttng.org/.
+ * It's licensed under the GNU General Public License,
+ * please see the file GPL in the top level source directory.
  */
 
-#include "mutt.h"
+#include <lib-lib/lib-lib.h>
 
-#ifdef USE_COMPRESSED
+#include <lib-sys/mutt_signal.h>
+#include <lib-sys/unix.h>
 
-#include "mx.h"
-#include "mailbox.h"
-#include "mutt_curses.h"
+#include <lib-ui/curses.h>
+
+#include "mutt.h"
 
-#include <errno.h>
-#include <string.h>
-#include <unistd.h>
-#include <sys/stat.h>
+#include "mx.h"
+#include "mbox.h"
+#include "compress.h"
 
-typedef struct {
+struct compress_info {
   const char *close;            /* close-hook  command */
   const char *open;             /* open-hook   command */
   const char *append;           /* append-hook command */
   off_t size;                   /* size of real folder */
-} COMPRESS_INFO;
+};
 
 char echo_cmd[HUGE_STRING];
 
@@ -43,7 +34,7 @@ char echo_cmd[HUGE_STRING];
  * excl - exclusive lock?
  * retry - should retry if unable to lock?
  */
-int mbox_lock_compressed (CONTEXT * ctx, FILE * fp, int excl, int retry)
+static int mbox_lock_compressed (CONTEXT * ctx, FILE * fp, int excl, int retry)
 {
   int r;
 
@@ -57,7 +48,7 @@ int mbox_lock_compressed (CONTEXT * ctx, FILE * fp, int excl, int retry)
   return (r);
 }
 
-void mbox_unlock_compressed (CONTEXT * ctx, FILE * fp)
+static void mbox_unlock_compressed (CONTEXT * ctx, FILE * fp)
 {
   if (ctx->locked) {
     fflush (fp);
@@ -89,9 +80,7 @@ int mutt_can_read_compressed (const char *path)
  */
 static const char *get_append_command (const char *path, const CONTEXT * ctx)
 {
-  COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
-
-  return (is_new (path)) ? ci->close : ci->append;
+  return is_new(path) ? ctx->cinfo->close : ctx->cinfo->append;
 }
 
 int mutt_can_append_compressed (const char *path)
@@ -112,17 +101,16 @@ int mutt_can_append_compressed (const char *path)
 }
 
 /* open a compressed mailbox */
-static COMPRESS_INFO *set_compress_info (CONTEXT * ctx)
+static compress_info *set_compress_info (CONTEXT * ctx)
 {
-  COMPRESS_INFO *ci;
+  compress_info *ci = p_new(compress_info, 1);
 
   /* Now lets uncompress this thing */
-  ci = safe_malloc (sizeof (COMPRESS_INFO));
-  ctx->compressinfo = (void *) ci;
   ci->append = find_compress_hook (M_APPENDHOOK, ctx->path);
   ci->open = find_compress_hook (M_OPENHOOK, ctx->path);
   ci->close = find_compress_hook (M_CLOSEHOOK, ctx->path);
-  return ci;
+
+  return (ctx->cinfo = ci);
 }
 
 static void set_path (CONTEXT * ctx)
@@ -134,8 +122,7 @@ static void set_path (CONTEXT * ctx)
 
   /* Uncompress to /tmp */
   mutt_mktemp (tmppath);
-  ctx->path = safe_malloc (strlen (tmppath) + 1);
-  strcpy (ctx->path, tmppath);
+  ctx->path = p_dupstr(tmppath, m_strlen(tmppath));
 }
 
 static int get_size (const char *path)
@@ -147,20 +134,13 @@ static int get_size (const char *path)
   return (sb.st_size);
 }
 
-static void store_size (CONTEXT * ctx)
-{
-  COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
-
-  ci->size = get_size (ctx->realpath);
-}
-
-static const char *compresshook_format_str (char *dest, size_t destlen,
+static const char *compresshook_format_str (char *dest, ssize_t destlen,
                                             char op, const char *src,
                                             const char *fmt,
-                                            const char *ifstring,
-                                            const char *elsestring,
+                                            const char *ifstring __attribute__ ((unused)),
+                                            const char *elsestring __attribute__ ((unused)),
                                             unsigned long data,
-                                            format_flag flags)
+                                            format_flag flags __attribute__ ((unused)))
 {
   char tmp[SHORT_STRING];
 
@@ -193,16 +173,14 @@ static char *get_compression_cmd (const char *cmd, const CONTEXT * ctx)
 
   mutt_FormatString (expanded, sizeof (expanded), cmd,
                      compresshook_format_str, (unsigned long) ctx, 0);
-  return safe_strdup (expanded);
+  return m_strdup(expanded);
 }
 
 int mutt_check_mailbox_compressed (CONTEXT * ctx)
 {
-  COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
-
-  if (ci->size != get_size (ctx->realpath)) {
-    FREE (&ctx->compressinfo);
-    FREE (&ctx->realpath);
+  if (ctx->cinfo->size != get_size (ctx->realpath)) {
+    p_delete(&ctx->cinfo);
+    p_delete(&ctx->realpath);
     mutt_error _("Mailbox was corrupted!");
 
     return (-1);
@@ -216,18 +194,18 @@ int mutt_open_read_compressed (CONTEXT * ctx)
   FILE *fp;
   int rc;
 
-  COMPRESS_INFO *ci = set_compress_info (ctx);
+  compress_info *ci = set_compress_info (ctx);
 
   if (!ci->open) {
     ctx->magic = 0;
-    FREE (ctx->compressinfo);
+    p_delete(&ctx->cinfo);
     return (-1);
   }
   if (!ci->close || access (ctx->path, W_OK) != 0)
     ctx->readonly = 1;
 
   set_path (ctx);
-  store_size (ctx);
+  ctx->cinfo->size = get_size(ctx->realpath);
 
   if (!ctx->quiet)
     mutt_message (_("Decompressing %s..."), ctx->realpath);
@@ -235,11 +213,10 @@ int mutt_open_read_compressed (CONTEXT * ctx)
   cmd = get_compression_cmd (ci->open, ctx);
   if (cmd == NULL)
     return (-1);
-  dprint (2, (debugfile, "DecompressCmd: '%s'\n", cmd));
 
   if ((fp = fopen (ctx->realpath, "r")) == NULL) {
     mutt_perror (ctx->realpath);
-    FREE (&cmd);
+    p_delete(&cmd);
     return (-1);
   }
   mutt_block_signals ();
@@ -248,7 +225,7 @@ int mutt_open_read_compressed (CONTEXT * ctx)
     mutt_unblock_signals ();
     mutt_error _("Unable to lock mailbox!");
 
-    FREE (&cmd);
+    p_delete(&cmd);
     return (-1);
   }
 
@@ -264,11 +241,10 @@ int mutt_open_read_compressed (CONTEXT * ctx)
   if (rc) {
     mutt_any_key_to_continue (NULL);
     ctx->magic = 0;
-    FREE (ctx->compressinfo);
-    mutt_error (_("Error executing: %s : unable to open the mailbox!\n"),
-                cmd);
+    p_delete(&ctx->cinfo);
+    mutt_error(_("Error executing: %s : unable to open the mailbox!\n"), cmd);
   }
-  FREE (&cmd);
+  p_delete(&cmd);
   if (rc)
     return (-1);
 
@@ -280,14 +256,14 @@ int mutt_open_read_compressed (CONTEXT * ctx)
   return (0);
 }
 
-void restore_path (CONTEXT * ctx)
+static void restore_path (CONTEXT * ctx)
 {
-  FREE (&ctx->path);
+  p_delete(&ctx->path);
   ctx->path = ctx->realpath;
 }
 
 /* remove the temporary mailbox */
-void remove_file (CONTEXT * ctx)
+static void remove_file (CONTEXT * ctx)
 {
   if (ctx->magic == M_MBOX || ctx->magic == M_MMDF)
     remove (ctx->path);
@@ -296,14 +272,14 @@ void remove_file (CONTEXT * ctx)
 int mutt_open_append_compressed (CONTEXT * ctx)
 {
   FILE *fh;
-  COMPRESS_INFO *ci = set_compress_info (ctx);
+  compress_info *ci = set_compress_info (ctx);
 
   if (!get_append_command (ctx->path, ctx)) {
     if (ci->open && ci->close)
       return (mutt_open_read_compressed (ctx));
 
     ctx->magic = 0;
-    FREE (&ctx->compressinfo);
+    p_delete(&ctx->cinfo);
     return (-1);
   }
 
@@ -323,10 +299,7 @@ int mutt_open_append_compressed (CONTEXT * ctx)
 /* close a compressed mailbox */
 void mutt_fast_close_compressed (CONTEXT * ctx)
 {
-  dprint (2, (debugfile, "mutt_fast_close_compressed called on '%s'\n",
-              ctx->path));
-
-  if (ctx->compressinfo) {
+  if (ctx->cinfo) {
     if (ctx->fp)
       fclose (ctx->fp);
     ctx->fp = NULL;
@@ -337,7 +310,7 @@ void mutt_fast_close_compressed (CONTEXT * ctx)
       remove_file (ctx);
 
     restore_path (ctx);
-    FREE (&ctx->compressinfo);
+    p_delete(&ctx->cinfo);
   }
 }
 
@@ -347,18 +320,17 @@ int mutt_sync_compressed (CONTEXT * ctx)
   char *cmd;
   int rc = 0;
   FILE *fp;
-  COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
 
   if (!ctx->quiet)
     mutt_message (_("Compressing %s..."), ctx->realpath);
 
-  cmd = get_compression_cmd (ci->close, ctx);
+  cmd = get_compression_cmd (ctx->cinfo->close, ctx);
   if (cmd == NULL)
     return (-1);
 
   if ((fp = fopen (ctx->realpath, "a")) == NULL) {
     mutt_perror (ctx->realpath);
-    FREE (&cmd);
+    p_delete(&cmd);
     return (-1);
   }
   mutt_block_signals ();
@@ -367,14 +339,12 @@ int mutt_sync_compressed (CONTEXT * ctx)
     mutt_unblock_signals ();
     mutt_error _("Unable to lock mailbox!");
 
-    store_size (ctx);
+    ctx->cinfo->size = get_size(ctx->realpath);
 
-    FREE (&cmd);
+    p_delete(&cmd);
     return (-1);
   }
 
-  dprint (2, (debugfile, "CompressCommand: '%s'\n", cmd));
-
   endwin ();
   fflush (stdout);
   sprintf (echo_cmd, _("echo Compressing %s..."), ctx->realpath);
@@ -391,9 +361,9 @@ int mutt_sync_compressed (CONTEXT * ctx)
   mutt_unblock_signals ();
   fclose (fp);
 
-  FREE (&cmd);
+  p_delete(&cmd);
 
-  store_size (ctx);
+  ctx->cinfo->size = get_size(ctx->realpath);
 
   return (rc);
 }
@@ -403,16 +373,12 @@ int mutt_slow_close_compressed (CONTEXT * ctx)
   FILE *fp;
   const char *append;
   char *cmd;
-  COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
-
-  dprint (2, (debugfile, "mutt_slow_close_compressed called on '%s'\n",
-              ctx->path));
+  compress_info *ci = ctx->cinfo;
 
   if (!(ctx->append && ((append = get_append_command (ctx->realpath, ctx))
-                        || (append = ci->close)))) {    /* if we can not or should not append,
-                                                         * we only have to remove the compressed info, because sync was already
-                                                         * called 
-                                                         */
+                        || (append = ci->close)))) {
+      /* if we can not or should not append, we only have to remove the
+         compressed info, because sync was already called */
     mutt_fast_close_compressed (ctx);
     return (0);
   }
@@ -434,7 +400,7 @@ int mutt_slow_close_compressed (CONTEXT * ctx)
 
   if ((fp = fopen (ctx->realpath, "a")) == NULL) {
     mutt_perror (ctx->realpath);
-    FREE (&cmd);
+    p_delete(&cmd);
     return (-1);
   }
   mutt_block_signals ();
@@ -443,12 +409,10 @@ int mutt_slow_close_compressed (CONTEXT * ctx)
     mutt_unblock_signals ();
     mutt_error _("Unable to lock mailbox!");
 
-    FREE (&cmd);
+    p_delete(&cmd);
     return (-1);
   }
 
-  dprint (2, (debugfile, "CompressCmd: '%s'\n", cmd));
-
   endwin ();
   fflush (stdout);
 
@@ -461,10 +425,9 @@ int mutt_slow_close_compressed (CONTEXT * ctx)
 
   if (mutt_system (cmd)) {
     mutt_any_key_to_continue (NULL);
-    mutt_error (_
-                (" %s: Error compressing mailbox!  Uncompressed one kept!\n"),
+    mutt_error (_(" %s: Error compressing mailbox!  Uncompressed one kept!\n"),
                 ctx->path);
-    FREE (&cmd);
+    p_delete(&cmd);
     mbox_unlock_compressed (ctx, fp);
     mutt_unblock_signals ();
     fclose (fp);
@@ -476,10 +439,23 @@ int mutt_slow_close_compressed (CONTEXT * ctx)
   fclose (fp);
   remove_file (ctx);
   restore_path (ctx);
-  FREE (&cmd);
-  FREE (&ctx->compressinfo);
+  p_delete(&cmd);
+  p_delete(&ctx->cinfo);
 
   return (0);
 }
 
-#endif /* USE_COMPRESSED */
+mx_t const compress_mx = {
+    M_COMPRESSED,
+    1,
+    mbox_is_magic,
+    mbox_check_empty,
+    access,
+    mutt_open_read_compressed,
+    NULL,
+    NULL,
+    NULL,
+    NULL,
+    NULL,
+    NULL,
+};