]> git.notmuchmail.org Git - notmuch/blob - lib/directory.cc
lib: Treat NULL as a valid (and empty) notmuch_filenames_t iterator.
[notmuch] / lib / directory.cc
1 /* directory.cc - Results of directory-based searches from a notmuch database
2  *
3  * Copyright © 2009 Carl Worth
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see http://www.gnu.org/licenses/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #include "notmuch-private.h"
22 #include "database-private.h"
23
24 #include <xapian.h>
25
26 struct _notmuch_filenames {
27     Xapian::TermIterator iterator;
28     Xapian::TermIterator end;
29     int prefix_len;
30     char *filename;
31 };
32
33 /* We end up having to call the destructors explicitly because we had
34  * to use "placement new" in order to initialize C++ objects within a
35  * block that we allocated with talloc. So C++ is making talloc
36  * slightly less simple to use, (we wouldn't need
37  * talloc_set_destructor at all otherwise).
38  */
39 static int
40 _notmuch_filenames_destructor (notmuch_filenames_t *filenames)
41 {
42     filenames->iterator.~TermIterator ();
43     filenames->end.~TermIterator ();
44
45     return 0;
46 }
47
48 /* Create an iterator to iterate over the basenames of files (or
49  * directories) that all share a common parent directory.
50  *
51  * The code here is general enough to be reused for any case of
52  * iterating over the non-prefixed portion of terms sharing a common
53  * prefix.
54  */
55 notmuch_filenames_t *
56 _notmuch_filenames_create (void *ctx,
57                            notmuch_database_t *notmuch,
58                            const char *prefix)
59 {
60     notmuch_filenames_t *filenames;
61
62     filenames = talloc (ctx, notmuch_filenames_t);
63     if (unlikely (filenames == NULL))
64         return NULL;
65
66     new (&filenames->iterator) Xapian::TermIterator ();
67     new (&filenames->end) Xapian::TermIterator ();
68
69     talloc_set_destructor (filenames, _notmuch_filenames_destructor);
70
71     filenames->iterator = notmuch->xapian_db->allterms_begin (prefix);
72     filenames->end = notmuch->xapian_db->allterms_end (prefix);
73
74     filenames->prefix_len = strlen (prefix);
75
76     filenames->filename = NULL;
77
78     return filenames;
79 }
80
81 notmuch_bool_t
82 notmuch_filenames_has_more (notmuch_filenames_t *filenames)
83 {
84     if (filenames == NULL)
85         return NULL;
86
87     return (filenames->iterator != filenames->end);
88 }
89
90 const char *
91 notmuch_filenames_get (notmuch_filenames_t *filenames)
92 {
93     if (filenames == NULL || filenames->iterator == filenames->end)
94         return NULL;
95
96     if (filenames->filename == NULL) {
97         std::string term = *filenames->iterator;
98
99         filenames->filename = talloc_strdup (filenames,
100                                              term.c_str () +
101                                              filenames->prefix_len);
102     }
103
104     return filenames->filename;
105 }
106
107 void
108 notmuch_filenames_advance (notmuch_filenames_t *filenames)
109 {
110     if (filenames == NULL)
111         return;
112
113     if (filenames->filename) {
114         talloc_free (filenames->filename);
115         filenames->filename = NULL;
116     }
117
118     if (filenames->iterator != filenames->end)
119         filenames->iterator++;
120 }
121
122 void
123 notmuch_filenames_destroy (notmuch_filenames_t *filenames)
124 {
125     if (filenames == NULL)
126         return;
127
128     talloc_free (filenames);
129 }
130
131 struct _notmuch_directory {
132     notmuch_database_t *notmuch;
133     Xapian::docid document_id;
134     Xapian::Document doc;
135     time_t mtime;
136 };
137
138 /* We end up having to call the destructor explicitly because we had
139  * to use "placement new" in order to initialize C++ objects within a
140  * block that we allocated with talloc. So C++ is making talloc
141  * slightly less simple to use, (we wouldn't need
142  * talloc_set_destructor at all otherwise).
143  */
144 static int
145 _notmuch_directory_destructor (notmuch_directory_t *directory)
146 {
147     directory->doc.~Document ();
148
149     return 0;
150 }
151
152 static notmuch_private_status_t
153 find_directory_document (notmuch_database_t *notmuch,
154                          const char *db_path,
155                          Xapian::Document *document)
156 {
157     notmuch_private_status_t status;
158     Xapian::docid doc_id;
159
160     status = _notmuch_database_find_unique_doc_id (notmuch, "directory",
161                                                    db_path, &doc_id);
162     if (status) {
163         *document = Xapian::Document ();
164         return status;
165     }
166
167     *document = notmuch->xapian_db->get_document (doc_id);
168     return NOTMUCH_PRIVATE_STATUS_SUCCESS;
169 }
170
171 notmuch_directory_t *
172 _notmuch_directory_create (notmuch_database_t *notmuch,
173                            const char *path,
174                            notmuch_status_t *status_ret)
175 {
176     Xapian::WritableDatabase *db;
177     notmuch_directory_t *directory;
178     notmuch_private_status_t private_status;
179     const char *db_path;
180
181     *status_ret = NOTMUCH_STATUS_SUCCESS;
182
183     path = _notmuch_database_relative_path (notmuch, path);
184
185     if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY) {
186         fprintf (stderr, "Attempted to update a read-only database.\n");
187         *status_ret = NOTMUCH_STATUS_READONLY_DATABASE;
188         return NULL;
189     }
190
191     db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
192
193     directory = talloc (notmuch, notmuch_directory_t);
194     if (unlikely (directory == NULL))
195         return NULL;
196
197     directory->notmuch = notmuch;
198
199     /* "placement new"---not actually allocating memory */
200     new (&directory->doc) Xapian::Document;
201
202     talloc_set_destructor (directory, _notmuch_directory_destructor);
203
204     db_path = _notmuch_database_get_directory_db_path (path);
205
206     try {
207         Xapian::TermIterator i, end;
208
209         private_status = find_directory_document (notmuch, db_path,
210                                                   &directory->doc);
211         directory->document_id = directory->doc.get_docid ();
212
213         if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
214             void *local = talloc_new (directory);
215             const char *parent, *basename;
216             Xapian::docid parent_id;
217             char *term = talloc_asprintf (local, "%s%s",
218                                           _find_prefix ("directory"), db_path);
219             directory->doc.add_term (term);
220
221             directory->doc.set_data (path);
222
223             _notmuch_database_split_path (local, path, &parent, &basename);
224
225             _notmuch_database_find_directory_id (notmuch, parent, &parent_id);
226
227             if (basename) {
228                 term = talloc_asprintf (local, "%s%u:%s",
229                                         _find_prefix ("directory-direntry"),
230                                         parent_id, basename);
231                 directory->doc.add_term (term);
232             }
233
234             directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
235                                       Xapian::sortable_serialise (0));
236
237             directory->document_id = db->add_document (directory->doc);
238             talloc_free (local);
239         }
240
241         directory->mtime = Xapian::sortable_unserialise (
242             directory->doc.get_value (NOTMUCH_VALUE_TIMESTAMP));
243     } catch (const Xapian::Error &error) {
244         fprintf (stderr,
245                  "A Xapian exception occurred creating a directory: %s.\n",
246                  error.get_msg().c_str());
247         notmuch->exception_reported = TRUE;
248         notmuch_directory_destroy (directory);
249         *status_ret = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
250         return NULL;
251     }
252
253     if (db_path != path)
254         free ((char *) db_path);
255
256     return directory;
257 }
258
259 unsigned int
260 _notmuch_directory_get_document_id (notmuch_directory_t *directory)
261 {
262     return directory->document_id;
263 }
264
265 notmuch_status_t
266 notmuch_directory_set_mtime (notmuch_directory_t *directory,
267                              time_t mtime)
268 {
269     notmuch_database_t *notmuch = directory->notmuch;
270     Xapian::WritableDatabase *db;
271
272     if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY) {
273         fprintf (stderr, "Attempted to update a read-only database.\n");
274         return NOTMUCH_STATUS_READONLY_DATABASE;
275     }
276
277     db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
278
279     try {
280         directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
281                                    Xapian::sortable_serialise (mtime));
282
283         db->replace_document (directory->document_id, directory->doc);
284     } catch (const Xapian::Error &error) {
285         fprintf (stderr,
286                  "A Xapian exception occurred setting directory mtime: %s.\n",
287                  error.get_msg().c_str());
288         notmuch->exception_reported = TRUE;
289         return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
290     }
291
292     return NOTMUCH_STATUS_SUCCESS;
293 }
294
295 time_t
296 notmuch_directory_get_mtime (notmuch_directory_t *directory)
297 {
298     return directory->mtime;
299 }
300
301 notmuch_filenames_t *
302 notmuch_directory_get_child_files (notmuch_directory_t *directory)
303 {
304     char *term;
305     notmuch_filenames_t *child_files;
306
307     term = talloc_asprintf (directory, "%s%u:",
308                             _find_prefix ("file-direntry"),
309                             directory->document_id);
310
311     child_files = _notmuch_filenames_create (directory,
312                                              directory->notmuch, term);
313
314     talloc_free (term);
315
316     return child_files;
317 }
318
319 notmuch_filenames_t *
320 notmuch_directory_get_child_directories (notmuch_directory_t *directory)
321 {
322     char *term;
323     notmuch_filenames_t *child_directories;
324
325     term = talloc_asprintf (directory, "%s%u:",
326                             _find_prefix ("directory-direntry"),
327                             directory->document_id);
328
329     child_directories = _notmuch_filenames_create (directory,
330                                                    directory->notmuch, term);
331
332     talloc_free (term);
333
334     return child_directories;
335 }
336
337 void
338 notmuch_directory_destroy (notmuch_directory_t *directory)
339 {
340     talloc_free (directory);
341 }