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