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"
24 /* Create an iterator to iterate over the basenames of files (or
25 * directories) that all share a common parent directory.
27 static notmuch_filenames_t *
28 _create_filenames_for_terms_with_prefix (void *ctx,
29 notmuch_database_t *notmuch,
32 notmuch_string_list_t *filename_list;
33 Xapian::TermIterator i, end;
35 i = notmuch->xapian_db->allterms_begin();
36 end = notmuch->xapian_db->allterms_end();
37 filename_list = _notmuch_database_get_terms_with_prefix (ctx, i, end,
39 if (unlikely (filename_list == NULL))
42 return _notmuch_filenames_create (ctx, filename_list);
45 struct _notmuch_directory {
46 notmuch_database_t *notmuch;
47 Xapian::docid document_id;
52 /* We end up having to call the destructor explicitly because we had
53 * to use "placement new" in order to initialize C++ objects within a
54 * block that we allocated with talloc. So C++ is making talloc
55 * slightly less simple to use, (we wouldn't need
56 * talloc_set_destructor at all otherwise).
59 _notmuch_directory_destructor (notmuch_directory_t *directory)
61 directory->doc.~Document ();
66 static notmuch_private_status_t
67 find_directory_document (notmuch_database_t *notmuch,
69 Xapian::Document *document)
71 notmuch_private_status_t status;
74 status = _notmuch_database_find_unique_doc_id (notmuch, "directory",
77 *document = Xapian::Document ();
81 *document = notmuch->xapian_db->get_document (doc_id);
82 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
86 _notmuch_directory_create (notmuch_database_t *notmuch,
88 notmuch_status_t *status_ret)
90 Xapian::WritableDatabase *db;
91 notmuch_directory_t *directory;
92 notmuch_private_status_t private_status;
95 *status_ret = NOTMUCH_STATUS_SUCCESS;
97 path = _notmuch_database_relative_path (notmuch, path);
99 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
100 INTERNAL_ERROR ("Failure to ensure database is writable");
102 db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
104 directory = talloc (notmuch, notmuch_directory_t);
105 if (unlikely (directory == NULL))
108 directory->notmuch = notmuch;
110 /* "placement new"---not actually allocating memory */
111 new (&directory->doc) Xapian::Document;
113 talloc_set_destructor (directory, _notmuch_directory_destructor);
115 db_path = _notmuch_database_get_directory_db_path (path);
118 Xapian::TermIterator i, end;
120 private_status = find_directory_document (notmuch, db_path,
122 directory->document_id = directory->doc.get_docid ();
124 if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
125 void *local = talloc_new (directory);
126 const char *parent, *basename;
127 Xapian::docid parent_id;
128 char *term = talloc_asprintf (local, "%s%s",
129 _find_prefix ("directory"), db_path);
130 directory->doc.add_term (term, 0);
132 directory->doc.set_data (path);
134 _notmuch_database_split_path (local, path, &parent, &basename);
136 _notmuch_database_find_directory_id (notmuch, parent, &parent_id);
139 term = talloc_asprintf (local, "%s%u:%s",
140 _find_prefix ("directory-direntry"),
141 parent_id, basename);
142 directory->doc.add_term (term, 0);
145 directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
146 Xapian::sortable_serialise (0));
148 directory->document_id = _notmuch_database_generate_doc_id (notmuch);
149 db->replace_document (directory->document_id, directory->doc);
153 directory->mtime = Xapian::sortable_unserialise (
154 directory->doc.get_value (NOTMUCH_VALUE_TIMESTAMP));
155 } catch (const Xapian::Error &error) {
157 "A Xapian exception occurred creating a directory: %s.\n",
158 error.get_msg().c_str());
159 notmuch->exception_reported = TRUE;
160 notmuch_directory_destroy (directory);
161 *status_ret = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
166 free ((char *) db_path);
172 _notmuch_directory_get_document_id (notmuch_directory_t *directory)
174 return directory->document_id;
178 notmuch_directory_set_mtime (notmuch_directory_t *directory,
181 notmuch_database_t *notmuch = directory->notmuch;
182 Xapian::WritableDatabase *db;
183 notmuch_status_t status;
185 status = _notmuch_database_ensure_writable (notmuch);
189 db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
192 directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
193 Xapian::sortable_serialise (mtime));
195 db->replace_document (directory->document_id, directory->doc);
196 } catch (const Xapian::Error &error) {
198 "A Xapian exception occurred setting directory mtime: %s.\n",
199 error.get_msg().c_str());
200 notmuch->exception_reported = TRUE;
201 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
204 return NOTMUCH_STATUS_SUCCESS;
208 notmuch_directory_get_mtime (notmuch_directory_t *directory)
210 return directory->mtime;
213 notmuch_filenames_t *
214 notmuch_directory_get_child_files (notmuch_directory_t *directory)
217 notmuch_filenames_t *child_files;
219 term = talloc_asprintf (directory, "%s%u:",
220 _find_prefix ("file-direntry"),
221 directory->document_id);
223 child_files = _create_filenames_for_terms_with_prefix (directory,
232 notmuch_filenames_t *
233 notmuch_directory_get_child_directories (notmuch_directory_t *directory)
236 notmuch_filenames_t *child_directories;
238 term = talloc_asprintf (directory, "%s%u:",
239 _find_prefix ("directory-direntry"),
240 directory->document_id);
242 child_directories = _create_filenames_for_terms_with_prefix (directory,
243 directory->notmuch, term);
247 return child_directories;
251 notmuch_directory_destroy (notmuch_directory_t *directory)
253 talloc_free (directory);