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. */
14 #include "server_cmd.lsg.h"
22 #include "portable_io.h"
27 * Compare two osl objects pointing to unsigned integers of 32 bit size.
29 * \param obj1 Pointer to the first integer.
30 * \param obj2 Pointer to the second integer.
32 * \return The values required for an osl compare function.
34 static int uint32_compare(const struct osl_object *obj1, const struct osl_object *obj2)
36 uint32_t d1 = read_u32(obj1->data);
37 uint32_t d2 = read_u32(obj2->data);
46 static struct osl_column_description blob_cols[] = {
48 .storage_type = OSL_MAPPED_STORAGE,
49 .storage_flags = OSL_RBTREE | OSL_UNIQUE | OSL_FIXED_SIZE,
52 .compare_function = uint32_compare
55 .storage_type = OSL_MAPPED_STORAGE,
56 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
58 .compare_function = string_compare
61 .storage_type = OSL_DISK_STORAGE,
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 \
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;
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);
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 */
93 static int print_blob(struct osl_table *table, struct osl_row *row,
94 const char *name, void *data)
96 struct afs_callback_arg *aca = data;
97 bool l_given = SERVER_CMD_OPT_GIVEN(LSMOOD, LONG, aca->lpr);
98 struct osl_object obj;
103 para_printf(&aca->pbout, "%s\n", name);
106 ret = osl(osl_get_object(table, row, BLOBCOL_ID, &obj));
108 para_printf(&aca->pbout, "cannot list %s\n", name);
111 id = *(uint32_t *)obj.data;
112 para_printf(&aca->pbout, "%u\t%s\n", id, name);
116 static int com_lsblob_callback(const struct lls_command * const cmd,
117 struct osl_table *table, struct afs_callback_arg *aca)
119 bool i_given, r_given;
120 struct pattern_match_data pmd = {
122 .pm_flags = PM_NO_PATTERN_MATCHES_EVERYTHING | PM_SKIP_EMPTY_NAME,
123 .match_col_num = BLOBCOL_NAME,
125 .action = print_blob,
128 ret = lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr);
131 i_given = SERVER_CMD_OPT_GIVEN(LSMOOD, ID_SORT, aca->lpr);
132 r_given = SERVER_CMD_OPT_GIVEN(LSMOOD, REVERSE, aca->lpr);
135 pmd.pm_flags |= PM_REVERSE_LOOP;
137 pmd.loop_col_num = BLOBCOL_ID;
139 pmd.loop_col_num = BLOBCOL_NAME;
140 ret = for_each_matching_row(&pmd);
143 if (pmd.num_matches == 0 && lls_num_inputs(aca->lpr) > 0)
146 lls_free_parse_result(aca->lpr, cmd);
150 static int com_lsblob(afs_callback *f, const struct lls_command * const cmd,
151 struct command_context *cc, struct lls_parse_result *lpr)
153 return send_lls_callback_request(f, cmd, lpr, cc);
156 static int cat_blob(struct osl_table *table, struct osl_row *row,
157 __a_unused const char *name, void *data)
159 int ret = 0, ret2, fd = *(int *)data;
160 struct osl_object obj;
162 ret = osl(osl_open_disk_object(table, row, BLOBCOL_DEF, &obj));
164 return (ret == osl(-E_OSL_EMPTY))? 0 : ret;
165 assert(obj.size > 0);
166 ret = pass_buffer_as_shm(fd, SBD_OUTPUT, obj.data, obj.size);
167 ret2 = osl(osl_close_disk_object(&obj));
168 return (ret < 0)? ret : ret2;
171 static int com_catblob_callback(const struct lls_command * const cmd,
172 struct osl_table *table, struct afs_callback_arg *aca)
175 struct pattern_match_data pmd = {
177 .loop_col_num = BLOBCOL_NAME,
178 .match_col_num = BLOBCOL_NAME,
179 .pm_flags = PM_SKIP_EMPTY_NAME,
183 ret = lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr);
186 ret = for_each_matching_row(&pmd);
189 if (pmd.num_matches == 0)
192 lls_free_parse_result(aca->lpr, cmd);
196 static int com_catblob(afs_callback *f, const struct lls_command * const cmd,
197 struct command_context *cc, struct lls_parse_result *lpr)
200 int ret = lls(lls_check_arg_count(lpr, 1, INT_MAX, &errctx));
203 send_errctx(cc, errctx);
206 return send_lls_callback_request(f, cmd, lpr, cc);
209 static int remove_blob(struct osl_table *table, struct osl_row *row,
210 const char *name, void *data)
212 struct afs_callback_arg *aca = data;
213 int ret = osl(osl_del_row(table, row));
215 para_printf(&aca->pbout, "cannot remove %s\n", name);
221 static int com_rmblob_callback(const struct lls_command * const cmd,
222 struct osl_table *table, struct afs_callback_arg *aca)
225 struct pattern_match_data pmd = {
227 .loop_col_num = BLOBCOL_NAME,
228 .match_col_num = BLOBCOL_NAME,
229 .pm_flags = PM_SKIP_EMPTY_NAME,
231 .action = remove_blob
233 ret = lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr);
236 ret = for_each_matching_row(&pmd);
239 if (pmd.num_matches == 0)
242 para_printf(&aca->pbout, "removed %u blob(s)\n",
244 ret = afs_event(BLOB_REMOVE, NULL, table);
247 lls_free_parse_result(aca->lpr, cmd);
251 static int com_rmblob(afs_callback *f, const struct lls_command * const cmd,
252 struct command_context *cc, struct lls_parse_result *lpr)
255 int ret = lls(lls_check_arg_count(lpr, 1, INT_MAX, &errctx));
258 send_errctx(cc, errctx);
261 return send_lls_callback_request(f, cmd, lpr, cc);
264 static int com_addblob_callback(__a_unused const struct lls_command * const cmd,
265 struct osl_table *table, struct afs_callback_arg *aca)
267 struct osl_object objs[NUM_BLOB_COLUMNS];
268 char *name = aca->query.data;
269 size_t name_len = strlen(name) + 1;
274 ret = osl(osl_get_num_rows(table, &num_rows));
277 if (!num_rows) { /* this is the first entry ever added */
278 /* insert dummy row containing the id */
279 id = 2; /* this entry will be entry #1, so 2 is the next */
280 objs[BLOBCOL_ID].data = &id;
281 objs[BLOBCOL_ID].size = sizeof(id);
282 objs[BLOBCOL_NAME].data = "";
283 objs[BLOBCOL_NAME].size = 1;
284 objs[BLOBCOL_DEF].data = "";
285 objs[BLOBCOL_DEF].size = 1;
286 ret = osl(osl_add_row(table, objs));
290 /* check if name already exists */
292 struct osl_object obj = {.data = name, .size = name_len};
293 ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
294 if (ret < 0 && ret != -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
296 if (ret >= 0) { /* we already have a blob with this name */
297 ret = osl(osl_get_object(table, row, BLOBCOL_ID, &obj));
300 id = *(uint32_t *)obj.data;
301 obj.data = name + name_len;
302 obj.size = aca->query.size - name_len;
303 ret = osl(osl_update_object(table, row, BLOBCOL_DEF, &obj));
306 /* new blob, get id of the dummy row and increment it */
309 ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
312 ret = osl(osl_get_object(table, row, BLOBCOL_ID, &obj));
315 id = *(uint32_t *)obj.data + 1;
317 ret = osl(osl_update_object(table, row, BLOBCOL_ID, &obj));
322 objs[BLOBCOL_ID].data = &id;
323 objs[BLOBCOL_ID].size = sizeof(id);
324 objs[BLOBCOL_NAME].data = name;
325 objs[BLOBCOL_NAME].size = name_len;
326 objs[BLOBCOL_DEF].data = name + name_len;
327 objs[BLOBCOL_DEF].size = aca->query.size - name_len;
328 ret = osl(osl_add_row(table, objs));
331 ret = afs_event(BLOB_ADD, NULL, table);
334 para_printf(&aca->pbout, "cannot add %s\n", name);
336 para_printf(&aca->pbout, "added %s as id %u\n", name, id);
340 /* Write input from fd to dynamically allocated buffer, but maximal 10M. */
341 static int fd2buf(struct stream_cipher_context *scc, struct osl_object *obj)
343 size_t max_size = 10 * 1024 * 1024;
351 ret = recv_sb(scc, SBD_BLOB_DATA, max_size, &iov);
360 if (iov.iov_len == 0) /* end of blob */
363 obj->data = iov.iov_base;
364 obj->size = iov.iov_len;
366 obj->data = para_realloc(obj->data, obj->size + iov.iov_len);
367 memcpy(obj->data + obj->size, iov.iov_base, iov.iov_len);
368 obj->size += iov.iov_len;
370 max_size -= iov.iov_len;
377 * Read blob from a file descriptor and send it to afs.
379 * This function is called from the addblob command handlers to instruct the
380 * afs process to store the input in a blob table. Input is read and decrypted
381 * from the file descriptor given by cc and appended to arg_obj, which contains
382 * the name of the blob to create. The combined buffer is made available to the
383 * afs process via the callback method.
385 static int stdin_command(struct command_context *cc,
386 struct lls_parse_result *lpr, afs_callback *f)
388 struct osl_object query, stdin_obj;
390 size_t len = strlen(lls_input(0, lpr));
392 ret = send_sb(&cc->scc, NULL, 0, SBD_AWAITING_DATA, false);
395 ret = fd2buf(&cc->scc, &stdin_obj);
398 query.size = len + 1 + stdin_obj.size;
399 query.data = para_malloc(query.size);
400 memcpy(query.data, lls_input(0, lpr), len + 1);
401 if (stdin_obj.size > 0)
402 memcpy((char *)query.data + len + 1, stdin_obj.data,
404 free(stdin_obj.data);
405 ret = send_callback_request(f, &query, afs_cb_result_handler, cc);
410 static int com_addblob(afs_callback *f, __a_unused const struct lls_command * const cmd,
411 struct command_context *cc, struct lls_parse_result *lpr)
414 int ret = lls(lls_check_arg_count(lpr, 1, 1, &errctx));
417 send_errctx(cc, errctx);
420 if (!lls_input(0, lpr)[0]) /* empty name is reserved for the dummy row */
421 return -E_BLOB_SYNTAX;
422 return stdin_command(cc, lpr, f);
425 static int com_mvblob_callback(const struct lls_command * const cmd,
426 struct osl_table *table, struct afs_callback_arg *aca)
428 const char *src, *dest;
429 struct osl_object obj;
433 ret = lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr);
435 src = lls_input(0, aca->lpr);
436 dest = lls_input(1, aca->lpr);
437 obj.data = (char *)src;
438 obj.size = strlen(src) + 1;
439 ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
442 para_printf(&aca->pbout, "cannot find source blob %s\n", src);
445 obj.data = (char *)dest;
446 obj.size = strlen(dest) + 1;
447 ret = osl(osl_update_object(table, row, BLOBCOL_NAME, &obj));
449 para_printf(&aca->pbout, "cannot rename blob %s to %s\n",
453 ret = afs_event(BLOB_RENAME, NULL, table);
455 lls_free_parse_result(aca->lpr, cmd);
459 static int com_mvblob(afs_callback *f, const struct lls_command * const cmd,
460 struct command_context *cc, struct lls_parse_result *lpr)
463 int ret = lls(lls_check_arg_count(lpr, 2, 2, &errctx));
466 send_errctx(cc, errctx);
469 return send_lls_callback_request(f, cmd, lpr, cc);
472 #define DEFINE_BLOB_COMMAND(cmd_name, c_cmd_name, table_name, short_name, c_short_name) \
473 static int com_ ## cmd_name ## short_name ## _callback(struct afs_callback_arg *aca) \
475 const struct lls_command *cmd = SERVER_CMD_CMD_PTR(c_cmd_name ## c_short_name); \
476 return com_ ## cmd_name ## blob_callback(cmd, table_name ## _table, aca); \
478 static int com_ ## cmd_name ## short_name(struct command_context *cc, struct lls_parse_result *lpr) \
480 const struct lls_command *cmd = SERVER_CMD_CMD_PTR(c_cmd_name ## c_short_name); \
481 return com_ ## cmd_name ## blob(com_ ## cmd_name ## short_name ## _callback, cmd, cc, lpr); \
483 EXPORT_SERVER_CMD_HANDLER(cmd_name ## short_name);
485 static int blob_get_name_by_id(struct osl_table *table, uint32_t id,
489 struct osl_object obj = {.data = &id, .size = sizeof(id)};
496 ret = osl(osl_get_row(table, BLOBCOL_ID, &obj, &row));
499 ret = osl(osl_get_object(table, row, BLOBCOL_NAME, &obj));
502 if (*(char *)obj.data == '\0')
505 *name = (char *)obj.data;
509 /** Define the \p get_name_by_id function for this blob type. */
510 #define DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix) \
511 int cmd_prefix ## _get_name_by_id(uint32_t id, char **name) \
513 return blob_get_name_by_id(table_name ## _table, id, name); \
517 static int blob_get_def_by_name(struct osl_table *table, char *name,
518 struct osl_object *def)
521 struct osl_object obj = {.data = name, .size = strlen(name) + 1};
527 ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
530 return osl(osl_open_disk_object(table, row, BLOBCOL_DEF, def));
533 /** Define the \p get_def_by_id function for this blob type. */
534 #define DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix) \
535 int cmd_prefix ## _get_def_by_name(char *name, struct osl_object *def) \
537 return blob_get_def_by_name(table_name ## _table, name, def); \
540 static int blob_get_def_by_id(struct osl_table *table, uint32_t id,
541 struct osl_object *def)
544 struct osl_object obj = {.data = &id, .size = sizeof(id)};
550 ret = osl(osl_get_row(table, BLOBCOL_ID, &obj, &row));
553 return osl(osl_open_disk_object(table, row, BLOBCOL_DEF, def));
556 /** Define the \p get_def_by_id function for this blob type. */
557 #define DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix) \
558 int cmd_prefix ## _get_def_by_id(uint32_t id, struct osl_object *def) \
560 return blob_get_def_by_id(table_name ## _table, id, def); \
563 static int blob_get_name_and_def_by_row(struct osl_table *table,
564 const struct osl_row *row, char **name, struct osl_object *def)
566 struct osl_object obj;
567 int ret = osl(osl_get_object(table, row, BLOBCOL_NAME, &obj));
571 return osl(osl_open_disk_object(table, row, BLOBCOL_DEF, def));
573 /** Define the \p get_name_and_def_by_row function for this blob type. */
574 #define DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix) \
575 int cmd_prefix ## _get_name_and_def_by_row(const struct osl_row *row, \
576 char **name, struct osl_object *def) \
578 return blob_get_name_and_def_by_row(table_name ## _table, \
582 /** Define the \p close function for this blob type. */
583 #define DEFINE_BLOB_CLOSE(table_name) \
584 static void table_name ## _close(void) \
586 osl_close_table(table_name ## _table, OSL_MARK_CLEAN); \
587 table_name ## _table = NULL; \
590 /** Define the \p create function for this blob type. */
591 #define DEFINE_BLOB_CREATE(table_name) \
592 static int table_name ## _create(const char *dir) \
594 table_name ## _table_desc.dir = dir; \
595 return osl(osl_create_table(&table_name ## _table_desc)); \
598 static int blob_open(struct osl_table **table,
599 struct osl_table_description *desc,
604 ret = osl(osl_open_table(desc, table));
608 if (ret >= 0 || ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT))
613 #define DEFINE_BLOB_OPEN(table_name) \
614 static int table_name ## _open(const char *dir) \
616 return blob_open(&table_name ## _table, \
617 &table_name ## _table_desc, dir); \
621 /** Define the \p init function for this blob type. */
622 #define DEFINE_BLOB_INIT(table_name) \
623 void table_name ## _init(struct afs_table *t) \
625 t->open = table_name ## _open; \
626 t->close = table_name ## _close; \
627 t->create = table_name ## _create;\
628 t->event_handler = table_name ##_event_handler; \
629 table_name ## _table = NULL; \
633 /** Define all functions for this blob type. */
634 #define DEFINE_BLOB_FUNCTIONS(table_name, short_name, c_short_name) \
635 DEFINE_BLOB_OPEN(table_name) \
636 DEFINE_BLOB_CLOSE(table_name) \
637 DEFINE_BLOB_CREATE(table_name) \
638 DEFINE_BLOB_INIT(table_name) \
639 DEFINE_BLOB_COMMAND(ls, LS, table_name, short_name, c_short_name) \
640 DEFINE_BLOB_COMMAND(cat, CAT, table_name, short_name, c_short_name) \
641 DEFINE_BLOB_COMMAND(add, ADD, table_name, short_name, c_short_name) \
642 DEFINE_BLOB_COMMAND(rm, RM, table_name, short_name, c_short_name) \
643 DEFINE_BLOB_COMMAND(mv, MV, table_name, short_name, c_short_name) \
644 DEFINE_GET_NAME_BY_ID(table_name, short_name); \
645 DEFINE_GET_DEF_BY_ID(table_name, short_name); \
646 DEFINE_GET_DEF_BY_NAME(table_name, short_name); \
647 DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, short_name); \
649 /* doxygen isn't smart enough to recognize these */
650 /** \cond blob_function */
651 DEFINE_BLOB_FUNCTIONS(lyrics, lyr, LYR);
652 DEFINE_BLOB_FUNCTIONS(images, img, IMG);
653 DEFINE_BLOB_FUNCTIONS(moods, mood, MOOD);
654 DEFINE_BLOB_FUNCTIONS(playlists, pl, PL);
655 /** \endcond blob_function */