2 * Copyright (C) 2007-2008 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);
152 send_va_buffer(fd, "%s\n", para_strerror(-ret));
155 ret = send_buffer(fd, (char *)result.data);
160 static int cat_blob(struct osl_table *table, struct osl_row *row,
161 __a_unused const char *name, void *data)
164 struct osl_object *blobs = data;
165 struct osl_object obj;
167 ret = osl_open_disk_object(table, row, BLOBCOL_DEF, &obj);
171 blobs->data = para_realloc(blobs->data, blobs->size + obj.size);
172 memcpy(blobs->data + blobs->size, obj.data, obj.size);
173 blobs->size += obj.size;
175 return osl_close_disk_object(&obj);
178 static int com_catblob_callback(struct osl_table *table,
179 const struct osl_object *query, struct osl_object *result)
182 struct pattern_match_data pmd = {
185 .loop_col_num = BLOBCOL_NAME,
186 .match_col_num = BLOBCOL_NAME,
187 .pm_flags = PM_SKIP_EMPTY_NAME,
192 ret = for_each_matching_row(&pmd);
195 return (ret > 0)? 0 : ret;
198 static int com_catblob(callback_function *f, int fd, int argc,
199 char * const * const argv)
201 struct osl_object result;
205 return -E_BLOB_SYNTAX;
206 ret = send_standard_callback_request(argc - 1, argv + 1, f, &result);
208 ret = send_bin_buffer(fd, (char *)result.data, result.size);
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));
232 return 1; /* return success to remove other matching blobs. */
235 static int com_rmblob_callback(struct osl_table *table,
236 const struct osl_object *query,
237 struct osl_object *result)
240 struct rmblob_data rmbd = {.num_removed = 0};
241 struct pattern_match_data pmd = {
244 .loop_col_num = BLOBCOL_NAME,
245 .match_col_num = BLOBCOL_NAME,
246 .pm_flags = PM_SKIP_EMPTY_NAME,
248 .action = remove_blob
251 ret = for_each_matching_row(&pmd);
253 para_printf(&rmbd.pb, "%s\n", para_strerror(-ret));
254 if (!rmbd.num_removed)
255 para_printf(&rmbd.pb, "no matches, nothing removed\n");
257 para_printf(&rmbd.pb, "removed %d blobs\n", rmbd.num_removed);
258 afs_event(BLOB_RENAME, NULL, table);
260 result->data = rmbd.pb.buf;
261 result->size = rmbd.pb.size;
265 static int com_rmblob(callback_function *f, int fd, int argc,
266 char * const * const argv)
269 struct osl_object result;
272 return -E_MOOD_SYNTAX;
273 ret = send_option_arg_callback_request(NULL, argc - 1, argv + 1, f,
276 send_buffer(fd, (char *)result.data);
282 static int com_addblob_callback(struct osl_table *table,
283 const struct osl_object *query,
284 __a_unused struct osl_object *result)
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_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 return osl_update_object(table, row, BLOBCOL_DEF, &obj);
320 /* new blob, get id of the dummy row and increment it */
323 ret = osl_get_row(table, BLOBCOL_NAME, &obj, &row);
326 ret = osl_get_object(table, row, BLOBCOL_ID, &obj);
329 id = *(uint32_t *)obj.data + 1;
331 ret = osl_update_object(table, row, BLOBCOL_ID, &obj);
336 objs[BLOBCOL_ID].data = &id;
337 objs[BLOBCOL_ID].size = sizeof(id);
338 objs[BLOBCOL_NAME].data = name;
339 objs[BLOBCOL_NAME].size = name_len;
340 objs[BLOBCOL_DEF].data = name + name_len;
341 objs[BLOBCOL_DEF].size = query->size - name_len;
342 ret = osl_add_row(table, objs);
345 afs_event(BLOB_ADD, NULL, table);
349 static int com_addblob(callback_function *f, int fd, int argc,
350 char * const * const argv)
352 struct osl_object arg_obj;
355 return -E_BLOB_SYNTAX;
356 if (!*argv[1]) /* empty name is reserved for the dummy row */
357 return -E_BLOB_SYNTAX;
358 PARA_NOTICE_LOG("argv[1]: %s\n", argv[1]);
359 arg_obj.size = strlen(argv[1]) + 1;
360 arg_obj.data = (char *)argv[1];
361 return stdin_command(fd, &arg_obj, f, 10 * 1024 * 1024, NULL);
364 static int com_mvblob_callback(struct osl_table *table,
365 const struct osl_object *query,
366 __a_unused struct osl_object *result)
368 char *src = (char *) query->data;
369 struct osl_object obj = {.data = src, .size = strlen(src) + 1};
370 char *dest = src + obj.size;
372 int ret = osl_get_row(table, BLOBCOL_NAME, &obj, &row);
377 obj.size = strlen(dest) + 1;
378 ret = osl_update_object(table, row, BLOBCOL_NAME, &obj);
381 afs_event(BLOB_RENAME, NULL, table);
385 static int com_mvblob(callback_function *f, __a_unused int fd,
386 int argc, char * const * const argv)
389 return -E_MOOD_SYNTAX;
390 return send_option_arg_callback_request(NULL, argc - 1, argv + 1, f,
394 #define DEFINE_BLOB_COMMAND(cmd_name, table_name, cmd_prefix) \
395 static int com_ ## cmd_name ## cmd_prefix ## _callback(const struct osl_object *query, \
396 struct osl_object *output) \
398 return com_ ## cmd_name ## blob_callback(table_name ## _table, query, output); \
400 int com_ ## cmd_name ## cmd_prefix(int fd, int argc, char * const * const argv) \
402 return com_ ## cmd_name ## blob(com_ ## cmd_name ## cmd_prefix ## _callback, fd, argc, argv); \
405 static int blob_get_name_by_id(struct osl_table *table, uint32_t id,
409 struct osl_object obj = {.data = &id, .size = sizeof(id)};
415 ret = osl_get_row(table, BLOBCOL_ID, &obj, &row);
418 ret = osl_get_object(table, row, BLOBCOL_NAME, &obj);
421 *name = (char *)obj.data;
425 /** Define the \p get_name_by_id function for this blob type. */
426 #define DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix) \
427 int cmd_prefix ## _get_name_by_id(uint32_t id, char **name) \
429 return blob_get_name_by_id(table_name ## _table, id, name); \
433 static int blob_get_def_by_name(struct osl_table *table, char *name,
434 struct osl_object *def)
437 struct osl_object obj = {.data = name, .size = strlen(name) + 1};
443 ret = osl_get_row(table, BLOBCOL_NAME, &obj, &row);
446 return osl_open_disk_object(table, row, BLOBCOL_DEF, def);
449 /** Define the \p get_def_by_id function for this blob type. */
450 #define DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix) \
451 int cmd_prefix ## _get_def_by_name(char *name, struct osl_object *def) \
453 return blob_get_def_by_name(table_name ## _table, name, def); \
456 static int blob_get_def_by_id(struct osl_table *table, uint32_t id,
457 struct osl_object *def)
460 struct osl_object obj = {.data = &id, .size = sizeof(id)};
466 ret = osl_get_row(table, BLOBCOL_ID, &obj, &row);
469 return osl_open_disk_object(table, row, BLOBCOL_DEF, def);
472 /** Define the \p get_def_by_id function for this blob type. */
473 #define DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix) \
474 int cmd_prefix ## _get_def_by_id(uint32_t id, struct osl_object *def) \
476 return blob_get_def_by_id(table_name ## _table, id, def); \
479 static int blob_get_name_and_def_by_row(struct osl_table *table,
480 const struct osl_row *row, char **name, struct osl_object *def)
482 struct osl_object obj;
483 int ret = osl_get_object(table, row, BLOBCOL_NAME, &obj);
487 return osl_open_disk_object(table, row, BLOBCOL_DEF, def);
489 /** Define the \p get_name_and_def_by_row function for this blob type. */
490 #define DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix) \
491 int cmd_prefix ## _get_name_and_def_by_row(const struct osl_row *row, \
492 char **name, struct osl_object *def) \
494 return blob_get_name_and_def_by_row(table_name ## _table, \
498 /** Define the \p close function for this blob type. */
499 #define DEFINE_BLOB_CLOSE(table_name) \
500 void table_name ## _close(void) \
502 osl_close_table(table_name ## _table, OSL_MARK_CLEAN); \
503 table_name ## _table = NULL; \
506 /** Define the \p create function for this blob type. */
507 #define DEFINE_BLOB_CREATE(table_name) \
508 int table_name ## _create(const char *dir) \
510 table_name ## _table_desc.dir = dir; \
511 return osl_create_table(&table_name ## _table_desc); \
514 static int blob_open(struct osl_table **table,
515 struct osl_table_description *desc,
520 ret = osl_open_table(desc, table);
524 if (ret >= 0 || is_errno(-ret, ENOENT))
529 #define DEFINE_BLOB_OPEN(table_name) \
530 int table_name ## _open(const char *dir) \
532 return blob_open(&table_name ## _table, \
533 &table_name ## _table_desc, dir); \
537 /** Define the \p init function for this blob type. */
538 #define DEFINE_BLOB_INIT(table_name) \
539 void table_name ## _init(struct afs_table *t) \
541 t->name = table_name ## _table_desc.name; \
542 t->open = table_name ## _open; \
543 t->close = table_name ## _close; \
544 t->create = table_name ## _create;\
545 t->event_handler = table_name ##_event_handler; \
546 table_name ## _table = NULL; \
550 /** Define all functions for this blob type. */
551 #define DEFINE_BLOB_FUNCTIONS(table_name, cmd_prefix) \
552 DEFINE_BLOB_OPEN(table_name) \
553 DEFINE_BLOB_CLOSE(table_name) \
554 DEFINE_BLOB_CREATE(table_name) \
555 DEFINE_BLOB_INIT(table_name) \
556 DEFINE_BLOB_COMMAND(ls, table_name, cmd_prefix) \
557 DEFINE_BLOB_COMMAND(cat, table_name, cmd_prefix) \
558 DEFINE_BLOB_COMMAND(add, table_name, cmd_prefix) \
559 DEFINE_BLOB_COMMAND(rm, table_name, cmd_prefix) \
560 DEFINE_BLOB_COMMAND(mv, table_name, cmd_prefix) \
561 DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix); \
562 DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix); \
563 DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix); \
564 DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix); \
566 /** \cond doxygen isn't smart enough to recognize these */
567 DEFINE_BLOB_FUNCTIONS(lyrics, lyr);
568 DEFINE_BLOB_FUNCTIONS(images, img);
569 DEFINE_BLOB_FUNCTIONS(moods, mood);
570 DEFINE_BLOB_FUNCTIONS(playlists, pl);