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 {
58 struct para_buffer pb;
61 static int print_blob(struct osl_table *table, struct osl_row *row,
62 const char *name, void *data)
64 struct lsblob_action_data *lbad = data;
65 struct osl_object obj;
69 if (!(lbad->flags & BLOB_LS_FLAG_LONG)) {
70 para_printf(&lbad->pb, "%s\n", name);
73 ret = osl_get_object(table, row, BLOBCOL_ID, &obj);
75 para_printf(&lbad->pb, "%s: %s\n", name, PARA_STRERROR(-ret));
78 id = *(uint32_t *)obj.data;
79 para_printf(&lbad->pb, "%u\t%s\n", id, name);
83 static int com_lsblob_callback(struct osl_table *table,
84 const struct osl_object *query, struct osl_object *result)
86 struct lsblob_action_data lbad = {.flags = *(uint32_t *)query};
87 struct pattern_match_data pmd = {
89 .patterns = {.data = (char *)query->data + sizeof(uint32_t),
90 .size = query->size - sizeof(uint32_t)},
91 .pm_flags = PM_NO_PATTERN_MATCHES_EVERYTHING | PM_SKIP_EMPTY_NAME,
92 .match_col_num = BLOBCOL_NAME,
98 if (lbad.flags & BLOB_LS_FLAG_REVERSE)
99 pmd.pm_flags |= PM_REVERSE_LOOP;
100 if (!(lbad.flags & BLOB_LS_FLAG_SORT_BY_ID))
101 pmd.loop_col_num = BLOBCOL_ID;
103 pmd.loop_col_num = BLOBCOL_NAME;
104 ret = for_each_matching_row(&pmd);
106 para_printf(&lbad.pb, "%s\n", PARA_STRERROR(-ret));
109 result->data = lbad.pb.buf;
110 result->size = lbad.pb.size;
114 static int com_lsblob(callback_function *f, int fd, int argc, char * const * const argv)
117 struct osl_object options = {.data = &flags, .size = sizeof(flags)},
121 for (i = 1; i < argc; i++) {
122 const char *arg = argv[i];
125 if (!strcmp(arg, "--")) {
129 if (!strcmp(arg, "-l")) {
130 flags |= BLOB_LS_FLAG_LONG;
133 if (!strcmp(arg, "-i")) {
134 flags |= BLOB_LS_FLAG_SORT_BY_ID;
137 if (!strcmp(arg, "-r")) {
138 flags |= BLOB_LS_FLAG_REVERSE;
143 // return -E_BLOB_SYNTAX;
144 ret = send_option_arg_callback_request(&options, argc - i,
145 argv + i, f, &result);
147 send_buffer(fd, (char *)result.data);
153 static int cat_blob(struct osl_table *table, struct osl_row *row,
154 __a_unused const char *name, void *data)
157 struct osl_object *blobs = data;
158 struct osl_object obj;
160 ret = osl_open_disk_object(table, row, BLOBCOL_DEF, &obj);
164 blobs->data = para_realloc(blobs->data, blobs->size + obj.size);
165 memcpy(blobs->data + blobs->size, obj.data, obj.size);
166 blobs->size += obj.size;
168 return osl_close_disk_object(&obj);
171 static int com_catblob_callback(struct osl_table *table,
172 const struct osl_object *query, struct osl_object *result)
175 struct pattern_match_data pmd = {
178 .loop_col_num = BLOBCOL_NAME,
179 .match_col_num = BLOBCOL_NAME,
180 .pm_flags = PM_SKIP_EMPTY_NAME,
185 ret = for_each_matching_row(&pmd);
188 return (ret > 0)? 0 : ret;
191 static int com_catblob(callback_function *f, int fd, int argc,
192 char * const * const argv)
194 struct osl_object result;
198 return -E_BLOB_SYNTAX;
199 ret = send_standard_callback_request(argc - 1, argv + 1, f, &result);
201 ret = send_bin_buffer(fd, (char *)result.data, result.size);
208 struct para_buffer pb;
209 unsigned num_removed;
212 static int remove_blob(struct osl_table *table, struct osl_row *row,
213 const char *name, void *data)
215 struct rmblob_data *rmbd = data;
216 int ret = osl_del_row(table, row);
218 para_printf(&rmbd->pb, "%s: %s\n", name, PARA_STRERROR(-ret));
222 return 1; /* return success to remove other matching blobs. */
225 static int com_rmblob_callback(struct osl_table *table,
226 const struct osl_object *query,
227 __a_unused struct osl_object *result)
230 struct rmblob_data rmbd = {.num_removed = 0};
231 struct pattern_match_data pmd = {
234 .loop_col_num = BLOBCOL_NAME,
235 .match_col_num = BLOBCOL_NAME,
236 .pm_flags = PM_SKIP_EMPTY_NAME,
238 .action = remove_blob
241 ret = for_each_matching_row(&pmd);
243 para_printf(&rmbd.pb, "%s\n", PARA_STRERROR(-ret));
244 if (!rmbd.num_removed)
245 para_printf(&rmbd.pb, "no matches, nothing removed\n");
247 para_printf(&rmbd.pb, "removed %d blobs\n", rmbd.num_removed);
248 result->data = rmbd.pb.buf;
249 result->size = rmbd.pb.size;
253 static int com_rmblob(callback_function *f, __a_unused int fd, int argc,
254 char * const * const argv)
257 struct osl_object result;
260 return -E_MOOD_SYNTAX;
261 ret = send_option_arg_callback_request(NULL, argc - 1, argv + 1, f,
264 send_buffer(fd, (char *)result.data);
270 static int com_addblob_callback(struct osl_table *table,
271 const struct osl_object *query,
272 __a_unused struct osl_object *result)
274 struct osl_object objs[NUM_BLOB_COLUMNS];
275 char *name = query->data;
276 size_t name_len = strlen(name) + 1;
281 ret = osl_get_num_rows(table, &num_rows);
284 if (!num_rows) { /* this is the first entry ever added */
285 /* insert dummy row containing the id */
286 id = 2; /* this entry will be entry #1, so 2 is the next */
287 objs[BLOBCOL_ID].data = &id;
288 objs[BLOBCOL_ID].size = sizeof(id);
289 objs[BLOBCOL_NAME].data = "";
290 objs[BLOBCOL_NAME].size = 1;
291 objs[BLOBCOL_DEF].data = "";
292 objs[BLOBCOL_DEF].size = 1;
293 ret = osl_add_row(table, objs);
297 /* check if name already exists */
299 struct osl_object obj = {.data = name, .size = name_len};
300 ret = osl_get_row(table, BLOBCOL_NAME, &obj, &row);
301 if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
303 if (ret >= 0) { /* we already have a blob with this name */
304 obj.data = name + name_len;
305 obj.size = query->size - name_len;
306 return osl_update_object(table, row, BLOBCOL_DEF, &obj);
308 /* new blob, get id of the dummy row and increment it */
311 ret = osl_get_row(table, BLOBCOL_NAME, &obj, &row);
314 ret = osl_get_object(table, row, BLOBCOL_ID, &obj);
317 id = *(uint32_t *)obj.data + 1;
319 ret = osl_update_object(table, row, BLOBCOL_ID, &obj);
324 objs[BLOBCOL_ID].data = &id;
325 objs[BLOBCOL_ID].size = sizeof(id);
326 objs[BLOBCOL_NAME].data = name;
327 objs[BLOBCOL_NAME].size = name_len;
328 objs[BLOBCOL_DEF].data = name + name_len;
329 objs[BLOBCOL_DEF].size = query->size - name_len;
330 return osl_add_row(table, objs);
333 static int com_addblob(callback_function *f, int fd, int argc,
334 char * const * const argv)
336 struct osl_object arg_obj;
339 return -E_BLOB_SYNTAX;
340 if (!*argv[1]) /* empty name is reserved for the dummy row */
341 return -E_BLOB_SYNTAX;
342 PARA_NOTICE_LOG("argv[1]: %s\n", argv[1]);
343 arg_obj.size = strlen(argv[1]) + 1;
344 arg_obj.data = (char *)argv[1];
345 return stdin_command(fd, &arg_obj, f, 10 * 1024 * 1024, NULL);
348 static int com_mvblob_callback(struct osl_table *table,
349 const struct osl_object *query,
350 __a_unused struct osl_object *result)
352 char *src = (char *) query->data;
353 struct osl_object obj = {.data = src, .size = strlen(src) + 1};
354 char *dest = src + obj.size;
356 int ret = osl_get_row(table, BLOBCOL_NAME, &obj, &row);
361 obj.size = strlen(dest) + 1;
362 return osl_update_object(table, row, BLOBCOL_NAME, &obj);
365 static int com_mvblob(callback_function *f, __a_unused int fd,
366 int argc, char * const * const argv)
369 return -E_MOOD_SYNTAX;
370 return send_option_arg_callback_request(NULL, argc - 1, argv + 1, f,
374 #define DEFINE_BLOB_COMMAND(cmd_name, table_name, cmd_prefix) \
375 static int com_ ## cmd_name ## cmd_prefix ## _callback(const struct osl_object *query, \
376 struct osl_object *output) \
378 return com_ ## cmd_name ## blob_callback(table_name ## _table, query, output); \
380 int com_ ## cmd_name ## cmd_prefix(int fd, int argc, char * const * const argv) \
382 return com_ ## cmd_name ## blob(com_ ## cmd_name ## cmd_prefix ## _callback, fd, argc, argv); \
385 static int blob_get_name_by_id(struct osl_table *table, uint32_t id,
389 struct osl_object obj = {.data = &id, .size = sizeof(id)};
395 ret = osl_get_row(table, BLOBCOL_ID, &obj, &row);
398 ret = osl_get_object(table, row, BLOBCOL_NAME, &obj);
401 *name = (char *)obj.data;
405 /** Define the \p get_name_by_id function for this blob type. */
406 #define DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix) \
407 int cmd_prefix ## _get_name_by_id(uint32_t id, char **name) \
409 return blob_get_name_by_id(table_name ## _table, id, name); \
412 static int blob_get_name_and_def_by_row(struct osl_table *table,
413 const struct osl_row *row, char **name, struct osl_object *def)
415 struct osl_object obj;
416 int ret = osl_get_object(table, row, BLOBCOL_NAME, &obj);
420 return osl_open_disk_object(table, row, BLOBCOL_DEF, def);
422 /** Define the \p get_name_and_def_by_row function for this blob type. */
423 #define DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix) \
424 int cmd_prefix ## _get_name_and_def_by_row(const struct osl_row *row, \
425 char **name, struct osl_object *def) \
427 return blob_get_name_and_def_by_row(table_name ## _table, \
431 /** Define the \p shutdown function for this blob type. */
432 #define DEFINE_BLOB_SHUTDOWN(table_name) \
433 void table_name ## _shutdown(enum osl_close_flags flags) \
435 osl_close_table(table_name ## _table, flags); \
436 table_name ## _table = NULL; \
439 static int blob_init(struct osl_table **table,
440 struct osl_table_description *desc,
441 struct table_info *ti, const char *db)
446 ret = osl_open_table(ti->desc, table);
450 return ret == -E_NOENT? 1 : ret;
453 /** Define the \p init function for this blob type. */
454 #define DEFINE_BLOB_INIT(table_name) \
455 int table_name ## _init(struct table_info *ti, const char *db) \
457 return blob_init(&table_name ## _table, \
458 &table_name ## _table_desc, ti, db); \
462 /** Define all functions for this blob type. */
463 #define DEFINE_BLOB_FUNCTIONS(table_name, cmd_prefix) \
464 DEFINE_BLOB_COMMAND(ls, table_name, cmd_prefix) \
465 DEFINE_BLOB_COMMAND(cat, table_name, cmd_prefix) \
466 DEFINE_BLOB_COMMAND(add, table_name, cmd_prefix) \
467 DEFINE_BLOB_COMMAND(rm, table_name, cmd_prefix) \
468 DEFINE_BLOB_COMMAND(mv, table_name, cmd_prefix) \
469 DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix); \
470 DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix); \
471 DEFINE_BLOB_SHUTDOWN(table_name); \
472 DEFINE_BLOB_INIT(table_name);
474 /** \cond doxygen isn't smart enough to recognize these */
475 DEFINE_BLOB_FUNCTIONS(lyrics, lyr);
476 DEFINE_BLOB_FUNCTIONS(images, img);
477 DEFINE_BLOB_FUNCTIONS(moods, mood);
478 DEFINE_BLOB_FUNCTIONS(playlists, pl);