[gmime] Added GError arg to g_mime_stream_[file,fs]_open()



commit 08c5739f0ad1773a143ddccc6a426e81c06a7c7b
Author: Jeffrey Stedfast <jestedfa microsoft com>
Date:   Fri Mar 17 14:33:10 2017 -0400

    Added GError arg to g_mime_stream_[file,fs]_open()

 PORTING                         |    4 +-
 docs/reference/changes-3.0.sgml |    4 +-
 gmime/gmime-events.c            |   79 ++++++++++++++++++++-------------------
 gmime/gmime-stream-file.c       |    8 +++-
 gmime/gmime-stream-file.h       |    2 +-
 gmime/gmime-stream-fs.c         |    8 +++-
 gmime/gmime-stream-fs.h         |    2 +-
 tests/test-mbox.c               |    6 +-
 tests/test-partial.c            |    6 +-
 9 files changed, 65 insertions(+), 54 deletions(-)
---
diff --git a/PORTING b/PORTING
index dcea2ea..26221bd 100644
--- a/PORTING
+++ b/PORTING
@@ -115,10 +115,10 @@ Porting from GMime 2.6 to GMime 3.0
   single bitfield (GMimeSignatureStatus) which mirrors gpgme_sigsum_t.
 
 - g_mime_stream_file_new_for_path() has been renamed to
-  g_mime_stream_file_open().
+  g_mime_stream_file_open() and now also takes a GError argument.
 
 - g_mime_stream_fs_new_for_path() has been renamed to
-  g_mime_stream_fs_open().
+  g_mime_stream_fs_open() and now also takes a GError argument.
 
 - g_mime_part_new() now returns a GMimePart with a Content-Type of
   "application/octet-stream" instead of "text/plain" since there is
diff --git a/docs/reference/changes-3.0.sgml b/docs/reference/changes-3.0.sgml
index ce47eef..9a774ff 100644
--- a/docs/reference/changes-3.0.sgml
+++ b/docs/reference/changes-3.0.sgml
@@ -50,8 +50,8 @@
       <listitem><para><function>internet_address_list_parse_string()</function> has been replaced by 
<function>internet_address_list_parse()</function> and now takes a GMimeParserOptions 
argument.</para></listitem>
       <listitem><para>GMimeHeaderIter has been dropped in favour of a more direct way of iterating over a 
GMimeHeaderList using int indexes.</para></listitem>
       <listitem><para><function>g_mime_stream_write_to_stream()</function>, 
<function>g_mime_stream_writev()</function>, and <function>g_mime_stream_printf()</function> now return a 
gint64.</para></listitem>
-      <listitem><para><function>g_mime_stream_file_new_for_path()</function> has been renamed to 
<function>g_mime_stream_file_open()</function>.</para></listitem>
-      <listitem><para><function>g_mime_stream_fs_new_for_path()</function> has been renamed to 
<function>g_mime_stream_fs_open()</function>.</para></listitem>
+      <listitem><para><function>g_mime_stream_file_new_for_path()</function> has been renamed to 
<function>g_mime_stream_file_open()</function> and now also takes a GError argument.</para></listitem>
+      <listitem><para><function>g_mime_stream_fs_new_for_path()</function> has been renamed to 
<function>g_mime_stream_fs_open()</function> and now also takes a GError argument.</para></listitem>
       <listitem><para><function>g_mime_part_new()</function> now returns a GMimePart with a Content-Type of 
"application/octet-stream" instead of "text/plain" since there is now a GMimeTextPart who's 
<function>g_mime_text_part_new()</function> returns a GMimeTextPart with a Content-Type of 
"text/plain".</para></listitem>
       <listitem><para><function>g_mime_part_get_content_object()</function> has been renamed to 
<function>g_mime_part_get_content()</function>.</para></listitem>
       <listitem><para><function>g_mime_part_set_content_object()</function> has been renamed to 
<function>g_mime_part_set_content()</function>.</para></listitem>
diff --git a/gmime/gmime-events.c b/gmime/gmime-events.c
index e1a6935..49820a9 100644
--- a/gmime/gmime-events.c
+++ b/gmime/gmime-events.c
@@ -24,11 +24,8 @@
 #endif
 
 #include "gmime-events.h"
