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 afs_error(aca, "cannot list %s\n", name);
106 id = read_u32(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 afs_error(aca, "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;
269 uint32_t id = (uint32_t)-1; /* STFU, gcc */
270 char id_buf[sizeof(id)];
274 ret = osl(osl_get_num_rows(table, &num_rows));
277 if (!num_rows) { /* this is the first entry ever added */
279 * Insert dummy row containing the next free ID. Since we are
280 * about to insert the first blob with ID 1, the next free ID
284 write_u32(id_buf, id);
285 objs[BLOBCOL_ID].data = id_buf;
286 objs[BLOBCOL_ID].size = sizeof(id_buf);
287 objs[BLOBCOL_NAME].data = "";
288 objs[BLOBCOL_NAME].size = 1;
289 objs[BLOBCOL_DEF].data = "";
290 objs[BLOBCOL_DEF].size = 1;
291 ret = osl(osl_add_row(table, objs));
295 /* check if name already exists */
297 struct osl_object obj = {.data = name, .size = name_len};
298 ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
299 if (ret < 0 && ret != -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
301 if (ret >= 0) { /* we already have a blob with this name */
302 ret = osl(osl_get_object(table, row, BLOBCOL_ID, &obj));
305 id = read_u32(obj.data);
306 obj.data = name + name_len;
307 obj.size = aca->query.size - name_len;
308 ret = osl(osl_update_object(table, row, BLOBCOL_DEF, &obj));
311 /* new blob, get id of the dummy row and increment it */
314 ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
317 ret = osl(osl_get_object(table, row, BLOBCOL_ID, &obj));
320 id = read_u32(obj.data) + 1;
321 write_u32(id_buf, id);
323 ret = osl(osl_update_object(table, row, BLOBCOL_ID, &obj));
328 write_u32(id_buf, id);
329 objs[BLOBCOL_ID].data = &id_buf;
330 objs[BLOBCOL_ID].size = sizeof(id_buf);
331 objs[BLOBCOL_NAME].data = name;
332 objs[BLOBCOL_NAME].size = name_len;
333 objs[BLOBCOL_DEF].data = name + name_len;
334 objs[BLOBCOL_DEF].size = aca->query.size - name_len;
335 ret = osl(osl_add_row(table, objs));
338 ret = afs_event(BLOB_ADD, NULL, table);
341 afs_error(aca, "cannot add %s\n", name);
343 para_printf(&aca->pbout, "added %s as id %u\n", name, id);
347 /* Write input from fd to dynamically allocated buffer, but maximal 10M. */
348 static int fd2buf(struct stream_cipher_context *scc, struct osl_object *obj)
350 size_t max_size = 10 * 1024 * 1024;
358 ret = recv_sb(scc, SBD_BLOB_DATA, max_size, &iov);
367 if (iov.iov_len == 0) /* end of blob */
370 obj->data = iov.iov_base;
371 obj->size = iov.iov_len;
373 obj->data = para_realloc(obj->data, obj->size + iov.iov_len);
374 memcpy(obj->data + obj->size, iov.iov_base, iov.iov_len);
375 obj->size += iov.iov_len;
377 max_size -= iov.iov_len;
384 * Read blob from a file descriptor and send it to afs.
386 * This function is called from the addblob command handlers to instruct the
387 * afs process to store the input in a blob table. Input is read and decrypted
388 * from the file descriptor given by cc and appended to a buffer which also contains
389 * the name of the blob to create. The combined buffer is made available to the
390 * afs process via the callback method.
392 static int stdin_command(struct command_context *cc,
393 struct lls_parse_result *lpr, afs_callback *f)
395 struct osl_object query, stdin_obj;
397 size_t len = strlen(lls_input(0, lpr));
399 ret = send_sb(&cc->scc, NULL, 0, SBD_AWAITING_DATA, false);
402 ret = fd2buf(&cc->scc, &stdin_obj);
405 query.size = len + 1 + stdin_obj.size;
406 query.data = alloc(query.size);
407 memcpy(query.data, lls_input(0, lpr), len + 1);
408 if (stdin_obj.size > 0)
409 memcpy((char *)query.data + len + 1, stdin_obj.data,
411 free(stdin_obj.data);
412 ret = send_callback_request(f, &query, afs_cb_result_handler, cc);
417 static int com_addblob(afs_callback *f, __a_unused const struct lls_command * const cmd,
418 struct command_context *cc, struct lls_parse_result *lpr)
421 int ret = lls(lls_check_arg_count(lpr, 1, 1, &errctx));
424 send_errctx(cc, errctx);
427 if (!lls_input(0, lpr)[0]) /* empty name is reserved for the dummy row */
428 return -E_BLOB_SYNTAX;
429 return stdin_command(cc, lpr, f);
432 static int com_mvblob_callback(const struct lls_command * const cmd,
433 struct osl_table *table, struct afs_callback_arg *aca)
435 const char *src, *dest;
436 struct osl_object obj;
440 ret = lls_deserialize_parse_result(aca->query.data, cmd, &aca->lpr);
442 src = lls_input(0, aca->lpr);
443 dest = lls_input(1, aca->lpr);
444 obj.data = (char *)src;
445 obj.size = strlen(src) + 1;
446 ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
449 afs_error(aca, "cannot find source blob %s\n", src);
452 obj.data = (char *)dest;
453 obj.size = strlen(dest) + 1;
454 ret = osl(osl_update_object(table, row, BLOBCOL_NAME, &obj));
456 afs_error(aca, "cannot rename blob %s to %s\n", src, dest);
459 ret = afs_event(BLOB_RENAME, NULL, table);
461 lls_free_parse_result(aca->lpr, cmd);
465 static int com_mvblob(afs_callback *f, const struct lls_command * const cmd,
466 struct command_context *cc, struct lls_parse_result *lpr)
469 int ret = lls(lls_check_arg_count(lpr, 2, 2, &errctx));
472 send_errctx(cc, errctx);
475 return send_lls_callback_request(f, cmd, lpr, cc);
478 #define DEFINE_BLOB_COMMAND(cmd_name, c_cmd_name, table_name, short_name, c_short_name) \
479 static int com_ ## cmd_name ## short_name ## _callback(struct afs_callback_arg *aca) \
481 const struct lls_command *cmd = SERVER_CMD_CMD_PTR(c_cmd_name ## c_short_name); \
482 return com_ ## cmd_name ## blob_callback(cmd, table_name ## _table, aca); \
484 static int com_ ## cmd_name ## short_name(struct command_context *cc, struct lls_parse_result *lpr) \
486 const struct lls_command *cmd = SERVER_CMD_CMD_PTR(c_cmd_name ## c_short_name); \
487 return com_ ## cmd_name ## blob(com_ ## cmd_name ## short_name ## _callback, cmd, cc, lpr); \
489 EXPORT_SERVER_CMD_HANDLER(cmd_name ## short_name);
491 static int blob_get_name_by_id(struct osl_table *table, uint32_t id,
495 struct osl_object obj = {.data = &id, .size = sizeof(id)};
502 ret = osl(osl_get_row(table, BLOBCOL_ID, &obj, &row));
505 ret = osl(osl_get_object(table, row, BLOBCOL_NAME, &obj));
508 if (*(char *)obj.data == '\0')
511 *name = (char *)obj.data;
515 /** Define the \p get_name_by_id function for this blob type. */
516 #define DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix) \
517 int cmd_prefix ## _get_name_by_id(uint32_t id, char **name) \
519 return blob_get_name_by_id(table_name ## _table, id, name); \
522 static int blob_get_def_by_name(struct osl_table *table, const char *name,
523 struct osl_object *def)
526 struct osl_object obj = {.data = (void *)name, .size = strlen(name) + 1};
532 ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
535 return osl(osl_open_disk_object(table, row, BLOBCOL_DEF, def));
538 /** Define the \p get_def_by_id function for this blob type. */
539 #define DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix) \
540 int cmd_prefix ## _get_def_by_name(const char *name, struct osl_object *def) \
542 return blob_get_def_by_name(table_name ## _table, name, def); \
545 static int blob_get_def_by_id(struct osl_table *table, uint32_t id,
546 struct osl_object *def)
549 struct osl_object obj = {.data = &id, .size = sizeof(id)};
555 ret = osl(osl_get_row(table, BLOBCOL_ID, &obj, &row));
558 return osl(osl_open_disk_object(table, row, BLOBCOL_DEF, def));
561 /** Define the \p get_def_by_id function for this blob type. */
562 #define DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix) \
563 int cmd_prefix ## _get_def_by_id(uint32_t id, struct osl_object *def) \
565 return blob_get_def_by_id(table_name ## _table, id, def); \
568 static int blob_get_name_and_def_by_row(struct osl_table *table,
569 const struct osl_row *row, char **name, struct osl_object *def)
571 struct osl_object obj;
572 int ret = osl(osl_get_object(table, row, BLOBCOL_NAME, &obj));
577 return osl(osl_open_disk_object(table, row, BLOBCOL_DEF, def));
579 /** Define the \p get_name_and_def_by_row function for this blob type. */
580 #define DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix) \
581 int cmd_prefix ## _get_name_and_def_by_row(const struct osl_row *row, \
582 char **name, struct osl_object *def) \
584 return blob_get_name_and_def_by_row(table_name ## _table, \
588 /** Define the \p close function for this blob type. */
589 #define DEFINE_BLOB_CLOSE(table_name) \
590 static void table_name ## _close(void) \
592 osl_close_table(table_name ## _table, OSL_MARK_CLEAN); \
593 table_name ## _table = NULL; \
596 /** Define the \p create function for this blob type. */
597 #define DEFINE_BLOB_CREATE(table_name) \
598 static int table_name ## _create(const char *dir) \
600 table_name ## _table_desc.dir = dir; \
601 return osl(osl_create_table(&table_name ## _table_desc)); \
604 static int blob_open(struct osl_table **table,
605 struct osl_table_description *desc,
611 ret = osl(osl_open_table(desc, table));
615 if (ret >= 0 || ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT))
620 #define DEFINE_BLOB_OPEN(table_name) \
621 static int table_name ## _open(const char *dir) \
623 return blob_open(&table_name ## _table, \
624 &table_name ## _table_desc, dir); \
627 /** Blob tables map integers to blobs. */
628 #define DEFINE_BLOB_AFS_TABLE_OPS(table_name) \
629 const struct afs_table_operations table_name ## _ops = { \
630 .open = table_name ## _open, \
631 .close = table_name ## _close, \
632 .create = table_name ## _create, \
633 .event_handler = table_name ##_event_handler, \
636 /** Define all functions for this blob type. */
637 #define DEFINE_BLOB_FUNCTIONS(table_name, short_name, c_short_name) \
638 DEFINE_BLOB_OPEN(table_name) \
639 DEFINE_BLOB_CLOSE(table_name) \
640 DEFINE_BLOB_CREATE(table_name) \
641 DEFINE_BLOB_AFS_TABLE_OPS(table_name) \
642 DEFINE_BLOB_COMMAND(ls, LS, table_name, short_name, c_short_name) \
643 DEFINE_BLOB_COMMAND(cat, CAT, table_name, short_name, c_short_name) \
644 DEFINE_BLOB_COMMAND(add, ADD, table_name, short_name, c_short_name) \
645 DEFINE_BLOB_COMMAND(rm, RM, table_name, short_name, c_short_name) \
646 DEFINE_BLOB_COMMAND(mv, MV, table_name, short_name, c_short_name) \
647 DEFINE_GET_NAME_BY_ID(table_name, short_name); \
648 DEFINE_GET_DEF_BY_ID(table_name, short_name); \
649 DEFINE_GET_DEF_BY_NAME(table_name, short_name); \
650 DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, short_name); \
652 /* doxygen isn't smart enough to recognize these */
653 /** \cond blob_function */
654 DEFINE_BLOB_FUNCTIONS(lyrics, lyr, LYR);
655 DEFINE_BLOB_FUNCTIONS(images, img, IMG);
656 DEFINE_BLOB_FUNCTIONS(moods, mood, MOOD);
657 DEFINE_BLOB_FUNCTIONS(playlists, pl, PL);
658 /** \endcond blob_function */