2 * Copyright (C) 2007 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 static struct osl_column_description blob_cols[] = {
19 .storage_type = OSL_MAPPED_STORAGE,
20 .storage_flags = OSL_RBTREE | OSL_UNIQUE | OSL_FIXED_SIZE,
23 .compare_function = uint32_compare
26 .storage_type = OSL_MAPPED_STORAGE,
27 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
29 .compare_function = string_compare
32 .storage_type = OSL_DISK_STORAGE,
38 /** \cond doxygen isn't smart enough to recognize these */
39 INIT_BLOB_TABLE(lyrics);
40 INIT_BLOB_TABLE(images);
41 INIT_BLOB_TABLE(moods);
42 INIT_BLOB_TABLE(playlists);
45 /** Flags that may be passed to the \p ls functions of each blob type. */
47 /** List both id and name. */
48 BLOB_LS_FLAG_LONG = 1,
49 /** Reverse sort order. */
50 BLOB_LS_FLAG_REVERSE = 2,
51 /** Sort by id instead of name. */
52 BLOB_LS_FLAG_SORT_BY_ID = 4,
55 /** Structure passed to the \p print_blob function. */
56 struct lsblob_action_data {
57 /* The flags given at the command line. */
59 /** Message buffer. */
60 struct para_buffer pb;
63 static int print_blob(struct osl_table *table, struct osl_row *row,
64 const char *name, void *data)
66 struct lsblob_action_data *lbad = data;
67 struct osl_object obj;
71 if (!(lbad->flags & BLOB_LS_FLAG_LONG)) {
72 para_printf(&lbad->pb, "%s\n", name);
75 ret = osl_get_object(table, row, BLOBCOL_ID, &obj);
77 para_printf(&lbad->pb, "%s: %s\n", name, PARA_STRERROR(-ret));
80 id = *(uint32_t *)obj.data;
81 para_printf(&lbad->pb, "%u\t%s\n", id, name);
85 static int com_lsblob_callback(struct osl_table *table,
86 const struct osl_object *query, struct osl_object *result)
88 struct lsblob_action_data lbad = {.flags = *(uint32_t *)query->data};
89 struct pattern_match_data pmd = {
91 .patterns = {.data = (char *)query->data + sizeof(uint32_t),
92 .size = query->size - sizeof(uint32_t)},
93 .pm_flags = PM_NO_PATTERN_MATCHES_EVERYTHING | PM_SKIP_EMPTY_NAME,
94 .match_col_num = BLOBCOL_NAME,
100 if (lbad.flags & BLOB_LS_FLAG_REVERSE)
101 pmd.pm_flags |= PM_REVERSE_LOOP;
102 if (!(lbad.flags & BLOB_LS_FLAG_SORT_BY_ID))
103 pmd.loop_col_num = BLOBCOL_NAME;
105 pmd.loop_col_num = BLOBCOL_ID;
106 ret = for_each_matching_row(&pmd);
108 para_printf(&lbad.pb, "%s\n", PARA_STRERROR(-ret));
111 result->data = lbad.pb.buf;
112 result->size = lbad.pb.size;
116 static int com_lsblob(callback_function *f, int fd, int argc, char * const * const argv)
119 struct osl_object options = {.data = &flags, .size = sizeof(flags)},
123 for (i = 1; i < argc; i++) {
124 const char *arg = argv[i];
127 if (!strcmp(arg, "--")) {
131 if (!strcmp(arg, "-l")) {
132 flags |= BLOB_LS_FLAG_LONG;
135 if (!strcmp(arg, "-i")) {
136 flags |= BLOB_LS_FLAG_SORT_BY_ID;
139 if (!strcmp(arg, "-r")) {
140 flags |= BLOB_LS_FLAG_REVERSE;
146 // return -E_BLOB_SYNTAX;
147 ret = send_option_arg_callback_request(&options, argc - i,
148 argv + i, f, &result);
150 send_buffer(fd, (char *)result.data);
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 *blobs = data;
161 struct osl_object obj;
163 ret = osl_open_disk_object(table, row, BLOBCOL_DEF, &obj);
167 blobs->data = para_realloc(blobs->data, blobs->size + obj.size);
168 memcpy(blobs->data + blobs->size, obj.data, obj.size);
169 blobs->size += obj.size;
171 return osl_close_disk_object(&obj);
174 static int com_catblob_callback(struct osl_table *table,
175 const struct osl_object *query, struct osl_object *result)
178 struct pattern_match_data pmd = {
181 .loop_col_num = BLOBCOL_NAME,
182 .match_col_num = BLOBCOL_NAME,
183 .pm_flags = PM_SKIP_EMPTY_NAME,
188 ret = for_each_matching_row(&pmd);
191 return (ret > 0)? 0 : ret;
194 static int com_catblob(callback_function *f, int fd, int argc,
195 char * const * const argv)
197 struct osl_object result;
201 return -E_BLOB_SYNTAX;
202 ret = send_standard_callback_request(argc - 1, argv + 1, f, &result);
204 ret = send_bin_buffer(fd, (char *)result.data, result.size);
210 /** Used for removing rows from a blob table. */
212 /** Message buffer. */
213 struct para_buffer pb;
214 /** Number of removed blobs. */
215 unsigned num_removed;
218 static int remove_blob(struct osl_table *table, struct osl_row *row,
219 const char *name, void *data)
221 struct rmblob_data *rmbd = data;
222 int ret = osl_del_row(table, row);
224 para_printf(&rmbd->pb, "%s: %s\n", name, PARA_STRERROR(-ret));
228 return 1; /* return success to remove other matching blobs. */
231 static int com_rmblob_callback(struct osl_table *table,
232 const struct osl_object *query,
233 struct osl_object *result)
236 struct rmblob_data rmbd = {.num_removed = 0};
237 struct pattern_match_data pmd = {
240 .loop_col_num = BLOBCOL_NAME,
241 .match_col_num = BLOBCOL_NAME,
242 .pm_flags = PM_SKIP_EMPTY_NAME,
244 .action = remove_blob
247 ret = for_each_matching_row(&pmd);
249 para_printf(&rmbd.pb, "%s\n", PARA_STRERROR(-ret));
250 if (!rmbd.num_removed)
251 para_printf(&rmbd.pb, "no matches, nothing removed\n");
253 para_printf(&rmbd.pb, "removed %d blobs\n", rmbd.num_removed);
254 afs_event(BLOB_RENAME, NULL, table);
256 result->data = rmbd.pb.buf;
257 result->size = rmbd.pb.size;
261 static int com_rmblob(callback_function *f, int fd, int argc,
262 char * const * const argv)
265 struct osl_object result;
268 return -E_MOOD_SYNTAX;
269 ret = send_option_arg_callback_request(NULL, argc - 1, argv + 1, f,
272 send_buffer(fd, (char *)result.data);
278 static int com_addblob_callback(struct osl_table *table,
279 const struct osl_object *query,
280 __a_unused struct osl_object *result)
282 struct osl_object objs[NUM_BLOB_COLUMNS];
283 char *name = query->data;
284 size_t name_len = strlen(name) + 1;
289 ret = osl_get_num_rows(table, &num_rows);
292 if (!num_rows) { /* this is the first entry ever added */
293 /* insert dummy row containing the id */
294 id = 2; /* this entry will be entry #1, so 2 is the next */
295 objs[BLOBCOL_ID].data = &id;
296 objs[BLOBCOL_ID].size = sizeof(id);
297 objs[BLOBCOL_NAME].data = "";
298 objs[BLOBCOL_NAME].size = 1;
299 objs[BLOBCOL_DEF].data = "";
300 objs[BLOBCOL_DEF].size = 1;
301 ret = osl_add_row(table, objs);
305 /* check if name already exists */
307 struct osl_object obj = {.data = name, .size = name_len};
308 ret = osl_get_row(table, BLOBCOL_NAME, &obj, &row);
309 if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
311 if (ret >= 0) { /* we already have a blob with this name */
312 obj.data = name + name_len;
313 obj.size = query->size - name_len;
314 return osl_update_object(table, row, BLOBCOL_DEF, &obj);
316 /* new blob, get id of the dummy row and increment it */
319 ret = osl_get_row(table, BLOBCOL_NAME, &obj, &row);
322 ret = osl_get_object(table, row, BLOBCOL_ID, &obj);
325 id = *(uint32_t *)obj.data + 1;
327 ret = osl_update_object(table, row, BLOBCOL_ID, &obj);
332 objs[BLOBCOL_ID].data = &id;
333 objs[BLOBCOL_ID].size = sizeof(id);
334 objs[BLOBCOL_NAME].data = name;
335 objs[BLOBCOL_NAME].size = name_len;
336 objs[BLOBCOL_DEF].data = name + name_len;
337 objs[BLOBCOL_DEF].size = query->size - name_len;
338 ret = osl_add_row(table, objs);
341 afs_event(BLOB_ADD, NULL, table);
345 static int com_addblob(callback_function *f, int fd, int argc,
346 char * const * const argv)
348 struct osl_object arg_obj;
351 return -E_BLOB_SYNTAX;
352 if (!*argv[1]) /* empty name is reserved for the dummy row */
353 return -E_BLOB_SYNTAX;
354 PARA_NOTICE_LOG("argv[1]: %s\n", argv[1]);
355 arg_obj.size = strlen(argv[1]) + 1;
356 arg_obj.data = (char *)argv[1];
357 return stdin_command(fd, &arg_obj, f, 10 * 1024 * 1024, NULL);
360 static int com_mvblob_callback(struct osl_table *table,
361 const struct osl_object *query,
362 __a_unused struct osl_object *result)
364 char *src = (char *) query->data;
365 struct osl_object obj = {.data = src, .size = strlen(src) + 1};
366 char *dest = src + obj.size;
368 int ret = osl_get_row(table, BLOBCOL_NAME, &obj, &row);
373 obj.size = strlen(dest) + 1;
374 ret = osl_update_object(table, row, BLOBCOL_NAME, &obj);
377 afs_event(BLOB_RENAME, NULL, table);
381 static int com_mvblob(callback_function *f, __a_unused int fd,
382 int argc, char * const * const argv)
385 return -E_MOOD_SYNTAX;
386 return send_option_arg_callback_request(NULL, argc - 1, argv + 1, f,
390 #define DEFINE_BLOB_COMMAND(cmd_name, table_name, cmd_prefix) \
391 static int com_ ## cmd_name ## cmd_prefix ## _callback(const struct osl_object *query, \
392 struct osl_object *output) \
394 return com_ ## cmd_name ## blob_callback(table_name ## _table, query, output); \
396 int com_ ## cmd_name ## cmd_prefix(int fd, int argc, char * const * const argv) \
398 return com_ ## cmd_name ## blob(com_ ## cmd_name ## cmd_prefix ## _callback, fd, argc, argv); \
401 static int blob_get_name_by_id(struct osl_table *table, uint32_t id,
405 struct osl_object obj = {.data = &id, .size = sizeof(id)};
411 ret = osl_get_row(table, BLOBCOL_ID, &obj, &row);
414 ret = osl_get_object(table, row, BLOBCOL_NAME, &obj);
417 *name = (char *)obj.data;
421 /** Define the \p get_name_by_id function for this blob type. */
422 #define DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix) \
423 int cmd_prefix ## _get_name_by_id(uint32_t id, char **name) \
425 return blob_get_name_by_id(table_name ## _table, id, name); \
429 static int blob_get_def_by_name(struct osl_table *table, char *name,
430 struct osl_object *def)
433 struct osl_object obj = {.data = name, .size = strlen(name) + 1};
439 ret = osl_get_row(table, BLOBCOL_NAME, &obj, &row);
442 return osl_open_disk_object(table, row, BLOBCOL_DEF, def);
445 /** Define the \p get_def_by_id function for this blob type. */
446 #define DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix) \
447 int cmd_prefix ## _get_def_by_name(char *name, struct osl_object *def) \
449 return blob_get_def_by_name(table_name ## _table, name, def); \
452 static int blob_get_def_by_id(struct osl_table *table, uint32_t id,
453 struct osl_object *def)
456 struct osl_object obj = {.data = &id, .size = sizeof(id)};
462 ret = osl_get_row(table, BLOBCOL_ID, &obj, &row);
465 return osl_open_disk_object(table, row, BLOBCOL_DEF, def);
468 /** Define the \p get_def_by_id function for this blob type. */
469 #define DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix) \
470 int cmd_prefix ## _get_def_by_id(uint32_t id, struct osl_object *def) \
472 return blob_get_def_by_id(table_name ## _table, id, def); \
475 static int blob_get_name_and_def_by_row(struct osl_table *table,
476 const struct osl_row *row, char **name, struct osl_object *def)
478 struct osl_object obj;
479 int ret = osl_get_object(table, row, BLOBCOL_NAME, &obj);
483 return osl_open_disk_object(table, row, BLOBCOL_DEF, def);
485 /** Define the \p get_name_and_def_by_row function for this blob type. */
486 #define DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix) \
487 int cmd_prefix ## _get_name_and_def_by_row(const struct osl_row *row, \
488 char **name, struct osl_object *def) \
490 return blob_get_name_and_def_by_row(table_name ## _table, \
494 /** Define the \p close function for this blob type. */
495 #define DEFINE_BLOB_CLOSE(table_name) \
496 void table_name ## _close(void) \
498 osl_close_table(table_name ## _table, OSL_MARK_CLEAN); \
499 table_name ## _table = NULL; \
502 /** Define the \p create function for this blob type. */
503 #define DEFINE_BLOB_CREATE(table_name) \
504 int table_name ## _create(const char *dir) \
506 table_name ## _table_desc.dir = dir; \
507 return osl_create_table(&table_name ## _table_desc); \
510 static int blob_open(struct osl_table **table,
511 struct osl_table_description *desc,
516 ret = osl_open_table(desc, table);
520 if (ret >= 0 || is_errno(-ret, ENOENT))
525 #define DEFINE_BLOB_OPEN(table_name) \
526 int table_name ## _open(const char *dir) \
528 return blob_open(&table_name ## _table, \
529 &table_name ## _table_desc, dir); \
533 /** Define the \p init function for this blob type. */
534 #define DEFINE_BLOB_INIT(table_name) \
535 void table_name ## _init(struct afs_table *t) \
537 t->name = table_name ## _table_desc.name; \
538 t->open = table_name ## _open; \
539 t->close = table_name ## _close; \
540 t->create = table_name ## _create;\
541 t->event_handler = table_name ##_event_handler; \
542 table_name ## _table = NULL; \
546 /** Define all functions for this blob type. */
547 #define DEFINE_BLOB_FUNCTIONS(table_name, cmd_prefix) \
548 DEFINE_BLOB_OPEN(table_name) \
549 DEFINE_BLOB_CLOSE(table_name) \
550 DEFINE_BLOB_CREATE(table_name) \
551 DEFINE_BLOB_INIT(table_name) \
552 DEFINE_BLOB_COMMAND(ls, table_name, cmd_prefix) \
553 DEFINE_BLOB_COMMAND(cat, table_name, cmd_prefix) \
554 DEFINE_BLOB_COMMAND(add, table_name, cmd_prefix) \
555 DEFINE_BLOB_COMMAND(rm, table_name, cmd_prefix) \
556 DEFINE_BLOB_COMMAND(mv, table_name, cmd_prefix) \
557 DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix); \
558 DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix); \
559 DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix); \
560 DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix); \
562 /** \cond doxygen isn't smart enough to recognize these */
563 DEFINE_BLOB_FUNCTIONS(lyrics, lyr);
564 DEFINE_BLOB_FUNCTIONS(images, img);
565 DEFINE_BLOB_FUNCTIONS(moods, mood);
566 DEFINE_BLOB_FUNCTIONS(playlists, pl);