Re: [PATCH 1/2] Add Google Inc. to AUTHORS as a contributor.
[notmuch-archives.git] / ea / 82e6e1371dd8b8c2bc01ca6cbb0b6efa5ee650
1 Return-Path: <bremner@tesseract.cs.unb.ca>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id 8D8F9431FC0\r
6         for <notmuch@notmuchmail.org>; Fri,  3 Oct 2014 14:19:54 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: 0\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
12         autolearn=disabled\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id fzeQLSamo+md for <notmuch@notmuchmail.org>;\r
16         Fri,  3 Oct 2014 14:19:48 -0700 (PDT)\r
17 Received: from yantan.tethera.net (yantan.tethera.net [199.188.72.155])\r
18         (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits))\r
19         (No client certificate requested)\r
20         by olra.theworths.org (Postfix) with ESMTPS id DC7A1431FC3\r
21         for <notmuch@notmuchmail.org>; Fri,  3 Oct 2014 14:19:35 -0700 (PDT)\r
22 Received: from remotemail by yantan.tethera.net with local (Exim 4.80)\r
23         (envelope-from <bremner@tesseract.cs.unb.ca>)\r
24         id 1XaAGF-0000LL-2p; Fri, 03 Oct 2014 18:19:35 -0300\r
25 Received: (nullmailer pid 21177 invoked by uid 1000); Fri, 03 Oct 2014\r
26         21:19:08 -0000\r
27 From: David Bremner <david@tethera.net>\r
28 To: notmuch@notmuchmail.org\r
29 Subject: [Patch v2.5 2/4] cli/insert: add fail path to add_file_to_database\r
30 Date: Fri,  3 Oct 2014 23:18:58 +0200\r
31 Message-Id: <1412371140-21051-3-git-send-email-david@tethera.net>\r
32 X-Mailer: git-send-email 2.1.0\r
33 In-Reply-To: <1412371140-21051-1-git-send-email-david@tethera.net>\r
34 References: <1412371140-21051-1-git-send-email-david@tethera.net>\r
35 X-BeenThere: notmuch@notmuchmail.org\r
36 X-Mailman-Version: 2.1.13\r
37 Precedence: list\r
38 List-Id: "Use and development of the notmuch mail system."\r
39         <notmuch.notmuchmail.org>\r
40 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
41         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
42 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
43 List-Post: <mailto:notmuch@notmuchmail.org>\r
44 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
45 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
46         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
47 X-List-Received-Date: Fri, 03 Oct 2014 21:19:54 -0000\r
48 \r
49 From: Jani Nikula <jani@nikula.org>\r
50 \r
51 Handle failures gracefully in add_file_to_database, renamed simply\r
52 add_file while at it. Add keep option to not remove the message from\r
53 database if tagging or tag syncing to maildir flags fails. Expand the\r
54 function documentation to cover the changes.\r
55 ---\r
56  notmuch-insert.c | 99 ++++++++++++++++++++++++++++++++++++--------------------\r
57  1 file changed, 64 insertions(+), 35 deletions(-)\r
58 \r
59 diff --git a/notmuch-insert.c b/notmuch-insert.c\r
60 index 5ef6e66..0ea4380 100644\r
61 --- a/notmuch-insert.c\r
62 +++ b/notmuch-insert.c\r
63 @@ -364,50 +364,80 @@ FAIL:\r
64      return NULL;\r
65  }\r
66  \r
67 -/* Add the specified message file to the notmuch database, applying tags.\r
68 - * The file is renamed to encode notmuch tags as maildir flags. */\r
69 -static void\r
70 -add_file_to_database (notmuch_database_t *notmuch, const char *path,\r
71 -                     tag_op_list_t *tag_ops, notmuch_bool_t synchronize_flags)\r
72 +/*\r
73 + * Add the specified message file to the notmuch database, applying\r
74 + * tags in tag_ops. If synchronize_flags is TRUE, the tags are\r
75 + * synchronized to maildir flags (which may result in message file\r
76 + * rename).\r
77 + *\r
78 + * Return NOTMUCH_STATUS_SUCCESS on success, errors otherwise. If keep\r
79 + * is TRUE, errors in tag changes and flag syncing are ignored and\r
80 + * success status is returned; otherwise such errors cause the message\r
81 + * to be removed from the database. Failure to add the message to the\r
82 + * database results in error status regardless of keep.\r
83 + */\r
84 +static notmuch_status_t\r
85 +add_file (notmuch_database_t *notmuch, const char *path, tag_op_list_t *tag_ops,\r
86 +         notmuch_bool_t synchronize_flags, notmuch_bool_t keep)\r
87  {\r
88      notmuch_message_t *message;\r
89      notmuch_status_t status;\r
90  \r
91      status = notmuch_database_add_message (notmuch, path, &message);\r
92 -    switch (status) {\r
93 -    case NOTMUCH_STATUS_SUCCESS:\r
94 -    case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:\r
95 -       break;\r
96 -    default:\r
97 -    case NOTMUCH_STATUS_FILE_NOT_EMAIL:\r
98 -    case NOTMUCH_STATUS_READ_ONLY_DATABASE:\r
99 -    case NOTMUCH_STATUS_XAPIAN_EXCEPTION:\r
100 -    case NOTMUCH_STATUS_OUT_OF_MEMORY:\r
101 -    case NOTMUCH_STATUS_FILE_ERROR:\r
102 -    case NOTMUCH_STATUS_NULL_POINTER:\r
103 -    case NOTMUCH_STATUS_TAG_TOO_LONG:\r
104 -    case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:\r
105 -    case NOTMUCH_STATUS_UNBALANCED_ATOMIC:\r
106 -    case NOTMUCH_STATUS_LAST_STATUS:\r
107 -       fprintf (stderr, "Error: failed to add `%s' to notmuch database: %s\n",\r
108 -                path, notmuch_status_to_string (status));\r
109 -       return;\r
110 -    }\r
111 -\r
112 -    if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {\r
113 -       /* Don't change tags of an existing message. */\r
114 -       if (synchronize_flags) {\r
115 -           status = notmuch_message_tags_to_maildir_flags (message);\r
116 -           if (status != NOTMUCH_STATUS_SUCCESS)\r
117 -               fprintf (stderr, "Error: failed to sync tags to maildir flags\n");\r
118 +    if (status == NOTMUCH_STATUS_SUCCESS) {\r
119 +       status = tag_op_list_apply (message, tag_ops, 0);\r
120 +       if (status) {\r
121 +           fprintf (stderr, "%s: failed to apply tags to file '%s': %s\n",\r
122 +                    keep ? "Warning" : "Error",\r
123 +                    path, notmuch_status_to_string (status));\r
124 +           goto DONE;\r
125         }\r
126 +    } else if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {\r
127 +       status = NOTMUCH_STATUS_SUCCESS;\r
128 +    } else if (status == NOTMUCH_STATUS_FILE_NOT_EMAIL) {\r
129 +       fprintf (stderr, "Error: delivery of non-mail file: '%s'\n", path);\r
130 +       goto FAIL;\r
131      } else {\r
132 -       tag_op_flag_t flags = synchronize_flags ? TAG_FLAG_MAILDIR_SYNC : 0;\r
133 +       fprintf (stderr, "Error: failed to add '%s' to notmuch database: %s\n",\r
134 +                path, notmuch_status_to_string (status));\r
135 +       goto FAIL;\r
136 +    }\r
137  \r
138 -       tag_op_list_apply (message, tag_ops, flags);\r
139 +    if (synchronize_flags) {\r
140 +       status = notmuch_message_tags_to_maildir_flags (message);\r
141 +       if (status != NOTMUCH_STATUS_SUCCESS)\r
142 +           fprintf (stderr, "%s: failed to sync tags to maildir flags for '%s': %s\n",\r
143 +                    keep ? "Warning" : "Error",\r
144 +                    path, notmuch_status_to_string (status));\r
145 +\r
146 +       /*\r
147 +        * Note: Unfortunately a failed maildir flag sync might\r
148 +        * already have renamed the file, in which case the cleanup\r
149 +        * path may fail.\r
150 +        */\r
151      }\r
152  \r
153 +  DONE:\r
154      notmuch_message_destroy (message);\r
155 +\r
156 +    if (status) {\r
157 +       if (keep) {\r
158 +           status = NOTMUCH_STATUS_SUCCESS;\r
159 +       } else {\r
160 +           notmuch_status_t cleanup_status;\r
161 +\r
162 +           cleanup_status = notmuch_database_remove_message (notmuch, path);\r
163 +           if (cleanup_status != NOTMUCH_STATUS_SUCCESS &&\r
164 +               cleanup_status != NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {\r
165 +               fprintf (stderr, "Warning: failed to remove '%s' from database "\r
166 +                        "after errors: %s. Please run 'notmuch new' to fix.\n",\r
167 +                        path, notmuch_status_to_string (cleanup_status));\r
168 +           }\r
169 +       }\r
170 +    }\r
171 +\r
172 +  FAIL:\r
173 +    return status;\r
174  }\r
175  \r
176  int\r
177 @@ -508,8 +538,7 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])\r
178      /* Add the message to the index.\r
179       * Even if adding the message to the notmuch database fails,\r
180       * the message is on disk and we consider the delivery completed. */\r
181 -    add_file_to_database (notmuch, newpath, tag_ops,\r
182 -                                   synchronize_flags);\r
183 +    add_file (notmuch, newpath, tag_ops, synchronize_flags, TRUE);\r
184  \r
185      notmuch_database_destroy (notmuch);\r
186      return EXIT_SUCCESS;\r
187 -- \r
188 2.1.0\r
189 \r