lib: Expose query debug output via an environment variable
[notmuch] / lib / directory.cc
1 /* directory.cc - Results of directory-based searches from a notmuch database
2  *
3  * Copyright © 2009 Carl Worth
4  *
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.
9  *
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.
14  *
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/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #include "notmuch-private.h"
22 #include "database-private.h"
23
24 /* Create an iterator to iterate over the basenames of files (or
25  * directories) that all share a common parent directory.
26  */
27 static notmuch_filenames_t *
28 _create_filenames_for_terms_with_prefix (void *ctx,
29                                          notmuch_database_t *notmuch,
30                                          const char *prefix)
31 {
32     notmuch_string_list_t *filename_list;
33     Xapian::TermIterator i, end;
34
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,
38                                                              prefix);
39     if (unlikely (filename_list == NULL))
40         return NULL;
41
42     return _notmuch_filenames_create (ctx, filename_list);
43 }
44
45 struct _notmuch_directory {
46     notmuch_database_t *notmuch;
47     Xapian::docid document_id;
48     Xapian::Document doc;
49     time_t mtime;
50 };
51
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).
57  */
58 static int
59 _notmuch_directory_destructor (notmuch_directory_t *directory)
60 {
61     directory->doc.~Document ();
62
63     return 0;
64 }
65
66 static notmuch_private_status_t
67 find_directory_document (notmuch_database_t *notmuch,
68                          const char *db_path,
69                          Xapian::Document *document)
70 {
71     notmuch_private_status_t status;
72     Xapian::docid doc_id;
73
74     status = _notmuch_database_find_unique_doc_id (notmuch, "directory",
75                                                    db_path, &doc_id);
76     if (status) {
77         *document = Xapian::Document ();
78         return status;
79     }
80
81     *document = notmuch->xapian_db->get_document (doc_id);
82     return NOTMUCH_PRIVATE_STATUS_SUCCESS;
83 }
84
85 notmuch_directory_t *
86 _notmuch_directory_create (notmuch_database_t *notmuch,
87                            const char *path,
88                            notmuch_status_t *status_ret)
89 {
90     Xapian::WritableDatabase *db;
91     notmuch_directory_t *directory;
92     notmuch_private_status_t private_status;
93     const char *db_path;
94
95     *status_ret = NOTMUCH_STATUS_SUCCESS;
96
97     path = _notmuch_database_relative_path (notmuch, path);
98
99     if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
100         INTERNAL_ERROR ("Failure to ensure database is writable");
101
102     db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
103
104     directory = talloc (notmuch, notmuch_directory_t);
105     if (unlikely (directory == NULL))
106         return NULL;
107
108     directory->notmuch = notmuch;
109
110     /* "placement new"---not actually allocating memory */
111     new (&directory->doc) Xapian::Document;
112
113     talloc_set_destructor (directory, _notmuch_directory_destructor);
114
115     db_path = _notmuch_database_get_directory_db_path (path);
116
117     try {
118         Xapian::TermIterator i, end;
119
120         private_status = find_directory_document (notmuch, db_path,
121                                                   &directory->doc);
122         directory->document_id = directory->doc.get_docid ();
123
124         if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
125             void *local = talloc_new (directory);
126             const char *parent, *basename;
127             Xapian::docid parent_id;
128             char *term = talloc_asprintf (local, "%s%s",
129                                           _find_prefix ("directory"), db_path);
130             directory->doc.add_term (term, 0);
131
132             directory->doc.set_data (path);
133
134             _notmuch_database_split_path (local, path, &parent, &basename);
135
136             _notmuch_database_find_directory_id (notmuch, parent, &parent_id);
137
138             if (basename) {
139                 term = talloc_asprintf (local, "%s%u:%s",
140                                         _find_prefix ("directory-direntry"),
141                                         parent_id, basename);
142                 directory->doc.add_term (term, 0);
143             }
144
145             directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
146                                       Xapian::sortable_serialise (0));
147
148             directory->document_id = _notmuch_database_generate_doc_id (notmuch);
149             db->replace_document (directory->document_id, directory->doc);
150             talloc_free (local);
151         }
152
153         directory->mtime = Xapian::sortable_unserialise (
154             directory->doc.get_value (NOTMUCH_VALUE_TIMESTAMP));
155     } catch (const Xapian::Error &error) {
156         fprintf (stderr,
157                  "A Xapian exception occurred creating a directory: %s.\n",
158                  error.get_msg().c_str());
159         notmuch->exception_reported = TRUE;
160         notmuch_directory_destroy (directory);
161         *status_ret = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
162         return NULL;
163     }
164
165     if (db_path != path)
166         free ((char *) db_path);
167
168     return directory;
169 }
170
171 unsigned int
172 _notmuch_directory_get_document_id (notmuch_directory_t *directory)
173 {
174     return directory->document_id;
175 }
176
177 notmuch_status_t
178 notmuch_directory_set_mtime (notmuch_directory_t *directory,
179                              time_t mtime)
180 {
181     notmuch_database_t *notmuch = directory->notmuch;
182     Xapian::WritableDatabase *db;
183     notmuch_status_t status;
184
185     status = _notmuch_database_ensure_writable (notmuch);
186     if (status)
187         return status;
188
189     db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
190
191     try {
192         directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
193                                    Xapian::sortable_serialise (mtime));
194
195         db->replace_document (directory->document_id, directory->doc);
196     } catch (const Xapian::Error &error) {
197         fprintf (stderr,
198                  "A Xapian exception occurred setting directory mtime: %s.\n",
199                  error.get_msg().c_str());
200         notmuch->exception_reported = TRUE;
201         return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
202     }
203
204     return NOTMUCH_STATUS_SUCCESS;
205 }
206
207 time_t
208 notmuch_directory_get_mtime (notmuch_directory_t *directory)
209 {
210     return directory->mtime;
211 }
212
213 notmuch_filenames_t *
214 notmuch_directory_get_child_files (notmuch_directory_t *directory)
215 {
216     char *term;
217     notmuch_filenames_t *child_files;
218
219     term = talloc_asprintf (directory, "%s%u:",
220                             _find_prefix ("file-direntry"),
221                             directory->document_id);
222
223     child_files = _create_filenames_for_terms_with_prefix (directory,
224                                                            directory->notmuch,
225                                                            term);
226
227     talloc_free (term);
228
229     return child_files;
230 }
231
232 notmuch_filenames_t *
233 notmuch_directory_get_child_directories (notmuch_directory_t *directory)
234 {
235     char *term;
236     notmuch_filenames_t *child_directories;
237
238     term = talloc_asprintf (directory, "%s%u:",
239                             _find_prefix ("directory-direntry"),
240                             directory->document_id);
241
242     child_directories = _create_filenames_for_terms_with_prefix (directory,
243                                                  directory->notmuch, term);
244
245     talloc_free (term);
246
247     return child_directories;
248 }
249
250 void
251 notmuch_directory_destroy (notmuch_directory_t *directory)
252 {
253     talloc_free (directory);
254 }