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_filenames_t *filenames;
37 Xapian::TermIterator i, end;
38 int prefix_len = strlen (prefix);
40 filenames = _notmuch_filenames_create (ctx);
41 if (unlikely (filenames == 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_filenames_add_filename (filenames, term.c_str () +
54 _notmuch_filenames_move_to_first (filenames);
59 struct _notmuch_directory {
60 notmuch_database_t *notmuch;
61 Xapian::docid document_id;
66 /* We end up having to call the destructor explicitly because we had
67 * to use "placement new" in order to initialize C++ objects within a
68 * block that we allocated with talloc. So C++ is making talloc
69 * slightly less simple to use, (we wouldn't need
70 * talloc_set_destructor at all otherwise).
73 _notmuch_directory_destructor (notmuch_directory_t *directory)
75 directory->doc.~Document ();
80 static notmuch_private_status_t
81 find_directory_document (notmuch_database_t *notmuch,
83 Xapian::Document *document)
85 notmuch_private_status_t status;
88 status = _notmuch_database_find_unique_doc_id (notmuch, "directory",
91 *document = Xapian::Document ();
95 *document = notmuch->xapian_db->get_document (doc_id);
96 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
100 _notmuch_directory_create (notmuch_database_t *notmuch,
102 notmuch_status_t *status_ret)
104 Xapian::WritableDatabase *db;
105 notmuch_directory_t *directory;
106 notmuch_private_status_t private_status;
109 *status_ret = NOTMUCH_STATUS_SUCCESS;
111 path = _notmuch_database_relative_path (notmuch, path);
113 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
114 INTERNAL_ERROR ("Failure to ensure database is writable");
116 db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
118 directory = talloc (notmuch, notmuch_directory_t);
119 if (unlikely (directory == NULL))
122 directory->notmuch = notmuch;
124 /* "placement new"---not actually allocating memory */
125 new (&directory->doc) Xapian::Document;
127 talloc_set_destructor (directory, _notmuch_directory_destructor);
129 db_path = _notmuch_database_get_directory_db_path (path);
132 Xapian::TermIterator i, end;
134 private_status = find_directory_document (notmuch, db_path,
136 directory->document_id = directory->doc.get_docid ();
138 if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
139 void *local = talloc_new (directory);
140 const char *parent, *basename;
141 Xapian::docid parent_id;
142 char *term = talloc_asprintf (local, "%s%s",
143 _find_prefix ("directory"), db_path);
144 directory->doc.add_term (term, 0);
146 directory->doc.set_data (path);
148 _notmuch_database_split_path (local, path, &parent, &basename);
150 _notmuch_database_find_directory_id (notmuch, parent, &parent_id);
153 term = talloc_asprintf (local, "%s%u:%s",
154 _find_prefix ("directory-direntry"),
155 parent_id, basename);
156 directory->doc.add_term (term, 0);
159 directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
160 Xapian::sortable_serialise (0));
162 directory->document_id = _notmuch_database_generate_doc_id (notmuch);
163 db->replace_document (directory->document_id, directory->doc);
167 directory->mtime = Xapian::sortable_unserialise (
168 directory->doc.get_value (NOTMUCH_VALUE_TIMESTAMP));
169 } catch (const Xapian::Error &error) {
171 "A Xapian exception occurred creating a directory: %s.\n",
172 error.get_msg().c_str());
173 notmuch->exception_reported = TRUE;
174 notmuch_directory_destroy (directory);
175 *status_ret = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
180 free ((char *) db_path);
186 _notmuch_directory_get_document_id (notmuch_directory_t *directory)
188 return directory->document_id;
192 notmuch_directory_set_mtime (notmuch_directory_t *directory,
195 notmuch_database_t *notmuch = directory->notmuch;
196 Xapian::WritableDatabase *db;
197 notmuch_status_t status;
199 status = _notmuch_database_ensure_writable (notmuch);
203 db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
206 directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
207 Xapian::sortable_serialise (mtime));
209 db->replace_document (directory->document_id, directory->doc);
210 } catch (const Xapian::Error &error) {
212 "A Xapian exception occurred setting directory mtime: %s.\n",
213 error.get_msg().c_str());
214 notmuch->exception_reported = TRUE;
215 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
218 return NOTMUCH_STATUS_SUCCESS;
222 notmuch_directory_get_mtime (notmuch_directory_t *directory)
224 return directory->mtime;
227 notmuch_filenames_t *
228 notmuch_directory_get_child_files (notmuch_directory_t *directory)
231 notmuch_filenames_t *child_files;
233 term = talloc_asprintf (directory, "%s%u:",
234 _find_prefix ("file-direntry"),
235 directory->document_id);
237 child_files = _create_filenames_for_terms_with_prefix (directory,
246 notmuch_filenames_t *
247 notmuch_directory_get_child_directories (notmuch_directory_t *directory)
250 notmuch_filenames_t *child_directories;
252 term = talloc_asprintf (directory, "%s%u:",
253 _find_prefix ("directory-direntry"),
254 directory->document_id);
256 child_directories = _create_filenames_for_terms_with_prefix (directory,
257 directory->notmuch, term);
261 return child_directories;
265 notmuch_directory_destroy (notmuch_directory_t *directory)
267 talloc_free (directory);