mirror of
https://git.notmuchmail.org/git/notmuch
synced 2024-11-25 04:18:08 +01:00
add_message: Propagate error status from notmuch_message_create_for_message_id
What a great feeling to remove an XXX comment.
This commit is contained in:
parent
9f673f3cf8
commit
3b8e3ab666
5 changed files with 95 additions and 28 deletions
19
database.cc
19
database.cc
|
@ -284,7 +284,7 @@ notmuch_database_find_message (notmuch_database_t *notmuch,
|
||||||
if (status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND)
|
if (status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
return _notmuch_message_create (notmuch, notmuch, doc_id);
|
return _notmuch_message_create (notmuch, notmuch, doc_id, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Return one or more thread_ids, (as a GPtrArray of strings), for the
|
/* Return one or more thread_ids, (as a GPtrArray of strings), for the
|
||||||
|
@ -762,17 +762,12 @@ notmuch_database_add_message (notmuch_database_t *notmuch,
|
||||||
* database). */
|
* database). */
|
||||||
|
|
||||||
/* Use NULL for owner since we want to free this locally. */
|
/* Use NULL for owner since we want to free this locally. */
|
||||||
|
message = _notmuch_message_create_for_message_id (NULL,
|
||||||
/* XXX: This call can fail by either out-of-memory or an
|
notmuch,
|
||||||
* "impossible" Xapian exception. We should rewrite it to
|
message_id,
|
||||||
* allow us to propagate the error status. */
|
&ret);
|
||||||
message = _notmuch_message_create_for_message_id (NULL, notmuch,
|
if (message == NULL)
|
||||||
message_id);
|
goto DONE;
|
||||||
if (message == NULL) {
|
|
||||||
fprintf (stderr, "Internal error. This shouldn't happen.\n\n");
|
|
||||||
fprintf (stderr, "I mean, it's possible you ran out of memory, but then this code path is still an internal error since it should have detected that and propagated the status value up the stack.\n");
|
|
||||||
exit (1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Has a message previously been added with the same ID? */
|
/* Has a message previously been added with the same ID? */
|
||||||
old_filename = notmuch_message_get_filename (message);
|
old_filename = notmuch_message_get_filename (message);
|
||||||
|
|
71
message.cc
71
message.cc
|
@ -80,23 +80,43 @@ _notmuch_message_destructor (notmuch_message_t *message)
|
||||||
* caller *is* responsible for calling notmuch_message_destroy.
|
* caller *is* responsible for calling notmuch_message_destroy.
|
||||||
*
|
*
|
||||||
* If no document exists in the database with document ID of 'doc_id'
|
* If no document exists in the database with document ID of 'doc_id'
|
||||||
* then this function returns NULL.
|
* then this function returns NULL and sets *status to
|
||||||
|
* NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND.
|
||||||
|
*
|
||||||
|
* This function can also fail to due lack of available memory,
|
||||||
|
* returning NULL and optionally setting *status to
|
||||||
|
* NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY. Caller can pass NULL
|
||||||
|
* for status if uninterested in distinguishing these two cases.
|
||||||
*/
|
*/
|
||||||
notmuch_message_t *
|
notmuch_message_t *
|
||||||
_notmuch_message_create (const void *talloc_owner,
|
_notmuch_message_create (const void *talloc_owner,
|
||||||
notmuch_database_t *notmuch,
|
notmuch_database_t *notmuch,
|
||||||
unsigned int doc_id)
|
unsigned int doc_id,
|
||||||
|
notmuch_private_status_t *status)
|
||||||
{
|
{
|
||||||
notmuch_message_t *message;
|
notmuch_message_t *message;
|
||||||
|
|
||||||
|
if (status)
|
||||||
|
*status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
|
||||||
|
|
||||||
message = talloc (talloc_owner, notmuch_message_t);
|
message = talloc (talloc_owner, notmuch_message_t);
|
||||||
if (unlikely (message == NULL))
|
if (unlikely (message == NULL)) {
|
||||||
|
if (status)
|
||||||
|
*status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
message->notmuch = notmuch;
|
message->notmuch = notmuch;
|
||||||
message->doc_id = doc_id;
|
message->doc_id = doc_id;
|
||||||
message->message_id = NULL; /* lazily created */
|
message->message_id = NULL; /* lazily created */
|
||||||
message->filename = NULL; /* lazily created */
|
message->filename = NULL; /* lazily created */
|
||||||
|
|
||||||
|
/* This is C++'s creepy "placement new", which is really just an
|
||||||
|
* ugly way to call a constructor for a pre-allocated object. So
|
||||||
|
* it's really not an error to not be checking for OUT_OF_MEMORY
|
||||||
|
* here, since this "new" isn't actually allocating memory. This
|
||||||
|
* is language-design comedy of the wrong kind. */
|
||||||
|
|
||||||
new (&message->doc) Xapian::Document;
|
new (&message->doc) Xapian::Document;
|
||||||
|
|
||||||
talloc_set_destructor (message, _notmuch_message_destructor);
|
talloc_set_destructor (message, _notmuch_message_destructor);
|
||||||
|
@ -105,6 +125,8 @@ _notmuch_message_create (const void *talloc_owner,
|
||||||
message->doc = notmuch->xapian_db->get_document (doc_id);
|
message->doc = notmuch->xapian_db->get_document (doc_id);
|
||||||
} catch (const Xapian::DocNotFoundError &error) {
|
} catch (const Xapian::DocNotFoundError &error) {
|
||||||
talloc_free (message);
|
talloc_free (message);
|
||||||
|
if (status)
|
||||||
|
*status = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,32 +149,61 @@ _notmuch_message_create (const void *talloc_owner,
|
||||||
* If there is already a document with message ID 'message_id' in the
|
* If there is already a document with message ID 'message_id' in the
|
||||||
* database, then the returned message can be used to query/modify the
|
* database, then the returned message can be used to query/modify the
|
||||||
* document. Otherwise, a new document will be inserted into the
|
* document. Otherwise, a new document will be inserted into the
|
||||||
* database before this function returns;
|
* database before this function returns.
|
||||||
|
*
|
||||||
|
* If an error occurs, this function will return NULL and *status
|
||||||
|
* will be set as appropriate. (The status pointer argument must
|
||||||
|
* not be NULL.)
|
||||||
*/
|
*/
|
||||||
notmuch_message_t *
|
notmuch_message_t *
|
||||||
_notmuch_message_create_for_message_id (const void *talloc_owner,
|
_notmuch_message_create_for_message_id (const void *talloc_owner,
|
||||||
notmuch_database_t *notmuch,
|
notmuch_database_t *notmuch,
|
||||||
const char *message_id)
|
const char *message_id,
|
||||||
|
notmuch_status_t *status)
|
||||||
{
|
{
|
||||||
|
notmuch_private_status_t private_status;
|
||||||
notmuch_message_t *message;
|
notmuch_message_t *message;
|
||||||
Xapian::Document doc;
|
Xapian::Document doc;
|
||||||
unsigned int doc_id;
|
unsigned int doc_id;
|
||||||
char *term;
|
char *term;
|
||||||
|
|
||||||
|
*status = NOTMUCH_STATUS_SUCCESS;
|
||||||
|
|
||||||
message = notmuch_database_find_message (notmuch, message_id);
|
message = notmuch_database_find_message (notmuch, message_id);
|
||||||
if (message)
|
if (message)
|
||||||
return talloc_steal (talloc_owner, message);
|
return talloc_steal (talloc_owner, message);
|
||||||
|
|
||||||
term = talloc_asprintf (NULL, "%s%s",
|
term = talloc_asprintf (NULL, "%s%s",
|
||||||
_find_prefix ("id"), message_id);
|
_find_prefix ("id"), message_id);
|
||||||
doc.add_term (term);
|
if (term == NULL) {
|
||||||
talloc_free (term);
|
*status = NOTMUCH_STATUS_OUT_OF_MEMORY;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
|
try {
|
||||||
|
doc.add_term (term);
|
||||||
|
talloc_free (term);
|
||||||
|
|
||||||
doc_id = notmuch->xapian_db->add_document (doc);
|
doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
|
||||||
|
|
||||||
return _notmuch_message_create (talloc_owner, notmuch, doc_id);
|
doc_id = notmuch->xapian_db->add_document (doc);
|
||||||
|
} catch (const Xapian::Error &error) {
|
||||||
|
*status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
message = _notmuch_message_create (talloc_owner, notmuch,
|
||||||
|
doc_id, &private_status);
|
||||||
|
|
||||||
|
if (private_status >= (notmuch_private_status_t) NOTMUCH_STATUS_LAST_STATUS)
|
||||||
|
{
|
||||||
|
fprintf (stderr, "Internal error: Failed to find document immediately after adding it.\n");
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
*status = (notmuch_status_t) private_status;
|
||||||
|
|
||||||
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *
|
const char *
|
||||||
|
|
|
@ -83,6 +83,7 @@ typedef enum {
|
||||||
typedef enum _notmuch_private_status {
|
typedef enum _notmuch_private_status {
|
||||||
/* First, copy all the public status values. */
|
/* First, copy all the public status values. */
|
||||||
NOTMUCH_PRIVATE_STATUS_SUCCESS = NOTMUCH_STATUS_SUCCESS,
|
NOTMUCH_PRIVATE_STATUS_SUCCESS = NOTMUCH_STATUS_SUCCESS,
|
||||||
|
NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY = NOTMUCH_STATUS_OUT_OF_MEMORY,
|
||||||
NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION = NOTMUCH_STATUS_XAPIAN_EXCEPTION,
|
NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION = NOTMUCH_STATUS_XAPIAN_EXCEPTION,
|
||||||
NOTMUCH_PRIVATE_STATUS_FILE_NOT_EMAIL = NOTMUCH_STATUS_FILE_NOT_EMAIL,
|
NOTMUCH_PRIVATE_STATUS_FILE_NOT_EMAIL = NOTMUCH_STATUS_FILE_NOT_EMAIL,
|
||||||
NOTMUCH_PRIVATE_STATUS_NULL_POINTER = NOTMUCH_STATUS_NULL_POINTER,
|
NOTMUCH_PRIVATE_STATUS_NULL_POINTER = NOTMUCH_STATUS_NULL_POINTER,
|
||||||
|
@ -100,12 +101,14 @@ typedef enum _notmuch_private_status {
|
||||||
notmuch_message_t *
|
notmuch_message_t *
|
||||||
_notmuch_message_create (const void *talloc_owner,
|
_notmuch_message_create (const void *talloc_owner,
|
||||||
notmuch_database_t *notmuch,
|
notmuch_database_t *notmuch,
|
||||||
unsigned int doc_id);
|
unsigned int doc_id,
|
||||||
|
notmuch_private_status_t *status);
|
||||||
|
|
||||||
notmuch_message_t *
|
notmuch_message_t *
|
||||||
_notmuch_message_create_for_message_id (const void *talloc_owner,
|
_notmuch_message_create_for_message_id (const void *talloc_owner,
|
||||||
notmuch_database_t *notmuch,
|
notmuch_database_t *notmuch,
|
||||||
const char *message_id);
|
const char *message_id,
|
||||||
|
notmuch_status_t *status);
|
||||||
|
|
||||||
/* Lookup a prefix value by name.
|
/* Lookup a prefix value by name.
|
||||||
*
|
*
|
||||||
|
|
10
notmuch.h
10
notmuch.h
|
@ -51,6 +51,8 @@ typedef int notmuch_bool_t;
|
||||||
*
|
*
|
||||||
* NOTMUCH_STATUS_SUCCESS: No error occurred.
|
* NOTMUCH_STATUS_SUCCESS: No error occurred.
|
||||||
*
|
*
|
||||||
|
* NOTMUCH_STATUS_OUT_OF_MEMORY: Out of memory
|
||||||
|
*
|
||||||
* XXX: We don't really want to expose this lame XAPIAN_EXCEPTION
|
* XXX: We don't really want to expose this lame XAPIAN_EXCEPTION
|
||||||
* value. Instead we should map to things like DATABASE_LOCKED or
|
* value. Instead we should map to things like DATABASE_LOCKED or
|
||||||
* whatever.
|
* whatever.
|
||||||
|
@ -78,6 +80,7 @@ typedef int notmuch_bool_t;
|
||||||
*/
|
*/
|
||||||
typedef enum _notmuch_status {
|
typedef enum _notmuch_status {
|
||||||
NOTMUCH_STATUS_SUCCESS = 0,
|
NOTMUCH_STATUS_SUCCESS = 0,
|
||||||
|
NOTMUCH_STATUS_OUT_OF_MEMORY,
|
||||||
NOTMUCH_STATUS_XAPIAN_EXCEPTION,
|
NOTMUCH_STATUS_XAPIAN_EXCEPTION,
|
||||||
NOTMUCH_STATUS_FILE_ERROR,
|
NOTMUCH_STATUS_FILE_ERROR,
|
||||||
NOTMUCH_STATUS_FILE_NOT_EMAIL,
|
NOTMUCH_STATUS_FILE_NOT_EMAIL,
|
||||||
|
@ -267,8 +270,8 @@ notmuch_database_add_message (notmuch_database_t *database,
|
||||||
* a new notmuch_message_t object is returned. The caller should call
|
* a new notmuch_message_t object is returned. The caller should call
|
||||||
* notmuch_message_destroy when done with the message.
|
* notmuch_message_destroy when done with the message.
|
||||||
*
|
*
|
||||||
* If no message is found with the given message_id, this function
|
* If no message is found with the given message_id or if an
|
||||||
* returns NULL.
|
* out-of-memory situation occurs, this function returns NULL.
|
||||||
*/
|
*/
|
||||||
notmuch_message_t *
|
notmuch_message_t *
|
||||||
notmuch_database_find_message (notmuch_database_t *database,
|
notmuch_database_find_message (notmuch_database_t *database,
|
||||||
|
@ -376,6 +379,9 @@ notmuch_results_has_more (notmuch_results_t *results);
|
||||||
*
|
*
|
||||||
* See the documentation of notmuch_query_search for example code
|
* See the documentation of notmuch_query_search for example code
|
||||||
* showing how to iterate over a notmuch_results_t object.
|
* showing how to iterate over a notmuch_results_t object.
|
||||||
|
*
|
||||||
|
* If an out-of-memory situation occurs, this function will return
|
||||||
|
* NULL.
|
||||||
*/
|
*/
|
||||||
notmuch_message_t *
|
notmuch_message_t *
|
||||||
notmuch_results_get (notmuch_results_t *results);
|
notmuch_results_get (notmuch_results_t *results);
|
||||||
|
|
16
query.cc
16
query.cc
|
@ -165,12 +165,24 @@ notmuch_results_has_more (notmuch_results_t *results)
|
||||||
notmuch_message_t *
|
notmuch_message_t *
|
||||||
notmuch_results_get (notmuch_results_t *results)
|
notmuch_results_get (notmuch_results_t *results)
|
||||||
{
|
{
|
||||||
|
notmuch_message_t *message;
|
||||||
Xapian::docid doc_id;
|
Xapian::docid doc_id;
|
||||||
|
notmuch_private_status_t status;
|
||||||
|
|
||||||
doc_id = *results->iterator;
|
doc_id = *results->iterator;
|
||||||
|
|
||||||
return _notmuch_message_create (results,
|
message = _notmuch_message_create (results,
|
||||||
results->notmuch, doc_id);
|
results->notmuch, doc_id,
|
||||||
|
&status);
|
||||||
|
|
||||||
|
if (message == NULL &&
|
||||||
|
status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND)
|
||||||
|
{
|
||||||
|
fprintf (stderr, "Internal error: a results iterator contains a non-existent document ID.\n");
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|
Loading…
Reference in a new issue