rework compress_info type a bit.
[apps/madmutt.git] / compress.c
index f4b8b67..3770fef 100644 (file)
 #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];
 
@@ -80,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)
@@ -103,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 = p_new(COMPRESS_INFO, 1);
-  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)
@@ -137,13 +134,6 @@ 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, ssize_t destlen,
                                             char op, const char *src,
                                             const char *fmt,
@@ -188,10 +178,8 @@ static char *get_compression_cmd (const char *cmd, const CONTEXT * ctx)
 
 int mutt_check_mailbox_compressed (CONTEXT * ctx)
 {
-  COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
-
-  if (ci->size != get_size (ctx->realpath)) {
-    p_delete(&ctx->compressinfo);
+  if (ctx->cinfo->size != get_size (ctx->realpath)) {
+    p_delete(&ctx->cinfo);
     p_delete(&ctx->realpath);
     mutt_error _("Mailbox was corrupted!");
 
@@ -206,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;
-    p_delete(&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);
@@ -253,9 +241,8 @@ int mutt_open_read_compressed (CONTEXT * ctx)
   if (rc) {
     mutt_any_key_to_continue (NULL);
     ctx->magic = 0;
-    p_delete(&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);
   }
   p_delete(&cmd);
   if (rc)
@@ -285,14 +272,14 @@ static 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;
-    p_delete(&ctx->compressinfo);
+    p_delete(&ctx->cinfo);
     return (-1);
   }
 
@@ -312,7 +299,7 @@ int mutt_open_append_compressed (CONTEXT * ctx)
 /* close a compressed mailbox */
 void mutt_fast_close_compressed (CONTEXT * ctx)
 {
-  if (ctx->compressinfo) {
+  if (ctx->cinfo) {
     if (ctx->fp)
       fclose (ctx->fp);
     ctx->fp = NULL;
@@ -323,7 +310,7 @@ void mutt_fast_close_compressed (CONTEXT * ctx)
       remove_file (ctx);
 
     restore_path (ctx);
-    p_delete(&ctx->compressinfo);
+    p_delete(&ctx->cinfo);
   }
 }
 
@@ -333,12 +320,11 @@ 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);
 
@@ -353,7 +339,7 @@ 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);
 
     p_delete(&cmd);
     return (-1);
@@ -377,7 +363,7 @@ int mutt_sync_compressed (CONTEXT * ctx)
 
   p_delete(&cmd);
 
-  store_size (ctx);
+  ctx->cinfo->size = get_size(ctx->realpath);
 
   return (rc);
 }
@@ -387,13 +373,12 @@ int mutt_slow_close_compressed (CONTEXT * ctx)
   FILE *fp;
   const char *append;
   char *cmd;
-  COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
+  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);
   }
@@ -440,8 +425,7 @@ 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);
     p_delete(&cmd);
     mbox_unlock_compressed (ctx, fp);
@@ -456,7 +440,7 @@ int mutt_slow_close_compressed (CONTEXT * ctx)
   remove_file (ctx);
   restore_path (ctx);
   p_delete(&cmd);
-  p_delete(&ctx->compressinfo);
+  p_delete(&ctx->cinfo);
 
   return (0);
 }