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 * \sa osl_compare_func, osl_hash_compare().
36 static int uint32_compare(const struct osl_object *obj1, const struct osl_object *obj2)
38 uint32_t d1 = read_u32((const char *)obj1->data);
39 uint32_t d2 = read_u32((const char *)obj2->data);
48 static struct osl_column_description blob_cols[] = {
50 .storage_type = OSL_MAPPED_STORAGE,
51 .storage_flags = OSL_RBTREE | OSL_UNIQUE | OSL_FIXED_SIZE,
54 .compare_function = uint32_compare
57 .storage_type = OSL_MAPPED_STORAGE,
58 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
60 .compare_function = string_compare
63 .storage_type = OSL_DISK_STORAGE,
69 /** Define an osl table description for a blob table. */
70 #define DEFINE_BLOB_TABLE_DESC(table_name) \
71 static struct osl_table_description table_name ## _table_desc = { \
72 .name = #table_name, \
73 .num_columns = NUM_BLOB_COLUMNS, \
74 .flags = OSL_LARGE_TABLE, \
75 .column_descriptions = blob_cols \
78 /** Define a pointer to an osl blob table with a canonical name. */
79 #define DEFINE_BLOB_TABLE_PTR(table_name) struct osl_table *table_name ## _table;
82 /** Define a blob table. */
83 #define INIT_BLOB_TABLE(table_name) \
84 DEFINE_BLOB_TABLE_DESC(table_name); \
85 DEFINE_BLOB_TABLE_PTR(table_name);
87 /* doxygen isn't smart enough to recognize these */
88 /** \cond blob_table */
89 INIT_BLOB_TABLE(lyrics);
90 INIT_BLOB_TABLE(images);
91 INIT_BLOB_TABLE(moods);
92 INIT_BLOB_TABLE(playlists);
93 /** \endcond blob_table */
95 static int print_blob(struct osl_table *table, struct osl_row *row,
96 const char *name, void *data)
98 struct afs_callback_arg *aca = data;
99 bool l_given = SERVER_CMD_OPT_GIVEN(LSMOOD, LONG, aca->lpr);
100 struct osl_object obj;
105 para_printf(&aca->pbout, "%s\n", name);
108 ret = osl(osl_get_object(table, row, BLOBCOL_ID, &obj));
110 para_printf(&aca->pbout, "cannot list %s\n", name);
113 id = *(uint32_t *)obj.data;
114 para_printf(&aca->pbout, "%u\t%s\n", id, name);
118 static int com_lsblob_callback(const struct lls_command * const cmd,
119 struct osl_table *table, struct afs_callback_arg *aca)
121 bool i_given, r_given;
122 struct pattern_match_data pmd = {
124 .pm_flags = PM_NO_PATTERN_MATCHES_EVERYTHING | PM_SKIP_EMPTY_NAME,
125 .match_col_num = BLOBCOL_NAME,
127 .action = print_blob,
130 ret = lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr);
133 i_given = SERVER_CMD_OPT_GIVEN(LSMOOD, ID_SORT, aca->lpr);
134 r_given = SERVER_CMD_OPT_GIVEN(LSMOOD, REVERSE, aca->lpr);
137 pmd.pm_flags |= PM_REVERSE_LOOP;
139 pmd.loop_col_num = BLOBCOL_ID;
141 pmd.loop_col_num = BLOBCOL_NAME;
142 ret = for_each_matching_row(&pmd);
145 if (pmd.num_matches == 0 && lls_num_inputs(aca->lpr) > 0)
148 lls_free_parse_result(aca->lpr, cmd);
152 static int com_lsblob(afs_callback *f, const struct lls_command * const cmd,
153 struct command_context *cc, struct lls_parse_result *lpr)
155 return send_lls_callback_request(f, cmd, lpr, cc);
158 static int cat_blob(struct osl_table *table, struct osl_row *row,
159 __a_unused const char *name, void *data)
161 int ret = 0, ret2, fd = *(int *)data;
162 struct osl_object obj;
164 ret = osl(osl_open_disk_object(table, row, BLOBCOL_DEF, &obj));
166 return (ret == osl(-E_OSL_EMPTY))? 0 : ret;
167 assert(obj.size > 0);
168 ret = pass_buffer_as_shm(fd, SBD_OUTPUT, obj.data, obj.size);
169 ret2 = osl(osl_close_disk_object(&obj));
170 return (ret < 0)? ret : ret2;
173 static int com_catblob_callback(const struct lls_command * const cmd,
174 struct osl_table *table, struct afs_callback_arg *aca)
177 struct pattern_match_data pmd = {
179 .loop_col_num = BLOBCOL_NAME,
180 .match_col_num = BLOBCOL_NAME,
181 .pm_flags = PM_SKIP_EMPTY_NAME,
185 ret = lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr);
188 ret = for_each_matching_row(&pmd);
191 if (pmd.num_matches == 0)
194 lls_free_parse_result(aca->lpr, cmd);
198 static int com_catblob(afs_callback *f, const struct lls_command * const cmd,
199 struct command_context *cc, struct lls_parse_result *lpr)
202 int ret = lls(lls_check_arg_count(lpr, 1, INT_MAX, &errctx));
205 send_errctx(cc, errctx);
208 return send_lls_callback_request(f, cmd, lpr, cc);
211 static int remove_blob(struct osl_table *table, struct osl_row *row,
212 const char *name, void *data)
214 struct afs_callback_arg *aca = data;
215 int ret = osl(osl_del_row(table, row));
217 para_printf(&aca->pbout, "cannot remove %s\n", name);
223 static int com_rmblob_callback(const struct lls_command * const cmd,
224 struct osl_table *table, struct afs_callback_arg *aca)
227 struct pattern_match_data pmd = {
229 .loop_col_num = BLOBCOL_NAME,
230 .match_col_num = BLOBCOL_NAME,
231 .pm_flags = PM_SKIP_EMPTY_NAME,
233 .action = remove_blob
235 ret = lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr);
238 ret = for_each_matching_row(&pmd);
241 if (pmd.num_matches == 0)
244 para_printf(&aca->pbout, "removed %u blob(s)\n",
246 ret = afs_event(BLOB_REMOVE, NULL, table);
249 lls_free_parse_result(aca->lpr, cmd);
253 static int com_rmblob(afs_callback *f, const struct lls_command * const cmd,
254 struct command_context *cc, struct lls_parse_result *lpr)
257 int ret = lls(lls_check_arg_count(lpr, 1, INT_MAX, &errctx));
260 send_errctx(cc, errctx);
263 return send_lls_callback_request(f, cmd, lpr, cc);
266 static int com_addblob_callback(__a_unused const struct lls_command * const cmd,
267 struct osl_table *table, struct afs_callback_arg *aca)
269 struct osl_object objs[NUM_BLOB_COLUMNS];
270 char *name = aca->query.data;
271 size_t name_len = strlen(name) + 1;
276 ret = osl(osl_get_num_rows(table, &num_rows));
279 if (!num_rows) { /* this is the first entry ever added */
280 /* insert dummy row containing the id */
281 id = 2; /* this entry will be entry #1, so 2 is the next */
282 objs[BLOBCOL_ID].data = &id;
283 objs[BLOBCOL_ID].size = sizeof(id);
284 objs[BLOBCOL_NAME].data = "";
285 objs[BLOBCOL_NAME].size = 1;
286 objs[BLOBCOL_DEF].data = "";
287 objs[BLOBCOL_DEF].size = 1;
288 ret = osl(osl_add_row(table, objs));
292 /* check if name already exists */
294 struct osl_object obj = {.data = name, .size = name_len};
295 ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
296 if (ret < 0 && ret != -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
298 if (ret >= 0) { /* we already have a blob with this name */
299 ret = osl(osl_get_object(table, row, BLOBCOL_ID, &obj));
302 id = *(uint32_t *)obj.data;
303 obj.data = name + name_len;
304 obj.size = aca->query.size - name_len;
305 ret = osl(osl_update_object(table, row, BLOBCOL_DEF, &obj));
308 /* new blob, get id of the dummy row and increment it */
311 ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
314 ret = osl(osl_get_object(table, row, BLOBCOL_ID, &obj));
317 id = *(uint32_t *)obj.data + 1;
319 ret = osl(osl_update_object(table, row, BLOBCOL_ID, &obj));
324 objs[BLOBCOL_ID].data = &id;
325 objs[BLOBCOL_ID].size = sizeof(id);
326 objs[BLOBCOL_NAME].data = name;
327 objs[BLOBCOL_NAME].size = name_len;
328 objs[BLOBCOL_DEF].data = name + name_len;
329 objs[BLOBCOL_DEF].size = aca->query.size - name_len;
330 ret = osl(osl_add_row(table, objs));
333 ret = afs_event(BLOB_ADD, NULL, table);
336 para_printf(&aca->pbout, "cannot add %s\n", name);
338 para_printf(&aca->pbout, "added %s as id %u\n", name, id);
342 /* Write input from fd to dynamically allocated buffer, but maximal 10M. */
343 static int fd2buf(struct stream_cipher_context *scc, struct osl_object *obj)
345 size_t max_size = 10 * 1024 * 1024;
353 ret = recv_sb(scc, SBD_BLOB_DATA, max_size, &iov);
362 if (iov.iov_len == 0) /* end of blob */
365 obj->data = iov.iov_base;
366 obj->size = iov.iov_len;
368 obj->data = para_realloc(obj->data, obj->size + iov.iov_len);
369 memcpy(obj->data + obj->size, iov.iov_base, iov.iov_len);
370 obj->size += iov.iov_len;
372 max_size -= iov.iov_len;
379 * Read blob from a file descriptor and send it to afs.
381 * This function is called from the addblob command handlers to instruct the
382 * afs process to store the input in a blob table. Input is read and decrypted
383 * from the file descriptor given by cc and appended to arg_obj, which contains
384 * the name of the blob to create. The combined buffer is made available to the
385 * afs process via the callback method.
387 static int stdin_command(struct command_context *cc,
388 struct lls_parse_result *lpr, afs_callback *f)
390 struct osl_object query, stdin_obj;
392 size_t len = strlen(lls_input(0, lpr));
394 ret = send_sb(&cc->scc, NULL, 0, SBD_AWAITING_DATA, false);
397 ret = fd2buf(&cc->scc, &stdin_obj);
400 query.size = len + 1 + stdin_obj.size;
401 query.data = para_malloc(query.size);
402 memcpy(query.data, lls_input(0, lpr), len + 1);
403 if (stdin_obj.size > 0)
404 memcpy((char *)query.data + len + 1, stdin_obj.data,
406 free(stdin_obj.data);
407 ret = send_callback_request(f, &query, afs_cb_result_handler, cc);
412 static int com_addblob(afs_callback *f, __a_unused const struct lls_command * const cmd,
413 struct command_context *cc, struct lls_parse_result *lpr)
416 int ret = lls(lls_check_arg_count(lpr, 1, 1, &errctx));
419 send_errctx(cc, errctx);
422 if (!lls_input(0, lpr)[0]) /* empty name is reserved for the dummy row */
423 return -E_BLOB_SYNTAX;
424 return stdin_command(cc, lpr, f);
427 static int com_mvblob_callback(const struct lls_command * const cmd,
428 struct osl_table *table, struct afs_callback_arg *aca)
430 const char *src, *dest;
431 struct osl_object obj;
435 ret = lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr);
437 src = lls_input(0, aca->lpr);
438 dest = lls_input(1, aca->lpr);
439 obj.data = (char *)src;
440 obj.size = strlen(src) + 1;
441 ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
444 para_printf(&aca->pbout, "cannot find source blob %s\n", src);
447 obj.data = (char *)dest;
448 obj.size = strlen(dest) + 1;
449 ret = osl(osl_update_object(table, row, BLOBCOL_NAME, &obj));
451 para_printf(&aca->pbout, "cannot rename blob %s to %s\n",
455 ret = afs_event(BLOB_RENAME, NULL, table);
457 lls_free_parse_result(aca->lpr, cmd);
461 static int com_mvblob(afs_callback *f, const struct lls_command * const cmd,
462 struct command_context *cc, struct lls_parse_result *lpr)
465 int ret = lls(lls_check_arg_count(lpr, 2, 2, &errctx));
468 send_errctx(cc, errctx);
471 return send_lls_callback_request(f, cmd, lpr, cc);
474 #define DEFINE_BLOB_COMMAND(cmd_name, c_cmd_name, table_name, short_name, c_short_name) \
475 static int com_ ## cmd_name ## short_name ## _callback(struct afs_callback_arg *aca) \
477 const struct lls_command *cmd = SERVER_CMD_CMD_PTR(c_cmd_name ## c_short_name); \
478 return com_ ## cmd_name ## blob_callback(cmd, table_name ## _table, aca); \
480 static int com_ ## cmd_name ## short_name(struct command_context *cc, struct lls_parse_result *lpr) \
482 const struct lls_command *cmd = SERVER_CMD_CMD_PTR(c_cmd_name ## c_short_name); \
483 return com_ ## cmd_name ## blob(com_ ## cmd_name ## short_name ## _callback, cmd, cc, lpr); \
485 EXPORT_SERVER_CMD_HANDLER(cmd_name ## short_name);
487 static int blob_get_name_by_id(struct osl_table *table, uint32_t id,
491 struct osl_object obj = {.data = &id, .size = sizeof(id)};
498 ret = osl(osl_get_row(table, BLOBCOL_ID, &obj, &row));
501 ret = osl(osl_get_object(table, row, BLOBCOL_NAME, &obj));
504 if (*(char *)obj.data == '\0')
507 *name = (char *)obj.data;
511 /** Define the \p get_name_by_id function for this blob type. */
512 #define DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix) \
513 int cmd_prefix ## _get_name_by_id(uint32_t id, char **name) \
515 return blob_get_name_by_id(table_name ## _table, id, name); \
519 static int blob_get_def_by_name(struct osl_table *table, char *name,
520 struct osl_object *def)
523 struct osl_object obj = {.data = name, .size = strlen(name) + 1};
529 ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
532 return osl(osl_open_disk_object(table, row, BLOBCOL_DEF, def));
535 /** Define the \p get_def_by_id function for this blob type. */
536 #define DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix) \
537 int cmd_prefix ## _get_def_by_name(char *name, struct osl_object *def) \
539 return blob_get_def_by_name(table_name ## _table, name, def); \
542 static int blob_get_def_by_id(struct osl_table *table, uint32_t id,
543 struct osl_object *def)
546 struct osl_object obj = {.data = &id, .size = sizeof(id)};
552 ret = osl(osl_get_row(table, BLOBCOL_ID, &obj, &row));
555 return osl(osl_open_disk_object(table, row, BLOBCOL_DEF, def));
558 /** Define the \p get_def_by_id function for this blob type. */
559 #define DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix) \
560 int cmd_prefix ## _get_def_by_id(uint32_t id, struct osl_object *def) \
562 return blob_get_def_by_id(table_name ## _table, id, def); \
565 static int blob_get_name_and_def_by_row(struct osl_table *table,
566 const struct osl_row *row, char **name, struct osl_object *def)
568 struct osl_object obj;
569 int ret = osl(osl_get_object(table, row, BLOBCOL_NAME, &obj));
573 return osl(osl_open_disk_object(table, row, BLOBCOL_DEF, def));
575 /** Define the \p get_name_and_def_by_row function for this blob type. */
576 #define DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix) \
577 int cmd_prefix ## _get_name_and_def_by_row(const struct osl_row *row, \
578 char **name, struct osl_object *def) \
580 return blob_get_name_and_def_by_row(table_name ## _table, \
584 /** Define the \p close function for this blob type. */
585 #define DEFINE_BLOB_CLOSE(table_name) \
586 static void table_name ## _close(void) \
588 osl_close_table(table_name ## _table, OSL_MARK_CLEAN); \
589 table_name ## _table = NULL; \
592 /** Define the \p create function for this blob type. */
593 #define DEFINE_BLOB_CREATE(table_name) \
594 static int table_name ## _create(const char *dir) \
596 table_name ## _table_desc.dir = dir; \
597 return osl(osl_create_table(&table_name ## _table_desc)); \
600 static int blob_open(struct osl_table **table,
601 struct osl_table_description *desc,
606 ret = osl(osl_open_table(desc, table));
610 if (ret >= 0 || ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT))
615 #define DEFINE_BLOB_OPEN(table_name) \
616 static int table_name ## _open(const char *dir) \
618 return blob_open(&table_name ## _table, \
619 &table_name ## _table_desc, dir); \
623 /** Define the \p init function for this blob type. */
624 #define DEFINE_BLOB_INIT(table_name) \
625 void table_name ## _init(struct afs_table *t) \
627 t->open = table_name ## _open; \
628 t->close = table_name ## _close; \
629 t->create = table_name ## _create;\
630 t->event_handler = table_name ##_event_handler; \
631 table_name ## _table = NULL; \
635 /** Define all functions for this blob type. */
636 #define DEFINE_BLOB_FUNCTIONS(table_name, short_name, c_short_name) \
637 DEFINE_BLOB_OPEN(table_name) \
638 DEFINE_BLOB_CLOSE(table_name) \
639 DEFINE_BLOB_CREATE(table_name) \
640 DEFINE_BLOB_INIT(table_name) \
641 DEFINE_BLOB_COMMAND(ls, LS, table_name, short_name, c_short_name) \
642 DEFINE_BLOB_COMMAND(cat, CAT, table_name, short_name, c_short_name) \
643 DEFINE_BLOB_COMMAND(add, ADD, table_name, short_name, c_short_name) \
644 DEFINE_BLOB_COMMAND(rm, RM, table_name, short_name, c_short_name) \
645 DEFINE_BLOB_COMMAND(mv, MV, table_name, short_name, c_short_name) \
646 DEFINE_GET_NAME_BY_ID(table_name, short_name); \
647 DEFINE_GET_DEF_BY_ID(table_name, short_name); \
648 DEFINE_GET_DEF_BY_NAME(table_name, short_name); \
649 DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, short_name); \
651 /* doxygen isn't smart enough to recognize these */
652 /** \cond blob_function */
653 DEFINE_BLOB_FUNCTIONS(lyrics, lyr, LYR);
654 DEFINE_BLOB_FUNCTIONS(images, img, IMG);
655 DEFINE_BLOB_FUNCTIONS(moods, mood, MOOD);
656 DEFINE_BLOB_FUNCTIONS(playlists, pl, PL);
657 /** \endcond blob_function */