[RFC PATCH 12/13] Close files using notmuch_mailstore_close instead of fclose

Ethan Glasser-Camp glasse at cs.rpi.edu
Wed Feb 15 14:02:05 PST 2012


From: Ethan Glasser-Camp <ethan at betacantrips.com>

This requires a little bit of juggling in lib/sha1.c. Wrapper
functions provide the FILE*. Instead of closing the file immediately
ourselves, we let the wrapper functions close it.

Signed-off-by: Ethan Glasser-Camp <ethan at betacantrips.com>
---
 lib/message-file.c |    6 ++++--
 lib/sha1.c         |   14 ++++++++------
 mime-node.c        |    4 +++-
 notmuch-show.c     |    8 ++++----
 4 files changed, 19 insertions(+), 13 deletions(-)

diff --git a/lib/message-file.c b/lib/message-file.c
index 61f4d04..51c77f9 100644
--- a/lib/message-file.c
+++ b/lib/message-file.c
@@ -34,6 +34,7 @@ typedef struct {
 
 struct _notmuch_message_file {
     /* File object */
+    notmuch_mailstore_t *mailstore;
     FILE *file;
 
     /* Header storage */
@@ -86,7 +87,7 @@ _notmuch_message_file_destructor (notmuch_message_file_t *message)
 	g_hash_table_destroy (message->headers);
 
     if (message->file)
-	fclose (message->file);
+	notmuch_mailstore_close (message->mailstore, message->file);
 
     return 0;
 }
@@ -105,6 +106,7 @@ _notmuch_message_file_open_ctx (void *ctx, notmuch_mailstore_t *mailstore,
 
     talloc_set_destructor (message, _notmuch_message_file_destructor);
 
+    message->mailstore = mailstore;
     message->file = notmuch_mailstore_open (mailstore, filename);
     if (message->file == NULL)
 	goto FAIL;
@@ -363,7 +365,7 @@ notmuch_message_file_get_header (notmuch_message_file_t *message,
     }
 
     if (message->parsing_finished) {
-        fclose (message->file);
+	notmuch_mailstore_close (message->mailstore, message->file);
         message->file = NULL;
     }
 
diff --git a/lib/sha1.c b/lib/sha1.c
index ea25999..4266720 100644
--- a/lib/sha1.c
+++ b/lib/sha1.c
@@ -87,7 +87,6 @@ _notmuch_sha1_of_filep (FILE *file)
 	    if (feof (file)) {
 		break;
 	    } else if (ferror (file)) {
-		fclose (file);
 		return NULL;
 	    }
 	} else {
@@ -99,8 +98,6 @@ _notmuch_sha1_of_filep (FILE *file)
 
     result = _hex_of_sha1_digest (digest);
 
-    fclose (file);
-
     return result;
 }
 
@@ -117,9 +114,12 @@ char *
 notmuch_sha1_of_file (const char *filename)
 {
     FILE *file;
+    char *ret;
 
     file = fopen (filename, "r");
-    return _notmuch_sha1_of_filep (file);
+    ret = _notmuch_sha1_of_filep (file);
+    fclose (file);
+    return ret;
 }
 
 /* Create a hexadecimal string version of the SHA-1 digest of the
@@ -135,7 +135,9 @@ char *
 notmuch_sha1_of_message (notmuch_mailstore_t *mailstore, const char *filename)
 {
     FILE *file;
-
+    char *ret;
     file = notmuch_mailstore_open (mailstore, filename);
-    return _notmuch_sha1_of_filep (file);
+    ret = _notmuch_sha1_of_filep (file);
+    notmuch_mailstore_close (mailstore, file);
+    return ret;
 }
diff --git a/mime-node.c b/mime-node.c
index 856fc3b..db37189 100644
--- a/mime-node.c
+++ b/mime-node.c
@@ -27,6 +27,7 @@
 typedef struct mime_node_context {
     /* Per-message resources.  These are allocated internally and must
      * be destroyed. */
+    notmuch_mailstore_t *mailstore;
     FILE *file;
     GMimeStream *stream;
     GMimeParser *parser;
@@ -54,7 +55,7 @@ _mime_node_context_free (mime_node_context_t *res)
 	g_object_unref (res->stream);
 
     if (res->file)
-	fclose (res->file);
+	notmuch_mailstore_close (res->mailstore, res->file);
 
     return 0;
 }
@@ -90,6 +91,7 @@ mime_node_open (const void *ctx, notmuch_message_t *message,
     }
     talloc_set_destructor (mctx, _mime_node_context_free);
 
+    mctx->mailstore = mailstore;
     mctx->file = notmuch_mailstore_open (mailstore, filename);
     if (! mctx->file) {
 	fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
diff --git a/notmuch-show.c b/notmuch-show.c
index 0d2a246..2e8e4c9 100644
--- a/notmuch-show.c
+++ b/notmuch-show.c
@@ -309,7 +309,7 @@ format_message_mbox (const void *ctx,
 
     printf ("\n");
 
-    fclose (file);
+    notmuch_mailstore_close (mailstore, file);
 }
 
 static void
@@ -966,18 +966,18 @@ do_show_single (void *ctx,
 	    size = fread (buf, 1, sizeof (buf), file);
 	    if (ferror (file)) {
 		fprintf (stderr, "Error: Read failed from %s\n", filename);
-		fclose (file);
+		notmuch_mailstore_close (mailstore, file);
 		return 1;
 	    }
 
 	    if (fwrite (buf, size, 1, stdout) != 1) {
 		fprintf (stderr, "Error: Write failed\n");
-		fclose (file);
+		notmuch_mailstore_close (mailstore, file);
 		return 1;
 	    }
 	}
 
-	fclose (file);
+	notmuch_mailstore_close (mailstore, file);
 
     } else {
 
-- 
1.7.5.4



More information about the notmuch mailing list