Debian Codegraph @

service/url-db.c

raw symbols SCIP
1 /**
2 * Copyright © 2014 Canonical, Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it under
5 * the terms of the GNU Lesser General Public License version 3, as published by
6 * the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
10 * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Author: Ted Gould <[email protected]>
17 *
18 */
19
20 #include <glib.h>
21 #include "url-db.h"
22 #include "create-db-sql.h"
23
24 sqlite3 *
25 url_db_create_database ()
26 {
27 const gchar * cachedir = g_getenv("URL_DISPATCHER_CACHE_DIR"); /* Mostly for testing */
28
29 if (G_LIKELY(cachedir == NULL)) {
30 cachedir = g_get_user_cache_dir();
31 }
32
33 gchar * urldispatchercachedir = g_build_filename(cachedir, "lomiri-url-dispatcher", NULL);
34 if (!g_file_test(urldispatchercachedir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
35 gint cachedirokay = g_mkdir_with_parents(urldispatchercachedir, 1 << 6 | 1 << 7 | 1 << 8); // 700
36
37 if (cachedirokay != 0) {
38 g_warning("Unable to make or find cache directory '%s'", urldispatchercachedir);
39 g_free(urldispatchercachedir);
40 return NULL;
41 }
42 }
43
44 gchar * dbfilename = g_build_filename(urldispatchercachedir, "urls-" DB_SCHEMA_VERSION ".db", NULL);
45 g_free(urldispatchercachedir);
46
47 int open_status = SQLITE_ERROR;
48 sqlite3 * db = NULL;
49
50 open_status = sqlite3_open(dbfilename, &db);
51 if (open_status != SQLITE_OK) {
52 g_warning("Unable to open URL database: %s", sqlite3_errmsg(db));
53 g_free(dbfilename);
54 if (db != NULL) {
55 sqlite3_close(db);
56 }
57 return NULL;
58 }
59 sqlite3_busy_timeout(db, 3000);
60
61 g_free(dbfilename);
62
63 int exec_status = SQLITE_ERROR;
64 char * failstring = NULL;
65
66 /* If the tables already exist, this command does nothing, because
67 * the SQL says to create "if not exists". We run it always to
68 * make this robust against the case where we are killed between
69 * creating the db file and creating the tables.
70 */
71 exec_status = sqlite3_exec(db, create_db_sql, NULL, NULL, &failstring);
72
73 if (exec_status != SQLITE_OK) {
74 g_warning("Unable to create tables: %s", failstring);
75 sqlite3_free(failstring);
76 sqlite3_close(db);
77 return NULL;
78 }
79
80 return db;
81 }
82
83 gboolean
84 url_db_get_file_motification_time (sqlite3 * db, const gchar * filename, guint64 * time)
85 {
86 g_return_val_if_fail(db != NULL, FALSE);
87 g_return_val_if_fail(filename != NULL, FALSE);
88 g_return_val_if_fail(time != NULL, FALSE);
89
90 sqlite3_stmt * stmt;
91 if (sqlite3_prepare_v2(db,
92 "select timestamp from configfiles where name = ?1",
93 -1, /* length */
94 &stmt,
95 NULL) != SQLITE_OK) {
96 g_warning("Unable to parse SQL to get file times: %s", sqlite3_errmsg(db));
97 return FALSE;
98 }
99
100 sqlite3_bind_text(stmt, 1, filename, -1, SQLITE_TRANSIENT);
101
102 gboolean valueset = FALSE;
103 int exec_status = SQLITE_ROW;
104 while ((exec_status = sqlite3_step(stmt)) == SQLITE_ROW) {
105 if (*time != 0) {
106 g_warning("Seemingly two timestamps for the same file");
107 }
108
109 *time = sqlite3_column_int(stmt, 0);
110 valueset = TRUE;
111 }
112
113 sqlite3_finalize(stmt);
114
115 if (exec_status != SQLITE_DONE) {
116 g_warning("Unable to execute insert");
117 return FALSE;
118 }
119
120 return valueset;
121 }
122
123 gboolean
124 url_db_set_file_motification_time (sqlite3 * db, const gchar * filename, guint64 time)
125 {
126 g_return_val_if_fail(db != NULL, FALSE);
127 g_return_val_if_fail(filename != NULL, FALSE);
128
129 sqlite3_stmt * stmt;
130 if (sqlite3_prepare_v2(db,
131 "insert or replace into configfiles values (?1, ?2)",
132 -1, /* length */
133 &stmt,
134 NULL) != SQLITE_OK) {
135 g_warning("Unable to parse SQL to set file times: %s", sqlite3_errmsg(db));
136 return FALSE;
137 }
138
139 sqlite3_bind_text(stmt, 1, filename, -1, SQLITE_TRANSIENT);
140 sqlite3_bind_int(stmt, 2, time);
141
142 int exec_status = SQLITE_ROW;
143 while ((exec_status = sqlite3_step(stmt)) == SQLITE_ROW) {}
144
145 sqlite3_finalize(stmt);
146
147 if (exec_status != SQLITE_DONE) {
148 g_warning("Unable to execute insert");
149 return FALSE;
150 }
151
152 return TRUE;
153 }
154
155 gboolean
156 url_db_insert_url (sqlite3 * db, const gchar * filename, const gchar * protocol, const gchar * domainsuffix)
157 {
158 g_return_val_if_fail(db != NULL, FALSE);
159 g_return_val_if_fail(filename != NULL, FALSE);
160 g_return_val_if_fail(protocol != NULL, FALSE);
161
162 if (domainsuffix == NULL) {
163 domainsuffix = "";
164 }
165
166 sqlite3_stmt * stmt;
167 if (sqlite3_prepare_v2(db,
168 "insert or replace into urls select rowid, ?2, ?3 from configfiles where name = ?1",
169 -1, /* length */
170 &stmt,
171 NULL) != SQLITE_OK) {
172 g_warning("Unable to parse SQL to insert");
173 return FALSE;
174 }
175
176 sqlite3_bind_text(stmt, 1, filename, -1, SQLITE_TRANSIENT);
177 sqlite3_bind_text(stmt, 2, protocol, -1, SQLITE_TRANSIENT);
178 sqlite3_bind_text(stmt, 3, domainsuffix, -1, SQLITE_TRANSIENT);
179
180 int exec_status = SQLITE_ROW;
181 while ((exec_status = sqlite3_step(stmt)) == SQLITE_ROW) {}
182
183 sqlite3_finalize(stmt);
184
185 if (exec_status != SQLITE_DONE) {
186 g_warning("Unable to execute insert: %s", sqlite3_errmsg(db));
187 return FALSE;
188 }
189
190 return TRUE;
191 }
192
193 gchar *
194 url_db_find_url (sqlite3 * db, const gchar * protocol, const gchar * domainsuffix)
195 {
196 g_return_val_if_fail(db != NULL, NULL);
197 g_return_val_if_fail(protocol != NULL, NULL);
198
199 if (domainsuffix == NULL) {
200 domainsuffix = "";
201 }
202
203 sqlite3_stmt * stmt;
204 if (sqlite3_prepare_v2(db,
205 "select configfiles.name from configfiles, urls where urls.sourcefile = configfiles.rowid and urls.protocol = ?1 and ?2 like '%' || urls.domainsuffix order by length(urls.domainsuffix) desc limit 1",
206 -1, /* length */
207 &stmt,
208 NULL) != SQLITE_OK) {
209 g_warning("Unable to parse SQL to find url: %s", sqlite3_errmsg(db));
210 return NULL;
211 }
212
213 sqlite3_bind_text(stmt, 1, protocol, -1, SQLITE_TRANSIENT);
214 sqlite3_bind_text(stmt, 2, domainsuffix, -1, SQLITE_TRANSIENT);
215
216 gchar * filename = NULL;
217 int exec_status = SQLITE_ROW;
218 while ((exec_status = sqlite3_step(stmt)) == SQLITE_ROW && filename == NULL) {
219 filename = g_strdup((const gchar *)sqlite3_column_text(stmt, 0));
220 }
221
222 gchar * output = NULL;
223 if (filename != NULL) {
224 g_debug("Found file: '%s'", filename);
225 gchar * basename = g_path_get_basename(filename);
226 gchar * suffix = g_strrstr(basename, ".url-dispatcher");
227 if (suffix != NULL) /* This should never not happen, but it's too scary not to throw this 'if' in */
228 suffix[0] = '\0';
229 output = g_strdup(basename);
230 g_free(basename);
231 g_free(filename);
232 }
233
234 sqlite3_finalize(stmt);
235
236 if (exec_status != SQLITE_DONE) {
237 g_warning("Unable to execute insert: %s", sqlite3_errmsg(db));
238 g_free(output);
239 return NULL;
240 }
241
242 return output;
243 }
244
245 GList *
246 url_db_files_for_dir (sqlite3 * db, const gchar * dir)
247 {
248 g_return_val_if_fail(db != NULL, NULL);
249
250 if (dir == NULL) {
251 dir = "";
252 }
253
254 sqlite3_stmt * stmt;
255 if (sqlite3_prepare_v2(db,
256 "select name from configfiles where name like ?1",
257 -1, /* length */
258 &stmt,
259 NULL) != SQLITE_OK) {
260 g_warning("Unable to parse SQL to find files: %s", sqlite3_errmsg(db));
261 return NULL;
262 }
263
264 gchar * dir_search = g_strdup_printf("%s%%", dir);
265 sqlite3_bind_text(stmt, 1, dir_search, -1, SQLITE_TRANSIENT);
266
267 GList * filelist = NULL;
268 int exec_status = SQLITE_ROW;
269 while ((exec_status = sqlite3_step(stmt)) == SQLITE_ROW) {
270 gchar * name = g_strdup((const gchar *)sqlite3_column_text(stmt, 0));
271 filelist = g_list_prepend(filelist, name);
272 }
273
274 sqlite3_finalize(stmt);
275 g_free(dir_search);
276
277 if (exec_status != SQLITE_DONE) {
278 g_warning("Unable to execute insert: %s", sqlite3_errmsg(db));
279 g_list_free_full(filelist, g_free);
280 return NULL;
281 }
282
283 return filelist;
284 }
285
286 /* Remove a file from the database along with all URLs that were
287 built because of it. */
288 gboolean
289 url_db_remove_file (sqlite3 * db, const gchar * path)
290 {
291 g_return_val_if_fail(db != NULL, FALSE);
292 g_return_val_if_fail(path != NULL, FALSE);
293
294 /* Start a transaction so the database doesn't end up
295 in an inconsistent state */
296 if (sqlite3_exec(db, "begin", NULL, NULL, NULL) != SQLITE_OK) {
297 g_warning("Unable to start transaction to delete: %s", sqlite3_errmsg(db));
298 return FALSE;
299 }
300
301 /* Remove all URLs for file */
302 sqlite3_stmt * stmt;
303 if (sqlite3_prepare_v2(db,
304 "delete from urls where sourcefile in (select rowid from configfiles where name = ?1);",
305 -1, /* length */
306 &stmt,
307 NULL) != SQLITE_OK) {
308 g_warning("Unable to parse SQL to remove urls: %s", sqlite3_errmsg(db));
309 goto rollback;
310 }
311
312 sqlite3_bind_text(stmt, 1, path, -1, SQLITE_TRANSIENT);
313
314 int exec_status = SQLITE_ROW;
315 while ((exec_status = sqlite3_step(stmt)) == SQLITE_ROW) {
316 }
317
318 sqlite3_finalize(stmt);
319
320 if (exec_status != SQLITE_DONE) {
321 g_warning("Unable to execute removal of URLs: %s", sqlite3_errmsg(db));
322 goto rollback;
323 }
324
325 /* Remove references to the file */
326 stmt = NULL;
327
328 if (sqlite3_prepare_v2(db,
329 "delete from configfiles where name = ?1;",
330 -1, /* length */
331 &stmt,
332 NULL) != SQLITE_OK) {
333 g_warning("Unable to parse SQL to remove urls: %s", sqlite3_errmsg(db));
334 goto rollback;
335 }
336
337 sqlite3_bind_text(stmt, 1, path, -1, SQLITE_TRANSIENT);
338
339 while ((exec_status = sqlite3_step(stmt)) == SQLITE_ROW) {
340 }
341
342 sqlite3_finalize(stmt);
343
344 if (exec_status != SQLITE_DONE) {
345 g_warning("Unable to execute removal of file: %s", sqlite3_errmsg(db));
346 goto rollback;
347 }
348
349 /* Commit the full transaction */
350 if (sqlite3_exec(db, "commit", NULL, NULL, NULL) != SQLITE_OK) {
351 g_warning("Unable to commit transaction to delete: %s", sqlite3_errmsg(db));
352 goto rollback;
353 }
354
355 return TRUE;
356
357 rollback:
358
359 if (sqlite3_exec(db, "rollback", NULL, NULL, NULL) != SQLITE_OK) {
360 g_warning("Unable to rollback transaction: %s", sqlite3_errmsg(db));
361 }
362 return FALSE;
363 }