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;
75 /** Define a blob table. */
76 #define INIT_BLOB_TABLE(table_name) \
77 DEFINE_BLOB_TABLE_DESC(table_name); \
78 DEFINE_BLOB_TABLE_PTR(table_name);
80 /* doxygen isn't smart enough to recognize these */
81 /** \cond blob_table */
82 INIT_BLOB_TABLE(lyrics);
83 INIT_BLOB_TABLE(images);
84 INIT_BLOB_TABLE(moods);
85 INIT_BLOB_TABLE(playlists);
86 /** \endcond blob_table */
88 static int print_blob(struct osl_table *table, struct osl_row *row,
89 const char *name, void *data)
91 struct afs_callback_arg *aca = data;
92 bool l_given = SERVER_CMD_OPT_GIVEN(LSMOOD, LONG, aca->lpr);
93 struct osl_object obj;
98 para_printf(&aca->pbout, "%s\n", name);
101 ret = osl(osl_get_object(table, row, BLOBCOL_ID, &obj));
103 para_printf(&aca->pbout, "cannot list %s\n", name);
106 id = *(uint32_t *)obj.data;
107 para_printf(&aca->pbout, "%u\t%s\n", id, name);
111 static int com_lsblob_callback(const struct lls_command * const cmd,
112 struct osl_table *table, struct afs_callback_arg *aca)
114 bool i_given, r_given;
115 struct pattern_match_data pmd = {
117 .pm_flags = PM_NO_PATTERN_MATCHES_EVERYTHING | PM_SKIP_EMPTY_NAME,
118 .match_col_num = BLOBCOL_NAME,
120 .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,
180 ret = lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr);
183 ret = for_each_matching_row(&pmd);
186 if (pmd.num_matches == 0)
189 lls_free_parse_result(aca->lpr, cmd);
193 static int com_catblob(afs_callback *f, const struct lls_command * const cmd,
194 struct command_context *cc, struct lls_parse_result *lpr)
197 int ret = lls(lls_check_arg_count(lpr, 1, INT_MAX, &errctx));
200 send_errctx(cc, errctx);
203 return send_lls_callback_request(f, cmd, lpr, cc);
206 static int remove_blob(struct osl_table *table, struct osl_row *row,
207 const char *name, void *data)
209 struct afs_callback_arg *aca = data;
210 int ret = osl(osl_del_row(table, row));
213 para_printf(&aca->pbout, "cannot remove %s\n", name);
219 static int com_rmblob_callback(const struct lls_command * const cmd,
220 struct osl_table *table, struct afs_callback_arg *aca)
223 struct pattern_match_data pmd = {
225 .loop_col_num = BLOBCOL_NAME,
226 .match_col_num = BLOBCOL_NAME,
227 .pm_flags = PM_SKIP_EMPTY_NAME,
229 .action = remove_blob
232 ret = lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr);
235 ret = for_each_matching_row(&pmd);
238 if (pmd.num_matches == 0)
241 para_printf(&aca->pbout, "removed %u blob(s)\n",
243 ret = afs_event(BLOB_REMOVE, NULL, table);
246 lls_free_parse_result(aca->lpr, cmd);
250 static int com_rmblob(afs_callback *f, const struct lls_command * const cmd,
251 struct command_context *cc, struct lls_parse_result *lpr)
254 int ret = lls(lls_check_arg_count(lpr, 1, INT_MAX, &errctx));
257 send_errctx(cc, errctx);
260 return send_lls_callback_request(f, cmd, lpr, cc);
263 static int com_addblob_callback(__a_unused const struct lls_command * const cmd,
264 struct osl_table *table, struct afs_callback_arg *aca)
266 struct osl_object objs[NUM_BLOB_COLUMNS];
267 char *name = aca->query.data;
268 size_t name_len = strlen(name) + 1;
273 ret = osl(osl_get_num_rows(table, &num_rows));
276 if (!num_rows) { /* this is the first entry ever added */
277 /* insert dummy row containing the id */
278 id = 2; /* this entry will be entry #1, so 2 is the next */
279 objs[BLOBCOL_ID].data = &id;
280 objs[BLOBCOL_ID].size = sizeof(id);
281 objs[BLOBCOL_NAME].data = "";
282 objs[BLOBCOL_NAME].size = 1;
283 objs[BLOBCOL_DEF].data = "";
284 objs[BLOBCOL_DEF].size = 1;
285 ret = osl(osl_add_row(table, objs));
289 /* check if name already exists */
291 struct osl_object obj = {.data = name, .size = name_len};
292 ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
293 if (ret < 0 && ret != -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
295 if (ret >= 0) { /* we already have a blob with this name */
296 ret = osl(osl_get_object(table, row, BLOBCOL_ID, &obj));
299 id = *(uint32_t *)obj.data;
300 obj.data = name + name_len;
301 obj.size = aca->query.size - name_len;
302 ret = osl(osl_update_object(table, row, BLOBCOL_DEF, &obj));
305 /* new blob, get id of the dummy row and increment it */
308 ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
311 ret = osl(osl_get_object(table, row, BLOBCOL_ID, &obj));
314 id = *(uint32_t *)obj.data + 1;
316 ret = osl(osl_update_object(table, row, BLOBCOL_ID, &obj));
321 objs[BLOBCOL_ID].data = &id;
322 objs[BLOBCOL_ID].size = sizeof(id);
323 objs[BLOBCOL_NAME].data = name;
324 objs[BLOBCOL_NAME].size = name_len;
325 objs[BLOBCOL_DEF].data = name + name_len;
326 objs[BLOBCOL_DEF].size = aca->query.size - name_len;
327 ret = osl(osl_add_row(table, objs));
330 ret = afs_event(BLOB_ADD, NULL, table);
333 para_printf(&aca->pbout, "cannot add %s\n", name);
335 para_printf(&aca->pbout, "added %s as id %u\n", name, id);
339 /* Write input from fd to dynamically allocated buffer, but maximal 10M. */
340 static int fd2buf(struct stream_cipher_context *scc, struct osl_object *obj)
342 size_t max_size = 10 * 1024 * 1024;
350 ret = recv_sb(scc, SBD_BLOB_DATA, max_size, &iov);
359 if (iov.iov_len == 0) /* end of blob */
362 obj->data = iov.iov_base;
363 obj->size = iov.iov_len;
365 obj->data = para_realloc(obj->data, obj->size + iov.iov_len);
366 memcpy(obj->data + obj->size, iov.iov_base, iov.iov_len);
367 obj->size += iov.iov_len;
369 max_size -= iov.iov_len;
376 * Read blob from a file descriptor and send it to afs.
378 * This function is called from the addblob command handlers to instruct the
379 * afs process to store the input in a blob table. Input is read and decrypted
380 * from the file descriptor given by cc and appended to a buffer which also contains
381 * the name of the blob to create. The combined buffer is made available to the
382 * afs process via the callback method.
384 static int stdin_command(struct command_context *cc,
385 struct lls_parse_result *lpr, afs_callback *f)
387 struct osl_object query, stdin_obj;
389 size_t len = strlen(lls_input(0, lpr));
391 ret = send_sb(&cc->scc, NULL, 0, SBD_AWAITING_DATA, false);
394 ret = fd2buf(&cc->scc, &stdin_obj);
397 query.size = len + 1 + stdin_obj.size;
398 query.data = para_malloc(query.size);
399 memcpy(query.data, lls_input(0, lpr), len + 1);
400 if (stdin_obj.size > 0)
401 memcpy((char *)query.data + len + 1, stdin_obj.data,
403 free(stdin_obj.data);
404 ret = send_callback_request(f, &query, afs_cb_result_handler, cc);
409 static int com_addblob(afs_callback *f, __a_unused const struct lls_command * const cmd,
410 struct command_context *cc, struct lls_parse_result *lpr)
413 int ret = lls(lls_check_arg_count(lpr, 1, 1, &errctx));
416 send_errctx(cc, errctx);
419 if (!lls_input(0, lpr)[0]) /* empty name is reserved for the dummy row */
420 return -E_BLOB_SYNTAX;
421 return stdin_command(cc, lpr, f);
424 static int com_mvblob_callback(const struct lls_command * const cmd,
425 struct osl_table *table, struct afs_callback_arg *aca)
427 const char *src, *dest;
428 struct osl_object obj;
432 ret = lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr);
434 src = lls_input(0, aca->lpr);
435 dest = lls_input(1, aca->lpr);
436 obj.data = (char *)src;
437 obj.size = strlen(src) + 1;
438 ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
441 para_printf(&aca->pbout, "cannot find source blob %s\n", src);
444 obj.data = (char *)dest;
445 obj.size = strlen(dest) + 1;
446 ret = osl(osl_update_object(table, row, BLOBCOL_NAME, &obj));
448 para_printf(&aca->pbout, "cannot rename blob %s to %s\n",
452 ret = afs_event(BLOB_RENAME, NULL, table);
454 lls_free_parse_result(aca->lpr, cmd);
458 static int com_mvblob(afs_callback *f, const struct lls_command * const cmd,
459 struct command_context *cc, struct lls_parse_result *lpr)
462 int ret = lls(lls_check_arg_count(lpr, 2, 2, &errctx));
465 send_errctx(cc, errctx);
468 return send_lls_callback_request(f, cmd, lpr, cc);
471 #define DEFINE_BLOB_COMMAND(cmd_name, c_cmd_name, table_name, short_name, c_short_name) \
472 static int com_ ## cmd_name ## short_name ## _callback(struct afs_callback_arg *aca) \
474 const struct lls_command *cmd = SERVER_CMD_CMD_PTR(c_cmd_name ## c_short_name); \
475 return com_ ## cmd_name ## blob_callback(cmd, table_name ## _table, aca); \
477 static int com_ ## cmd_name ## short_name(struct command_context *cc, struct lls_parse_result *lpr) \
479 const struct lls_command *cmd = SERVER_CMD_CMD_PTR(c_cmd_name ## c_short_name); \
480 return com_ ## cmd_name ## blob(com_ ## cmd_name ## short_name ## _callback, cmd, cc, lpr); \
482 EXPORT_SERVER_CMD_HANDLER(cmd_name ## short_name);
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); \
515 static int blob_get_def_by_name(struct osl_table *table, char *name,
516 struct osl_object *def)
519 struct osl_object obj = {.data = name, .size = strlen(name) + 1};
525 ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
528 return osl(osl_open_disk_object(table, row, BLOBCOL_DEF, def));
531 /** Define the \p get_def_by_id function for this blob type. */
532 #define DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix) \
533 int cmd_prefix ## _get_def_by_name(char *name, struct osl_object *def) \
535 return blob_get_def_by_name(table_name ## _table, name, def); \
538 static int blob_get_def_by_id(struct osl_table *table, uint32_t id,
539 struct osl_object *def)
542 struct osl_object obj = {.data = &id, .size = sizeof(id)};
548 ret = osl(osl_get_row(table, BLOBCOL_ID, &obj, &row));
551 return osl(osl_open_disk_object(table, row, BLOBCOL_DEF, def));
554 /** Define the \p get_def_by_id function for this blob type. */
555 #define DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix) \
556 int cmd_prefix ## _get_def_by_id(uint32_t id, struct osl_object *def) \
558 return blob_get_def_by_id(table_name ## _table, id, def); \
561 static int blob_get_name_and_def_by_row(struct osl_table *table,
562 const struct osl_row *row, char **name, struct osl_object *def)
564 struct osl_object obj;
565 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,
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 */