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. */
17 #include "portable_io.h"
20 * Compare two osl objects pointing to unsigned integers of 32 bit size.
22 * \param obj1 Pointer to the first integer.
23 * \param obj2 Pointer to the second integer.
25 * \return The values required for an osl compare function.
27 * \sa osl_compare_func, osl_hash_compare().
29 static int uint32_compare(const struct osl_object *obj1, const struct osl_object *obj2)
31 uint32_t d1 = read_u32((const char *)obj1->data);
32 uint32_t d2 = read_u32((const char *)obj2->data);
41 static struct osl_column_description blob_cols[] = {
43 .storage_type = OSL_MAPPED_STORAGE,
44 .storage_flags = OSL_RBTREE | OSL_UNIQUE | OSL_FIXED_SIZE,
47 .compare_function = uint32_compare
50 .storage_type = OSL_MAPPED_STORAGE,
51 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
53 .compare_function = string_compare
56 .storage_type = OSL_DISK_STORAGE,
62 /** \cond doxygen isn't smart enough to recognize these */
63 INIT_BLOB_TABLE(lyrics);
64 INIT_BLOB_TABLE(images);
65 INIT_BLOB_TABLE(moods);
66 INIT_BLOB_TABLE(playlists);
69 /** Flags that may be passed to the \p ls functions of each blob type. */
71 /** List both id and name. */
72 BLOB_LS_FLAG_LONG = 1,
73 /** Reverse sort order. */
74 BLOB_LS_FLAG_REVERSE = 2,
75 /** Sort by id instead of name. */
76 BLOB_LS_FLAG_SORT_BY_ID = 4,
79 /** Structure passed to the \p print_blob function. */
80 struct lsblob_action_data {
81 /** The flags given at the command line. */
83 /** Message buffer. */
84 struct para_buffer pb;
87 static int print_blob(struct osl_table *table, struct osl_row *row,
88 const char *name, void *data)
90 struct lsblob_action_data *lbad = data;
91 struct osl_object obj;
95 if (!(lbad->flags & BLOB_LS_FLAG_LONG))
96 return para_printf(&lbad->pb, "%s\n", name);
97 ret = osl_get_object(table, row, BLOBCOL_ID, &obj);
99 para_printf(&lbad->pb, "%s: %s\n", name, para_strerror(-ret));
102 id = *(uint32_t *)obj.data;
103 return para_printf(&lbad->pb, "%u\t%s\n", id, name);
106 static void com_lsblob_callback(struct osl_table *table,
107 int fd, const struct osl_object *query)
109 struct lsblob_action_data lbad = {
110 .flags = *(uint32_t *)query->data,
114 .max_size_handler = pass_buffer_as_shm
117 struct pattern_match_data pmd = {
119 .patterns = {.data = (char *)query->data + sizeof(uint32_t),
120 .size = query->size - sizeof(uint32_t)},
121 .pm_flags = PM_NO_PATTERN_MATCHES_EVERYTHING | PM_SKIP_EMPTY_NAME,
122 .match_col_num = BLOBCOL_NAME,
124 .action = print_blob,
128 if (lbad.flags & BLOB_LS_FLAG_REVERSE)
129 pmd.pm_flags |= PM_REVERSE_LOOP;
130 if (!(lbad.flags & BLOB_LS_FLAG_SORT_BY_ID))
131 pmd.loop_col_num = BLOBCOL_NAME;
133 pmd.loop_col_num = BLOBCOL_ID;
134 ret = for_each_matching_row(&pmd);
136 para_printf(&lbad.pb, "%s\n", para_strerror(-ret));
138 pass_buffer_as_shm(lbad.pb.buf, lbad.pb.offset, &fd);
142 static int com_lsblob(callback_function *f, int fd, int argc, char * const * const argv)
145 struct osl_object options = {.data = &flags, .size = sizeof(flags)};
148 for (i = 1; i < argc; i++) {
149 const char *arg = argv[i];
152 if (!strcmp(arg, "--")) {
156 if (!strcmp(arg, "-l")) {
157 flags |= BLOB_LS_FLAG_LONG;
160 if (!strcmp(arg, "-i")) {
161 flags |= BLOB_LS_FLAG_SORT_BY_ID;
164 if (!strcmp(arg, "-r")) {
165 flags |= BLOB_LS_FLAG_REVERSE;
171 // return -E_BLOB_SYNTAX;
172 return send_option_arg_callback_request(&options, argc - i,
173 argv + i, f, send_result, &fd);
176 static int cat_blob(struct osl_table *table, struct osl_row *row,
177 __a_unused const char *name, void *data)
180 struct osl_object obj;
182 ret = osl_open_disk_object(table, row, BLOBCOL_DEF, &obj);
186 ret = pass_buffer_as_shm(obj.data, obj.size, data);
187 ret2 = osl_close_disk_object(&obj);
188 return (ret < 0)? ret : ret2;
191 static void com_catblob_callback(struct osl_table *table, int fd,
192 const struct osl_object *query)
194 struct pattern_match_data pmd = {
197 .loop_col_num = BLOBCOL_NAME,
198 .match_col_num = BLOBCOL_NAME,
199 .pm_flags = PM_SKIP_EMPTY_NAME,
203 for_each_matching_row(&pmd);
206 static int com_catblob(callback_function *f, int fd, int argc,
207 char * const * const argv)
210 return -E_BLOB_SYNTAX;
211 return send_standard_callback_request(argc - 1, argv + 1, f, send_result, &fd);
214 /** Used for removing rows from a blob table. */
216 /** Message buffer. */
217 struct para_buffer pb;
218 /** Number of removed blobs. */
219 unsigned num_removed;
222 static int remove_blob(struct osl_table *table, struct osl_row *row,
223 const char *name, void *data)
225 struct rmblob_data *rmbd = data;
226 int ret = osl_del_row(table, row);
228 para_printf(&rmbd->pb, "%s: %s\n", name, para_strerror(-ret));
235 static void com_rmblob_callback(struct osl_table *table, int fd,
236 const struct osl_object *query)
239 struct rmblob_data rmbd = {
244 .max_size_handler = pass_buffer_as_shm
247 struct pattern_match_data pmd = {
250 .loop_col_num = BLOBCOL_NAME,
251 .match_col_num = BLOBCOL_NAME,
252 .pm_flags = PM_SKIP_EMPTY_NAME,
254 .action = remove_blob
256 ret = for_each_matching_row(&pmd);
258 ret2 = para_printf(&rmbd.pb, "%s\n", para_strerror(-ret));
262 if (!rmbd.num_removed)
263 ret2 = para_printf(&rmbd.pb, "no matches, nothing removed\n");
265 ret2 = para_printf(&rmbd.pb, "removed %d blobs\n", rmbd.num_removed);
266 afs_event(BLOB_RENAME, NULL, table);
269 if (ret2 >= 0 && rmbd.pb.offset)
270 pass_buffer_as_shm(rmbd.pb.buf, rmbd.pb.offset, &fd);
274 static int com_rmblob(callback_function *f, int fd, int argc,
275 char * const * const argv)
278 return -E_MOOD_SYNTAX;
279 return send_option_arg_callback_request(NULL, argc - 1, argv + 1, f,
283 static void com_addblob_callback(struct osl_table *table, __a_unused int fd,
284 const struct osl_object *query)
286 struct osl_object objs[NUM_BLOB_COLUMNS];
287 char *name = query->data;
288 size_t name_len = strlen(name) + 1;
293 ret = osl_get_num_rows(table, &num_rows);
296 if (!num_rows) { /* this is the first entry ever added */
297 /* insert dummy row containing the id */
298 id = 2; /* this entry will be entry #1, so 2 is the next */
299 objs[BLOBCOL_ID].data = &id;
300 objs[BLOBCOL_ID].size = sizeof(id);
301 objs[BLOBCOL_NAME].data = "";
302 objs[BLOBCOL_NAME].size = 1;
303 objs[BLOBCOL_DEF].data = "";
304 objs[BLOBCOL_DEF].size = 1;
305 ret = osl_add_row(table, objs);
309 /* check if name already exists */
311 struct osl_object obj = {.data = name, .size = name_len};
312 ret = osl_get_row(table, BLOBCOL_NAME, &obj, &row);
313 if (ret < 0 && ret != -E_OSL_RB_KEY_NOT_FOUND)
315 if (ret >= 0) { /* we already have a blob with this name */
316 obj.data = name + name_len;
317 obj.size = query->size - name_len;
318 ret = osl_update_object(table, row, BLOBCOL_DEF, &obj);
321 /* new blob, get id of the dummy row and increment it */
324 ret = osl_get_row(table, BLOBCOL_NAME, &obj, &row);
327 ret = osl_get_object(table, row, BLOBCOL_ID, &obj);
330 id = *(uint32_t *)obj.data + 1;
332 ret = osl_update_object(table, row, BLOBCOL_ID, &obj);
337 objs[BLOBCOL_ID].data = &id;
338 objs[BLOBCOL_ID].size = sizeof(id);
339 objs[BLOBCOL_NAME].data = name;
340 objs[BLOBCOL_NAME].size = name_len;
341 objs[BLOBCOL_DEF].data = name + name_len;
342 objs[BLOBCOL_DEF].size = query->size - name_len;
343 ret = osl_add_row(table, objs);
346 afs_event(BLOB_ADD, NULL, table);
349 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
352 static int com_addblob(callback_function *f, int fd, int argc,
353 char * const * const argv)
355 struct osl_object arg_obj;
358 return -E_BLOB_SYNTAX;
359 if (!*argv[1]) /* empty name is reserved for the dummy row */
360 return -E_BLOB_SYNTAX;
361 arg_obj.size = strlen(argv[1]) + 1;
362 arg_obj.data = (char *)argv[1];
363 return stdin_command(fd, &arg_obj, f, 10 * 1024 * 1024, NULL, NULL);
366 /* FIXME: Print output to client, not to log file */
367 static void com_mvblob_callback(struct osl_table *table, __a_unused int fd,
368 const struct osl_object *query)
370 char *src = (char *) query->data;
371 struct osl_object obj = {.data = src, .size = strlen(src) + 1};
372 char *dest = src + obj.size;
374 int ret = osl_get_row(table, BLOBCOL_NAME, &obj, &row);
379 obj.size = strlen(dest) + 1;
380 ret = osl_update_object(table, row, BLOBCOL_NAME, &obj);
383 afs_event(BLOB_RENAME, NULL, table);
386 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
389 static int com_mvblob(callback_function *f, __a_unused int fd,
390 int argc, char * const * const argv)
393 return -E_MOOD_SYNTAX;
394 return send_option_arg_callback_request(NULL, argc - 1, argv + 1, f,
398 #define DEFINE_BLOB_COMMAND(cmd_name, table_name, cmd_prefix) \
399 static void com_ ## cmd_name ## cmd_prefix ## _callback(int fd, const struct osl_object *query) \
401 return com_ ## cmd_name ## blob_callback(table_name ## _table, fd, query); \
403 int com_ ## cmd_name ## cmd_prefix(int fd, int argc, char * const * const argv) \
405 return com_ ## cmd_name ## blob(com_ ## cmd_name ## cmd_prefix ## _callback, fd, argc, argv); \
408 static int blob_get_name_by_id(struct osl_table *table, uint32_t id,
412 struct osl_object obj = {.data = &id, .size = sizeof(id)};
418 ret = osl_get_row(table, BLOBCOL_ID, &obj, &row);
421 ret = osl_get_object(table, row, BLOBCOL_NAME, &obj);
424 *name = (char *)obj.data;
428 /** Define the \p get_name_by_id function for this blob type. */
429 #define DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix) \
430 int cmd_prefix ## _get_name_by_id(uint32_t id, char **name) \
432 return blob_get_name_by_id(table_name ## _table, id, name); \
436 static int blob_get_def_by_name(struct osl_table *table, char *name,
437 struct osl_object *def)
440 struct osl_object obj = {.data = name, .size = strlen(name) + 1};
446 ret = osl_get_row(table, BLOBCOL_NAME, &obj, &row);
449 return osl_open_disk_object(table, row, BLOBCOL_DEF, def);
452 /** Define the \p get_def_by_id function for this blob type. */
453 #define DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix) \
454 int cmd_prefix ## _get_def_by_name(char *name, struct osl_object *def) \
456 return blob_get_def_by_name(table_name ## _table, name, def); \
459 static int blob_get_def_by_id(struct osl_table *table, uint32_t id,
460 struct osl_object *def)
463 struct osl_object obj = {.data = &id, .size = sizeof(id)};
469 ret = osl_get_row(table, BLOBCOL_ID, &obj, &row);
472 return osl_open_disk_object(table, row, BLOBCOL_DEF, def);
475 /** Define the \p get_def_by_id function for this blob type. */
476 #define DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix) \
477 int cmd_prefix ## _get_def_by_id(uint32_t id, struct osl_object *def) \
479 return blob_get_def_by_id(table_name ## _table, id, def); \
482 static int blob_get_name_and_def_by_row(struct osl_table *table,
483 const struct osl_row *row, char **name, struct osl_object *def)
485 struct osl_object obj;
486 int ret = osl_get_object(table, row, BLOBCOL_NAME, &obj);
490 return osl_open_disk_object(table, row, BLOBCOL_DEF, def);
492 /** Define the \p get_name_and_def_by_row function for this blob type. */
493 #define DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix) \
494 int cmd_prefix ## _get_name_and_def_by_row(const struct osl_row *row, \
495 char **name, struct osl_object *def) \
497 return blob_get_name_and_def_by_row(table_name ## _table, \
501 /** Define the \p close function for this blob type. */
502 #define DEFINE_BLOB_CLOSE(table_name) \
503 void table_name ## _close(void) \
505 osl_close_table(table_name ## _table, OSL_MARK_CLEAN); \
506 table_name ## _table = NULL; \
509 /** Define the \p create function for this blob type. */
510 #define DEFINE_BLOB_CREATE(table_name) \
511 int table_name ## _create(const char *dir) \
513 table_name ## _table_desc.dir = dir; \
514 return osl_create_table(&table_name ## _table_desc); \
517 static int blob_open(struct osl_table **table,
518 struct osl_table_description *desc,
523 ret = osl_open_table(desc, table);
527 if (ret >= 0 || is_errno(-ret, ENOENT))
532 #define DEFINE_BLOB_OPEN(table_name) \
533 int table_name ## _open(const char *dir) \
535 return blob_open(&table_name ## _table, \
536 &table_name ## _table_desc, dir); \
540 /** Define the \p init function for this blob type. */
541 #define DEFINE_BLOB_INIT(table_name) \
542 void table_name ## _init(struct afs_table *t) \
544 t->name = table_name ## _table_desc.name; \
545 t->open = table_name ## _open; \
546 t->close = table_name ## _close; \
547 t->create = table_name ## _create;\
548 t->event_handler = table_name ##_event_handler; \
549 table_name ## _table = NULL; \
553 /** Define all functions for this blob type. */
554 #define DEFINE_BLOB_FUNCTIONS(table_name, cmd_prefix) \
555 DEFINE_BLOB_OPEN(table_name) \
556 DEFINE_BLOB_CLOSE(table_name) \
557 DEFINE_BLOB_CREATE(table_name) \
558 DEFINE_BLOB_INIT(table_name) \
559 DEFINE_BLOB_COMMAND(ls, table_name, cmd_prefix) \
560 DEFINE_BLOB_COMMAND(cat, table_name, cmd_prefix) \
561 DEFINE_BLOB_COMMAND(add, table_name, cmd_prefix) \
562 DEFINE_BLOB_COMMAND(rm, table_name, cmd_prefix) \
563 DEFINE_BLOB_COMMAND(mv, table_name, cmd_prefix) \
564 DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix); \
565 DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix); \
566 DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix); \
567 DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix); \
569 /** \cond doxygen isn't smart enough to recognize these */
570 DEFINE_BLOB_FUNCTIONS(lyrics, lyr);
571 DEFINE_BLOB_FUNCTIONS(images, img);
572 DEFINE_BLOB_FUNCTIONS(moods, mood);
573 DEFINE_BLOB_FUNCTIONS(playlists, pl);