1 /* Copyright (C) 2007 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file blob.c Macros and functions for blob handling. */
10 #include "server_cmd.lsg.h"
18 #include "portable_io.h"
23 * Compare two osl objects pointing to unsigned integers of 32 bit size.
25 * \param obj1 Pointer to the first integer.
26 * \param obj2 Pointer to the second integer.
28 * \return The values required for an osl compare function.
30 static int uint32_compare(const struct osl_object *obj1, const struct osl_object *obj2)
32 uint32_t d1 = read_u32(obj1->data);
33 uint32_t d2 = read_u32(obj2->data);
42 static struct osl_column_description blob_cols[] = {
44 .storage_type = OSL_MAPPED_STORAGE,
45 .storage_flags = OSL_RBTREE | OSL_UNIQUE | OSL_FIXED_SIZE,
48 .compare_function = uint32_compare
51 .storage_type = OSL_MAPPED_STORAGE,
52 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
54 .compare_function = string_compare
57 .storage_type = OSL_DISK_STORAGE,
63 /** Define an osl table description for a blob table. */
64 #define DEFINE_BLOB_TABLE_DESC(table_name) \
65 static struct osl_table_description table_name ## _table_desc = { \
66 .name = #table_name, \
67 .num_columns = NUM_BLOB_COLUMNS, \
68 .flags = OSL_LARGE_TABLE, \
69 .column_descriptions = blob_cols \
72 /** Define a pointer to an osl blob table with a canonical name. */
73 #define DEFINE_BLOB_TABLE_PTR(table_name) struct osl_table *table_name ## _table;
76 /** Define a blob table. */
77 #define INIT_BLOB_TABLE(table_name) \
78 DEFINE_BLOB_TABLE_DESC(table_name); \
79 DEFINE_BLOB_TABLE_PTR(table_name);
81 /* doxygen isn't smart enough to recognize these */
82 /** \cond blob_table */
83 INIT_BLOB_TABLE(lyrics);
84 INIT_BLOB_TABLE(images);
85 INIT_BLOB_TABLE(moods);
86 INIT_BLOB_TABLE(playlists);
87 /** \endcond blob_table */
89 static int print_blob(struct osl_table *table, struct osl_row *row,
90 const char *name, void *data)
92 struct afs_callback_arg *aca = data;
93 bool l_given = SERVER_CMD_OPT_GIVEN(LSMOOD, LONG, aca->lpr);
94 struct osl_object obj;
99 para_printf(&aca->pbout, "%s\n", name);
102 ret = osl(osl_get_object(table, row, BLOBCOL_ID, &obj));
104 para_printf(&aca->pbout, "cannot list %s\n", name);
107 id = *(uint32_t *)obj.data;
108 para_printf(&aca->pbout, "%u\t%s\n", id, name);
112 static int com_lsblob_callback(const struct lls_command * const cmd,
113 struct osl_table *table, struct afs_callback_arg *aca)
115 bool i_given, r_given;
116 struct pattern_match_data pmd = {
118 .pm_flags = PM_NO_PATTERN_MATCHES_EVERYTHING | PM_SKIP_EMPTY_NAME,
119 .match_col_num = BLOBCOL_NAME,
121 .action = print_blob,
124 ret = lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr);
127 i_given = SERVER_CMD_OPT_GIVEN(LSMOOD, ID_SORT, aca->lpr);
128 r_given = SERVER_CMD_OPT_GIVEN(LSMOOD, REVERSE, aca->lpr);
131 pmd.pm_flags |= PM_REVERSE_LOOP;
133 pmd.loop_col_num = BLOBCOL_ID;
135 pmd.loop_col_num = BLOBCOL_NAME;
136 ret = for_each_matching_row(&pmd);
139 if (pmd.num_matches == 0 && lls_num_inputs(aca->lpr) > 0)
142 lls_free_parse_result(aca->lpr, cmd);
146 static int com_lsblob(afs_callback *f, const struct lls_command * const cmd,
147 struct command_context *cc, struct lls_parse_result *lpr)
149 return send_lls_callback_request(f, cmd, lpr, cc);
152 static int cat_blob(struct osl_table *table, struct osl_row *row,
153 __a_unused const char *name, void *data)
155 int ret = 0, ret2, fd = *(int *)data;
156 struct osl_object obj;
158 ret = osl(osl_open_disk_object(table, row, BLOBCOL_DEF, &obj));
160 return (ret == osl(-E_OSL_EMPTY))? 0 : ret;
161 assert(obj.size > 0);
162 ret = pass_buffer_as_shm(fd, SBD_OUTPUT, obj.data, obj.size);
163 ret2 = osl(osl_close_disk_object(&obj));
164 return (ret < 0)? ret : ret2;
167 static int com_catblob_callback(const struct lls_command * const cmd,
168 struct osl_table *table, struct afs_callback_arg *aca)
171 struct pattern_match_data pmd = {
173 .loop_col_num = BLOBCOL_NAME,
174 .match_col_num = BLOBCOL_NAME,
175 .pm_flags = PM_SKIP_EMPTY_NAME,
179 ret = lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr);
182 ret = for_each_matching_row(&pmd);
185 if (pmd.num_matches == 0)
188 lls_free_parse_result(aca->lpr, cmd);
192 static int com_catblob(afs_callback *f, const struct lls_command * const cmd,
193 struct command_context *cc, struct lls_parse_result *lpr)
196 int ret = lls(lls_check_arg_count(lpr, 1, INT_MAX, &errctx));
199 send_errctx(cc, errctx);
202 return send_lls_callback_request(f, cmd, lpr, cc);
205 static int remove_blob(struct osl_table *table, struct osl_row *row,
206 const char *name, void *data)
208 struct afs_callback_arg *aca = data;
209 int ret = osl(osl_del_row(table, row));
211 para_printf(&aca->pbout, "cannot remove %s\n", name);
217 static int com_rmblob_callback(const struct lls_command * const cmd,
218 struct osl_table *table, struct afs_callback_arg *aca)
221 struct pattern_match_data pmd = {
223 .loop_col_num = BLOBCOL_NAME,
224 .match_col_num = BLOBCOL_NAME,
225 .pm_flags = PM_SKIP_EMPTY_NAME,
227 .action = remove_blob
229 ret = lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr);
232 ret = for_each_matching_row(&pmd);
235 if (pmd.num_matches == 0)
238 para_printf(&aca->pbout, "removed %u blob(s)\n",
240 ret = afs_event(BLOB_REMOVE, NULL, table);
243 lls_free_parse_result(aca->lpr, cmd);
247 static int com_rmblob(afs_callback *f, const struct lls_command * const cmd,
248 struct command_context *cc, struct lls_parse_result *lpr)
251 int ret = lls(lls_check_arg_count(lpr, 1, INT_MAX, &errctx));
254 send_errctx(cc, errctx);
257 return send_lls_callback_request(f, cmd, lpr, cc);
260 static int com_addblob_callback(__a_unused const struct lls_command * const cmd,
261 struct osl_table *table, struct afs_callback_arg *aca)
263 struct osl_object objs[NUM_BLOB_COLUMNS];
264 char *name = aca->query.data;
265 size_t name_len = strlen(name) + 1;
270 ret = osl(osl_get_num_rows(table, &num_rows));
273 if (!num_rows) { /* this is the first entry ever added */
274 /* insert dummy row containing the id */
275 id = 2; /* this entry will be entry #1, so 2 is the next */
276 objs[BLOBCOL_ID].data = &id;
277 objs[BLOBCOL_ID].size = sizeof(id);
278 objs[BLOBCOL_NAME].data = "";
279 objs[BLOBCOL_NAME].size = 1;
280 objs[BLOBCOL_DEF].data = "";
281 objs[BLOBCOL_DEF].size = 1;
282 ret = osl(osl_add_row(table, objs));
286 /* check if name already exists */
288 struct osl_object obj = {.data = name, .size = name_len};
289 ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
290 if (ret < 0 && ret != -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
292 if (ret >= 0) { /* we already have a blob with this name */
293 ret = osl(osl_get_object(table, row, BLOBCOL_ID, &obj));
296 id = *(uint32_t *)obj.data;
297 obj.data = name + name_len;
298 obj.size = aca->query.size - name_len;
299 ret = osl(osl_update_object(table, row, BLOBCOL_DEF, &obj));
302 /* new blob, get id of the dummy row and increment it */
305 ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
308 ret = osl(osl_get_object(table, row, BLOBCOL_ID, &obj));
311 id = *(uint32_t *)obj.data + 1;
313 ret = osl(osl_update_object(table, row, BLOBCOL_ID, &obj));
318 objs[BLOBCOL_ID].data = &id;
319 objs[BLOBCOL_ID].size = sizeof(id);
320 objs[BLOBCOL_NAME].data = name;
321 objs[BLOBCOL_NAME].size = name_len;
322 objs[BLOBCOL_DEF].data = name + name_len;
323 objs[BLOBCOL_DEF].size = aca->query.size - name_len;
324 ret = osl(osl_add_row(table, objs));
327 ret = afs_event(BLOB_ADD, NULL, table);
330 para_printf(&aca->pbout, "cannot add %s\n", name);
332 para_printf(&aca->pbout, "added %s as id %u\n", name, id);
336 /* Write input from fd to dynamically allocated buffer, but maximal 10M. */
337 static int fd2buf(struct stream_cipher_context *scc, struct osl_object *obj)
339 size_t max_size = 10 * 1024 * 1024;
347 ret = recv_sb(scc, SBD_BLOB_DATA, max_size, &iov);
356 if (iov.iov_len == 0) /* end of blob */
359 obj->data = iov.iov_base;
360 obj->size = iov.iov_len;
362 obj->data = para_realloc(obj->data, obj->size + iov.iov_len);
363 memcpy(obj->data + obj->size, iov.iov_base, iov.iov_len);
364 obj->size += iov.iov_len;
366 max_size -= iov.iov_len;
373 * Read blob from a file descriptor and send it to afs.
375 * This function is called from the addblob command handlers to instruct the
376 * afs process to store the input in a blob table. Input is read and decrypted
377 * from the file descriptor given by cc and appended to arg_obj, which contains
378 * the name of the blob to create. The combined buffer is made available to the
379 * afs process via the callback method.
381 static int stdin_command(struct command_context *cc,
382 struct lls_parse_result *lpr, afs_callback *f)
384 struct osl_object query, stdin_obj;
386 size_t len = strlen(lls_input(0, lpr));
388 ret = send_sb(&cc->scc, NULL, 0, SBD_AWAITING_DATA, false);
391 ret = fd2buf(&cc->scc, &stdin_obj);
394 query.size = len + 1 + stdin_obj.size;
395 query.data = para_malloc(query.size);
396 memcpy(query.data, lls_input(0, lpr), len + 1);
397 if (stdin_obj.size > 0)
398 memcpy((char *)query.data + len + 1, stdin_obj.data,
400 free(stdin_obj.data);
401 ret = send_callback_request(f, &query, afs_cb_result_handler, cc);
406 static int com_addblob(afs_callback *f, __a_unused const struct lls_command * const cmd,
407 struct command_context *cc, struct lls_parse_result *lpr)
410 int ret = lls(lls_check_arg_count(lpr, 1, 1, &errctx));
413 send_errctx(cc, errctx);
416 if (!lls_input(0, lpr)[0]) /* empty name is reserved for the dummy row */
417 return -E_BLOB_SYNTAX;
418 return stdin_command(cc, lpr, f);
421 static int com_mvblob_callback(const struct lls_command * const cmd,
422 struct osl_table *table, struct afs_callback_arg *aca)
424 const char *src, *dest;
425 struct osl_object obj;
429 ret = lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr);
431 src = lls_input(0, aca->lpr);
432 dest = lls_input(1, aca->lpr);
433 obj.data = (char *)src;
434 obj.size = strlen(src) + 1;
435 ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
438 para_printf(&aca->pbout, "cannot find source blob %s\n", src);
441 obj.data = (char *)dest;
442 obj.size = strlen(dest) + 1;
443 ret = osl(osl_update_object(table, row, BLOBCOL_NAME, &obj));
445 para_printf(&aca->pbout, "cannot rename blob %s to %s\n",
449 ret = afs_event(BLOB_RENAME, NULL, table);
451 lls_free_parse_result(aca->lpr, cmd);
455 static int com_mvblob(afs_callback *f, const struct lls_command * const cmd,
456 struct command_context *cc, struct lls_parse_result *lpr)
459 int ret = lls(lls_check_arg_count(lpr, 2, 2, &errctx));
462 send_errctx(cc, errctx);
465 return send_lls_callback_request(f, cmd, lpr, cc);
468 #define DEFINE_BLOB_COMMAND(cmd_name, c_cmd_name, table_name, short_name, c_short_name) \
469 static int com_ ## cmd_name ## short_name ## _callback(struct afs_callback_arg *aca) \
471 const struct lls_command *cmd = SERVER_CMD_CMD_PTR(c_cmd_name ## c_short_name); \
472 return com_ ## cmd_name ## blob_callback(cmd, table_name ## _table, aca); \
474 static int com_ ## cmd_name ## short_name(struct command_context *cc, struct lls_parse_result *lpr) \
476 const struct lls_command *cmd = SERVER_CMD_CMD_PTR(c_cmd_name ## c_short_name); \
477 return com_ ## cmd_name ## blob(com_ ## cmd_name ## short_name ## _callback, cmd, cc, lpr); \
479 EXPORT_SERVER_CMD_HANDLER(cmd_name ## short_name);
481 static int blob_get_name_by_id(struct osl_table *table, uint32_t id,
485 struct osl_object obj = {.data = &id, .size = sizeof(id)};
492 ret = osl(osl_get_row(table, BLOBCOL_ID, &obj, &row));
495 ret = osl(osl_get_object(table, row, BLOBCOL_NAME, &obj));
498 if (*(char *)obj.data == '\0')
501 *name = (char *)obj.data;
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) \
509 return blob_get_name_by_id(table_name ## _table, id, name); \
513 static int blob_get_def_by_name(struct osl_table *table, char *name,
514 struct osl_object *def)
517 struct osl_object obj = {.data = name, .size = strlen(name) + 1};
523 ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
526 return osl(osl_open_disk_object(table, row, BLOBCOL_DEF, def));
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) \
533 return blob_get_def_by_name(table_name ## _table, name, def); \
536 static int blob_get_def_by_id(struct osl_table *table, uint32_t id,
537 struct osl_object *def)
540 struct osl_object obj = {.data = &id, .size = sizeof(id)};
546 ret = osl(osl_get_row(table, BLOBCOL_ID, &obj, &row));
549 return osl(osl_open_disk_object(table, row, BLOBCOL_DEF, def));
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) \
556 return blob_get_def_by_id(table_name ## _table, id, def); \
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)
562 struct osl_object obj;
563 int ret = osl(osl_get_object(table, row, BLOBCOL_NAME, &obj));
567 return osl(osl_open_disk_object(table, row, BLOBCOL_DEF, def));
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) \
574 return blob_get_name_and_def_by_row(table_name ## _table, \
578 /** Define the \p close function for this blob type. */
579 #define DEFINE_BLOB_CLOSE(table_name) \
580 static void table_name ## _close(void) \
582 osl_close_table(table_name ## _table, OSL_MARK_CLEAN); \
583 table_name ## _table = NULL; \
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) \
590 table_name ## _table_desc.dir = dir; \
591 return osl(osl_create_table(&table_name ## _table_desc)); \
594 static int blob_open(struct osl_table **table,
595 struct osl_table_description *desc,
600 ret = osl(osl_open_table(desc, table));
604 if (ret >= 0 || ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT))
609 #define DEFINE_BLOB_OPEN(table_name) \
610 static int table_name ## _open(const char *dir) \
612 return blob_open(&table_name ## _table, \
613 &table_name ## _table_desc, dir); \
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) \
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; \
629 /** Define all functions for this blob type. */
630 #define DEFINE_BLOB_FUNCTIONS(table_name, short_name, c_short_name) \
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, LS, table_name, short_name, c_short_name) \
636 DEFINE_BLOB_COMMAND(cat, CAT, table_name, short_name, c_short_name) \
637 DEFINE_BLOB_COMMAND(add, ADD, table_name, short_name, c_short_name) \
638 DEFINE_BLOB_COMMAND(rm, RM, table_name, short_name, c_short_name) \
639 DEFINE_BLOB_COMMAND(mv, MV, table_name, short_name, c_short_name) \
640 DEFINE_GET_NAME_BY_ID(table_name, short_name); \
641 DEFINE_GET_DEF_BY_ID(table_name, short_name); \
642 DEFINE_GET_DEF_BY_NAME(table_name, short_name); \
643 DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, short_name); \
645 /* doxygen isn't smart enough to recognize these */
646 /** \cond blob_function */
647 DEFINE_BLOB_FUNCTIONS(lyrics, lyr, LYR);
648 DEFINE_BLOB_FUNCTIONS(images, img, IMG);
649 DEFINE_BLOB_FUNCTIONS(moods, mood, MOOD);
650 DEFINE_BLOB_FUNCTIONS(playlists, pl, PL);
651 /** \endcond blob_function */