new codename, reset version to git
[paraslash.git] / blob.c
1 /*
2  * Copyright (C) 2007-2012 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file blob.c Macros and functions for blob handling. */
8
9 #include <regex.h>
10 #include <fnmatch.h>
11 #include <osl.h>
12
13 #include "para.h"
14 #include "error.h"
15 #include "crypt.h"
16 #include "string.h"
17 #include "afh.h"
18 #include "afs.h"
19 #include "ipc.h"
20 #include "portable_io.h"
21 #include "command.h"
22
23 /**
24  * Compare two osl objects pointing to unsigned integers of 32 bit size.
25  *
26  * \param obj1 Pointer to the first integer.
27  * \param obj2 Pointer to the second integer.
28  *
29  * \return The values required for an osl compare function.
30  *
31  * \sa osl_compare_func, osl_hash_compare().
32  */
33 static int uint32_compare(const struct osl_object *obj1, const struct osl_object *obj2)
34 {
35         uint32_t d1 = read_u32((const char *)obj1->data);
36         uint32_t d2 = read_u32((const char *)obj2->data);
37
38         if (d1 < d2)
39                 return 1;
40         if (d1 > d2)
41                 return -1;
42         return 0;
43 }
44
45 static struct osl_column_description blob_cols[] = {
46         [BLOBCOL_ID] = {
47                 .storage_type = OSL_MAPPED_STORAGE,
48                 .storage_flags = OSL_RBTREE | OSL_UNIQUE | OSL_FIXED_SIZE,
49                 .name = "id",
50                 .data_size = 4,
51                 .compare_function = uint32_compare
52         },
53         [BLOBCOL_NAME] = {
54                 .storage_type = OSL_MAPPED_STORAGE,
55                 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
56                 .name = "name",
57                 .compare_function = string_compare
58         },
59         [BLOBCOL_DEF] = {
60                 .storage_type = OSL_DISK_STORAGE,
61                 .storage_flags = 0,
62                 .name = "definition"
63         }
64 };
65
66 /** Define an osl table description for a blob table. */
67 #define DEFINE_BLOB_TABLE_DESC(table_name) \
68         struct osl_table_description table_name ## _table_desc = { \
69                 .name = #table_name, \
70                 .num_columns = NUM_BLOB_COLUMNS, \
71                 .flags = OSL_LARGE_TABLE, \
72                 .column_descriptions = blob_cols \
73         };
74
75 /** Define a pointer to an osl blob table with a canonical name. */
76 #define DEFINE_BLOB_TABLE_PTR(table_name) struct osl_table *table_name ## _table;
77
78
79 /** Define a blob table. */
80 #define INIT_BLOB_TABLE(table_name) \
81         DEFINE_BLOB_TABLE_DESC(table_name); \
82         DEFINE_BLOB_TABLE_PTR(table_name);
83
84 /* doxygen isn't smart enough to recognize these */
85 /** \cond blob_table */
86 INIT_BLOB_TABLE(lyrics);
87 INIT_BLOB_TABLE(images);
88 INIT_BLOB_TABLE(moods);
89 INIT_BLOB_TABLE(playlists);
90 /** \endcond blob_table */
91
92 /** Flags that may be passed to the \p ls functions of each blob  type. */
93 enum blob_ls_flags {
94         /** List both id and name. */
95         BLOB_LS_FLAG_LONG = 1,
96         /** Reverse sort order. */
97         BLOB_LS_FLAG_REVERSE = 2,
98         /** Sort by id instead of name. */
99         BLOB_LS_FLAG_SORT_BY_ID = 4,
100 };
101
102 /** Structure passed to the \p print_blob function. */
103 struct lsblob_action_data {
104         /** The flags given at the command line. */
105         uint32_t flags;
106         /** Message buffer. */
107         struct para_buffer pb;
108 };
109
110 static int print_blob(struct osl_table *table, struct osl_row *row,
111                 const char *name, void *data)
112 {
113         struct lsblob_action_data *lbad = data;
114         struct osl_object obj;
115         uint32_t id;
116         int ret;
117
118         if (!(lbad->flags & BLOB_LS_FLAG_LONG))
119                 return para_printf(&lbad->pb, "%s\n", name);
120         ret = osl(osl_get_object(table, row, BLOBCOL_ID, &obj));
121         if (ret < 0) {
122                 para_printf(&lbad->pb, "%s: %s\n", name, para_strerror(-ret));
123                 return ret;
124         }
125         id = *(uint32_t *)obj.data;
126         return para_printf(&lbad->pb, "%u\t%s\n", id, name);
127 }
128
129 static void com_lsblob_callback(struct osl_table *table,
130                 int fd, const struct osl_object *query)
131 {
132         struct lsblob_action_data lbad = {
133                 .flags = *(uint32_t *)query->data,
134                 .pb = {
135                         .max_size = shm_get_shmmax(),
136                         .private_data = &fd,
137                         .max_size_handler = pass_buffer_as_shm
138                 }
139         };
140         struct pattern_match_data pmd = {
141                 .table = table,
142                 .patterns = {.data = (char *)query->data + sizeof(uint32_t),
143                         .size = query->size - sizeof(uint32_t)},
144                 .pm_flags = PM_NO_PATTERN_MATCHES_EVERYTHING | PM_SKIP_EMPTY_NAME,
145                 .match_col_num = BLOBCOL_NAME,
146                 .data = &lbad,
147                 .action = print_blob,
148         };
149         int ret;
150
151         if (lbad.flags & BLOB_LS_FLAG_REVERSE)
152                 pmd.pm_flags |= PM_REVERSE_LOOP;
153         if (!(lbad.flags & BLOB_LS_FLAG_SORT_BY_ID))
154                 pmd.loop_col_num = BLOBCOL_NAME;
155         else
156                 pmd.loop_col_num = BLOBCOL_ID;
157         ret = for_each_matching_row(&pmd);
158         if (ret < 0)
159                 para_printf(&lbad.pb, "%s\n", para_strerror(-ret));
160         else if (pmd.num_matches == 0 && pmd.patterns.size > 0)
161                 para_printf(&lbad.pb, "no matches\n");
162         if (lbad.pb.offset)
163                 pass_buffer_as_shm(lbad.pb.buf, lbad.pb.offset, &fd);
164         free(lbad.pb.buf);
165 }
166
167 static int com_lsblob(callback_function *f, struct command_context *cc)
168 {
169         uint32_t flags = 0;
170         struct osl_object options = {.data = &flags, .size = sizeof(flags)};
171         int i;
172
173         for (i = 1; i < cc->argc; i++) {
174                 const char *arg = cc->argv[i];
175                 if (arg[0] != '-')
176                         break;
177                 if (!strcmp(arg, "--")) {
178                         i++;
179                         break;
180                 }
181                 if (!strcmp(arg, "-l")) {
182                         flags |= BLOB_LS_FLAG_LONG;
183                         continue;
184                 }
185                 if (!strcmp(arg, "-i")) {
186                         flags |= BLOB_LS_FLAG_SORT_BY_ID;
187                         continue;
188                 }
189                 if (!strcmp(arg, "-r")) {
190                         flags |= BLOB_LS_FLAG_REVERSE;
191                         continue;
192                 }
193                 break;
194         }
195 //      if (argc > i)
196 //              return -E_BLOB_SYNTAX;
197         return send_option_arg_callback_request(&options, cc->argc - i,
198                 cc->argv + i, f, sc_send_result, cc);
199 }
200
201 static int cat_blob(struct osl_table *table, struct osl_row *row,
202                 __a_unused const char *name, void *data)
203 {
204         int ret = 0, ret2;
205         struct osl_object obj;
206
207         ret = osl(osl_open_disk_object(table, row, BLOBCOL_DEF, &obj));
208         if (ret < 0)
209                 return (ret == osl(-E_OSL_EMPTY))? 0 : ret;
210         assert(obj.size > 0);
211         ret = pass_buffer_as_shm(obj.data, obj.size, data);
212         ret2 = osl(osl_close_disk_object(&obj));
213         return (ret < 0)? ret : ret2;
214 }
215
216 static void com_catblob_callback(struct osl_table *table, int fd,
217                 const struct osl_object *query)
218 {
219         struct pattern_match_data pmd = {
220                 .table = table,
221                 .patterns = *query,
222                 .loop_col_num = BLOBCOL_NAME,
223                 .match_col_num = BLOBCOL_NAME,
224                 .pm_flags = PM_SKIP_EMPTY_NAME,
225                 .data = &fd,
226                 .action = cat_blob
227         };
228         for_each_matching_row(&pmd);
229         if (pmd.num_matches == 0) {
230                 char err_msg[] = "no matches\n";
231                 pass_buffer_as_shm(err_msg, sizeof(err_msg), &fd);
232         }
233 }
234
235 static int com_catblob(callback_function *f, struct command_context *cc)
236 {
237         if (cc->argc < 2)
238                 return -E_BLOB_SYNTAX;
239         return send_standard_callback_request(cc->argc - 1, cc->argv + 1, f,
240                 sc_send_result, cc);
241 }
242
243 /** Used for removing rows from a blob table. */
244 struct rmblob_data {
245         /** Message buffer. */
246         struct para_buffer pb;
247 };
248
249 static int remove_blob(struct osl_table *table, struct osl_row *row,
250                 const char *name, void *data)
251 {
252         struct rmblob_data *rmbd = data;
253         int ret = osl(osl_del_row(table, row));
254         if (ret < 0) {
255                 para_printf(&rmbd->pb, "%s: %s\n", name, para_strerror(-ret));
256                 return ret;
257         }
258         return 1;
259 }
260
261 static void com_rmblob_callback(struct osl_table *table, int fd,
262                 const struct osl_object *query)
263 {
264         int ret, ret2 = 0;
265         struct rmblob_data rmbd = {
266                 .pb = {
267                         .max_size = shm_get_shmmax(),
268                         .private_data = &fd,
269                         .max_size_handler = pass_buffer_as_shm
270                 }
271         };
272         struct pattern_match_data pmd = {
273                 .table = table,
274                 .patterns = *query,
275                 .loop_col_num = BLOBCOL_NAME,
276                 .match_col_num = BLOBCOL_NAME,
277                 .pm_flags = PM_SKIP_EMPTY_NAME,
278                 .data = &rmbd,
279                 .action = remove_blob
280         };
281         ret = for_each_matching_row(&pmd);
282         if (ret < 0) {
283                 ret2 = para_printf(&rmbd.pb, "%s\n", para_strerror(-ret));
284                 if (ret2 < 0)
285                         goto out;
286         }
287         if (pmd.num_matches == 0)
288                 ret2 = para_printf(&rmbd.pb, "no matches, nothing removed\n");
289         else {
290                 ret2 = para_printf(&rmbd.pb, "removed %d blobs\n", pmd.num_matches);
291                 afs_event(BLOB_RENAME, NULL, table);
292         }
293 out:
294         if (ret2 >= 0 && rmbd.pb.offset)
295                 pass_buffer_as_shm(rmbd.pb.buf, rmbd.pb.offset, &fd);
296         free(rmbd.pb.buf);
297 }
298
299 static int com_rmblob(callback_function *f, struct command_context *cc)
300 {
301         if (cc->argc < 2)
302                 return -E_MOOD_SYNTAX;
303         return send_option_arg_callback_request(NULL, cc->argc - 1, cc->argv + 1, f,
304                 sc_send_result, cc);
305 }
306
307 static void com_addblob_callback(struct osl_table *table, __a_unused int fd,
308                 const struct osl_object *query)
309 {
310         struct osl_object objs[NUM_BLOB_COLUMNS];
311         char *name = query->data;
312         size_t name_len = strlen(name) + 1;
313         uint32_t id;
314         unsigned num_rows;
315         int ret;
316
317         ret = osl(osl_get_num_rows(table, &num_rows));
318         if (ret < 0)
319                 goto out;
320         if (!num_rows) { /* this is the first entry ever added */
321                 /* insert dummy row containing the id */
322                 id = 2; /* this entry will be entry #1, so 2 is the next */
323                 objs[BLOBCOL_ID].data = &id;
324                 objs[BLOBCOL_ID].size = sizeof(id);
325                 objs[BLOBCOL_NAME].data = "";
326                 objs[BLOBCOL_NAME].size = 1;
327                 objs[BLOBCOL_DEF].data = "";
328                 objs[BLOBCOL_DEF].size = 1;
329                 ret = osl(osl_add_row(table, objs));
330                 if (ret < 0)
331                         goto out;
332         } else {
333                 /* check if name already exists */
334                 struct osl_row *row;
335                 struct osl_object obj = {.data = name, .size = name_len};
336                 ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
337                 if (ret < 0 && ret != -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
338                         goto out;
339                 if (ret >= 0) { /* we already have a blob with this name */
340                         obj.data = name + name_len;
341                         obj.size = query->size - name_len;
342                         ret = osl(osl_update_object(table, row, BLOBCOL_DEF, &obj));
343                         goto out;
344                 }
345                 /* new blob, get id of the dummy row and increment it */
346                 obj.data = "";
347                 obj.size = 1;
348                 ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
349                 if (ret < 0)
350                         goto out;
351                 ret = osl(osl_get_object(table, row, BLOBCOL_ID, &obj));
352                 if (ret < 0)
353                         goto out;
354                 id = *(uint32_t *)obj.data + 1;
355                 obj.data = &id;
356                 ret = osl(osl_update_object(table, row, BLOBCOL_ID, &obj));
357                 if (ret < 0)
358                         goto out;
359         }
360         id--;
361         objs[BLOBCOL_ID].data = &id;
362         objs[BLOBCOL_ID].size = sizeof(id);
363         objs[BLOBCOL_NAME].data = name;
364         objs[BLOBCOL_NAME].size = name_len;
365         objs[BLOBCOL_DEF].data = name + name_len;
366         objs[BLOBCOL_DEF].size = query->size - name_len;
367         ret = osl(osl_add_row(table, objs));
368         if (ret < 0)
369                 goto out;
370         afs_event(BLOB_ADD, NULL, table);
371 out:
372         if (ret < 0)
373                 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
374 }
375
376 /*
377  * write input from fd to dynamically allocated buffer,
378  * but maximal max_size byte.
379  */
380 static int fd2buf(struct stream_cipher_context *scc, unsigned max_size, struct osl_object *obj)
381 {
382         const size_t chunk_size = 1024;
383         size_t size = 2048, received = 0;
384         int ret;
385         char *buf = para_malloc(size);
386
387         for (;;) {
388                 ret = sc_recv_bin_buffer(scc, buf + received, chunk_size);
389                 if (ret <= 0)
390                         break;
391                 received += ret;
392                 if (received + chunk_size >= size) {
393                         size *= 2;
394                         ret = -E_INPUT_TOO_LARGE;
395                         if (size > max_size)
396                                 break;
397                         buf = para_realloc(buf, size);
398                 }
399         }
400         obj->data = buf;
401         obj->size = received;
402         if (ret < 0)
403                 free(buf);
404         return ret;
405 }
406
407 /*
408  * Read data from a file descriptor, and send it to the afs process.
409  *
410  * \param scc crypt context containing the file descriptor to read data from.
411  * \param arg_obj Pointer to the arguments to \a f.
412  * \param f The callback function.
413  * \param max_len Don't read more than that many bytes from stdin.
414  * \param result_handler See \ref send_callback_request.
415  * \param private_result_data See \ref send_callback_request.
416  *
417  * This function is used by commands that wish to let para_server store
418  * arbitrary data specified by the user (for instance the add_blob family of
419  * commands). First, at most \a max_len bytes are read and decrypted from the
420  * file descriptor given by \a scc. The result is concatenated with the buffer
421  * given by \a arg_obj, and the combined buffer is made available to the afs
422  * process via the callback method. See \ref send_callback_request for details.
423  *
424  * \return Negative on errors, the return value of the underlying call to
425  * send_callback_request() otherwise.
426  */
427 static int stdin_command(struct command_context *cc, struct osl_object *arg_obj,
428                 callback_function *f, unsigned max_len,
429                 callback_result_handler *result_handler,
430                 void *private_result_data)
431 {
432         struct osl_object query, stdin_obj;
433         int ret;
434
435         ret = sc_send_buffer(&cc->scc, AWAITING_DATA_MSG);
436         if (ret < 0)
437                 return ret;
438         ret = fd2buf(&cc->scc, max_len, &stdin_obj);
439         if (ret < 0)
440                 return ret;
441         query.size = arg_obj->size + stdin_obj.size;
442         query.data = para_malloc(query.size);
443         memcpy(query.data, arg_obj->data, arg_obj->size);
444         memcpy((char *)query.data + arg_obj->size, stdin_obj.data, stdin_obj.size);
445         free(stdin_obj.data);
446         ret = send_callback_request(f, &query, result_handler, private_result_data);
447         free(query.data);
448         return ret;
449 }
450
451 static int com_addblob(callback_function *f, struct command_context *cc)
452 {
453         struct osl_object arg_obj;
454
455         if (cc->argc != 2)
456                 return -E_BLOB_SYNTAX;
457         if (!*cc->argv[1]) /* empty name is reserved for the dummy row */
458                 return -E_BLOB_SYNTAX;
459         arg_obj.size = strlen(cc->argv[1]) + 1;
460         arg_obj.data = (char *)cc->argv[1];
461         return stdin_command(cc, &arg_obj, f, 10 * 1024 * 1024, NULL, NULL);
462 }
463
464 /* FIXME: Print output to client, not to log file */
465 static void com_mvblob_callback(struct osl_table *table, __a_unused int fd,
466                 const struct osl_object *query)
467 {
468         char *src = (char *) query->data;
469         struct osl_object obj = {.data = src, .size = strlen(src) + 1};
470         char *dest = src + obj.size;
471         struct osl_row *row;
472         int ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
473
474         if (ret < 0)
475                 goto out;
476         obj.data = dest;
477         obj.size = strlen(dest) + 1;
478         ret = osl(osl_update_object(table, row, BLOBCOL_NAME, &obj));
479         if (ret < 0)
480                 goto out;
481         afs_event(BLOB_RENAME, NULL, table);
482 out:
483         if (ret < 0)
484                 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
485 }
486
487 static int com_mvblob(callback_function *f, struct command_context *cc)
488 {
489         if (cc->argc != 3)
490                 return -E_MOOD_SYNTAX;
491         return send_option_arg_callback_request(NULL, cc->argc - 1,
492                 cc->argv + 1, f, NULL, NULL);
493 }
494
495 #define DEFINE_BLOB_COMMAND(cmd_name, table_name, cmd_prefix) \
496         static void com_ ## cmd_name ## cmd_prefix ## _callback(int fd, const struct osl_object *query) \
497         { \
498                 return com_ ## cmd_name ## blob_callback(table_name ## _table, fd, query); \
499         } \
500         int com_ ## cmd_name ## cmd_prefix(struct command_context *cc) \
501         { \
502                 return com_ ## cmd_name ## blob(com_ ## cmd_name ## cmd_prefix ## _callback, cc); \
503         }
504
505 static int blob_get_name_by_id(struct osl_table *table, uint32_t id,
506                 char **name)
507 {
508         struct osl_row *row;
509         struct osl_object obj = {.data = &id, .size = sizeof(id)};
510         int ret;
511
512         *name = NULL;
513         if (!id)
514                 return 1;
515         ret = osl(osl_get_row(table, BLOBCOL_ID, &obj, &row));
516         if (ret < 0)
517                 return ret;
518         ret = osl(osl_get_object(table, row, BLOBCOL_NAME, &obj));
519         if (ret < 0)
520                 return ret;
521         *name = (char *)obj.data;
522         return 1;
523 }
524
525 /** Define the \p get_name_by_id function for this blob type. */
526 #define DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix) \
527         int cmd_prefix ## _get_name_by_id(uint32_t id, char **name) \
528         { \
529                 return blob_get_name_by_id(table_name ## _table, id, name); \
530         }
531
532
533 static int blob_get_def_by_name(struct osl_table *table, char *name,
534                 struct osl_object *def)
535 {
536         struct osl_row *row;
537         struct osl_object obj = {.data = name, .size = strlen(name) + 1};
538         int ret;
539
540         def->data = NULL;
541         if (!*name)
542                 return 1;
543         ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
544         if (ret < 0)
545                 return ret;
546         return osl(osl_open_disk_object(table, row, BLOBCOL_DEF, def));
547 }
548
549 /** Define the \p get_def_by_id function for this blob type. */
550 #define DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix) \
551         int cmd_prefix ## _get_def_by_name(char *name, struct osl_object *def) \
552         { \
553                 return blob_get_def_by_name(table_name ## _table, name, def); \
554         }
555
556 static int blob_get_def_by_id(struct osl_table *table, uint32_t id,
557                 struct osl_object *def)
558 {
559         struct osl_row *row;
560         struct osl_object obj = {.data = &id, .size = sizeof(id)};
561         int ret;
562
563         def->data = NULL;
564         if (!id)
565                 return 1;
566         ret = osl(osl_get_row(table, BLOBCOL_ID, &obj, &row));
567         if (ret < 0)
568                 return ret;
569         return osl(osl_open_disk_object(table, row, BLOBCOL_DEF, def));
570 }
571
572 /** Define the \p get_def_by_id function for this blob type. */
573 #define DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix) \
574         int cmd_prefix ## _get_def_by_id(uint32_t id, struct osl_object *def) \
575         { \
576                 return blob_get_def_by_id(table_name ## _table, id, def); \
577         }
578
579 static int blob_get_name_and_def_by_row(struct osl_table *table,
580                 const struct osl_row *row, char **name, struct osl_object *def)
581 {
582         struct osl_object obj;
583         int ret = osl(osl_get_object(table, row, BLOBCOL_NAME, &obj));
584         if (ret < 0)
585                 return ret;
586         *name = obj.data;
587         return osl(osl_open_disk_object(table, row, BLOBCOL_DEF, def));
588 }
589 /** Define the \p get_name_and_def_by_row function for this blob type. */
590 #define DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix) \
591         int cmd_prefix ## _get_name_and_def_by_row(const struct osl_row *row, \
592                 char **name, struct osl_object *def) \
593         { \
594                 return blob_get_name_and_def_by_row(table_name ## _table, \
595                         row, name, def); \
596         }
597
598 /** Define the \p close function for this blob type. */
599 #define DEFINE_BLOB_CLOSE(table_name) \
600         static void table_name ## _close(void) \
601         { \
602                 osl_close_table(table_name ## _table, OSL_MARK_CLEAN); \
603                 table_name ## _table = NULL; \
604         }
605
606 /** Define the \p create function for this blob type. */
607 #define DEFINE_BLOB_CREATE(table_name) \
608         static int table_name ## _create(const char *dir) \
609         { \
610                 table_name ## _table_desc.dir = dir; \
611                 return osl_create_table(&table_name ## _table_desc); \
612         }
613
614 static int blob_open(struct osl_table **table,
615                 struct osl_table_description *desc,
616                 const char *dir)
617 {
618         int ret;
619         desc->dir = dir;
620         ret = osl(osl_open_table(desc, table));
621         if (ret >= 0)
622                 return ret;
623         *table = NULL;
624         if (ret >= 0 || ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT))
625                 return 1;
626         return ret;
627 }
628
629 #define DEFINE_BLOB_OPEN(table_name) \
630         static int table_name ## _open(const char *dir) \
631         { \
632                 return blob_open(&table_name ## _table, \
633                         &table_name ## _table_desc, dir); \
634         }
635
636
637 /** Define the \p init function for this blob type. */
638 #define DEFINE_BLOB_INIT(table_name) \
639         void table_name ## _init(struct afs_table *t) \
640         { \
641                 t->open = table_name ## _open; \
642                 t->close = table_name ## _close; \
643                 t->create = table_name ## _create;\
644                 t->event_handler = table_name ##_event_handler; \
645                 table_name ## _table = NULL; \
646         }
647
648
649 /** Define all functions for this blob type. */
650 #define DEFINE_BLOB_FUNCTIONS(table_name, cmd_prefix) \
651         DEFINE_BLOB_OPEN(table_name) \
652         DEFINE_BLOB_CLOSE(table_name) \
653         DEFINE_BLOB_CREATE(table_name) \
654         DEFINE_BLOB_INIT(table_name) \
655         DEFINE_BLOB_COMMAND(ls, table_name, cmd_prefix) \
656         DEFINE_BLOB_COMMAND(cat, table_name, cmd_prefix) \
657         DEFINE_BLOB_COMMAND(add, table_name, cmd_prefix) \
658         DEFINE_BLOB_COMMAND(rm, table_name, cmd_prefix) \
659         DEFINE_BLOB_COMMAND(mv, table_name, cmd_prefix) \
660         DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix); \
661         DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix); \
662         DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix); \
663         DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix); \
664
665 /* doxygen isn't smart enough to recognize these */
666 /** \cond blob_function */
667 DEFINE_BLOB_FUNCTIONS(lyrics, lyr);
668 DEFINE_BLOB_FUNCTIONS(images, img);
669 DEFINE_BLOB_FUNCTIONS(moods, mood);
670 DEFINE_BLOB_FUNCTIONS(playlists, pl);
671 /** \endcond blob_function */