1 /* directory.cc - Results of directory-based searches from a notmuch database
3 * Copyright © 2009 Carl Worth
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.
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.
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/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 #include "notmuch-private.h"
22 #include "database-private.h"
26 struct _notmuch_filenames {
27 Xapian::TermIterator iterator;
28 Xapian::TermIterator end;
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).
40 _notmuch_filenames_destructor (notmuch_filenames_t *filenames)
42 filenames->iterator.~TermIterator ();
43 filenames->end.~TermIterator ();
48 /* Create an iterator to iterate over the basenames of files (or
49 * directories) that all share a common parent directory.
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
56 _notmuch_filenames_create (void *ctx,
57 notmuch_database_t *notmuch,
60 notmuch_filenames_t *filenames;
62 filenames = talloc (ctx, notmuch_filenames_t);
63 if (unlikely (filenames == NULL))
66 new (&filenames->iterator) Xapian::TermIterator ();
67 new (&filenames->end) Xapian::TermIterator ();
69 talloc_set_destructor (filenames, _notmuch_filenames_destructor);
71 filenames->iterator = notmuch->xapian_db->allterms_begin (prefix);
72 filenames->end = notmuch->xapian_db->allterms_end (prefix);
74 filenames->prefix_len = strlen (prefix);
76 filenames->filename = NULL;
82 notmuch_filenames_valid (notmuch_filenames_t *filenames)
84 if (filenames == NULL)
87 return (filenames->iterator != filenames->end);
91 notmuch_filenames_get (notmuch_filenames_t *filenames)
93 if (filenames == NULL || filenames->iterator == filenames->end)
96 if (filenames->filename == NULL) {
97 std::string term = *filenames->iterator;
99 filenames->filename = talloc_strdup (filenames,
101 filenames->prefix_len);
104 return filenames->filename;
108 notmuch_filenames_move_to_next (notmuch_filenames_t *filenames)
110 if (filenames == NULL)
113 if (filenames->filename) {
114 talloc_free (filenames->filename);
115 filenames->filename = NULL;
118 if (filenames->iterator != filenames->end)
119 filenames->iterator++;
123 notmuch_filenames_destroy (notmuch_filenames_t *filenames)
125 if (filenames == NULL)
128 talloc_free (filenames);
131 struct _notmuch_directory {
132 notmuch_database_t *notmuch;
133 Xapian::docid document_id;
134 Xapian::Document doc;
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).
145 _notmuch_directory_destructor (notmuch_directory_t *directory)
147 directory->doc.~Document ();
152 static notmuch_private_status_t
153 find_directory_document (notmuch_database_t *notmuch,
155 Xapian::Document *document)
157 notmuch_private_status_t status;
158 Xapian::docid doc_id;
160 status = _notmuch_database_find_unique_doc_id (notmuch, "directory",
163 *document = Xapian::Document ();
167 *document = notmuch->xapian_db->get_document (doc_id);
168 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
171 notmuch_directory_t *
172 _notmuch_directory_create (notmuch_database_t *notmuch,
174 notmuch_status_t *status_ret)
176 Xapian::WritableDatabase *db;
177 notmuch_directory_t *directory;
178 notmuch_private_status_t private_status;
181 *status_ret = NOTMUCH_STATUS_SUCCESS;
183 path = _notmuch_database_relative_path (notmuch, path);
185 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
186 INTERNAL_ERROR ("Failure to ensure database is writable");
188 db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
190 directory = talloc (notmuch, notmuch_directory_t);
191 if (unlikely (directory == NULL))
194 directory->notmuch = notmuch;
196 /* "placement new"---not actually allocating memory */
197 new (&directory->doc) Xapian::Document;
199 talloc_set_destructor (directory, _notmuch_directory_destructor);
201 db_path = _notmuch_database_get_directory_db_path (path);
204 Xapian::TermIterator i, end;
206 private_status = find_directory_document (notmuch, db_path,
208 directory->document_id = directory->doc.get_docid ();
210 if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
211 void *local = talloc_new (directory);
212 const char *parent, *basename;
213 Xapian::docid parent_id;
214 char *term = talloc_asprintf (local, "%s%s",
215 _find_prefix ("directory"), db_path);
216 directory->doc.add_term (term, 0);
218 directory->doc.set_data (path);
220 _notmuch_database_split_path (local, path, &parent, &basename);
222 _notmuch_database_find_directory_id (notmuch, parent, &parent_id);
225 term = talloc_asprintf (local, "%s%u:%s",
226 _find_prefix ("directory-direntry"),
227 parent_id, basename);
228 directory->doc.add_term (term, 0);
231 directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
232 Xapian::sortable_serialise (0));
234 directory->document_id = db->add_document (directory->doc);
238 directory->mtime = Xapian::sortable_unserialise (
239 directory->doc.get_value (NOTMUCH_VALUE_TIMESTAMP));
240 } catch (const Xapian::Error &error) {
242 "A Xapian exception occurred creating a directory: %s.\n",
243 error.get_msg().c_str());
244 notmuch->exception_reported = TRUE;
245 notmuch_directory_destroy (directory);
246 *status_ret = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
251 free ((char *) db_path);
257 _notmuch_directory_get_document_id (notmuch_directory_t *directory)
259 return directory->document_id;
263 notmuch_directory_set_mtime (notmuch_directory_t *directory,
266 notmuch_database_t *notmuch = directory->notmuch;
267 Xapian::WritableDatabase *db;
268 notmuch_status_t status;
270 status = _notmuch_database_ensure_writable (notmuch);
274 db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
277 directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
278 Xapian::sortable_serialise (mtime));
280 db->replace_document (directory->document_id, directory->doc);
281 } catch (const Xapian::Error &error) {
283 "A Xapian exception occurred setting directory mtime: %s.\n",
284 error.get_msg().c_str());
285 notmuch->exception_reported = TRUE;
286 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
289 return NOTMUCH_STATUS_SUCCESS;
293 notmuch_directory_get_mtime (notmuch_directory_t *directory)
295 return directory->mtime;
298 notmuch_filenames_t *
299 notmuch_directory_get_child_files (notmuch_directory_t *directory)
302 notmuch_filenames_t *child_files;
304 term = talloc_asprintf (directory, "%s%u:",
305 _find_prefix ("file-direntry"),
306 directory->document_id);
308 child_files = _notmuch_filenames_create (directory,
309 directory->notmuch, term);
316 notmuch_filenames_t *
317 notmuch_directory_get_child_directories (notmuch_directory_t *directory)
320 notmuch_filenames_t *child_directories;
322 term = talloc_asprintf (directory, "%s%u:",
323 _find_prefix ("directory-direntry"),
324 directory->document_id);
326 child_directories = _notmuch_filenames_create (directory,
327 directory->notmuch, term);
331 return child_directories;
335 notmuch_directory_destroy (notmuch_directory_t *directory)
337 talloc_free (directory);