The open_append and close were partially duplicating mbox open_append
and close operations internally.  Change it to call the actual
delegate functions instead.

Inline the open_read() function inside open_mailbox().  Having it
split improved nothing and just complicated the code (i.e. added
error-handling checks in open_mailbox() that in reality could not
fail).


# HG changeset patch
# User Kevin McCarthy <ke...@8t8.us>
# Date 1478734908 28800
#      Wed Nov 09 15:41:48 2016 -0800
# Node ID b08235dfc7855189252702449b298d58a0b1dec6
# Parent  1cc878cc71bf90d5358230075b05029b7c839d77
Compress: add delegate calls to open_append and close mx_ops functions.

The open_append and close were partially duplicating mbox open_append
and close operations internally.  Change it to call the actual
delegate functions instead.

Inline the open_read() function inside open_mailbox().  Having it
split improved nothing and just complicated the code (i.e. added
error-handling checks in open_mailbox() that in reality could not
fail).

diff --git a/compress.c b/compress.c
--- a/compress.c
+++ b/compress.c
@@ -397,105 +397,66 @@
     unlock_mailbox (ctx, fp);
   mutt_unblock_signals();
   safe_fclose (&fp);
 
   return rc;
 }
 
 /**
- * open_read - Open a compressed mailbox for reading
+ * open_mailbox - Open a compressed mailbox
  * @ctx: Mailbox to open
  *
+ * Set up a compressed mailbox to be read.
  * Decompress the mailbox and set up the paths and hooks needed.
- *
- * Note: The message handling will be delegated to the mbox code.
- *
- * Returns:
- *      1: Success
- *      0: Failure
+ * Then determine the type of the mailbox so we can delegate the handling of
+ * messages.
  */
 static int
-open_read (CONTEXT *ctx)
+open_mailbox (CONTEXT *ctx)
 {
-  if (!ctx)
-    return 0;
+  if (!ctx || (ctx->magic != MUTT_COMPRESSED))
+    return -1;
 
   COMPRESS_INFO *ci = set_compress_info (ctx);
   if (!ci)
-  {
-    ctx->magic = 0;
-    return 0;
-  }
+    return -1;
 
   /* If there's no close-hook, or the file isn't writable */
   if (!ci->close || (access (ctx->path, W_OK) != 0))
     ctx->readonly = 1;
 
   setup_paths (ctx);
   store_size (ctx);
 
   int rc = execute_command (ctx, ci->open, 0, _("Decompressing %s"));
   if (rc == 0)
-  {
     goto or_fail;
-  }
 
   ctx->magic = mx_get_magic (ctx->path);
   if (ctx->magic == 0)
   {
     mutt_error (_("Can't identify the contents of the compressed file"));
     goto or_fail;
   }
 
   ci->child_ops = mx_get_ops (ctx->magic);
   if (!ci->child_ops)
   {
     mutt_error (_("Can't find mailbox ops for mailbox type %d"), ctx->magic);
     goto or_fail;
   }
 
-  return 1;
+  return ci->child_ops->open (ctx);
 
 or_fail:
   /* remove the partial uncompressed file */
   remove (ctx->path);
   mutt_free_compress_info (ctx);
-  return 0;
-}
-
-
-/**
- * open_mailbox - Open a compressed mailbox
- * @ctx: Mailbox to open
- *
- * Set up a compressed mailbox to be read.
- * First call open_read() to decompress the file.
- * Then determine the type of the mailbox so we can delegate the handling of
- * messages.
- */
-static int
-open_mailbox (CONTEXT *ctx)
-{
-  if (!ctx || (ctx->magic != MUTT_COMPRESSED))
-    return 1;
-
-  if (!open_read (ctx))
-    return 1;
-
-  COMPRESS_INFO *ci = ctx->compress_info;
-  if (!ci)
-    return 1;
-
-  struct mx_ops *ops = ci->child_ops;
-  if (!ops)
-    return 1;
-
-  /* Delegate */
-  return ops->open (ctx);
+  return -1;
 }
 
 /**
  * open_append_mailbox - Open a compressed mailbox for appending
  * @ctx:   Mailbox to open
  * @flags: e.g. Does the file already exist?
  *
  * To append to a compressed mailbox we need an append-hook (or both open- and
@@ -533,43 +494,30 @@
   ctx->mx_ops = &mx_comp_ops;
   ci->child_ops = mx_get_ops (ctx->magic);
   if (!ci->child_ops)
   {
     mutt_error (_("Can't find mailbox ops for mailbox type %d"), ctx->magic);
     goto oa_fail2;
   }
 
-  if (ci->append || (access (ctx->realpath, F_OK) != 0))
+  /* Open the existing mailbox, unless we are appending */
+  if (!ci->append && (access (ctx->realpath, F_OK) == 0))
   {
-    /* Create an empty temporary file */
-    ctx->fp = safe_fopen (ctx->path, "w");
-    if (!ctx->fp)
-    {
-      mutt_perror (ctx->path);
-      goto oa_fail2;
-    }
-  }
-  else
-  {
-    /* Open the existing mailbox */
     int rc = execute_command (ctx, ci->open, 0, _("Decompressing %s"));
     if (rc == 0)
     {
       mutt_error (_("Compress command failed: %s"), ci->open);
       goto oa_fail2;
     }
-    ctx->fp = safe_fopen (ctx->path, "a");
-    if (!ctx->fp)
-    {
-      mutt_perror (ctx->path);
-      goto oa_fail2;
-    }
   }
 
+  if (ci->child_ops->open_append (ctx, flags) != 0)
+    goto oa_fail2;
+
   return 0;
 
 oa_fail2:
   /* remove the partial uncompressed file */
   remove (ctx->path);
 oa_fail1:
   /* Free the compress_info to prevent close from trying to recompress */
   mutt_free_compress_info (ctx);
@@ -593,17 +541,21 @@
 {
   if (!ctx)
     return -1;
 
   COMPRESS_INFO *ci = ctx->compress_info;
   if (!ci)
     return -1;
 
-  safe_fclose (&ctx->fp);
+  struct mx_ops *ops = ci->child_ops;
+  if (!ops)
+    return -1;
+
+  ops->close (ctx);
 
   /* sync has already been called, so we only need to delete some files */
   if (!ctx->append)
   {
     /* If the file was removed, remove the compressed folder too */
     if ((access (ctx->path, F_OK) != 0) && !option (OPTSAVEEMPTY))
     {
       remove (ctx->realpath);

Reply via email to