2 * Copyright (C) 2007-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file blob.c Macros and functions for blob handling. */
11 #include <openssl/rc4.h>
22 #include "portable_io.h"
25 * Compare two osl objects pointing to unsigned integers of 32 bit size.
27 * \param obj1 Pointer to the first integer.
28 * \param obj2 Pointer to the second integer.
30 * \return The values required for an osl compare function.
32 * \sa osl_compare_func, osl_hash_compare().
34 static int uint32_compare(const struct osl_object *obj1, const struct osl_object *obj2)
36 uint32_t d1 = read_u32((const char *)obj1->data);
37 uint32_t d2 = read_u32((const char *)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 /** \cond doxygen isn't smart enough to recognize these */
68 INIT_BLOB_TABLE(lyrics);
69 INIT_BLOB_TABLE(images);
70 INIT_BLOB_TABLE(moods);
71 INIT_BLOB_TABLE(playlists);
74 /** Flags that may be passed to the \p ls functions of each blob type. */
76 /** List both id and name. */
77 BLOB_LS_FLAG_LONG = 1,
78 /** Reverse sort order. */
79 BLOB_LS_FLAG_REVERSE = 2,
80 /** Sort by id instead of name. */
81 BLOB_LS_FLAG_SORT_BY_ID = 4,
84 /** Structure passed to the \p print_blob function. */
85 struct lsblob_action_data {
86 /** The flags given at the command line. */
88 /** Message buffer. */
89 struct para_buffer pb;
92 static int print_blob(struct osl_table *table, struct osl_row *row,
93 const char *name, void *data)
95 struct lsblob_action_data *lbad = data;
96 struct osl_object obj;
100 if (!(lbad->flags & BLOB_LS_FLAG_LONG))
101 return para_printf(&lbad->pb, "%s\n", name);
102 ret = osl(osl_get_object(table, row, BLOBCOL_ID, &obj));
104 para_printf(&lbad->pb, "%s: %s\n", name, para_strerror(-ret));
107 id = *(uint32_t *)obj.data;
108 return para_printf(&lbad->pb, "%u\t%s\n", id, name);
111 static void com_lsblob_callback(struct osl_table *table,
112 int fd, const struct osl_object *query)
114 struct lsblob_action_data lbad = {
115 .flags = *(uint32_t *)query->data,
119 .max_size_handler = pass_buffer_as_shm
122 struct pattern_match_data pmd = {
124 .patterns = {.data = (char *)query->data + sizeof(uint32_t),
125 .size = query->size - sizeof(uint32_t)},
126 .pm_flags = PM_NO_PATTERN_MATCHES_EVERYTHING | PM_SKIP_EMPTY_NAME,
127 .match_col_num = BLOBCOL_NAME,
129 .action = print_blob,
133 if (lbad.flags & BLOB_LS_FLAG_REVERSE)
134 pmd.pm_flags |= PM_REVERSE_LOOP;
135 if (!(lbad.flags & BLOB_LS_FLAG_SORT_BY_ID))
136 pmd.loop_col_num = BLOBCOL_NAME;
138 pmd.loop_col_num = BLOBCOL_ID;
139 ret = for_each_matching_row(&pmd);
141 para_printf(&lbad.pb, "%s\n", para_strerror(-ret));
143 pass_buffer_as_shm(lbad.pb.buf, lbad.pb.offset, &fd);
147 static int com_lsblob(callback_function *f, struct rc4_context *rc4c, int argc, char * const * const argv)
150 struct osl_object options = {.data = &flags, .size = sizeof(flags)};
153 for (i = 1; i < argc; i++) {
154 const char *arg = argv[i];
157 if (!strcmp(arg, "--")) {
161 if (!strcmp(arg, "-l")) {
162 flags |= BLOB_LS_FLAG_LONG;
165 if (!strcmp(arg, "-i")) {
166 flags |= BLOB_LS_FLAG_SORT_BY_ID;
169 if (!strcmp(arg, "-r")) {
170 flags |= BLOB_LS_FLAG_REVERSE;
176 // return -E_BLOB_SYNTAX;
177 return send_option_arg_callback_request(&options, argc - i,
178 argv + i, f, rc4_send_result, rc4c);
181 static int cat_blob(struct osl_table *table, struct osl_row *row,
182 __a_unused const char *name, void *data)
185 struct osl_object obj;
187 ret = osl(osl_open_disk_object(table, row, BLOBCOL_DEF, &obj));
191 ret = pass_buffer_as_shm(obj.data, obj.size, data);
192 ret2 = osl(osl_close_disk_object(&obj));
193 return (ret < 0)? ret : ret2;
196 static void com_catblob_callback(struct osl_table *table, int fd,
197 const struct osl_object *query)
199 struct pattern_match_data pmd = {
202 .loop_col_num = BLOBCOL_NAME,
203 .match_col_num = BLOBCOL_NAME,
204 .pm_flags = PM_SKIP_EMPTY_NAME,
208 for_each_matching_row(&pmd);
211 static int com_catblob(callback_function *f, struct rc4_context *rc4c, int argc,
212 char * const * const argv)
215 return -E_BLOB_SYNTAX;
216 return send_standard_callback_request(argc - 1, argv + 1, f,
217 rc4_send_result, rc4c);
220 /** Used for removing rows from a blob table. */
222 /** Message buffer. */
223 struct para_buffer pb;
224 /** Number of removed blobs. */
225 unsigned num_removed;
228 static int remove_blob(struct osl_table *table, struct osl_row *row,
229 const char *name, void *data)
231 struct rmblob_data *rmbd = data;
232 int ret = osl(osl_del_row(table, row));
234 para_printf(&rmbd->pb, "%s: %s\n", name, para_strerror(-ret));
241 static void com_rmblob_callback(struct osl_table *table, int fd,
242 const struct osl_object *query)
245 struct rmblob_data rmbd = {
250 .max_size_handler = pass_buffer_as_shm
253 struct pattern_match_data pmd = {
256 .loop_col_num = BLOBCOL_NAME,
257 .match_col_num = BLOBCOL_NAME,
258 .pm_flags = PM_SKIP_EMPTY_NAME,
260 .action = remove_blob
262 ret = for_each_matching_row(&pmd);
264 ret2 = para_printf(&rmbd.pb, "%s\n", para_strerror(-ret));
268 if (!rmbd.num_removed)
269 ret2 = para_printf(&rmbd.pb, "no matches, nothing removed\n");
271 ret2 = para_printf(&rmbd.pb, "removed %d blobs\n", rmbd.num_removed);
272 afs_event(BLOB_RENAME, NULL, table);
275 if (ret2 >= 0 && rmbd.pb.offset)
276 pass_buffer_as_shm(rmbd.pb.buf, rmbd.pb.offset, &fd);
280 static int com_rmblob(callback_function *f, struct rc4_context *rc4c, int argc,
281 char * const * const argv)
284 return -E_MOOD_SYNTAX;
285 return send_option_arg_callback_request(NULL, argc - 1, argv + 1, f,
286 rc4_send_result, rc4c);
289 static void com_addblob_callback(struct osl_table *table, __a_unused int fd,
290 const struct osl_object *query)
292 struct osl_object objs[NUM_BLOB_COLUMNS];
293 char *name = query->data;
294 size_t name_len = strlen(name) + 1;
299 ret = osl(osl_get_num_rows(table, &num_rows));
302 if (!num_rows) { /* this is the first entry ever added */
303 /* insert dummy row containing the id */
304 id = 2; /* this entry will be entry #1, so 2 is the next */
305 objs[BLOBCOL_ID].data = &id;
306 objs[BLOBCOL_ID].size = sizeof(id);
307 objs[BLOBCOL_NAME].data = "";
308 objs[BLOBCOL_NAME].size = 1;
309 objs[BLOBCOL_DEF].data = "";
310 objs[BLOBCOL_DEF].size = 1;
311 ret = osl(osl_add_row(table, objs));
315 /* check if name already exists */
317 struct osl_object obj = {.data = name, .size = name_len};
318 ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
319 if (ret < 0 && ret != -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
321 if (ret >= 0) { /* we already have a blob with this name */
322 obj.data = name + name_len;
323 obj.size = query->size - name_len;
324 ret = osl(osl_update_object(table, row, BLOBCOL_DEF, &obj));
327 /* new blob, get id of the dummy row and increment it */
330 ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
333 ret = osl(osl_get_object(table, row, BLOBCOL_ID, &obj));
336 id = *(uint32_t *)obj.data + 1;
338 ret = osl(osl_update_object(table, row, BLOBCOL_ID, &obj));
343 objs[BLOBCOL_ID].data = &id;
344 objs[BLOBCOL_ID].size = sizeof(id);
345 objs[BLOBCOL_NAME].data = name;
346 objs[BLOBCOL_NAME].size = name_len;
347 objs[BLOBCOL_DEF].data = name + name_len;
348 objs[BLOBCOL_DEF].size = query->size - name_len;
349 ret = osl(osl_add_row(table, objs));
352 afs_event(BLOB_ADD, NULL, table);
355 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
359 * write input from fd to dynamically allocated buffer,
360 * but maximal max_size byte.
362 static int fd2buf(struct rc4_context *rc4c, unsigned max_size, struct osl_object *obj)
364 const size_t chunk_size = 1024;
365 size_t size = 2048, received = 0;
367 char *buf = para_malloc(size);
370 ret = rc4_recv_bin_buffer(rc4c, buf + received, chunk_size);
374 if (received + chunk_size >= size) {
376 ret = -E_INPUT_TOO_LARGE;
379 buf = para_realloc(buf, size);
383 obj->size = received;
390 * Read data from a file descriptor, and send it to the afs process.
392 * \param rc4c crypt context containing the file descriptor to read data from.
393 * \param arg_obj Pointer to the arguments to \a f.
394 * \param f The callback function.
395 * \param max_len Don't read more than that many bytes from stdin.
396 * \param result_handler See \ref send_callback_request.
397 * \param private_result_data See \ref send_callback_request.
399 * This function is used by commands that wish to let para_server store
400 * arbitrary data specified by the user (for instance the add_blob family of
401 * commands). First, at most \a max_len bytes are read and decrypted from the
402 * file descriptor given by \a rc4c. The result is concatenated with the buffer
403 * given by \a arg_obj, and the combined buffer is made available to the afs
404 * process via the callback method. See \ref send_callback_request for details.
406 * \return Negative on errors, the return value of the underlying call to
407 * send_callback_request() otherwise.
409 static int stdin_command(struct rc4_context *rc4c, struct osl_object *arg_obj,
410 callback_function *f, unsigned max_len,
411 callback_result_handler *result_handler,
412 void *private_result_data)
414 struct osl_object query, stdin_obj;
417 ret = rc4_send_buffer(rc4c, AWAITING_DATA_MSG);
420 ret = fd2buf(rc4c, max_len, &stdin_obj);
423 query.size = arg_obj->size + stdin_obj.size;
424 query.data = para_malloc(query.size);
425 memcpy(query.data, arg_obj->data, arg_obj->size);
426 memcpy((char *)query.data + arg_obj->size, stdin_obj.data, stdin_obj.size);
427 free(stdin_obj.data);
428 ret = send_callback_request(f, &query, result_handler, private_result_data);
433 static int com_addblob(callback_function *f, struct rc4_context *rc4c, int argc,
434 char * const * const argv)
436 struct osl_object arg_obj;
439 return -E_BLOB_SYNTAX;
440 if (!*argv[1]) /* empty name is reserved for the dummy row */
441 return -E_BLOB_SYNTAX;
442 arg_obj.size = strlen(argv[1]) + 1;
443 arg_obj.data = (char *)argv[1];
444 return stdin_command(rc4c, &arg_obj, f, 10 * 1024 * 1024, NULL, NULL);
447 /* FIXME: Print output to client, not to log file */
448 static void com_mvblob_callback(struct osl_table *table, __a_unused int fd,
449 const struct osl_object *query)
451 char *src = (char *) query->data;
452 struct osl_object obj = {.data = src, .size = strlen(src) + 1};
453 char *dest = src + obj.size;
455 int ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
460 obj.size = strlen(dest) + 1;
461 ret = osl(osl_update_object(table, row, BLOBCOL_NAME, &obj));
464 afs_event(BLOB_RENAME, NULL, table);
467 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
470 static int com_mvblob(callback_function *f, __a_unused struct rc4_context *rc4c,
471 int argc, char * const * const argv)
474 return -E_MOOD_SYNTAX;
475 return send_option_arg_callback_request(NULL, argc - 1, argv + 1, f,
479 #define DEFINE_BLOB_COMMAND(cmd_name, table_name, cmd_prefix) \
480 static void com_ ## cmd_name ## cmd_prefix ## _callback(int fd, const struct osl_object *query) \
482 return com_ ## cmd_name ## blob_callback(table_name ## _table, fd, query); \
484 int com_ ## cmd_name ## cmd_prefix(struct rc4_context *rc4c, int argc, char * const * const argv) \
486 return com_ ## cmd_name ## blob(com_ ## cmd_name ## cmd_prefix ## _callback, rc4c, argc, argv); \
489 static int blob_get_name_by_id(struct osl_table *table, uint32_t id,
493 struct osl_object obj = {.data = &id, .size = sizeof(id)};
499 ret = osl(osl_get_row(table, BLOBCOL_ID, &obj, &row));
502 ret = osl(osl_get_object(table, row, BLOBCOL_NAME, &obj));
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 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 int table_name ## _create(const char *dir) \
594 table_name ## _table_desc.dir = dir; \
595 return 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 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->name = table_name ## _table_desc.name; \
626 t->open = table_name ## _open; \
627 t->close = table_name ## _close; \
628 t->create = table_name ## _create;\
629 t->event_handler = table_name ##_event_handler; \
630 table_name ## _table = NULL; \
634 /** Define all functions for this blob type. */
635 #define DEFINE_BLOB_FUNCTIONS(table_name, cmd_prefix) \
636 DEFINE_BLOB_OPEN(table_name) \
637 DEFINE_BLOB_CLOSE(table_name) \
638 DEFINE_BLOB_CREATE(table_name) \
639 DEFINE_BLOB_INIT(table_name) \
640 DEFINE_BLOB_COMMAND(ls, table_name, cmd_prefix) \
641 DEFINE_BLOB_COMMAND(cat, table_name, cmd_prefix) \
642 DEFINE_BLOB_COMMAND(add, table_name, cmd_prefix) \
643 DEFINE_BLOB_COMMAND(rm, table_name, cmd_prefix) \
644 DEFINE_BLOB_COMMAND(mv, table_name, cmd_prefix) \
645 DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix); \
646 DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix); \
647 DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix); \
648 DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix); \
650 /** \cond doxygen isn't smart enough to recognize these */
651 DEFINE_BLOB_FUNCTIONS(lyrics, lyr);
652 DEFINE_BLOB_FUNCTIONS(images, img);
653 DEFINE_BLOB_FUNCTIONS(moods, mood);
654 DEFINE_BLOB_FUNCTIONS(playlists, pl);