diff --git a/src/mu-cmd-extract.c b/src/mu-cmd-extract.c index 0ad373c5..4627cb11 100644 --- a/src/mu-cmd-extract.c +++ b/src/mu-cmd-extract.c @@ -134,11 +134,16 @@ save_parts (const char *path, MuConfigOptions *opts) { MuMsg* msg; gboolean rv; - - msg = mu_msg_new (path, NULL); - if (!msg) - return FALSE; + GError *err; + err = NULL; + msg = mu_msg_new (path, NULL, &err); + if (!msg) { + g_warning ("Error: %s", err->message); + g_error_free (err); + return FALSE; + } + /* note, mu_cmd_extract already checks whether what's in opts * is somewhat, so no need for extensive checking here */ @@ -176,10 +181,15 @@ static gboolean show_parts (const char* path, MuConfigOptions *opts) { MuMsg* msg; - - msg = mu_msg_new (path, NULL); - if (!msg) + GError *err; + + err = NULL; + msg = mu_msg_new (path, NULL, &err); + if (!msg) { + g_warning ("Error: %s", err->message); + g_error_free (err); return FALSE; + } g_print ("MIME-parts in this message:\n"); mu_msg_msg_part_foreach (msg, each_part_show, NULL); diff --git a/src/mu-cmd-view.c b/src/mu-cmd-view.c index 7ee84309..8f0fa37f 100644 --- a/src/mu-cmd-view.c +++ b/src/mu-cmd-view.c @@ -39,11 +39,16 @@ view_file (const gchar *path, const gchar *fields, size_t summary_len) MuMsg* msg; const char *field; time_t date; - - msg = mu_msg_new (path, NULL); - if (!msg) + GError *err; + + err = NULL; + msg = mu_msg_new (path, NULL, &err); + if (!msg) { + g_warning ("Error: %s", err->message); + g_error_free (err); return FALSE; - + } + if ((field = mu_msg_get_from (msg))) g_print ("From: %s\n", field); diff --git a/src/mu-error.h b/src/mu-error.h index 3d3f278a..2cb1954a 100644 --- a/src/mu-error.h +++ b/src/mu-error.h @@ -29,8 +29,14 @@ enum _MuError { /* database version is not uptodate (ie. not compatible with * the version that mu expects) */ MU_ERROR_XAPIAN_NOT_UPTODATE, + /* missing data for a document */ + MU_ERROR_XAPIAN_MISSING_DATA, /* (parsnng) error in the query */ MU_ERROR_QUERY, + /* file loading related error */ + MU_ERROR_FILE, + /* gmime parsing related error */ + MU_ERROR_GMIME, /* some other, internal error */ MU_ERROR_INTERNAL }; diff --git a/src/mu-index.c b/src/mu-index.c index 4ac0f99f..d5ac298e 100644 --- a/src/mu-index.c +++ b/src/mu-index.c @@ -100,6 +100,7 @@ insert_or_update_maybe (const char* fullpath, const char* mdir, MuIndexCallbackData *data, gboolean *updated) { MuMsg *msg; + GError *err; *updated = FALSE; @@ -125,9 +126,9 @@ insert_or_update_maybe (const char* fullpath, const char* mdir, return MU_OK; /* nope: no need to insert/update! */ } while (0); - - - msg = mu_msg_new (fullpath, mdir); + + err = NULL; + msg = mu_msg_new (fullpath, mdir, &err); if (!msg) { g_warning ("%s: failed to create mu_msg for %s", __FUNCTION__, fullpath); diff --git a/src/mu-msg-iter.cc b/src/mu-msg-iter.cc index 69cb8bdb..36c52dd3 100644 --- a/src/mu-msg-iter.cc +++ b/src/mu-msg-iter.cc @@ -82,8 +82,8 @@ mu_msg_iter_destroy (MuMsgIter *iter) MuMsg* -mu_msg_iter_get_msg (MuMsgIter *iter) -{ +mu_msg_iter_get_msg (MuMsgIter *iter, GError **err) +{ const char *path; MuMsg *msg; @@ -92,15 +92,14 @@ mu_msg_iter_get_msg (MuMsgIter *iter) path = mu_msg_iter_get_path (iter); if (!path) { - g_warning ("%s: no path for message", __FUNCTION__); + g_set_error (err, 0, MU_ERROR_XAPIAN_MISSING_DATA, + "no path for message"); return NULL; } - msg = mu_msg_new (path, NULL); - if (!msg) { - g_warning ("%s: failed to create msg object", __FUNCTION__); + msg = mu_msg_new (path, NULL, err); + if (!msg) return NULL; - } return msg; } diff --git a/src/mu-msg-iter.h b/src/mu-msg-iter.h index dee8fd94..d9979ce0 100644 --- a/src/mu-msg-iter.h +++ b/src/mu-msg-iter.h @@ -28,7 +28,7 @@ struct _MuMsgIter; typedef struct _MuMsgIter MuMsgIter; /** - * get the next next message (which you got from + * get the next message (which you got from * e.g. mu_query_run) * * @param iter a valid MuMsgIter iterator @@ -66,12 +66,14 @@ void mu_msg_iter_destroy (MuMsgIter *iter); * MuMsg should use only when information is needed that is not * provided from the iter). * - * @param iter a valid MuMsgIter instance + * @param iter a valid MuMsgIter instance + * @param err which receives error info or NULL. err is only filled + * when the function returns NULL * * @return a MuMsgGMime instance, or NULL in case of error. Use * mu_msg_gmime_destroy when the instance is no longer needed */ -MuMsg* mu_msg_iter_get_msg (MuMsgIter *iter); +MuMsg* mu_msg_iter_get_msg (MuMsgIter *iter, GError **err); /** * get the document id for the current message diff --git a/src/mu-msg.c b/src/mu-msg.c index fca65f2b..1f2a0f92 100644 --- a/src/mu-msg.c +++ b/src/mu-msg.c @@ -78,25 +78,28 @@ mu_msg_destroy (MuMsg *msg) static gboolean -init_file_metadata (MuMsg* msg, const char* path, const gchar* mdir) +init_file_metadata (MuMsg* msg, const char* path, const gchar* mdir, + GError **err) { struct stat statbuf; if (access (path, R_OK) != 0) { - g_warning ("%s: cannot read file %s: %s", - __FUNCTION__, path, strerror(errno)); + g_set_error (err, 0, MU_ERROR_FILE, + "cannot read file %s: %s", + path, strerror(errno)); return FALSE; } if (stat (path, &statbuf) < 0) { - g_warning ("%s: cannot stat %s: %s", - __FUNCTION__, path, strerror(errno)); + g_set_error (err, 0, MU_ERROR_FILE, + "cannot stat %s: %s", + path, strerror(errno)); return FALSE; } if (!S_ISREG(statbuf.st_mode)) { - g_warning ("%s: not a regular file: %s", - __FUNCTION__, path); + g_set_error (err, 0, MU_ERROR_FILE, + "not a regular file: %s", path); return FALSE; } @@ -114,7 +117,7 @@ init_file_metadata (MuMsg* msg, const char* path, const gchar* mdir) static gboolean -init_mime_msg (MuMsg *msg) +init_mime_msg (MuMsg *msg, GError **err) { FILE *file; GMimeStream *stream; @@ -122,15 +125,17 @@ init_mime_msg (MuMsg *msg) file = fopen (mu_msg_get_path(msg), "r"); if (!file) { - g_warning ("%s:cannot open %s: %s", - __FUNCTION__, mu_msg_get_path(msg), - strerror (errno)); + g_set_error (err, 0, MU_ERROR_FILE, + "cannot open %s: %s", mu_msg_get_path(msg), + strerror (errno)); return FALSE; } stream = g_mime_stream_file_new (file); if (!stream) { - g_warning ("%s: cannot create mime stream", __FUNCTION__); + g_set_error (err, 0, MU_ERROR_GMIME, + "cannot create mime stream for %s", + mu_msg_get_path(msg)); fclose (file); return FALSE; } @@ -138,14 +143,18 @@ init_mime_msg (MuMsg *msg) parser = g_mime_parser_new_with_stream (stream); g_object_unref (stream); if (!parser) { - g_warning ("%s: cannot create mime parser", __FUNCTION__); + g_set_error (err, 0, MU_ERROR_GMIME, + "cannot create mime parser for %s", + mu_msg_get_path(msg)); return FALSE; } msg->_mime_msg = g_mime_parser_construct_message (parser); g_object_unref (parser); if (!msg->_mime_msg) { - g_warning ("%s: cannot create mime message", __FUNCTION__); + g_set_error (err, 0, MU_ERROR_GMIME, + "cannot construct mime message for %s", + mu_msg_get_path(msg)); return FALSE; } @@ -154,7 +163,7 @@ init_mime_msg (MuMsg *msg) MuMsg* -mu_msg_new (const char* filepath, const gchar* mdir) +mu_msg_new (const char* filepath, const gchar* mdir, GError **err) { MuMsg *msg; @@ -165,12 +174,12 @@ mu_msg_new (const char* filepath, const gchar* mdir) msg = g_slice_new0 (MuMsg); msg->_prio = MU_MSG_PRIO_NONE; - if (!init_file_metadata(msg, filepath, mdir)) { + if (!init_file_metadata(msg, filepath, mdir, err)) { mu_msg_destroy (msg); return NULL; } - if (!init_mime_msg(msg)) { + if (!init_mime_msg(msg, err)) { mu_msg_destroy (msg); return NULL; } diff --git a/src/mu-msg.h b/src/mu-msg.h index 478bc45e..849bcb28 100644 --- a/src/mu-msg.h +++ b/src/mu-msg.h @@ -24,6 +24,7 @@ #include "mu-msg-fields.h" #include "mu-msg-status.h" #include "mu-msg-prio.h" +#include "mu-error.h" G_BEGIN_DECLS @@ -35,16 +36,18 @@ typedef struct _MuMsg MuMsg; * read access to its properties; call mu_msg_destroy when done with it. * * @param path full path to an email message file - * * @param mdir the maildir for this message; ie, if the path is * ~/Maildir/foo/bar/cur/msg, the maildir would be foo/bar; you can * pass NULL for this parameter, in which case some maildir-specific * information is not available. + * @param err receive error information (MU_ERROR_FILE or MU_ERROR_GMIME), or NULL. There + * will only be err info if the function returns NULL * * @return a new MuMsg instance or NULL in case of error */ MuMsg* mu_msg_new (const char* filepath, - const char *maildir); + const char *maildir, + GError **err); /** diff --git a/src/mu-output-plain.c b/src/mu-output-plain.c index 246cb281..93f63c11 100644 --- a/src/mu-output-plain.c +++ b/src/mu-output-plain.c @@ -75,12 +75,15 @@ display_field (MuMsgIter *iter, MuMsgFieldId mfid) static void print_summary (MuMsgIter *iter, size_t summary_len) { + GError *err; const char *summ; MuMsg *msg; - msg = mu_msg_iter_get_msg (iter); + err = NULL; + msg = mu_msg_iter_get_msg (iter, &err); if (!msg) { - g_warning ("%s: failed to create msg object", __FUNCTION__); + g_warning ("error get message: %s", err->message); + g_error_free (err); return; } diff --git a/src/mu-query.cc b/src/mu-query.cc index 4e42b406..08a97af3 100644 --- a/src/mu-query.cc +++ b/src/mu-query.cc @@ -117,7 +117,8 @@ private: date = cleanup; } - void complete_date (std::string& date, size_t len, bool is_begin) const { + void complete_date (std::string& date, size_t len, + bool is_begin) const { const std::string bsuffix ("00000101000000"); const std::string esuffix ("99991231235959");