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 https://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;
85 /* Find or create a directory document.
87 * 'path' should be a path relative to the path of 'database', or else
88 * should be an absolute path with initial components that match the
91 * If (flags & NOTMUCH_FIND_CREATE), then the directory document will
92 * be created if it does not exist. Otherwise, if the directory
93 * document does not exist, *status_ret is set to
94 * NOTMUCH_STATUS_SUCCESS and this returns NULL.
97 _notmuch_directory_create (notmuch_database_t *notmuch,
99 notmuch_find_flags_t flags,
100 notmuch_status_t *status_ret)
102 Xapian::WritableDatabase *db;
103 notmuch_directory_t *directory;
104 notmuch_private_status_t private_status;
106 bool create = (flags & NOTMUCH_FIND_CREATE);
108 if (! (notmuch->features & NOTMUCH_FEATURE_DIRECTORY_DOCS)) {
109 *status_ret = NOTMUCH_STATUS_UPGRADE_REQUIRED;
113 *status_ret = NOTMUCH_STATUS_SUCCESS;
115 path = _notmuch_database_relative_path (notmuch, path);
117 if (create && notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
118 INTERNAL_ERROR ("Failure to ensure database is writable");
120 directory = talloc (notmuch, notmuch_directory_t);
121 if (unlikely (directory == NULL)) {
122 *status_ret = NOTMUCH_STATUS_OUT_OF_MEMORY;
126 directory->notmuch = notmuch;
128 /* "placement new"---not actually allocating memory */
129 new (&directory->doc) Xapian::Document;
131 talloc_set_destructor (directory, _notmuch_directory_destructor);
133 db_path = _notmuch_database_get_directory_db_path (path);
136 Xapian::TermIterator i, end;
138 private_status = find_directory_document (notmuch, db_path,
140 directory->document_id = directory->doc.get_docid ();
142 if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
144 notmuch_directory_destroy (directory);
146 *status_ret = NOTMUCH_STATUS_SUCCESS;
150 void *local = talloc_new (directory);
151 const char *parent, *basename;
152 Xapian::docid parent_id;
153 char *term = talloc_asprintf (local, "%s%s",
154 _find_prefix ("directory"), db_path);
155 directory->doc.add_term (term, 0);
157 directory->doc.set_data (path);
159 _notmuch_database_split_path (local, path, &parent, &basename);
161 *status_ret = _notmuch_database_find_directory_id (
162 notmuch, parent, NOTMUCH_FIND_CREATE, &parent_id);
164 notmuch_directory_destroy (directory);
170 term = talloc_asprintf (local, "%s%u:%s",
171 _find_prefix ("directory-direntry"),
172 parent_id, basename);
173 directory->doc.add_term (term, 0);
176 directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
177 Xapian::sortable_serialise (0));
179 db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
181 directory->document_id = _notmuch_database_generate_doc_id (notmuch);
182 db->replace_document (directory->document_id, directory->doc);
186 directory->mtime = Xapian::sortable_unserialise (
187 directory->doc.get_value (NOTMUCH_VALUE_TIMESTAMP));
188 } catch (const Xapian::Error &error) {
189 _notmuch_database_log (notmuch,
190 "A Xapian exception occurred creating a directory: %s.\n",
191 error.get_msg().c_str());
192 notmuch->exception_reported = true;
193 notmuch_directory_destroy (directory);
195 *status_ret = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
200 free ((char *) db_path);
206 _notmuch_directory_get_document_id (notmuch_directory_t *directory)
208 return directory->document_id;
212 notmuch_directory_set_mtime (notmuch_directory_t *directory,
215 notmuch_database_t *notmuch = directory->notmuch;
216 Xapian::WritableDatabase *db;
217 notmuch_status_t status;
219 status = _notmuch_database_ensure_writable (notmuch);
223 db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
226 directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
227 Xapian::sortable_serialise (mtime));
229 db->replace_document (directory->document_id, directory->doc);
231 directory->mtime = mtime;
233 } catch (const Xapian::Error &error) {
234 _notmuch_database_log (notmuch,
235 "A Xapian exception occurred setting directory mtime: %s.\n",
236 error.get_msg().c_str());
237 notmuch->exception_reported = true;
238 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
241 return NOTMUCH_STATUS_SUCCESS;
245 notmuch_directory_get_mtime (notmuch_directory_t *directory)
247 return directory->mtime;
250 notmuch_filenames_t *
251 notmuch_directory_get_child_files (notmuch_directory_t *directory)
254 notmuch_filenames_t *child_files;
256 term = talloc_asprintf (directory, "%s%u:",
257 _find_prefix ("file-direntry"),
258 directory->document_id);
260 child_files = _create_filenames_for_terms_with_prefix (directory,
269 notmuch_filenames_t *
270 notmuch_directory_get_child_directories (notmuch_directory_t *directory)
273 notmuch_filenames_t *child_directories;
275 term = talloc_asprintf (directory, "%s%u:",
276 _find_prefix ("directory-direntry"),
277 directory->document_id);
279 child_directories = _create_filenames_for_terms_with_prefix (directory,
280 directory->notmuch, term);
284 return child_directories;
288 notmuch_directory_delete (notmuch_directory_t *directory)
290 notmuch_status_t status;
291 Xapian::WritableDatabase *db;
293 status = _notmuch_database_ensure_writable (directory->notmuch);
298 db = static_cast <Xapian::WritableDatabase *> (directory->notmuch->xapian_db);
299 db->delete_document (directory->document_id);
300 } catch (const Xapian::Error &error) {
301 _notmuch_database_log (directory->notmuch,
302 "A Xapian exception occurred deleting directory entry: %s.\n",
303 error.get_msg().c_str());
304 directory->notmuch->exception_reported = true;
305 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
307 notmuch_directory_destroy (directory);
309 return NOTMUCH_STATUS_SUCCESS;
313 notmuch_directory_destroy (notmuch_directory_t *directory)
315 talloc_free (directory);