]> git.cworth.org Git - obsolete/notmuch-old/blob - lib/directory.cc
emacs: Fix "no such file or directory" error
[obsolete/notmuch-old] / 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 /* 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_create (notmuch_database_t *notmuch,
98                            const char *path,
99                            notmuch_find_flags_t flags,
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     notmuch_bool_t create = (flags & NOTMUCH_FIND_CREATE);
107
108     *status_ret = NOTMUCH_STATUS_SUCCESS;
109
110     path = _notmuch_database_relative_path (notmuch, path);
111
112     if (create && notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
113         INTERNAL_ERROR ("Failure to ensure database is writable");
114
115     directory = talloc (notmuch, notmuch_directory_t);
116     if (unlikely (directory == NULL)) {
117         *status_ret = NOTMUCH_STATUS_OUT_OF_MEMORY;
118         return NULL;
119     }
120
121     directory->notmuch = notmuch;
122
123     /* "placement new"---not actually allocating memory */
124     new (&directory->doc) Xapian::Document;
125
126     talloc_set_destructor (directory, _notmuch_directory_destructor);
127
128     db_path = _notmuch_database_get_directory_db_path (path);
129
130     try {
131         Xapian::TermIterator i, end;
132
133         private_status = find_directory_document (notmuch, db_path,
134                                                   &directory->doc);
135         directory->document_id = directory->doc.get_docid ();
136
137         if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
138             if (!create) {
139                 notmuch_directory_destroy (directory);
140                 directory = NULL;
141                 *status_ret = NOTMUCH_STATUS_SUCCESS;
142                 goto DONE;
143             }
144
145             void *local = talloc_new (directory);
146             const char *parent, *basename;
147             Xapian::docid parent_id;
148             char *term = talloc_asprintf (local, "%s%s",
149                                           _find_prefix ("directory"), db_path);
150             directory->doc.add_term (term, 0);
151
152             directory->doc.set_data (path);
153
154             _notmuch_database_split_path (local, path, &parent, &basename);
155
156             *status_ret = _notmuch_database_find_directory_id (
157                 notmuch, parent, NOTMUCH_FIND_CREATE, &parent_id);
158             if (*status_ret) {
159                 notmuch_directory_destroy (directory);
160                 directory = NULL;
161                 goto DONE;
162             }
163
164             if (basename) {
165                 term = talloc_asprintf (local, "%s%u:%s",
166                                         _find_prefix ("directory-direntry"),
167                                         parent_id, basename);
168                 directory->doc.add_term (term, 0);
169             }
170
171             directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
172                                       Xapian::sortable_serialise (0));
173
174             db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
175
176             directory->document_id = _notmuch_database_generate_doc_id (notmuch);
177             db->replace_document (directory->document_id, directory->doc);
178             talloc_free (local);
179         }
180
181         directory->mtime = Xapian::sortable_unserialise (
182             directory->doc.get_value (NOTMUCH_VALUE_TIMESTAMP));
183     } catch (const Xapian::Error &error) {
184         fprintf (stderr,
185                  "A Xapian exception occurred creating a directory: %s.\n",
186                  error.get_msg().c_str());
187         notmuch->exception_reported = TRUE;
188         notmuch_directory_destroy (directory);
189         directory = NULL;
190         *status_ret = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
191     }
192
193   DONE:
194     if (db_path != path)
195         free ((char *) db_path);
196
197     return directory;
198 }
199
200 unsigned int
201 _notmuch_directory_get_document_id (notmuch_directory_t *directory)
202 {
203     return directory->document_id;
204 }
205
206 notmuch_status_t
207 notmuch_directory_set_mtime (notmuch_directory_t *directory,
208                              time_t mtime)
209 {
210     notmuch_database_t *notmuch = directory->notmuch;
211     Xapian::WritableDatabase *db;
212     notmuch_status_t status;
213
214     status = _notmuch_database_ensure_writable (notmuch);
215     if (status)
216         return status;
217
218     db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
219
220     try {
221         directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
222                                    Xapian::sortable_serialise (mtime));
223
224         db->replace_document (directory->document_id, directory->doc);
225     } catch (const Xapian::Error &error) {
226         fprintf (stderr,
227                  "A Xapian exception occurred setting directory mtime: %s.\n",
228                  error.get_msg().c_str());
229         notmuch->exception_reported = TRUE;
230         return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
231     }
232
233     return NOTMUCH_STATUS_SUCCESS;
234 }
235
236 time_t
237 notmuch_directory_get_mtime (notmuch_directory_t *directory)
238 {
239     return directory->mtime;
240 }
241
242 notmuch_filenames_t *
243 notmuch_directory_get_child_files (notmuch_directory_t *directory)
244 {
245     char *term;
246     notmuch_filenames_t *child_files;
247
248     term = talloc_asprintf (directory, "%s%u:",
249                             _find_prefix ("file-direntry"),
250                             directory->document_id);
251
252     child_files = _create_filenames_for_terms_with_prefix (directory,
253                                                            directory->notmuch,
254                                                            term);
255
256     talloc_free (term);
257
258     return child_files;
259 }
260
261 notmuch_filenames_t *
262 notmuch_directory_get_child_directories (notmuch_directory_t *directory)
263 {
264     char *term;
265     notmuch_filenames_t *child_directories;
266
267     term = talloc_asprintf (directory, "%s%u:",
268                             _find_prefix ("directory-direntry"),
269                             directory->document_id);
270
271     child_directories = _create_filenames_for_terms_with_prefix (directory,
272                                                  directory->notmuch, term);
273
274     talloc_free (term);
275
276     return child_directories;
277 }
278
279 void
280 notmuch_directory_destroy (notmuch_directory_t *directory)
281 {
282     talloc_free (directory);
283 }