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 * The code here is general enough to be reused for any case of
28 * iterating over the non-prefixed portion of terms sharing a common
31 static notmuch_filenames_t *
32 _create_filenames_for_terms_with_prefix (void *ctx,
33 notmuch_database_t *notmuch,
36 notmuch_string_list_t *filename_list;
37 Xapian::TermIterator i, end;
38 int prefix_len = strlen (prefix);
40 filename_list = _notmuch_string_list_create (ctx);
41 if (unlikely (filename_list == NULL))
44 end = notmuch->xapian_db->allterms_end (prefix);
46 for (i = notmuch->xapian_db->allterms_begin (prefix); i != end; i++)
48 std::string term = *i;
50 _notmuch_string_list_append (filename_list, term.c_str () + prefix_len);
53 return _notmuch_filenames_create (ctx, filename_list);
56 struct _notmuch_directory {
57 notmuch_database_t *notmuch;
58 Xapian::docid document_id;
63 /* We end up having to call the destructor explicitly because we had
64 * to use "placement new" in order to initialize C++ objects within a
65 * block that we allocated with talloc. So C++ is making talloc
66 * slightly less simple to use, (we wouldn't need
67 * talloc_set_destructor at all otherwise).
70 _notmuch_directory_destructor (notmuch_directory_t *directory)
72 directory->doc.~Document ();
77 static notmuch_private_status_t
78 find_directory_document (notmuch_database_t *notmuch,
80 Xapian::Document *document)
82 notmuch_private_status_t status;
85 status = _notmuch_database_find_unique_doc_id (notmuch, "directory",
88 *document = Xapian::Document ();
92 *document = notmuch->xapian_db->get_document (doc_id);
93 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
97 _notmuch_directory_create (notmuch_database_t *notmuch,
99 notmuch_status_t *status_ret)
101 Xapian::WritableDatabase *db;
102 notmuch_directory_t *directory;
103 notmuch_private_status_t private_status;
106 *status_ret = NOTMUCH_STATUS_SUCCESS;
108 path = _notmuch_database_relative_path (notmuch, path);
110 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
111 INTERNAL_ERROR ("Failure to ensure database is writable");
113 db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
115 directory = talloc (notmuch, notmuch_directory_t);
116 if (unlikely (directory == NULL))
119 directory->notmuch = notmuch;
121 /* "placement new"---not actually allocating memory */
122 new (&directory->doc) Xapian::Document;
124 talloc_set_destructor (directory, _notmuch_directory_destructor);
126 db_path = _notmuch_database_get_directory_db_path (path);
129 Xapian::TermIterator i, end;
131 private_status = find_directory_document (notmuch, db_path,
133 directory->document_id = directory->doc.get_docid ();
135 if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
136 void *local = talloc_new (directory);
137 const char *parent, *basename;
138 Xapian::docid parent_id;
139 char *term = talloc_asprintf (local, "%s%s",
140 _find_prefix ("directory"), db_path);
141 directory->doc.add_term (term, 0);
143 directory->doc.set_data (path);
145 _notmuch_database_split_path (local, path, &parent, &basename);
147 _notmuch_database_find_directory_id (notmuch, parent, &parent_id);
150 term = talloc_asprintf (local, "%s%u:%s",
151 _find_prefix ("directory-direntry"),
152 parent_id, basename);
153 directory->doc.add_term (term, 0);
156 directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
157 Xapian::sortable_serialise (0));
159 directory->document_id = _notmuch_database_generate_doc_id (notmuch);
160 db->replace_document (directory->document_id, directory->doc);
164 directory->mtime = Xapian::sortable_unserialise (
165 directory->doc.get_value (NOTMUCH_VALUE_TIMESTAMP));
166 } catch (const Xapian::Error &error) {
168 "A Xapian exception occurred creating a directory: %s.\n",
169 error.get_msg().c_str());
170 notmuch->exception_reported = TRUE;
171 notmuch_directory_destroy (directory);
172 *status_ret = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
177 free ((char *) db_path);
183 _notmuch_directory_get_document_id (notmuch_directory_t *directory)
185 return directory->document_id;
189 notmuch_directory_set_mtime (notmuch_directory_t *directory,
192 notmuch_database_t *notmuch = directory->notmuch;
193 Xapian::WritableDatabase *db;
194 notmuch_status_t status;
196 status = _notmuch_database_ensure_writable (notmuch);
200 db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
203 directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
204 Xapian::sortable_serialise (mtime));
206 db->replace_document (directory->document_id, directory->doc);
207 } catch (const Xapian::Error &error) {
209 "A Xapian exception occurred setting directory mtime: %s.\n",
210 error.get_msg().c_str());
211 notmuch->exception_reported = TRUE;
212 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
215 return NOTMUCH_STATUS_SUCCESS;
219 notmuch_directory_get_mtime (notmuch_directory_t *directory)
221 return directory->mtime;
224 notmuch_filenames_t *
225 notmuch_directory_get_child_files (notmuch_directory_t *directory)
228 notmuch_filenames_t *child_files;
230 term = talloc_asprintf (directory, "%s%u:",
231 _find_prefix ("file-direntry"),
232 directory->document_id);
234 child_files = _create_filenames_for_terms_with_prefix (directory,
243 notmuch_filenames_t *
244 notmuch_directory_get_child_directories (notmuch_directory_t *directory)
247 notmuch_filenames_t *child_directories;
249 term = talloc_asprintf (directory, "%s%u:",
250 _find_prefix ("directory-direntry"),
251 directory->document_id);
253 child_directories = _create_filenames_for_terms_with_prefix (directory,
254 directory->notmuch, term);
258 return child_directories;
262 notmuch_directory_destroy (notmuch_directory_t *directory)
264 talloc_free (directory);