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. */
10 #include <openssl/rc4.h>
21 static struct osl_column_description blob_cols[] = {
23 .storage_type = OSL_MAPPED_STORAGE,
24 .storage_flags = OSL_RBTREE | OSL_UNIQUE | OSL_FIXED_SIZE,
27 .compare_function = uint32_compare
30 .storage_type = OSL_MAPPED_STORAGE,
31 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
33 .compare_function = string_compare
36 .storage_type = OSL_DISK_STORAGE,
42 /** \cond doxygen isn't smart enough to recognize these */
43 INIT_BLOB_TABLE(lyrics);
44 INIT_BLOB_TABLE(images);
45 INIT_BLOB_TABLE(moods);
46 INIT_BLOB_TABLE(playlists);
49 /** Flags that may be passed to the \p ls functions of each blob type. */
51 /** List both id and name. */
52 BLOB_LS_FLAG_LONG = 1,
53 /** Reverse sort order. */
54 BLOB_LS_FLAG_REVERSE = 2,
55 /** Sort by id instead of name. */
56 BLOB_LS_FLAG_SORT_BY_ID = 4,
59 /** Structure passed to the \p print_blob function. */
60 struct lsblob_action_data {
61 /** The flags given at the command line. */
63 /** Message buffer. */
64 struct para_buffer pb;
67 static int print_blob(struct osl_table *table, struct osl_row *row,
68 const char *name, void *data)
70 struct lsblob_action_data *lbad = data;
71 struct osl_object obj;
75 if (!(lbad->flags & BLOB_LS_FLAG_LONG))
76 return para_printf(&lbad->pb, "%s\n", name);
77 ret = osl_get_object(table, row, BLOBCOL_ID, &obj);
79 para_printf(&lbad->pb, "%s: %s\n", name, para_strerror(-ret));
82 id = *(uint32_t *)obj.data;
83 return para_printf(&lbad->pb, "%u\t%s\n", id, name);
86 static void com_lsblob_callback(struct osl_table *table,
87 int fd, const struct osl_object *query)
89 struct lsblob_action_data lbad = {
90 .flags = *(uint32_t *)query->data,
94 .max_size_handler = pass_buffer_as_shm
97 struct pattern_match_data pmd = {
99 .patterns = {.data = (char *)query->data + sizeof(uint32_t),
100 .size = query->size - sizeof(uint32_t)},
101 .pm_flags = PM_NO_PATTERN_MATCHES_EVERYTHING | PM_SKIP_EMPTY_NAME,
102 .match_col_num = BLOBCOL_NAME,
104 .action = print_blob,
108 if (lbad.flags & BLOB_LS_FLAG_REVERSE)
109 pmd.pm_flags |= PM_REVERSE_LOOP;
110 if (!(lbad.flags & BLOB_LS_FLAG_SORT_BY_ID))
111 pmd.loop_col_num = BLOBCOL_NAME;
113 pmd.loop_col_num = BLOBCOL_ID;
114 ret = for_each_matching_row(&pmd);
116 para_printf(&lbad.pb, "%s\n", para_strerror(-ret));
118 pass_buffer_as_shm(lbad.pb.buf, lbad.pb.offset, &fd);
122 static int com_lsblob(callback_function *f, struct rc4_context *rc4c, int argc, char * const * const argv)
125 struct osl_object options = {.data = &flags, .size = sizeof(flags)};
128 for (i = 1; i < argc; i++) {
129 const char *arg = argv[i];
132 if (!strcmp(arg, "--")) {
136 if (!strcmp(arg, "-l")) {
137 flags |= BLOB_LS_FLAG_LONG;
140 if (!strcmp(arg, "-i")) {
141 flags |= BLOB_LS_FLAG_SORT_BY_ID;
144 if (!strcmp(arg, "-r")) {
145 flags |= BLOB_LS_FLAG_REVERSE;
151 // return -E_BLOB_SYNTAX;
152 return send_option_arg_callback_request(&options, argc - i,
153 argv + i, f, rc4_send_result, rc4c);
156 static int cat_blob(struct osl_table *table, struct osl_row *row,
157 __a_unused const char *name, void *data)
160 struct osl_object obj;
162 ret = osl_open_disk_object(table, row, BLOBCOL_DEF, &obj);
166 ret = pass_buffer_as_shm(obj.data, obj.size, data);
167 ret2 = osl_close_disk_object(&obj);
168 return (ret < 0)? ret : ret2;
171 static void com_catblob_callback(struct osl_table *table, int fd,
172 const struct osl_object *query)
174 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 for_each_matching_row(&pmd);
186 static int com_catblob(callback_function *f, struct rc4_context *rc4c, int argc,
187 char * const * const argv)
190 return -E_BLOB_SYNTAX;
191 return send_standard_callback_request(argc - 1, argv + 1, f,
192 rc4_send_result, rc4c);
195 /** Used for removing rows from a blob table. */
197 /** Message buffer. */
198 struct para_buffer pb;
199 /** Number of removed blobs. */
200 unsigned num_removed;
203 static int remove_blob(struct osl_table *table, struct osl_row *row,
204 const char *name, void *data)
206 struct rmblob_data *rmbd = data;
207 int ret = osl_del_row(table, row);
209 para_printf(&rmbd->pb, "%s: %s\n", name, para_strerror(-ret));
216 static void com_rmblob_callback(struct osl_table *table, int fd,
217 const struct osl_object *query)
220 struct rmblob_data rmbd = {
225 .max_size_handler = pass_buffer_as_shm
228 struct pattern_match_data pmd = {
231 .loop_col_num = BLOBCOL_NAME,
232 .match_col_num = BLOBCOL_NAME,
233 .pm_flags = PM_SKIP_EMPTY_NAME,
235 .action = remove_blob
237 ret = for_each_matching_row(&pmd);
239 ret2 = para_printf(&rmbd.pb, "%s\n", para_strerror(-ret));
243 if (!rmbd.num_removed)
244 ret2 = para_printf(&rmbd.pb, "no matches, nothing removed\n");
246 ret2 = para_printf(&rmbd.pb, "removed %d blobs\n", rmbd.num_removed);
247 afs_event(BLOB_RENAME, NULL, table);
250 if (ret2 >= 0 && rmbd.pb.offset)
251 pass_buffer_as_shm(rmbd.pb.buf, rmbd.pb.offset, &fd);
255 static int com_rmblob(callback_function *f, struct rc4_context *rc4c, int argc,
256 char * const * const argv)
259 return -E_MOOD_SYNTAX;
260 return send_option_arg_callback_request(NULL, argc - 1, argv + 1, f,
261 rc4_send_result, rc4c);
264 static void com_addblob_callback(struct osl_table *table, __a_unused int fd,
265 const struct osl_object *query)
267 struct osl_object objs[NUM_BLOB_COLUMNS];
268 char *name = query->data;
269 size_t name_len = strlen(name) + 1;
274 ret = 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_add_row(table, objs);
290 /* check if name already exists */
292 struct osl_object obj = {.data = name, .size = name_len};
293 ret = osl_get_row(table, BLOBCOL_NAME, &obj, &row);
294 if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
296 if (ret >= 0) { /* we already have a blob with this name */
297 obj.data = name + name_len;
298 obj.size = query->size - name_len;
299 ret = osl_update_object(table, row, BLOBCOL_DEF, &obj);
302 /* new blob, get id of the dummy row and increment it */
305 ret = osl_get_row(table, BLOBCOL_NAME, &obj, &row);
308 ret = osl_get_object(table, row, BLOBCOL_ID, &obj);
311 id = *(uint32_t *)obj.data + 1;
313 ret = osl_update_object(table, row, BLOBCOL_ID, &obj);
318 objs[BLOBCOL_ID].data = &id;
319 objs[BLOBCOL_ID].size = sizeof(id);
320 objs[BLOBCOL_NAME].data = name;
321 objs[BLOBCOL_NAME].size = name_len;
322 objs[BLOBCOL_DEF].data = name + name_len;
323 objs[BLOBCOL_DEF].size = query->size - name_len;
324 ret = osl_add_row(table, objs);
327 afs_event(BLOB_ADD, NULL, table);
330 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
334 * write input from fd to dynamically allocated buffer,
335 * but maximal max_size byte.
337 static int fd2buf(struct rc4_context *rc4c, unsigned max_size, struct osl_object *obj)
339 const size_t chunk_size = 1024;
340 size_t size = 2048, received = 0;
342 char *buf = para_malloc(size);
345 ret = rc4_recv_bin_buffer(rc4c, buf + received, chunk_size);
349 if (received + chunk_size >= size) {
351 ret = -E_INPUT_TOO_LARGE;
354 buf = para_realloc(buf, size);
358 obj->size = received;
365 * Read data from a file descriptor, and send it to the afs process.
367 * \param rc4c crypt context containing the file descriptor to read data from.
368 * \param arg_obj Pointer to the arguments to \a f.
369 * \param f The callback function.
370 * \param max_len Don't read more than that many bytes from stdin.
371 * \param result_handler See \ref send_callback_request.
372 * \param private_result_data See \ref send_callback_request.
374 * This function is used by commands that wish to let para_server store
375 * arbitrary data specified by the user (for instance the add_blob family of
376 * commands). First, at most \a max_len bytes are read and decrypted from the
377 * file descriptor given by \a rc4c. The result is concatenated with the buffer
378 * given by \a arg_obj, and the combined buffer is made available to the afs
379 * process via the callback method. See \ref send_callback_request for details.
381 * \return Negative on errors, the return value of the underlying call to
382 * send_callback_request() otherwise.
384 static int stdin_command(struct rc4_context *rc4c, struct osl_object *arg_obj,
385 callback_function *f, unsigned max_len,
386 callback_result_handler *result_handler,
387 void *private_result_data)
389 struct osl_object query, stdin_obj;
392 ret = rc4_send_buffer(rc4c, AWAITING_DATA_MSG);
395 ret = fd2buf(rc4c, max_len, &stdin_obj);
398 query.size = arg_obj->size + stdin_obj.size;
399 query.data = para_malloc(query.size);
400 memcpy(query.data, arg_obj->data, arg_obj->size);
401 memcpy((char *)query.data + arg_obj->size, stdin_obj.data, stdin_obj.size);
402 free(stdin_obj.data);
403 ret = send_callback_request(f, &query, result_handler, private_result_data);
408 static int com_addblob(callback_function *f, struct rc4_context *rc4c, int argc,
409 char * const * const argv)
411 struct osl_object arg_obj;
414 return -E_BLOB_SYNTAX;
415 if (!*argv[1]) /* empty name is reserved for the dummy row */
416 return -E_BLOB_SYNTAX;
417 arg_obj.size = strlen(argv[1]) + 1;
418 arg_obj.data = (char *)argv[1];
419 return stdin_command(rc4c, &arg_obj, f, 10 * 1024 * 1024, NULL, NULL);
422 /* FIXME: Print output to client, not to log file */
423 static void com_mvblob_callback(struct osl_table *table, __a_unused int fd,
424 const struct osl_object *query)
426 char *src = (char *) query->data;
427 struct osl_object obj = {.data = src, .size = strlen(src) + 1};
428 char *dest = src + obj.size;
430 int ret = osl_get_row(table, BLOBCOL_NAME, &obj, &row);
435 obj.size = strlen(dest) + 1;
436 ret = osl_update_object(table, row, BLOBCOL_NAME, &obj);
439 afs_event(BLOB_RENAME, NULL, table);
442 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
445 static int com_mvblob(callback_function *f, __a_unused struct rc4_context *rc4c,
446 int argc, char * const * const argv)
449 return -E_MOOD_SYNTAX;
450 return send_option_arg_callback_request(NULL, argc - 1, argv + 1, f,
454 #define DEFINE_BLOB_COMMAND(cmd_name, table_name, cmd_prefix) \
455 static void com_ ## cmd_name ## cmd_prefix ## _callback(int fd, const struct osl_object *query) \
457 return com_ ## cmd_name ## blob_callback(table_name ## _table, fd, query); \
459 int com_ ## cmd_name ## cmd_prefix(struct rc4_context *rc4c, int argc, char * const * const argv) \
461 return com_ ## cmd_name ## blob(com_ ## cmd_name ## cmd_prefix ## _callback, rc4c, argc, argv); \
464 static int blob_get_name_by_id(struct osl_table *table, uint32_t id,
468 struct osl_object obj = {.data = &id, .size = sizeof(id)};
474 ret = osl_get_row(table, BLOBCOL_ID, &obj, &row);
477 ret = osl_get_object(table, row, BLOBCOL_NAME, &obj);
480 *name = (char *)obj.data;
484 /** Define the \p get_name_by_id function for this blob type. */
485 #define DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix) \
486 int cmd_prefix ## _get_name_by_id(uint32_t id, char **name) \
488 return blob_get_name_by_id(table_name ## _table, id, name); \
492 static int blob_get_def_by_name(struct osl_table *table, char *name,
493 struct osl_object *def)
496 struct osl_object obj = {.data = name, .size = strlen(name) + 1};
502 ret = osl_get_row(table, BLOBCOL_NAME, &obj, &row);
505 return osl_open_disk_object(table, row, BLOBCOL_DEF, def);
508 /** Define the \p get_def_by_id function for this blob type. */
509 #define DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix) \
510 int cmd_prefix ## _get_def_by_name(char *name, struct osl_object *def) \
512 return blob_get_def_by_name(table_name ## _table, name, def); \
515 static int blob_get_def_by_id(struct osl_table *table, uint32_t id,
516 struct osl_object *def)
519 struct osl_object obj = {.data = &id, .size = sizeof(id)};
525 ret = osl_get_row(table, BLOBCOL_ID, &obj, &row);
528 return 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_ID(table_name, cmd_prefix) \
533 int cmd_prefix ## _get_def_by_id(uint32_t id, struct osl_object *def) \
535 return blob_get_def_by_id(table_name ## _table, id, def); \
538 static int blob_get_name_and_def_by_row(struct osl_table *table,
539 const struct osl_row *row, char **name, struct osl_object *def)
541 struct osl_object obj;
542 int ret = osl_get_object(table, row, BLOBCOL_NAME, &obj);
546 return osl_open_disk_object(table, row, BLOBCOL_DEF, def);
548 /** Define the \p get_name_and_def_by_row function for this blob type. */
549 #define DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix) \
550 int cmd_prefix ## _get_name_and_def_by_row(const struct osl_row *row, \
551 char **name, struct osl_object *def) \
553 return blob_get_name_and_def_by_row(table_name ## _table, \
557 /** Define the \p close function for this blob type. */
558 #define DEFINE_BLOB_CLOSE(table_name) \
559 void table_name ## _close(void) \
561 osl_close_table(table_name ## _table, OSL_MARK_CLEAN); \
562 table_name ## _table = NULL; \
565 /** Define the \p create function for this blob type. */
566 #define DEFINE_BLOB_CREATE(table_name) \
567 int table_name ## _create(const char *dir) \
569 table_name ## _table_desc.dir = dir; \
570 return osl_create_table(&table_name ## _table_desc); \
573 static int blob_open(struct osl_table **table,
574 struct osl_table_description *desc,
579 ret = osl_open_table(desc, table);
583 if (ret >= 0 || is_errno(-ret, ENOENT))
588 #define DEFINE_BLOB_OPEN(table_name) \
589 int table_name ## _open(const char *dir) \
591 return blob_open(&table_name ## _table, \
592 &table_name ## _table_desc, dir); \
596 /** Define the \p init function for this blob type. */
597 #define DEFINE_BLOB_INIT(table_name) \
598 void table_name ## _init(struct afs_table *t) \
600 t->name = table_name ## _table_desc.name; \
601 t->open = table_name ## _open; \
602 t->close = table_name ## _close; \
603 t->create = table_name ## _create;\
604 t->event_handler = table_name ##_event_handler; \
605 table_name ## _table = NULL; \
609 /** Define all functions for this blob type. */
610 #define DEFINE_BLOB_FUNCTIONS(table_name, cmd_prefix) \
611 DEFINE_BLOB_OPEN(table_name) \
612 DEFINE_BLOB_CLOSE(table_name) \
613 DEFINE_BLOB_CREATE(table_name) \
614 DEFINE_BLOB_INIT(table_name) \
615 DEFINE_BLOB_COMMAND(ls, table_name, cmd_prefix) \
616 DEFINE_BLOB_COMMAND(cat, table_name, cmd_prefix) \
617 DEFINE_BLOB_COMMAND(add, table_name, cmd_prefix) \
618 DEFINE_BLOB_COMMAND(rm, table_name, cmd_prefix) \
619 DEFINE_BLOB_COMMAND(mv, table_name, cmd_prefix) \
620 DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix); \
621 DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix); \
622 DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix); \
623 DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix); \
625 /** \cond doxygen isn't smart enough to recognize these */
626 DEFINE_BLOB_FUNCTIONS(lyrics, lyr);
627 DEFINE_BLOB_FUNCTIONS(images, img);
628 DEFINE_BLOB_FUNCTIONS(moods, mood);
629 DEFINE_BLOB_FUNCTIONS(playlists, pl);