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