afh.h: Trivial whitespace fix.
[paraslash.git] / blob.c
1 /*
2 * Copyright (C) 2007 Andre Noll <maan@tuebingen.mpg.de>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file blob.c Macros and functions for blob handling. */
8
9 #include <regex.h>
10 #include <fnmatch.h>
11 #include <osl.h>
12
13 #include "para.h"
14 #include "error.h"
15 #include "crypt.h"
16 #include "string.h"
17 #include "afh.h"
18 #include "afs.h"
19 #include "ipc.h"
20 #include "portable_io.h"
21 #include "sideband.h"
22 #include "command.h"
23
24 /**
25 * Compare two osl objects pointing to unsigned integers of 32 bit size.
26 *
27 * \param obj1 Pointer to the first integer.
28 * \param obj2 Pointer to the second integer.
29 *
30 * \return The values required for an osl compare function.
31 *
32 * \sa osl_compare_func, osl_hash_compare().
33 */
34 static int uint32_compare(const struct osl_object *obj1, const struct osl_object *obj2)
35 {
36 uint32_t d1 = read_u32((const char *)obj1->data);
37 uint32_t d2 = read_u32((const char *)obj2->data);
38
39 if (d1 < d2)
40 return 1;
41 if (d1 > d2)
42 return -1;
43 return 0;
44 }
45
46 static struct osl_column_description blob_cols[] = {
47 [BLOBCOL_ID] = {
48 .storage_type = OSL_MAPPED_STORAGE,
49 .storage_flags = OSL_RBTREE | OSL_UNIQUE | OSL_FIXED_SIZE,
50 .name = "id",
51 .data_size = 4,
52 .compare_function = uint32_compare
53 },
54 [BLOBCOL_NAME] = {
55 .storage_type = OSL_MAPPED_STORAGE,
56 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
57 .name = "name",
58 .compare_function = string_compare
59 },
60 [BLOBCOL_DEF] = {
61 .storage_type = OSL_DISK_STORAGE,
62 .storage_flags = 0,
63 .name = "definition"
64 }
65 };
66
67 /** Define an osl table description for a blob table. */
68 #define DEFINE_BLOB_TABLE_DESC(table_name) \
69 static struct osl_table_description table_name ## _table_desc = { \
70 .name = #table_name, \
71 .num_columns = NUM_BLOB_COLUMNS, \
72 .flags = OSL_LARGE_TABLE, \
73 .column_descriptions = blob_cols \
74 };
75
76 /** Define a pointer to an osl blob table with a canonical name. */
77 #define DEFINE_BLOB_TABLE_PTR(table_name) struct osl_table *table_name ## _table;
78
79
80 /** Define a blob table. */
81 #define INIT_BLOB_TABLE(table_name) \
82 DEFINE_BLOB_TABLE_DESC(table_name); \
83 DEFINE_BLOB_TABLE_PTR(table_name);
84
85 /* doxygen isn't smart enough to recognize these */
86 /** \cond blob_table */
87 INIT_BLOB_TABLE(lyrics);
88 INIT_BLOB_TABLE(images);
89 INIT_BLOB_TABLE(moods);
90 INIT_BLOB_TABLE(playlists);
91 /** \endcond blob_table */
92
93 /** Flags that may be passed to the \p ls functions of each blob type. */
94 enum blob_ls_flags {
95 /** List both id and name. */
96 BLOB_LS_FLAG_LONG = 1,
97 /** Reverse sort order. */
98 BLOB_LS_FLAG_REVERSE = 2,
99 /** Sort by id instead of name. */
100 BLOB_LS_FLAG_SORT_BY_ID = 4,
101 };
102
103 static int print_blob(struct osl_table *table, struct osl_row *row,
104 const char *name, void *data)
105 {
106 struct afs_callback_arg *aca = data;
107 uint32_t flags = *(uint32_t *)aca->query.data;
108 struct osl_object obj;
109 uint32_t id;
110 int ret;
111
112 if (!(flags & BLOB_LS_FLAG_LONG)) {
113 para_printf(&aca->pbout, "%s\n", name);
114 return 0;
115 }
116 ret = osl(osl_get_object(table, row, BLOBCOL_ID, &obj));
117 if (ret < 0) {
118 para_printf(&aca->pbout, "cannot list %s\n", name);
119 return ret;
120 }
121 id = *(uint32_t *)obj.data;
122 para_printf(&aca->pbout, "%u\t%s\n", id, name);
123 return 1;
124 }
125
126 static int com_lsblob_callback(struct osl_table *table,
127 struct afs_callback_arg *aca)
128 {
129 uint32_t flags = *(uint32_t *)aca->query.data;
130 struct pattern_match_data pmd = {
131 .table = table,
132 .patterns = {.data = (char *)aca->query.data + sizeof(uint32_t),
133 .size = aca->query.size - sizeof(uint32_t)},
134 .pm_flags = PM_NO_PATTERN_MATCHES_EVERYTHING | PM_SKIP_EMPTY_NAME,
135 .match_col_num = BLOBCOL_NAME,
136 .data = aca,
137 .action = print_blob,
138 };
139 int ret;
140
141 if (flags & BLOB_LS_FLAG_REVERSE)
142 pmd.pm_flags |= PM_REVERSE_LOOP;
143 if (!(flags & BLOB_LS_FLAG_SORT_BY_ID))
144 pmd.loop_col_num = BLOBCOL_NAME;
145 else
146 pmd.loop_col_num = BLOBCOL_ID;
147 ret = for_each_matching_row(&pmd);
148 if (ret < 0)
149 goto out;
150 if (pmd.num_matches == 0 && pmd.patterns.size > 0)
151 ret = -E_NO_MATCH;
152 out:
153 return ret;
154 }
155
156 static int com_lsblob(afs_callback *f, struct command_context *cc)
157 {
158 uint32_t flags = 0;
159 struct osl_object options = {.data = &flags, .size = sizeof(flags)};
160 int i;
161
162 for (i = 1; i < cc->argc; i++) {
163 const char *arg = cc->argv[i];
164 if (arg[0] != '-')
165 break;
166 if (!strcmp(arg, "--")) {
167 i++;
168 break;
169 }
170 if (!strcmp(arg, "-l")) {
171 flags |= BLOB_LS_FLAG_LONG;
172 continue;
173 }
174 if (!strcmp(arg, "-i")) {
175 flags |= BLOB_LS_FLAG_SORT_BY_ID;
176 continue;
177 }
178 if (!strcmp(arg, "-r")) {
179 flags |= BLOB_LS_FLAG_REVERSE;
180 continue;
181 }
182 break;
183 }
184 // if (argc > i)
185 // return -E_BLOB_SYNTAX;
186 return send_option_arg_callback_request(&options, cc->argc - i,
187 cc->argv + i, f, afs_cb_result_handler, cc);
188 }
189
190 static int cat_blob(struct osl_table *table, struct osl_row *row,
191 __a_unused const char *name, void *data)
192 {
193 int ret = 0, ret2, fd = *(int *)data;
194 struct osl_object obj;
195
196 ret = osl(osl_open_disk_object(table, row, BLOBCOL_DEF, &obj));
197 if (ret < 0)
198 return (ret == osl(-E_OSL_EMPTY))? 0 : ret;
199 assert(obj.size > 0);
200 ret = pass_buffer_as_shm(fd, SBD_OUTPUT, obj.data, obj.size);
201 ret2 = osl(osl_close_disk_object(&obj));
202 return (ret < 0)? ret : ret2;
203 }
204
205 static int com_catblob_callback(struct osl_table *table,
206 struct afs_callback_arg *aca)
207 {
208 int ret;
209 struct pattern_match_data pmd = {
210 .table = table,
211 .patterns = aca->query,
212 .loop_col_num = BLOBCOL_NAME,
213 .match_col_num = BLOBCOL_NAME,
214 .pm_flags = PM_SKIP_EMPTY_NAME,
215 .data = &aca->fd,
216 .action = cat_blob
217 };
218 ret = for_each_matching_row(&pmd);
219 if (ret < 0)
220 return ret;
221 if (pmd.num_matches == 0)
222 ret = -E_NO_MATCH;
223 return ret;
224 }
225
226 static int com_catblob(afs_callback *f, struct command_context *cc)
227 {
228 if (cc->argc < 2)
229 return -E_BLOB_SYNTAX;
230 return send_standard_callback_request(cc->argc - 1, cc->argv + 1, f,
231 afs_cb_result_handler, cc);
232 }
233
234 static int remove_blob(struct osl_table *table, struct osl_row *row,
235 const char *name, void *data)
236 {
237 struct afs_callback_arg *aca = data;
238 int ret = osl(osl_del_row(table, row));
239 if (ret < 0) {
240 para_printf(&aca->pbout, "cannot remove %s\n", name);
241 return ret;
242 }
243 return 1;
244 }
245
246 static int com_rmblob_callback(struct osl_table *table,
247 struct afs_callback_arg *aca)
248 {
249 int ret;
250 struct pattern_match_data pmd = {
251 .table = table,
252 .patterns = aca->query,
253 .loop_col_num = BLOBCOL_NAME,
254 .match_col_num = BLOBCOL_NAME,
255 .pm_flags = PM_SKIP_EMPTY_NAME,
256 .data = aca,
257 .action = remove_blob
258 };
259 ret = for_each_matching_row(&pmd);
260 if (ret < 0)
261 goto out;
262 if (pmd.num_matches == 0)
263 ret = -E_NO_MATCH;
264 else {
265 para_printf(&aca->pbout, "removed %d blob(s)\n",
266 pmd.num_matches);
267 ret = afs_event(BLOB_REMOVE, NULL, table);
268 }
269 out:
270 return ret;
271 }
272
273 static int com_rmblob(afs_callback *f, struct command_context *cc)
274 {
275 if (cc->argc < 2)
276 return -E_MOOD_SYNTAX;
277 return send_option_arg_callback_request(NULL, cc->argc - 1, cc->argv + 1, f,
278 afs_cb_result_handler, cc);
279 }
280
281 static int com_addblob_callback(struct osl_table *table,
282 struct afs_callback_arg *aca)
283 {
284 struct osl_object objs[NUM_BLOB_COLUMNS];
285 char *name = aca->query.data;
286 size_t name_len = strlen(name) + 1;
287 uint32_t id;
288 unsigned num_rows;
289 int ret;
290
291 ret = osl(osl_get_num_rows(table, &num_rows));
292 if (ret < 0)
293 goto out;
294 if (!num_rows) { /* this is the first entry ever added */
295 /* insert dummy row containing the id */
296 id = 2; /* this entry will be entry #1, so 2 is the next */
297 objs[BLOBCOL_ID].data = &id;
298 objs[BLOBCOL_ID].size = sizeof(id);
299 objs[BLOBCOL_NAME].data = "";
300 objs[BLOBCOL_NAME].size = 1;
301 objs[BLOBCOL_DEF].data = "";
302 objs[BLOBCOL_DEF].size = 1;
303 ret = osl(osl_add_row(table, objs));
304 if (ret < 0)
305 goto out;
306 } else {
307 /* check if name already exists */
308 struct osl_row *row;
309 struct osl_object obj = {.data = name, .size = name_len};
310 ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
311 if (ret < 0 && ret != -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
312 goto out;
313 if (ret >= 0) { /* we already have a blob with this name */
314 ret = osl(osl_get_object(table, row, BLOBCOL_ID, &obj));
315 if (ret < 0)
316 goto out;
317 id = *(uint32_t *)obj.data;
318 obj.data = name + name_len;
319 obj.size = aca->query.size - name_len;
320 ret = osl(osl_update_object(table, row, BLOBCOL_DEF, &obj));
321 goto out;
322 }
323 /* new blob, get id of the dummy row and increment it */
324 obj.data = "";
325 obj.size = 1;
326 ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
327 if (ret < 0)
328 goto out;
329 ret = osl(osl_get_object(table, row, BLOBCOL_ID, &obj));
330 if (ret < 0)
331 goto out;
332 id = *(uint32_t *)obj.data + 1;
333 obj.data = &id;
334 ret = osl(osl_update_object(table, row, BLOBCOL_ID, &obj));
335 if (ret < 0)
336 goto out;
337 }
338 id--;
339 objs[BLOBCOL_ID].data = &id;
340 objs[BLOBCOL_ID].size = sizeof(id);
341 objs[BLOBCOL_NAME].data = name;
342 objs[BLOBCOL_NAME].size = name_len;
343 objs[BLOBCOL_DEF].data = name + name_len;
344 objs[BLOBCOL_DEF].size = aca->query.size - name_len;
345 ret = osl(osl_add_row(table, objs));
346 if (ret < 0)
347 goto out;
348 ret = afs_event(BLOB_ADD, NULL, table);
349 out:
350 if (ret < 0)
351 para_printf(&aca->pbout, "cannot add %s\n", name);
352 else
353 para_printf(&aca->pbout, "added %s as id %u\n", name, id);
354 return ret;
355 }
356
357 /* Write input from fd to dynamically allocated buffer, but maximal 10M. */
358 static int fd2buf(struct stream_cipher_context *scc, struct osl_object *obj)
359 {
360 size_t max_size = 10 * 1024 * 1024;
361 int ret;
362 struct iovec iov;
363
364 obj->data = NULL;
365 obj->size = 0;
366 again:
367 do {
368 ret = recv_sb(scc, SBD_BLOB_DATA, max_size, &iov);
369 } while (ret == 0);
370
371 if (ret < 0) {
372 free(obj->data);
373 obj->data = NULL;
374 obj->size = 0;
375 return ret;
376 }
377 if (iov.iov_len == 0) /* end of blob */
378 return 1;
379 if (!obj->data) {
380 obj->data = iov.iov_base;
381 obj->size = iov.iov_len;
382 } else {
383 obj->data = para_realloc(obj->data, obj->size + iov.iov_len);
384 memcpy(obj->data + obj->size, iov.iov_base, iov.iov_len);
385 obj->size += iov.iov_len;
386 free(iov.iov_base);
387 max_size -= iov.iov_len;
388 }
389 goto again;
390 return 1;
391 }
392
393 /*
394 * Read blob from a file descriptor and send it to afs.
395 *
396 * This function is called from the addblob command handlers to instruct the
397 * afs process to store the input in a blob table. Input is read and decrypted
398 * from the file descriptor given by cc and appended to arg_obj, which contains
399 * the name of the blob to create. The combined buffer is made available to the
400 * afs process via the callback method.
401 */
402 static int stdin_command(struct command_context *cc, struct osl_object *arg_obj,
403 afs_callback *f)
404 {
405 struct osl_object query, stdin_obj;
406 int ret;
407
408 ret = send_sb(&cc->scc, NULL, 0, SBD_AWAITING_DATA, false);
409 if (ret < 0)
410 return ret;
411 ret = fd2buf(&cc->scc, &stdin_obj);
412 if (ret < 0)
413 return ret;
414 query.size = arg_obj->size + stdin_obj.size;
415 query.data = para_malloc(query.size);
416 memcpy(query.data, arg_obj->data, arg_obj->size);
417 if (stdin_obj.size > 0)
418 memcpy((char *)query.data + arg_obj->size, stdin_obj.data,
419 stdin_obj.size);
420 free(stdin_obj.data);
421 ret = send_callback_request(f, &query, afs_cb_result_handler, cc);
422 free(query.data);
423 return ret;
424 }
425
426 static int com_addblob(afs_callback *f, struct command_context *cc)
427 {
428 struct osl_object arg_obj;
429
430 if (cc->argc != 2)
431 return -E_BLOB_SYNTAX;
432 if (!*cc->argv[1]) /* empty name is reserved for the dummy row */
433 return -E_BLOB_SYNTAX;
434 arg_obj.size = strlen(cc->argv[1]) + 1;
435 arg_obj.data = (char *)cc->argv[1];
436 return stdin_command(cc, &arg_obj, f);
437 }
438
439 static int com_mvblob_callback(struct osl_table *table,
440 struct afs_callback_arg *aca)
441 {
442 char *src = (char *)aca->query.data;
443 struct osl_object obj = {.data = src, .size = strlen(src) + 1};
444 char *dest = src + obj.size;
445 struct osl_row *row;
446 int ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
447
448 if (ret < 0) {
449 para_printf(&aca->pbout, "cannot find source blob %s\n", src);
450 goto out;
451 }
452 obj.data = dest;
453 obj.size = strlen(dest) + 1;
454 ret = osl(osl_update_object(table, row, BLOBCOL_NAME, &obj));
455 if (ret < 0) {
456 para_printf(&aca->pbout, "cannot rename blob %s to %s\n",
457 src, dest);
458 goto out;
459 }
460 ret = afs_event(BLOB_RENAME, NULL, table);
461 out:
462 return ret;
463 }
464
465 static int com_mvblob(afs_callback *f, struct command_context *cc)
466 {
467 if (cc->argc != 3)
468 return -E_MOOD_SYNTAX;
469 return send_option_arg_callback_request(NULL, cc->argc - 1,
470 cc->argv + 1, f, afs_cb_result_handler, cc);
471 }
472
473 #define DEFINE_BLOB_COMMAND(cmd_name, table_name, cmd_prefix) \
474 static int com_ ## cmd_name ## cmd_prefix ## _callback(struct afs_callback_arg *aca) \
475 { \
476 return com_ ## cmd_name ## blob_callback(table_name ## _table, aca); \
477 } \
478 int com_ ## cmd_name ## cmd_prefix(struct command_context *cc) \
479 { \
480 return com_ ## cmd_name ## blob(com_ ## cmd_name ## cmd_prefix ## _callback, cc); \
481 }
482
483 static int blob_get_name_by_id(struct osl_table *table, uint32_t id,
484 char **name)
485 {
486 struct osl_row *row;
487 struct osl_object obj = {.data = &id, .size = sizeof(id)};
488 int ret;
489
490 *name = NULL;
491 if (!id)
492 return 1;
493 ret = osl(osl_get_row(table, BLOBCOL_ID, &obj, &row));
494 if (ret < 0)
495 return ret;
496 ret = osl(osl_get_object(table, row, BLOBCOL_NAME, &obj));
497 if (ret < 0)
498 return ret;
499 if (*(char *)obj.data == '\0')
500 return -E_DUMMY_ROW;
501 *name = (char *)obj.data;
502 return 1;
503 }
504
505 /** Define the \p get_name_by_id function for this blob type. */
506 #define DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix) \
507 int cmd_prefix ## _get_name_by_id(uint32_t id, char **name) \
508 { \
509 return blob_get_name_by_id(table_name ## _table, id, name); \
510 }
511
512
513 static int blob_get_def_by_name(struct osl_table *table, char *name,
514 struct osl_object *def)
515 {
516 struct osl_row *row;
517 struct osl_object obj = {.data = name, .size = strlen(name) + 1};
518 int ret;
519
520 def->data = NULL;
521 if (!*name)
522 return 1;
523 ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
524 if (ret < 0)
525 return ret;
526 return osl(osl_open_disk_object(table, row, BLOBCOL_DEF, def));
527 }
528
529 /** Define the \p get_def_by_id function for this blob type. */
530 #define DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix) \
531 int cmd_prefix ## _get_def_by_name(char *name, struct osl_object *def) \
532 { \
533 return blob_get_def_by_name(table_name ## _table, name, def); \
534 }
535
536 static int blob_get_def_by_id(struct osl_table *table, uint32_t id,
537 struct osl_object *def)
538 {
539 struct osl_row *row;
540 struct osl_object obj = {.data = &id, .size = sizeof(id)};
541 int ret;
542
543 def->data = NULL;
544 if (!id)
545 return 1;
546 ret = osl(osl_get_row(table, BLOBCOL_ID, &obj, &row));
547 if (ret < 0)
548 return ret;
549 return osl(osl_open_disk_object(table, row, BLOBCOL_DEF, def));
550 }
551
552 /** Define the \p get_def_by_id function for this blob type. */
553 #define DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix) \
554 int cmd_prefix ## _get_def_by_id(uint32_t id, struct osl_object *def) \
555 { \
556 return blob_get_def_by_id(table_name ## _table, id, def); \
557 }
558
559 static int blob_get_name_and_def_by_row(struct osl_table *table,
560 const struct osl_row *row, char **name, struct osl_object *def)
561 {
562 struct osl_object obj;
563 int ret = osl(osl_get_object(table, row, BLOBCOL_NAME, &obj));
564 if (ret < 0)
565 return ret;
566 *name = obj.data;
567 return osl(osl_open_disk_object(table, row, BLOBCOL_DEF, def));
568 }
569 /** Define the \p get_name_and_def_by_row function for this blob type. */
570 #define DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix) \
571 int cmd_prefix ## _get_name_and_def_by_row(const struct osl_row *row, \
572 char **name, struct osl_object *def) \
573 { \
574 return blob_get_name_and_def_by_row(table_name ## _table, \
575 row, name, def); \
576 }
577
578 /** Define the \p close function for this blob type. */
579 #define DEFINE_BLOB_CLOSE(table_name) \
580 static void table_name ## _close(void) \
581 { \
582 osl_close_table(table_name ## _table, OSL_MARK_CLEAN); \
583 table_name ## _table = NULL; \
584 }
585
586 /** Define the \p create function for this blob type. */
587 #define DEFINE_BLOB_CREATE(table_name) \
588 static int table_name ## _create(const char *dir) \
589 { \
590 table_name ## _table_desc.dir = dir; \
591 return osl(osl_create_table(&table_name ## _table_desc)); \
592 }
593
594 static int blob_open(struct osl_table **table,
595 struct osl_table_description *desc,
596 const char *dir)
597 {
598 int ret;
599 desc->dir = dir;
600 ret = osl(osl_open_table(desc, table));
601 if (ret >= 0)
602 return ret;
603 *table = NULL;
604 if (ret >= 0 || ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT))
605 return 1;
606 return ret;
607 }
608
609 #define DEFINE_BLOB_OPEN(table_name) \
610 static int table_name ## _open(const char *dir) \
611 { \
612 return blob_open(&table_name ## _table, \
613 &table_name ## _table_desc, dir); \
614 }
615
616
617 /** Define the \p init function for this blob type. */
618 #define DEFINE_BLOB_INIT(table_name) \
619 void table_name ## _init(struct afs_table *t) \
620 { \
621 t->open = table_name ## _open; \
622 t->close = table_name ## _close; \
623 t->create = table_name ## _create;\
624 t->event_handler = table_name ##_event_handler; \
625 table_name ## _table = NULL; \
626 }
627
628
629 /** Define all functions for this blob type. */
630 #define DEFINE_BLOB_FUNCTIONS(table_name, cmd_prefix) \
631 DEFINE_BLOB_OPEN(table_name) \
632 DEFINE_BLOB_CLOSE(table_name) \
633 DEFINE_BLOB_CREATE(table_name) \
634 DEFINE_BLOB_INIT(table_name) \
635 DEFINE_BLOB_COMMAND(ls, table_name, cmd_prefix) \
636 DEFINE_BLOB_COMMAND(cat, table_name, cmd_prefix) \
637 DEFINE_BLOB_COMMAND(add, table_name, cmd_prefix) \
638 DEFINE_BLOB_COMMAND(rm, table_name, cmd_prefix) \
639 DEFINE_BLOB_COMMAND(mv, table_name, cmd_prefix) \
640 DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix); \
641 DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix); \
642 DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix); \
643 DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix); \
644
645 /* doxygen isn't smart enough to recognize these */
646 /** \cond blob_function */
647 DEFINE_BLOB_FUNCTIONS(lyrics, lyr);
648 DEFINE_BLOB_FUNCTIONS(images, img);
649 DEFINE_BLOB_FUNCTIONS(moods, mood);
650 DEFINE_BLOB_FUNCTIONS(playlists, pl);
651 /** \endcond blob_function */