2 * Copyright (C) 2007 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file blob.c Macros and functions for blob handling. */
21 #include "portable_io.h"
26 * Compare two osl objects pointing to unsigned integers of 32 bit size.
28 * \param obj1 Pointer to the first integer.
29 * \param obj2 Pointer to the second integer.
31 * \return The values required for an osl compare function.
33 * \sa osl_compare_func, osl_hash_compare().
35 static int uint32_compare(const struct osl_object *obj1, const struct osl_object *obj2)
37 uint32_t d1 = read_u32((const char *)obj1->data);
38 uint32_t d2 = read_u32((const char *)obj2->data);
47 static struct osl_column_description blob_cols[] = {
49 .storage_type = OSL_MAPPED_STORAGE,
50 .storage_flags = OSL_RBTREE | OSL_UNIQUE | OSL_FIXED_SIZE,
53 .compare_function = uint32_compare
56 .storage_type = OSL_MAPPED_STORAGE,
57 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
59 .compare_function = string_compare
62 .storage_type = OSL_DISK_STORAGE,
68 /** Define an osl table description for a blob table. */
69 #define DEFINE_BLOB_TABLE_DESC(table_name) \
70 static struct osl_table_description table_name ## _table_desc = { \
71 .name = #table_name, \
72 .num_columns = NUM_BLOB_COLUMNS, \
73 .flags = OSL_LARGE_TABLE, \
74 .column_descriptions = blob_cols \
77 /** Define a pointer to an osl blob table with a canonical name. */
78 #define DEFINE_BLOB_TABLE_PTR(table_name) struct osl_table *table_name ## _table;
81 /** Define a blob table. */
82 #define INIT_BLOB_TABLE(table_name) \
83 DEFINE_BLOB_TABLE_DESC(table_name); \
84 DEFINE_BLOB_TABLE_PTR(table_name);
86 /* doxygen isn't smart enough to recognize these */
87 /** \cond blob_table */
88 INIT_BLOB_TABLE(lyrics);
89 INIT_BLOB_TABLE(images);
90 INIT_BLOB_TABLE(moods);
91 INIT_BLOB_TABLE(playlists);
92 /** \endcond blob_table */
94 /** Flags that may be passed to the \p ls functions of each blob type. */
96 /** List both id and name. */
97 BLOB_LS_FLAG_LONG = 1,
98 /** Reverse sort order. */
99 BLOB_LS_FLAG_REVERSE = 2,
100 /** Sort by id instead of name. */
101 BLOB_LS_FLAG_SORT_BY_ID = 4,
104 static int print_blob(struct osl_table *table, struct osl_row *row,
105 const char *name, void *data)
107 struct afs_callback_arg *aca = data;
108 uint32_t flags = *(uint32_t *)aca->query.data;
109 struct osl_object obj;
113 if (!(flags & BLOB_LS_FLAG_LONG)) {
114 para_printf(&aca->pbout, "%s\n", name);
117 ret = osl(osl_get_object(table, row, BLOBCOL_ID, &obj));
119 para_printf(&aca->pbout, "cannot list %s\n", name);
122 id = *(uint32_t *)obj.data;
123 para_printf(&aca->pbout, "%u\t%s\n", id, name);
127 static int com_lsblob_callback(struct osl_table *table,
128 struct afs_callback_arg *aca)
130 uint32_t flags = *(uint32_t *)aca->query.data;
131 struct pattern_match_data pmd = {
133 .patterns = {.data = (char *)aca->query.data + sizeof(uint32_t),
134 .size = aca->query.size - sizeof(uint32_t)},
135 .pm_flags = PM_NO_PATTERN_MATCHES_EVERYTHING | PM_SKIP_EMPTY_NAME,
136 .match_col_num = BLOBCOL_NAME,
138 .action = print_blob,
142 if (flags & BLOB_LS_FLAG_REVERSE)
143 pmd.pm_flags |= PM_REVERSE_LOOP;
144 if (!(flags & BLOB_LS_FLAG_SORT_BY_ID))
145 pmd.loop_col_num = BLOBCOL_NAME;
147 pmd.loop_col_num = BLOBCOL_ID;
148 ret = for_each_matching_row(&pmd);
151 if (pmd.num_matches == 0 && pmd.patterns.size > 0)
157 static int com_lsblob(afs_callback *f, struct command_context *cc)
160 struct osl_object options = {.data = &flags, .size = sizeof(flags)};
163 for (i = 1; i < cc->argc; i++) {
164 const char *arg = cc->argv[i];
167 if (!strcmp(arg, "--")) {
171 if (!strcmp(arg, "-l")) {
172 flags |= BLOB_LS_FLAG_LONG;
175 if (!strcmp(arg, "-i")) {
176 flags |= BLOB_LS_FLAG_SORT_BY_ID;
179 if (!strcmp(arg, "-r")) {
180 flags |= BLOB_LS_FLAG_REVERSE;
186 // return -E_BLOB_SYNTAX;
187 return send_option_arg_callback_request(&options, cc->argc - i,
188 cc->argv + i, f, afs_cb_result_handler, cc);
191 static int cat_blob(struct osl_table *table, struct osl_row *row,
192 __a_unused const char *name, void *data)
194 int ret = 0, ret2, fd = *(int *)data;
195 struct osl_object obj;
197 ret = osl(osl_open_disk_object(table, row, BLOBCOL_DEF, &obj));
199 return (ret == osl(-E_OSL_EMPTY))? 0 : ret;
200 assert(obj.size > 0);
201 ret = pass_buffer_as_shm(fd, SBD_OUTPUT, obj.data, obj.size);
202 ret2 = osl(osl_close_disk_object(&obj));
203 return (ret < 0)? ret : ret2;
206 static int com_catblob_callback(struct osl_table *table,
207 struct afs_callback_arg *aca)
210 struct pattern_match_data pmd = {
212 .patterns = aca->query,
213 .loop_col_num = BLOBCOL_NAME,
214 .match_col_num = BLOBCOL_NAME,
215 .pm_flags = PM_SKIP_EMPTY_NAME,
219 ret = for_each_matching_row(&pmd);
222 if (pmd.num_matches == 0)
227 static int com_catblob(afs_callback *f, struct command_context *cc)
230 return -E_BLOB_SYNTAX;
231 return send_standard_callback_request(cc->argc - 1, cc->argv + 1, f,
232 afs_cb_result_handler, cc);
235 static int remove_blob(struct osl_table *table, struct osl_row *row,
236 const char *name, void *data)
238 struct afs_callback_arg *aca = data;
239 int ret = osl(osl_del_row(table, row));
241 para_printf(&aca->pbout, "cannot remove %s\n", name);
247 static int com_rmblob_callback(struct osl_table *table,
248 struct afs_callback_arg *aca)
251 struct pattern_match_data pmd = {
253 .patterns = aca->query,
254 .loop_col_num = BLOBCOL_NAME,
255 .match_col_num = BLOBCOL_NAME,
256 .pm_flags = PM_SKIP_EMPTY_NAME,
258 .action = remove_blob
260 ret = for_each_matching_row(&pmd);
263 if (pmd.num_matches == 0)
266 para_printf(&aca->pbout, "removed %u blob(s)\n",
268 ret = afs_event(BLOB_REMOVE, NULL, table);
274 static int com_rmblob(afs_callback *f, struct command_context *cc)
277 return -E_MOOD_SYNTAX;
278 return send_option_arg_callback_request(NULL, cc->argc - 1, cc->argv + 1, f,
279 afs_cb_result_handler, cc);
282 static int com_addblob_callback(struct osl_table *table,
283 struct afs_callback_arg *aca)
285 struct osl_object objs[NUM_BLOB_COLUMNS];
286 char *name = aca->query.data;
287 size_t name_len = strlen(name) + 1;
292 ret = osl(osl_get_num_rows(table, &num_rows));
295 if (!num_rows) { /* this is the first entry ever added */
296 /* insert dummy row containing the id */
297 id = 2; /* this entry will be entry #1, so 2 is the next */
298 objs[BLOBCOL_ID].data = &id;
299 objs[BLOBCOL_ID].size = sizeof(id);
300 objs[BLOBCOL_NAME].data = "";
301 objs[BLOBCOL_NAME].size = 1;
302 objs[BLOBCOL_DEF].data = "";
303 objs[BLOBCOL_DEF].size = 1;
304 ret = osl(osl_add_row(table, objs));
308 /* check if name already exists */
310 struct osl_object obj = {.data = name, .size = name_len};
311 ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
312 if (ret < 0 && ret != -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
314 if (ret >= 0) { /* we already have a blob with this name */
315 ret = osl(osl_get_object(table, row, BLOBCOL_ID, &obj));
318 id = *(uint32_t *)obj.data;
319 obj.data = name + name_len;
320 obj.size = aca->query.size - name_len;
321 ret = osl(osl_update_object(table, row, BLOBCOL_DEF, &obj));
324 /* new blob, get id of the dummy row and increment it */
327 ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
330 ret = osl(osl_get_object(table, row, BLOBCOL_ID, &obj));
333 id = *(uint32_t *)obj.data + 1;
335 ret = osl(osl_update_object(table, row, BLOBCOL_ID, &obj));
340 objs[BLOBCOL_ID].data = &id;
341 objs[BLOBCOL_ID].size = sizeof(id);
342 objs[BLOBCOL_NAME].data = name;
343 objs[BLOBCOL_NAME].size = name_len;
344 objs[BLOBCOL_DEF].data = name + name_len;
345 objs[BLOBCOL_DEF].size = aca->query.size - name_len;
346 ret = osl(osl_add_row(table, objs));
349 ret = afs_event(BLOB_ADD, NULL, table);
352 para_printf(&aca->pbout, "cannot add %s\n", name);
354 para_printf(&aca->pbout, "added %s as id %u\n", name, id);
358 /* Write input from fd to dynamically allocated buffer, but maximal 10M. */
359 static int fd2buf(struct stream_cipher_context *scc, struct osl_object *obj)
361 size_t max_size = 10 * 1024 * 1024;
369 ret = recv_sb(scc, SBD_BLOB_DATA, max_size, &iov);
378 if (iov.iov_len == 0) /* end of blob */
381 obj->data = iov.iov_base;
382 obj->size = iov.iov_len;
384 obj->data = para_realloc(obj->data, obj->size + iov.iov_len);
385 memcpy(obj->data + obj->size, iov.iov_base, iov.iov_len);
386 obj->size += iov.iov_len;
388 max_size -= iov.iov_len;
395 * Read blob from a file descriptor and send it to afs.
397 * This function is called from the addblob command handlers to instruct the
398 * afs process to store the input in a blob table. Input is read and decrypted
399 * from the file descriptor given by cc and appended to arg_obj, which contains
400 * the name of the blob to create. The combined buffer is made available to the
401 * afs process via the callback method.
403 static int stdin_command(struct command_context *cc, struct osl_object *arg_obj,
406 struct osl_object query, stdin_obj;
409 ret = send_sb(&cc->scc, NULL, 0, SBD_AWAITING_DATA, false);
412 ret = fd2buf(&cc->scc, &stdin_obj);
415 query.size = arg_obj->size + stdin_obj.size;
416 query.data = para_malloc(query.size);
417 memcpy(query.data, arg_obj->data, arg_obj->size);
418 if (stdin_obj.size > 0)
419 memcpy((char *)query.data + arg_obj->size, stdin_obj.data,
421 free(stdin_obj.data);
422 ret = send_callback_request(f, &query, afs_cb_result_handler, cc);
427 static int com_addblob(afs_callback *f, struct command_context *cc)
429 struct osl_object arg_obj;
432 return -E_BLOB_SYNTAX;
433 if (!*cc->argv[1]) /* empty name is reserved for the dummy row */
434 return -E_BLOB_SYNTAX;
435 arg_obj.size = strlen(cc->argv[1]) + 1;
436 arg_obj.data = (char *)cc->argv[1];
437 return stdin_command(cc, &arg_obj, f);
440 static int com_mvblob_callback(struct osl_table *table,
441 struct afs_callback_arg *aca)
443 char *src = (char *)aca->query.data;
444 struct osl_object obj = {.data = src, .size = strlen(src) + 1};
445 char *dest = src + obj.size;
447 int ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
450 para_printf(&aca->pbout, "cannot find source blob %s\n", src);
454 obj.size = strlen(dest) + 1;
455 ret = osl(osl_update_object(table, row, BLOBCOL_NAME, &obj));
457 para_printf(&aca->pbout, "cannot rename blob %s to %s\n",
461 ret = afs_event(BLOB_RENAME, NULL, table);
466 static int com_mvblob(afs_callback *f, struct command_context *cc)
469 return -E_MOOD_SYNTAX;
470 return send_option_arg_callback_request(NULL, cc->argc - 1,
471 cc->argv + 1, f, afs_cb_result_handler, cc);
474 #define DEFINE_BLOB_COMMAND(cmd_name, table_name, cmd_prefix) \
475 static int com_ ## cmd_name ## cmd_prefix ## _callback(struct afs_callback_arg *aca) \
477 return com_ ## cmd_name ## blob_callback(table_name ## _table, aca); \
479 int com_ ## cmd_name ## cmd_prefix(struct command_context *cc) \
481 return com_ ## cmd_name ## blob(com_ ## cmd_name ## cmd_prefix ## _callback, cc); \
484 static int blob_get_name_by_id(struct osl_table *table, uint32_t id,
488 struct osl_object obj = {.data = &id, .size = sizeof(id)};
495 ret = osl(osl_get_row(table, BLOBCOL_ID, &obj, &row));
498 ret = osl(osl_get_object(table, row, BLOBCOL_NAME, &obj));
501 if (*(char *)obj.data == '\0')
504 *name = (char *)obj.data;
508 /** Define the \p get_name_by_id function for this blob type. */
509 #define DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix) \
510 int cmd_prefix ## _get_name_by_id(uint32_t id, char **name) \
512 return blob_get_name_by_id(table_name ## _table, id, name); \
516 static int blob_get_def_by_name(struct osl_table *table, char *name,
517 struct osl_object *def)
520 struct osl_object obj = {.data = name, .size = strlen(name) + 1};
526 ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
529 return osl(osl_open_disk_object(table, row, BLOBCOL_DEF, def));
532 /** Define the \p get_def_by_id function for this blob type. */
533 #define DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix) \
534 int cmd_prefix ## _get_def_by_name(char *name, struct osl_object *def) \
536 return blob_get_def_by_name(table_name ## _table, name, def); \
539 static int blob_get_def_by_id(struct osl_table *table, uint32_t id,
540 struct osl_object *def)
543 struct osl_object obj = {.data = &id, .size = sizeof(id)};
549 ret = osl(osl_get_row(table, BLOBCOL_ID, &obj, &row));
552 return osl(osl_open_disk_object(table, row, BLOBCOL_DEF, def));
555 /** Define the \p get_def_by_id function for this blob type. */
556 #define DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix) \
557 int cmd_prefix ## _get_def_by_id(uint32_t id, struct osl_object *def) \
559 return blob_get_def_by_id(table_name ## _table, id, def); \
562 static int blob_get_name_and_def_by_row(struct osl_table *table,
563 const struct osl_row *row, char **name, struct osl_object *def)
565 struct osl_object obj;
566 int ret = osl(osl_get_object(table, row, BLOBCOL_NAME, &obj));
570 return osl(osl_open_disk_object(table, row, BLOBCOL_DEF, def));
572 /** Define the \p get_name_and_def_by_row function for this blob type. */
573 #define DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix) \
574 int cmd_prefix ## _get_name_and_def_by_row(const struct osl_row *row, \
575 char **name, struct osl_object *def) \
577 return blob_get_name_and_def_by_row(table_name ## _table, \
581 /** Define the \p close function for this blob type. */
582 #define DEFINE_BLOB_CLOSE(table_name) \
583 static void table_name ## _close(void) \
585 osl_close_table(table_name ## _table, OSL_MARK_CLEAN); \
586 table_name ## _table = NULL; \
589 /** Define the \p create function for this blob type. */
590 #define DEFINE_BLOB_CREATE(table_name) \
591 static int table_name ## _create(const char *dir) \
593 table_name ## _table_desc.dir = dir; \
594 return osl(osl_create_table(&table_name ## _table_desc)); \
597 static int blob_open(struct osl_table **table,
598 struct osl_table_description *desc,
603 ret = osl(osl_open_table(desc, table));
607 if (ret >= 0 || ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT))
612 #define DEFINE_BLOB_OPEN(table_name) \
613 static int table_name ## _open(const char *dir) \
615 return blob_open(&table_name ## _table, \
616 &table_name ## _table_desc, dir); \
620 /** Define the \p init function for this blob type. */
621 #define DEFINE_BLOB_INIT(table_name) \
622 void table_name ## _init(struct afs_table *t) \
624 t->open = table_name ## _open; \
625 t->close = table_name ## _close; \
626 t->create = table_name ## _create;\
627 t->event_handler = table_name ##_event_handler; \
628 table_name ## _table = NULL; \
632 /** Define all functions for this blob type. */
633 #define DEFINE_BLOB_FUNCTIONS(table_name, cmd_prefix) \
634 DEFINE_BLOB_OPEN(table_name) \
635 DEFINE_BLOB_CLOSE(table_name) \
636 DEFINE_BLOB_CREATE(table_name) \
637 DEFINE_BLOB_INIT(table_name) \
638 DEFINE_BLOB_COMMAND(ls, table_name, cmd_prefix) \
639 DEFINE_BLOB_COMMAND(cat, table_name, cmd_prefix) \
640 DEFINE_BLOB_COMMAND(add, table_name, cmd_prefix) \
641 DEFINE_BLOB_COMMAND(rm, table_name, cmd_prefix) \
642 DEFINE_BLOB_COMMAND(mv, table_name, cmd_prefix) \
643 DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix); \
644 DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix); \
645 DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix); \
646 DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix); \
648 /* doxygen isn't smart enough to recognize these */
649 /** \cond blob_function */
650 DEFINE_BLOB_FUNCTIONS(lyrics, lyr);
651 DEFINE_BLOB_FUNCTIONS(images, img);
652 DEFINE_BLOB_FUNCTIONS(moods, mood);
653 DEFINE_BLOB_FUNCTIONS(playlists, pl);
654 /** \endcond blob_function */