]> git.notmuchmail.org Git - notmuch/blob - lib/directory.cc
b9c3d77f663fa3e2b4dbfb596e4e3915546375bc
[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 https://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 /* Find or create a directory document.
86  *
87  * 'path' should be a path relative to the path of 'database', or else
88  * should be an absolute path with initial components that match the
89  * path of 'database'.
90  *
91  * If (flags & NOTMUCH_FIND_CREATE), then the directory document will
92  * be created if it does not exist.  Otherwise, if the directory
93  * document does not exist, *status_ret is set to
94  * NOTMUCH_STATUS_SUCCESS and this returns NULL.
95  */
96 notmuch_directory_t *
97 _notmuch_directory_find_or_create (notmuch_database_t *notmuch,
98                                    const char *path,
99                                    notmuch_find_flags_t flags,
100                                    notmuch_status_t *status_ret)
101 {
102     notmuch_directory_t *directory;
103     notmuch_private_status_t private_status;
104     const char *db_path;
105     bool create = (flags & NOTMUCH_FIND_CREATE);
106
107     if (! (notmuch->features & NOTMUCH_FEATURE_DIRECTORY_DOCS)) {
108         *status_ret = NOTMUCH_STATUS_UPGRADE_REQUIRED;
109         return NULL;
110     }
111
112     *status_ret = NOTMUCH_STATUS_SUCCESS;
113
114     path = _notmuch_database_relative_path (notmuch, path);
115
116     if (create && _notmuch_database_mode (notmuch) == NOTMUCH_DATABASE_MODE_READ_ONLY)
117         INTERNAL_ERROR ("Failure to ensure database is writable");
118
119     directory = talloc (notmuch, notmuch_directory_t);
120     if (unlikely (directory == NULL)) {
121         *status_ret = NOTMUCH_STATUS_OUT_OF_MEMORY;
122         return NULL;
123     }
124
125     directory->notmuch = notmuch;
126
127     /* "placement new"---not actually allocating memory */
128     new (&directory->doc) Xapian::Document;
129
130     talloc_set_destructor (directory, _notmuch_directory_destructor);
131
132     db_path = _notmuch_database_get_directory_db_path (path);
133
134     try {
135         Xapian::TermIterator i, end;
136
137         private_status = find_directory_document (notmuch, db_path,
138                                                   &directory->doc);
139         directory->document_id = directory->doc.get_docid ();
140
141         if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
142             if (! create) {
143                 notmuch_directory_destroy (directory);
144                 directory = NULL;
145                 *status_ret = NOTMUCH_STATUS_SUCCESS;
146                 goto DONE;
147             }
148
149             void *local = talloc_new (directory);
150             const char *parent, *basename;
151             Xapian::docid parent_id;
152             char *term = talloc_asprintf (local, "%s%s",
153                                           _find_prefix ("directory"), db_path);
154             directory->doc.add_term (term, 0);
155
156             directory->doc.set_data (path);
157
158             _notmuch_database_split_path (local, path, &parent, &basename);
159
160             *status_ret = _notmuch_database_find_directory_id (
161                 notmuch, parent, NOTMUCH_FIND_CREATE, &parent_id);
162             if (*status_ret) {
163                 notmuch_directory_destroy (directory);
164                 directory = NULL;
165                 goto DONE;
166             }
167
168             if (basename) {
169                 term = talloc_asprintf (local, "%s%u:%s",
170                                         _find_prefix ("directory-direntry"),
171                                         parent_id, basename);
172                 directory->doc.add_term (term, 0);
173             }
174
175             directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
176                                       Xapian::sortable_serialise (0));
177
178             directory->document_id = _notmuch_database_generate_doc_id (notmuch);
179             directory->notmuch->
180                 writable_xapian_db
181                 -> replace_document (directory->document_id, directory->doc);
182             talloc_free (local);
183         }
184
185         directory->mtime = Xapian::sortable_unserialise (
186             directory->doc.get_value (NOTMUCH_VALUE_TIMESTAMP));
187     } catch (const Xapian::Error &error) {
188         _notmuch_database_log (notmuch,
189                                "A Xapian exception occurred finding/creating a directory: %s.\n",
190                                error.get_msg ().c_str ());
191         notmuch->exception_reported = true;
192         notmuch_directory_destroy (directory);
193         directory = NULL;
194         *status_ret = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
195     }
196
197   DONE:
198     if (db_path != path)
199         free ((char *) db_path);
200
201     return directory;
202 }
203
204 unsigned int
205 _notmuch_directory_get_document_id (notmuch_directory_t *directory)
206 {
207     return directory->document_id;
208 }
209
210 notmuch_status_t
211 notmuch_directory_set_mtime (notmuch_directory_t *directory,
212                              time_t mtime)
213 {
214     notmuch_database_t *notmuch = directory->notmuch;
215     notmuch_status_t status;
216
217     status = _notmuch_database_ensure_writable (notmuch);
218     if (status)
219         return status;
220
221     try {
222         directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
223                                   Xapian::sortable_serialise (mtime));
224
225         directory->notmuch
226             ->writable_xapian_db->replace_document (directory->document_id, directory->doc);
227
228         directory->mtime = mtime;
229
230     } catch (const Xapian::Error &error) {
231         _notmuch_database_log (notmuch,
232                                "A Xapian exception occurred setting directory mtime: %s.\n",
233                                error.get_msg ().c_str ());
234         notmuch->exception_reported = true;
235         return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
236     }
237
238     return NOTMUCH_STATUS_SUCCESS;
239 }
240
241 time_t
242 notmuch_directory_get_mtime (notmuch_directory_t *directory)
243 {
244     return directory->mtime;
245 }
246
247 notmuch_filenames_t *
248 notmuch_directory_get_child_files (notmuch_directory_t *directory)
249 {
250     char *term;
251     notmuch_filenames_t *child_files;
252
253     term = talloc_asprintf (directory, "%s%u:",
254                             _find_prefix ("file-direntry"),
255                             directory->document_id);
256
257     child_files = _create_filenames_for_terms_with_prefix (directory,
258                                                            directory->notmuch,
259                                                            term);
260
261     talloc_free (term);
262
263     return child_files;
264 }
265
266 notmuch_filenames_t *
267 notmuch_directory_get_child_directories (notmuch_directory_t *directory)
268 {
269     char *term;
270     notmuch_filenames_t *child_directories;
271
272     term = talloc_asprintf (directory, "%s%u:",
273                             _find_prefix ("directory-direntry"),
274                             directory->document_id);
275
276     child_directories = _create_filenames_for_terms_with_prefix (directory,
277                                                                  directory->notmuch, term);
278
279     talloc_free (term);
280
281     return child_directories;
282 }
283
284 notmuch_status_t
285 notmuch_directory_delete (notmuch_directory_t *directory)
286 {
287     notmuch_status_t status;
288
289     status = _notmuch_database_ensure_writable (directory->notmuch);
290     if (status)
291         return status;
292
293     try {
294         directory->notmuch->
295             writable_xapian_db->delete_document (directory->document_id);
296     } catch (const Xapian::Error &error) {
297         _notmuch_database_log (directory->notmuch,
298                                "A Xapian exception occurred deleting directory entry: %s.\n",
299                                error.get_msg ().c_str ());
300         directory->notmuch->exception_reported = true;
301         status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
302     }
303     notmuch_directory_destroy (directory);
304
305     return NOTMUCH_STATUS_SUCCESS;
306 }
307
308 void
309 notmuch_directory_destroy (notmuch_directory_t *directory)
310 {
311     talloc_free (directory);
312 }