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_filename_list_t *filename_list;
37 Xapian::TermIterator i, end;
38 int prefix_len = strlen (prefix);
40 filename_list = _notmuch_filename_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_filename_list_add_filename (filename_list, term.c_str () +
54 return _notmuch_filenames_create (ctx, filename_list);
57 struct _notmuch_directory {
58 notmuch_database_t *notmuch;
59 Xapian::docid document_id;
64 /* We end up having to call the destructor explicitly because we had
65 * to use "placement new" in order to initialize C++ objects within a
66 * block that we allocated with talloc. So C++ is making talloc
67 * slightly less simple to use, (we wouldn't need
68 * talloc_set_destructor at all otherwise).
71 _notmuch_directory_destructor (notmuch_directory_t *directory)
73 directory->doc.~Document ();
78 static notmuch_private_status_t
79 find_directory_document (notmuch_database_t *notmuch,
81 Xapian::Document *document)
83 notmuch_private_status_t status;
86 status = _notmuch_database_find_unique_doc_id (notmuch, "directory",
89 *document = Xapian::Document ();
93 *document = notmuch->xapian_db->get_document (doc_id);
94 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
98 _notmuch_directory_create (notmuch_database_t *notmuch,
100 notmuch_status_t *status_ret)
102 Xapian::WritableDatabase *db;
103 notmuch_directory_t *directory;
104 notmuch_private_status_t private_status;
107 *status_ret = NOTMUCH_STATUS_SUCCESS;
109 path = _notmuch_database_relative_path (notmuch, path);
111 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
112 INTERNAL_ERROR ("Failure to ensure database is writable");
114 db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
116 directory = talloc (notmuch, notmuch_directory_t);
117 if (unlikely (directory == NULL))
120 directory->notmuch = notmuch;
122 /* "placement new"---not actually allocating memory */
123 new (&directory->doc) Xapian::Document;
125 talloc_set_destructor (directory, _notmuch_directory_destructor);
127 db_path = _notmuch_database_get_directory_db_path (path);
130 Xapian::TermIterator i, end;
132 private_status = find_directory_document (notmuch, db_path,
134 directory->document_id = directory->doc.get_docid ();
136 if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
137 void *local = talloc_new (directory);
138 const char *parent, *basename;
139 Xapian::docid parent_id;
140 char *term = talloc_asprintf (local, "%s%s",
141 _find_prefix ("directory"), db_path);
142 directory->doc.add_term (term, 0);
144 directory->doc.set_data (path);
146 _notmuch_database_split_path (local, path, &parent, &basename);
148 _notmuch_database_find_directory_id (notmuch, parent, &parent_id);
151 term = talloc_asprintf (local, "%s%u:%s",
152 _find_prefix ("directory-direntry"),
153 parent_id, basename);
154 directory->doc.add_term (term, 0);
157 directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
158 Xapian::sortable_serialise (0));
160 directory->document_id = _notmuch_database_generate_doc_id (notmuch);
161 db->replace_document (directory->document_id, directory->doc);
165 directory->mtime = Xapian::sortable_unserialise (
166 directory->doc.get_value (NOTMUCH_VALUE_TIMESTAMP));
167 } catch (const Xapian::Error &error) {
169 "A Xapian exception occurred creating a directory: %s.\n",
170 error.get_msg().c_str());
171 notmuch->exception_reported = TRUE;
172 notmuch_directory_destroy (directory);
173 *status_ret = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
178 free ((char *) db_path);
184 _notmuch_directory_get_document_id (notmuch_directory_t *directory)
186 return directory->document_id;
190 notmuch_directory_set_mtime (notmuch_directory_t *directory,
193 notmuch_database_t *notmuch = directory->notmuch;
194 Xapian::WritableDatabase *db;
195 notmuch_status_t status;
197 status = _notmuch_database_ensure_writable (notmuch);
201 db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
204 directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
205 Xapian::sortable_serialise (mtime));
207 db->replace_document (directory->document_id, directory->doc);
208 } catch (const Xapian::Error &error) {
210 "A Xapian exception occurred setting directory mtime: %s.\n",
211 error.get_msg().c_str());
212 notmuch->exception_reported = TRUE;
213 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
216 return NOTMUCH_STATUS_SUCCESS;
220 notmuch_directory_get_mtime (notmuch_directory_t *directory)
222 return directory->mtime;
225 notmuch_filenames_t *
226 notmuch_directory_get_child_files (notmuch_directory_t *directory)
229 notmuch_filenames_t *child_files;
231 term = talloc_asprintf (directory, "%s%u:",
232 _find_prefix ("file-direntry"),
233 directory->document_id);
235 child_files = _create_filenames_for_terms_with_prefix (directory,
244 notmuch_filenames_t *
245 notmuch_directory_get_child_directories (notmuch_directory_t *directory)
248 notmuch_filenames_t *child_directories;
250 term = talloc_asprintf (directory, "%s%u:",
251 _find_prefix ("directory-direntry"),
252 directory->document_id);
254 child_directories = _create_filenames_for_terms_with_prefix (directory,
255 directory->notmuch, term);
259 return child_directories;
263 notmuch_directory_destroy (notmuch_directory_t *directory)
265 talloc_free (directory);