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 #define LOG_XAPIAN_EXCEPTION(directory, error) _log_xapian_exception (__location__, directory, error)
55 _log_xapian_exception (const char *where, notmuch_directory_t *dir, const Xapian::Error error)
57 notmuch_database_t *notmuch = dir->notmuch;
59 _notmuch_database_log (notmuch,
60 "A Xapian exception occurred at %s: %s\n",
62 error.get_msg ().c_str ());
63 notmuch->exception_reported = true;
67 /* We end up having to call the destructor explicitly because we had
68 * to use "placement new" in order to initialize C++ objects within a
69 * block that we allocated with talloc. So C++ is making talloc
70 * slightly less simple to use, (we wouldn't need
71 * talloc_set_destructor at all otherwise).
74 _notmuch_directory_destructor (notmuch_directory_t *directory)
76 directory->doc.~Document ();
81 static notmuch_private_status_t
82 find_directory_document (notmuch_database_t *notmuch,
84 Xapian::Document *document)
86 notmuch_private_status_t status;
89 status = _notmuch_database_find_unique_doc_id (notmuch, "directory",
92 *document = Xapian::Document ();
96 *document = notmuch->xapian_db->get_document (doc_id);
97 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
100 /* Find or create a directory document.
102 * 'path' should be a path relative to the path of 'database', or else
103 * should be an absolute path with initial components that match the
104 * path of 'database'.
106 * If (flags & NOTMUCH_FIND_CREATE), then the directory document will
107 * be created if it does not exist. Otherwise, if the directory
108 * document does not exist, *status_ret is set to
109 * NOTMUCH_STATUS_SUCCESS and this returns NULL.
111 notmuch_directory_t *
112 _notmuch_directory_find_or_create (notmuch_database_t *notmuch,
114 notmuch_find_flags_t flags,
115 notmuch_status_t *status_ret)
117 notmuch_directory_t *directory;
118 notmuch_private_status_t private_status;
120 bool create = (flags & NOTMUCH_FIND_CREATE);
122 if (! (notmuch->features & NOTMUCH_FEATURE_DIRECTORY_DOCS)) {
123 *status_ret = NOTMUCH_STATUS_UPGRADE_REQUIRED;
127 *status_ret = NOTMUCH_STATUS_SUCCESS;
129 path = _notmuch_database_relative_path (notmuch, path);
131 if (create && _notmuch_database_mode (notmuch) == NOTMUCH_DATABASE_MODE_READ_ONLY)
132 INTERNAL_ERROR ("Failure to ensure database is writable");
134 directory = talloc (notmuch, notmuch_directory_t);
135 if (unlikely (directory == NULL)) {
136 *status_ret = NOTMUCH_STATUS_OUT_OF_MEMORY;
140 directory->notmuch = notmuch;
142 /* "placement new"---not actually allocating memory */
143 new (&directory->doc) Xapian::Document;
145 talloc_set_destructor (directory, _notmuch_directory_destructor);
147 db_path = _notmuch_database_get_directory_db_path (path);
150 Xapian::TermIterator i, end;
152 private_status = find_directory_document (notmuch, db_path,
154 directory->document_id = directory->doc.get_docid ();
156 if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
158 notmuch_directory_destroy (directory);
160 *status_ret = NOTMUCH_STATUS_SUCCESS;
164 void *local = talloc_new (directory);
165 const char *parent, *basename;
166 Xapian::docid parent_id;
167 char *term = talloc_asprintf (local, "%s%s",
168 _find_prefix ("directory"), db_path);
169 directory->doc.add_term (term, 0);
171 directory->doc.set_data (path);
173 _notmuch_database_split_path (local, path, &parent, &basename);
175 *status_ret = _notmuch_database_find_directory_id (
176 notmuch, parent, NOTMUCH_FIND_CREATE, &parent_id);
178 notmuch_directory_destroy (directory);
184 term = talloc_asprintf (local, "%s%u:%s",
185 _find_prefix ("directory-direntry"),
186 parent_id, basename);
187 directory->doc.add_term (term, 0);
190 directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
191 Xapian::sortable_serialise (0));
193 directory->document_id = _notmuch_database_generate_doc_id (notmuch);
196 -> replace_document (directory->document_id, directory->doc);
200 directory->mtime = Xapian::sortable_unserialise (
201 directory->doc.get_value (NOTMUCH_VALUE_TIMESTAMP));
202 } catch (const Xapian::Error &error) {
203 _notmuch_database_log (notmuch,
204 "A Xapian exception occurred finding/creating a directory: %s.\n",
205 error.get_msg ().c_str ());
206 notmuch->exception_reported = true;
207 notmuch_directory_destroy (directory);
209 *status_ret = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
214 free ((char *) db_path);
220 _notmuch_directory_get_document_id (notmuch_directory_t *directory)
222 return directory->document_id;
226 notmuch_directory_set_mtime (notmuch_directory_t *directory,
229 notmuch_database_t *notmuch = directory->notmuch;
230 notmuch_status_t status;
232 status = _notmuch_database_ensure_writable (notmuch);
237 directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
238 Xapian::sortable_serialise (mtime));
241 ->writable_xapian_db->replace_document (directory->document_id, directory->doc);
243 directory->mtime = mtime;
245 } catch (const Xapian::Error &error) {
246 _notmuch_database_log (notmuch,
247 "A Xapian exception occurred setting directory mtime: %s.\n",
248 error.get_msg ().c_str ());
249 notmuch->exception_reported = true;
250 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
253 return NOTMUCH_STATUS_SUCCESS;
257 notmuch_directory_get_mtime (notmuch_directory_t *directory)
259 return directory->mtime;
262 notmuch_filenames_t *
263 notmuch_directory_get_child_files (notmuch_directory_t *directory)
266 notmuch_filenames_t *child_files = NULL;
268 term = talloc_asprintf (directory, "%s%u:",
269 _find_prefix ("file-direntry"),
270 directory->document_id);
273 child_files = _create_filenames_for_terms_with_prefix (directory,
276 } catch (Xapian::Error &error) {
277 LOG_XAPIAN_EXCEPTION (directory, error);
285 notmuch_filenames_t *
286 notmuch_directory_get_child_directories (notmuch_directory_t *directory)
289 notmuch_filenames_t *child_directories = NULL;
291 term = talloc_asprintf (directory, "%s%u:",
292 _find_prefix ("directory-direntry"),
293 directory->document_id);
296 child_directories = _create_filenames_for_terms_with_prefix (directory,
297 directory->notmuch, term);
298 } catch (Xapian::Error &error) {
299 LOG_XAPIAN_EXCEPTION (directory, error);
304 return child_directories;
308 notmuch_directory_delete (notmuch_directory_t *directory)
310 notmuch_status_t status;
312 status = _notmuch_database_ensure_writable (directory->notmuch);
318 writable_xapian_db->delete_document (directory->document_id);
319 } catch (const Xapian::Error &error) {
320 _notmuch_database_log (directory->notmuch,
321 "A Xapian exception occurred deleting directory entry: %s.\n",
322 error.get_msg ().c_str ());
323 directory->notmuch->exception_reported = true;
324 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
326 notmuch_directory_destroy (directory);
332 notmuch_directory_destroy (notmuch_directory_t *directory)
334 talloc_free (directory);