-#include "list.h"
 
 typedef struct _EventListener {
-       struct _EventListener *next;
-       struct _EventListener *prev;
        GMimeEventCallback callback;
        gpointer user_data;
        int blocked;
@@ -42,8 +39,6 @@ event_listener_new (GMimeEventCallback callback, gpointer user_data)
        listener = g_slice_new (EventListener);
        listener->user_data = user_data;
        listener->callback = callback;
-       listener->prev = NULL;
-       listener->next = NULL;
        listener->blocked = 0;
        
        return listener;
@@ -57,8 +52,8 @@ event_listener_free (EventListener *listener)
 
 
 struct _GMimeEvent {
+       GPtrArray *array;
        gpointer owner;
-       List list;
 };
 
 
@@ -76,7 +71,7 @@ g_mime_event_new (gpointer owner)
        GMimeEvent *event;
        
        event = g_slice_new (GMimeEvent);
-       list_init (&event->list);
+       event->array = g_ptr_array_new ();
        event->owner = owner;
        
        return event;
@@ -92,32 +87,29 @@ g_mime_event_new (gpointer owner)
 void
 g_mime_event_free (GMimeEvent *event)
 {
-       EventListener *node, *next;
+       guint i;
        
-       node = (EventListener *) event->list.head;
-       while (node->next) {
-               next = node->next;
-               event_listener_free (node);
-               node = next;
-       }
+       for (i = 0; i < event->array->len; i++)
+               event_listener_free (event->array->pdata[i]);
+       g_ptr_array_free (event->array, TRUE);
        
        g_slice_free (GMimeEvent, event);
 }
 
 
-static EventListener *
-g_mime_event_find_listener (GMimeEvent *event, GMimeEventCallback callback, gpointer user_data)
+static int
+g_mime_event_index_of (GMimeEvent *event, GMimeEventCallback callback, gpointer user_data)
 {
-       EventListener *node;
+       EventListener *listener;
+       int i;
        
-       node = (EventListener *) event->list.head;
-       while (node->next) {
-               if (node->callback == callback && node->user_data == user_data)
-                       return node;
-               node = node->next;
+       for (i = 0; i < event->array->len; i++) {
+               listener = (EventListener *) event->array->pdata[i];
+               if (listener->callback == callback && listener->user_data == user_data)
+                       return i;
        }
        
-       return NULL;
+       return -1;
 }
 
 
@@ -133,9 +125,13 @@ void
 g_mime_event_block (GMimeEvent *event, GMimeEventCallback callback, gpointer user_data)
 {
        EventListener *listener;
+       int index;
+       
+       if ((index = g_mime_event_index_of (event, callback, user_data)) == -1)
+               return;
        
-       if ((listener = g_mime_event_find_listener (event, callback, user_data)))
-               listener->blocked++;
+       listener = (EventListener *) event->array->pdata[index];
+       listener->blocked++;
 }
 
 
@@ -152,9 +148,13 @@ void
 g_mime_event_unblock (GMimeEvent *event, GMimeEventCallback callback, gpointer user_data)
 {
        EventListener *listener;
+       int index;
        
-       if ((listener = g_mime_event_find_listener (event, callback, user_data)))
-               listener->blocked--;
+       if ((index = g_mime_event_index_of (event, callback, user_data)) == -1)
+               return;
+       
+       listener = (EventListener *) event->array->pdata[index];
+       listener->blocked--;
 }
 
 
@@ -173,7 +173,7 @@ g_mime_event_add (GMimeEvent *event, GMimeEventCallback callback, gpointer user_
        EventListener *listener;
        
        listener = event_listener_new (callback, user_data);
-       list_append (&event->list, (ListNode *) listener);
+       g_ptr_array_add (event->array, listener);
 }
 
 
@@ -190,11 +190,14 @@ void
 g_mime_event_remove (GMimeEvent *event, GMimeEventCallback callback, gpointer user_data)
 {
        EventListener *listener;
+       int index;
        
-       if ((listener = g_mime_event_find_listener (event, callback, user_data))) {
-               list_unlink ((ListNode *) listener);
-               event_listener_free (listener);
-       }
+       if ((index = g_mime_event_index_of (event, callback, user_data)) == -1)
+               return;
+       
+       listener = (EventListener *) event->array->pdata[index];
+       g_ptr_array_remove_index (event->array, index);
+       event_listener_free (listener);
 }
 
 
@@ -209,12 +212,12 @@ g_mime_event_remove (GMimeEvent *event, GMimeEventCallback callback, gpointer us
 void
 g_mime_event_emit (GMimeEvent *event, gpointer args)
 {
-       EventListener *node;
+       EventListener *listener;
+       guint i;
        
-       node = (EventListener *) event->list.head;
-       while (node->next) {
-               if (node->blocked <= 0)
-                       node->callback (event->owner, args, node->user_data);
-               node = node->next;
+       for (i = 0; i < event->array->len; i++) {
+               listener = (EventListener *) event->array->pdata[i];
+               if (listener->blocked <= 0)
+                       listener->callback (event->owner, args, listener->user_data);
        }
 }
diff --git a/gmime/gmime-stream-file.c b/gmime/gmime-stream-file.c
index e637e98..e38d8b1 100644
--- a/gmime/gmime-stream-file.c
+++ b/gmime/gmime-stream-file.c
@@ -32,6 +32,7 @@
 #include <errno.h>
 
 #include "gmime-stream-file.h"
+#include "gmime-error.h"
 
 
 /**
@@ -426,6 +427,7 @@ g_mime_stream_file_new_with_bounds (FILE *fp, gint64 start, gint64 end)
  * g_mime_stream_file_open:
  * @path: the path to a file
  * @mode: as in fopen(3)
+ * @err: a #GError
  *
  * Creates a new #GMimeStreamFile object for the specified @path.
  *
@@ -433,15 +435,17 @@ g_mime_stream_file_new_with_bounds (FILE *fp, gint64 start, gint64 end)
  * file path or %NULL on error.
  **/
 GMimeStream *
-g_mime_stream_file_open (const char *path, const char *mode)
+g_mime_stream_file_open (const char *path, const char *mode, GError **err)
 {
        FILE *fp;
        
        g_return_val_if_fail (path != NULL, NULL);
        g_return_val_if_fail (mode != NULL, NULL);
        
-       if (!(fp = fopen (path, mode)))
+       if (!(fp = fopen (path, mode))) {
+               g_set_error (err, GMIME_ERROR, errno, "Failed to open `%s': %s", path, g_strerror (errno));
                return NULL;
+       }
        
        return g_mime_stream_file_new (fp);
 }
diff --git a/gmime/gmime-stream-file.h b/gmime/gmime-stream-file.h
index e0d0b0c..8092a7e 100644
--- a/gmime/gmime-stream-file.h
+++ b/gmime/gmime-stream-file.h
@@ -64,7 +64,7 @@ GType g_mime_stream_file_get_type (void);
 GMimeStream *g_mime_stream_file_new (FILE *fp);
 GMimeStream *g_mime_stream_file_new_with_bounds (FILE *fp, gint64 start, gint64 end);
 
-GMimeStream *g_mime_stream_file_open (const char *path, const char *mode);
+GMimeStream *g_mime_stream_file_open (const char *path, const char *mode, GError **err);
 
 gboolean g_mime_stream_file_get_owner (GMimeStreamFile *stream);
 void g_mime_stream_file_set_owner (GMimeStreamFile *stream, gboolean owner);
diff --git a/gmime/gmime-stream-fs.c b/gmime/gmime-stream-fs.c
index eccb317..5d14b2b 100644
--- a/gmime/gmime-stream-fs.c
+++ b/gmime/gmime-stream-fs.c
@@ -33,6 +33,7 @@
 #include <errno.h>
 
 #include "gmime-stream-fs.h"
+#include "gmime-error.h"
 
 #ifndef HAVE_FSYNC
 #ifdef G_OS_WIN32
@@ -469,6 +470,7 @@ g_mime_stream_fs_new_with_bounds (int fd, gint64 start, gint64 end)
  * @path: the path to a file
  * @flags: as in open(2)
  * @mode: as in open(2)
+ * @err: a #GError
  *
  * Creates a new #GMimeStreamFs object for the specified @path.
  *
@@ -476,14 +478,16 @@ g_mime_stream_fs_new_with_bounds (int fd, gint64 start, gint64 end)
  * file path or %NULL on error.
  **/
 GMimeStream *
-g_mime_stream_fs_open (const char *path, int flags, int mode)
+g_mime_stream_fs_open (const char *path, int flags, int mode, GError **err)
 {
        int fd;
        
        g_return_val_if_fail (path != NULL, NULL);
        
-       if ((fd = g_open (path, flags, mode)) == -1)
+       if ((fd = g_open (path, flags, mode)) == -1) {
+               g_set_error (err, GMIME_ERROR, errno, "Failed to open `%s': %s", path, g_strerror (errno));
                return NULL;
+       }
        
        return g_mime_stream_fs_new (fd);
 }
diff --git a/gmime/gmime-stream-fs.h b/gmime/gmime-stream-fs.h
index f58cd4e..bb791c7 100644
--- a/gmime/gmime-stream-fs.h
+++ b/gmime/gmime-stream-fs.h
@@ -64,7 +64,7 @@ GType g_mime_stream_fs_get_type (void);
 GMimeStream *g_mime_stream_fs_new (int fd);
 GMimeStream *g_mime_stream_fs_new_with_bounds (int fd, gint64 start, gint64 end);
 
-GMimeStream *g_mime_stream_fs_open (const char *path, int flags, int mode);
+GMimeStream *g_mime_stream_fs_open (const char *path, int flags, int mode, GError **err);
 
 gboolean g_mime_stream_fs_get_owner (GMimeStreamFs *stream);
 void g_mime_stream_fs_set_owner (GMimeStreamFs *stream, gboolean owner);
diff --git a/tests/test-mbox.c b/tests/test-mbox.c
index e91b060..ca5df2a 100644
--- a/tests/test-mbox.c
+++ b/tests/test-mbox.c
@@ -332,12 +332,12 @@ int main (int argc, char **argv)
                        
                        testsuite_check ("%s", dent);
                        try {
-                               if (!(istream = g_mime_stream_fs_open (input, O_RDONLY, 0))) {
+                               if (!(istream = g_mime_stream_fs_open (input, O_RDONLY, 0, NULL))) {
                                        throw (exception_new ("could not open `%s': %s",
                                                              input, g_strerror (errno)));
                                }
                                
-                               if (!(ostream = g_mime_stream_fs_open (output, O_RDONLY, 0))) {
+                               if (!(ostream = g_mime_stream_fs_open (output, O_RDONLY, 0, NULL))) {
                                        throw (exception_new ("could not open `%s': %s",
                                                              output, g_strerror (errno)));
                                }
@@ -425,7 +425,7 @@ int main (int argc, char **argv)
                g_dir_close (dir);
        } else if (S_ISREG (st.st_mode)) {
                /* manually run test on a single file */
-               if (!(istream = g_mime_stream_fs_open (path, O_RDONLY, 0)))
+               if (!(istream = g_mime_stream_fs_open (path, O_RDONLY, 0, NULL)))
                        goto exit;
                
                parser = g_mime_parser_new_with_stream (istream);
diff --git a/tests/test-partial.c b/tests/test-partial.c
index 928badb..bc94939 100644
--- a/tests/test-partial.c
+++ b/tests/test-partial.c
@@ -183,7 +183,7 @@ int main (int argc, char **argv)
                        while ((dent = g_dir_read_name (dir))) {
                                path = g_build_filename (input->str, dent, NULL);
                                
-                               if (!(stream = g_mime_stream_file_open (path, "r")))
+                               if (!(stream = g_mime_stream_file_open (path, "r", NULL)))
                                        throw (exception_new ("Failed to open `%s'", path));
                                
                                g_mime_parser_init_with_stream (parser, stream);
@@ -221,8 +221,8 @@ int main (int argc, char **argv)
                        g_mime_object_write_to_stream (GMIME_OBJECT (message), combined);
                        g_mime_stream_reset (combined);
                        
-                       if (!(expected = g_mime_stream_file_open (output->str, "r"))) {
-                               expected = g_mime_stream_file_open (output->str, "w");
+                       if (!(expected = g_mime_stream_file_open (output->str, "r", NULL))) {
+                               expected = g_mime_stream_file_open (output->str, "w", NULL);
                                g_mime_stream_write_to_stream (combined, expected);
                                g_mime_stream_flush (expected);
                                g_object_unref (expected);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]