]> git.cworth.org Git - notmuch/blob - lib/directory.cc
new: Add all initial tags at once
[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  * 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
29  * prefix.
30  */
31 static notmuch_filenames_t *
32 _create_filenames_for_terms_with_prefix (void *ctx,
33                                          notmuch_database_t *notmuch,
34                                          const char *prefix)
35 {
36     notmuch_filename_list_t *filename_list;
37     Xapian::TermIterator i, end;
38     int prefix_len = strlen (prefix);
39
40     filename_list = _notmuch_filename_list_create (ctx);
41     if (unlikely (filename_list == NULL))
42         return NULL;
43
44     end = notmuch->xapian_db->allterms_end (prefix);
45
46     for (i = notmuch->xapian_db->allterms_begin (prefix); i != end; i++)
47     {
48         std::string term = *i;
49
50         _notmuch_filename_list_add_filename (filename_list, term.c_str () +
51                                              prefix_len);
52     }
53
54     return _notmuch_filenames_create (ctx, filename_list);
55 }
56
57 struct _notmuch_directory {
58     notmuch_database_t *notmuch;
59     Xapian::docid document_id;
60     Xapian::Document doc;
61     time_t mtime;
62 };
63
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).
69  */
70 static int
71 _notmuch_directory_destructor (notmuch_directory_t *directory)
72 {
73     directory->doc.~Document ();
74
75     return 0;
76 }
77
78 static notmuch_private_status_t
79 find_directory_document (notmuch_database_t *notmuch,
80                          const char *db_path,
81                          Xapian::Document *document)
82 {
83     notmuch_private_status_t status;
84     Xapian::docid doc_id;
85
86     status = _notmuch_database_find_unique_doc_id (notmuch, "directory",
87                                                    db_path, &doc_id);
88     if (status) {
89         *document = Xapian::Document ();
90         return status;
91     }
92
93     *document = notmuch->xapian_db->get_document (doc_id);
94     return NOTMUCH_PRIVATE_STATUS_SUCCESS;
95 }
96
97 notmuch_directory_t *
98 _notmuch_directory_create (notmuch_database_t *notmuch,
99                            const char *path,
100                            notmuch_status_t *status_ret)
101 {
102     Xapian::WritableDatabase *db;
103     notmuch_directory_t *directory;
104     notmuch_private_status_t private_status;
105     const char *db_path;
106
107     *status_ret = NOTMUCH_STATUS_SUCCESS;
108
109     path = _notmuch_database_relative_path (notmuch, path);
110
111     if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
112         INTERNAL_ERROR ("Failure to ensure database is writable");
113
114     db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
115
116     directory = talloc (notmuch, notmuch_directory_t);
117     if (unlikely (directory == NULL))
118         return NULL;
119
120     directory->notmuch = notmuch;
121
122     /* "placement new"---not actually allocating memory */
123     new (&directory->doc) Xapian::Document;
124
125     talloc_set_destructor (directory, _notmuch_directory_destructor);
126
127     db_path = _notmuch_database_get_directory_db_path (path);
128
129     try {
130         Xapian::TermIterator i, end;
131
132         private_status = find_directory_document (notmuch, db_path,
133                                                   &directory->doc);
134         directory->document_id = directory->doc.get_docid ();
135
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);
143
144             directory->doc.set_data (path);
145
146             _notmuch_database_split_path (local, path, &parent, &basename);
147
148             _notmuch_database_find_directory_id (notmuch, parent, &parent_id);
149
150             if (basename) {
151                 term = talloc_asprintf (local, "%s%u:%s",
152                                         _find_prefix ("directory-direntry"),
153                                         parent_id, basename);
154                 directory->doc.add_term (term, 0);
155             }
156
157             directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
158                                       Xapian::sortable_serialise (0));
159
160             directory->document_id = _notmuch_database_generate_doc_id (notmuch);
161             db->replace_document (directory->document_id, directory->doc);
162             talloc_free (local);
163         }
164
165         directory->mtime = Xapian::sortable_unserialise (
166             directory->doc.get_value (NOTMUCH_VALUE_TIMESTAMP));
167     } catch (const Xapian::Error &error) {
168         fprintf (stderr,
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;
174         return NULL;
175     }
176
177     if (db_path != path)
178         free ((char *) db_path);
179
180     return directory;
181 }
182
183 unsigned int
184 _notmuch_directory_get_document_id (notmuch_directory_t *directory)
185 {
186     return directory->document_id;
187 }
188
189 notmuch_status_t
190 notmuch_directory_set_mtime (notmuch_directory_t *directory,
191                              time_t mtime)
192 {
193     notmuch_database_t *notmuch = directory->notmuch;
194     Xapian::WritableDatabase *db;
195     notmuch_status_t status;
196
197     status = _notmuch_database_ensure_writable (notmuch);
198     if (status)
199         return status;
200
201     db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
202
203     try {
204         directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
205                                    Xapian::sortable_serialise (mtime));
206
207         db->replace_document (directory->document_id, directory->doc);
208     } catch (const Xapian::Error &error) {
209         fprintf (stderr,
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;
214     }
215
216     return NOTMUCH_STATUS_SUCCESS;
217 }
218
219 time_t
220 notmuch_directory_get_mtime (notmuch_directory_t *directory)
221 {
222     return directory->mtime;
223 }
224
225 notmuch_filenames_t *
226 notmuch_directory_get_child_files (notmuch_directory_t *directory)
227 {
228     char *term;
229     notmuch_filenames_t *child_files;
230
231     term = talloc_asprintf (directory, "%s%u:",
232                             _find_prefix ("file-direntry"),
233                             directory->document_id);
234
235     child_files = _create_filenames_for_terms_with_prefix (directory,
236                                                            directory->notmuch,
237                                                            term);
238
239     talloc_free (term);
240
241     return child_files;
242 }
243
244 notmuch_filenames_t *
245 notmuch_directory_get_child_directories (notmuch_directory_t *directory)
246 {
247     char *term;
248     notmuch_filenames_t *child_directories;
249
250     term = talloc_asprintf (directory, "%s%u:",
251                             _find_prefix ("directory-direntry"),
252                             directory->document_id);
253
254     child_directories = _create_filenames_for_terms_with_prefix (directory,
255                                                  directory->notmuch, term);
256
257     talloc_free (term);
258
259     return child_directories;
260 }
261
262 void
263 notmuch_directory_destroy (notmuch_directory_t *directory)
264 {
265     talloc_free (directory);
266 }