]> git.tuebingen.mpg.de Git - paraslash.git/blob - blob.c
client: Remove client_recv_buffer().
[paraslash.git] / blob.c
1 /*
2  * Copyright (C) 2007-2013 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 "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         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                 return para_printf(&lbad->pb, "%s\n", name);
121         ret = osl(osl_get_object(table, row, BLOBCOL_ID, &obj));
122         if (ret < 0) {
123                 para_printf(&lbad->pb, "%s: %s\n", name, para_strerror(-ret));
124                 return ret;
125         }
126         id = *(uint32_t *)obj.data;
127         return para_printf(&lbad->pb, "%u\t%s\n", id, name);
128 }
129
130 static void com_lsblob_callback(struct osl_table *table,
131                 int fd, const struct osl_object *query)
132 {
133         struct lsblob_action_data lbad = {
134                 .flags = *(uint32_t *)query->data,
135                 .pb = {
136                         .max_size = shm_get_shmmax(),
137                         .private_data = &(struct afs_max_size_handler_data) {
138                                 .fd = fd,
139                                 .band = SBD_OUTPUT
140                         },
141                         .max_size_handler = afs_max_size_handler,
142                 }
143         };
144         struct pattern_match_data pmd = {
145                 .table = table,
146                 .patterns = {.data = (char *)query->data + sizeof(uint32_t),
147                         .size = query->size - sizeof(uint32_t)},
148                 .pm_flags = PM_NO_PATTERN_MATCHES_EVERYTHING | PM_SKIP_EMPTY_NAME,
149                 .match_col_num = BLOBCOL_NAME,
150                 .data = &lbad,
151                 .action = print_blob,
152         };
153         int ret;
154
155         if (lbad.flags & BLOB_LS_FLAG_REVERSE)
156                 pmd.pm_flags |= PM_REVERSE_LOOP;
157         if (!(lbad.flags & BLOB_LS_FLAG_SORT_BY_ID))
158                 pmd.loop_col_num = BLOBCOL_NAME;
159         else
160                 pmd.loop_col_num = BLOBCOL_ID;
161         ret = for_each_matching_row(&pmd);
162         if (ret < 0)
163                 para_printf(&lbad.pb, "%s\n", para_strerror(-ret));
164         else if (pmd.num_matches == 0 && pmd.patterns.size > 0)
165                 para_printf(&lbad.pb, "no matches\n");
166         if (lbad.pb.offset)
167                 pass_buffer_as_shm(fd, SBD_OUTPUT, lbad.pb.buf, lbad.pb.offset);
168         free(lbad.pb.buf);
169 }
170
171 static int com_lsblob(callback_function *f, struct command_context *cc)
172 {
173         uint32_t flags = 0;
174         struct osl_object options = {.data = &flags, .size = sizeof(flags)};
175         int i;
176
177         for (i = 1; i < cc->argc; i++) {
178                 const char *arg = cc->argv[i];
179                 if (arg[0] != '-')
180                         break;
181                 if (!strcmp(arg, "--")) {
182                         i++;
183                         break;
184                 }
185                 if (!strcmp(arg, "-l")) {
186                         flags |= BLOB_LS_FLAG_LONG;
187                         continue;
188                 }
189                 if (!strcmp(arg, "-i")) {
190                         flags |= BLOB_LS_FLAG_SORT_BY_ID;
191                         continue;
192                 }
193                 if (!strcmp(arg, "-r")) {
194                         flags |= BLOB_LS_FLAG_REVERSE;
195                         continue;
196                 }
197                 break;
198         }
199 //      if (argc > i)
200 //              return -E_BLOB_SYNTAX;
201         return send_option_arg_callback_request(&options, cc->argc - i,
202                 cc->argv + i, f, afs_cb_result_handler, cc);
203 }
204
205 static int cat_blob(struct osl_table *table, struct osl_row *row,
206                 __a_unused const char *name, void *data)
207 {
208         int ret = 0, ret2, fd = *(int *)data;
209         struct osl_object obj;
210
211         ret = osl(osl_open_disk_object(table, row, BLOBCOL_DEF, &obj));
212         if (ret < 0)
213                 return (ret == osl(-E_OSL_EMPTY))? 0 : ret;
214         assert(obj.size > 0);
215         ret = pass_buffer_as_shm(fd, SBD_OUTPUT, obj.data, obj.size);
216         ret2 = osl(osl_close_disk_object(&obj));
217         return (ret < 0)? ret : ret2;
218 }
219
220 static void com_catblob_callback(struct osl_table *table, int fd,
221                 const struct osl_object *query)
222 {
223         struct pattern_match_data pmd = {
224                 .table = table,
225                 .patterns = *query,
226                 .loop_col_num = BLOBCOL_NAME,
227                 .match_col_num = BLOBCOL_NAME,
228                 .pm_flags = PM_SKIP_EMPTY_NAME,
229                 .data = &fd,
230                 .action = cat_blob
231         };
232         for_each_matching_row(&pmd);
233         if (pmd.num_matches == 0) {
234                 char err_msg[] = "no matches\n";
235                 pass_buffer_as_shm(fd, SBD_OUTPUT, err_msg, sizeof(err_msg));
236         }
237 }
238
239 static int com_catblob(callback_function *f, struct command_context *cc)
240 {
241         if (cc->argc < 2)
242                 return -E_BLOB_SYNTAX;
243         return send_standard_callback_request(cc->argc - 1, cc->argv + 1, f,
244                 afs_cb_result_handler, cc);
245 }
246
247 /** Used for removing rows from a blob table. */
248 struct rmblob_data {
249         /** Message buffer. */
250         struct para_buffer pb;
251 };
252
253 static int remove_blob(struct osl_table *table, struct osl_row *row,
254                 const char *name, void *data)
255 {
256         struct rmblob_data *rmbd = data;
257         int ret = osl(osl_del_row(table, row));
258         if (ret < 0) {
259                 para_printf(&rmbd->pb, "%s: %s\n", name, para_strerror(-ret));
260                 return ret;
261         }
262         return 1;
263 }
264
265 static void com_rmblob_callback(struct osl_table *table, int fd,
266                 const struct osl_object *query)
267 {
268         int ret, ret2 = 0;
269         struct rmblob_data rmbd = {
270                 .pb = {
271                         .max_size = shm_get_shmmax(),
272                         .private_data = &(struct afs_max_size_handler_data) {
273                                 .fd = fd,
274                                 .band = SBD_OUTPUT
275                         },
276                         .max_size_handler = afs_max_size_handler,
277                 }
278         };
279         struct pattern_match_data pmd = {
280                 .table = table,
281                 .patterns = *query,
282                 .loop_col_num = BLOBCOL_NAME,
283                 .match_col_num = BLOBCOL_NAME,
284                 .pm_flags = PM_SKIP_EMPTY_NAME,
285                 .data = &rmbd,
286                 .action = remove_blob
287         };
288         ret = for_each_matching_row(&pmd);
289         if (ret < 0) {
290                 ret2 = para_printf(&rmbd.pb, "%s\n", para_strerror(-ret));
291                 if (ret2 < 0)
292                         goto out;
293         }
294         if (pmd.num_matches == 0)
295                 ret2 = para_printf(&rmbd.pb, "no matches, nothing removed\n");
296         else {
297                 ret2 = para_printf(&rmbd.pb, "removed %d blobs\n", pmd.num_matches);
298                 afs_event(BLOB_RENAME, NULL, table);
299         }
300 out:
301         if (ret2 >= 0 && rmbd.pb.offset)
302                 pass_buffer_as_shm(fd, SBD_OUTPUT, rmbd.pb.buf, rmbd.pb.offset);
303         free(rmbd.pb.buf);
304 }
305
306 static int com_rmblob(callback_function *f, struct command_context *cc)
307 {
308         if (cc->argc < 2)
309                 return -E_MOOD_SYNTAX;
310         return send_option_arg_callback_request(NULL, cc->argc - 1, cc->argv + 1, f,
311                 afs_cb_result_handler, cc);
312 }
313
314 static void com_addblob_callback(struct osl_table *table, __a_unused int fd,
315                 const struct osl_object *query)
316 {
317         struct osl_object objs[NUM_BLOB_COLUMNS];
318         char *name = query->data;
319         size_t name_len = strlen(name) + 1;
320         uint32_t id;
321         unsigned num_rows;
322         int ret;
323
324         ret = osl(osl_get_num_rows(table, &num_rows));
325         if (ret < 0)
326                 goto out;
327         if (!num_rows) { /* this is the first entry ever added */
328                 /* insert dummy row containing the id */
329                 id = 2; /* this entry will be entry #1, so 2 is the next */
330                 objs[BLOBCOL_ID].data = &id;
331                 objs[BLOBCOL_ID].size = sizeof(id);
332                 objs[BLOBCOL_NAME].data = "";
333                 objs[BLOBCOL_NAME].size = 1;
334                 objs[BLOBCOL_DEF].data = "";
335                 objs[BLOBCOL_DEF].size = 1;
336                 ret = osl(osl_add_row(table, objs));
337                 if (ret < 0)
338                         goto out;
339         } else {
340                 /* check if name already exists */
341                 struct osl_row *row;
342                 struct osl_object obj = {.data = name, .size = name_len};
343                 ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
344                 if (ret < 0 && ret != -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
345                         goto out;
346                 if (ret >= 0) { /* we already have a blob with this name */
347                         obj.data = name + name_len;
348                         obj.size = query->size - name_len;
349                         ret = osl(osl_update_object(table, row, BLOBCOL_DEF, &obj));
350                         goto out;
351                 }
352                 /* new blob, get id of the dummy row and increment it */
353                 obj.data = "";
354                 obj.size = 1;
355                 ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
356                 if (ret < 0)
357                         goto out;
358                 ret = osl(osl_get_object(table, row, BLOBCOL_ID, &obj));
359                 if (ret < 0)
360                         goto out;
361                 id = *(uint32_t *)obj.data + 1;
362                 obj.data = &id;
363                 ret = osl(osl_update_object(table, row, BLOBCOL_ID, &obj));
364                 if (ret < 0)
365                         goto out;
366         }
367         id--;
368         objs[BLOBCOL_ID].data = &id;
369         objs[BLOBCOL_ID].size = sizeof(id);
370         objs[BLOBCOL_NAME].data = name;
371         objs[BLOBCOL_NAME].size = name_len;
372         objs[BLOBCOL_DEF].data = name + name_len;
373         objs[BLOBCOL_DEF].size = query->size - name_len;
374         ret = osl(osl_add_row(table, objs));
375         if (ret < 0)
376                 goto out;
377         afs_event(BLOB_ADD, NULL, table);
378 out:
379         if (ret < 0)
380                 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
381 }
382
383 /*
384  * write input from fd to dynamically allocated buffer,
385  * but maximal 10M.
386  */
387 static int fd2buf(struct stream_cipher_context *scc, struct osl_object *obj)
388 {
389         const size_t chunk_size = 1024, max_size = 10 * 1024 * 1024;
390         size_t size = 2048, received = 0;
391         int ret;
392         char *buf = para_malloc(size);
393
394         for (;;) {
395                 ret = sc_recv_bin_buffer(scc, buf + received, chunk_size);
396                 if (ret <= 0)
397                         break;
398                 received += ret;
399                 if (received + chunk_size >= size) {
400                         size *= 2;
401                         ret = -E_INPUT_TOO_LARGE;
402                         if (size > max_size)
403                                 break;
404                         buf = para_realloc(buf, size);
405                 }
406         }
407         obj->data = buf;
408         obj->size = received;
409         if (ret < 0)
410                 free(buf);
411         return ret;
412 }
413
414 /*
415  * Read data from a file descriptor, and send it to the afs process.
416  *
417  * \param cc Contains the file descriptor to read data from.
418  * \param arg_obj Pointer to the arguments to \a f.
419  * \param f The callback function.
420  * \param result_handler See \ref send_callback_request.
421  * \param private_result_data See \ref send_callback_request.
422  *
423  * This function is used by the addblob commands that instruct para_server to
424  * store arbitrary data in a blob table. Input data is read and decrypted from
425  * the file descriptor given by \a cc. This data is concatenated with the
426  * buffer given by \a arg_obj, and the combined buffer is made available to the
427  * afs process via the callback method. See \ref send_callback_request for
428  * details.
429  *
430  * \return Negative on errors, the return value of the underlying call to
431  * send_callback_request() otherwise.
432  */
433 static int stdin_command(struct command_context *cc, struct osl_object *arg_obj,
434                 callback_function *f, callback_result_handler *result_handler,
435                 void *private_result_data)
436 {
437         struct osl_object query, stdin_obj;
438         int ret;
439
440         ret = send_sb(&cc->scc, NULL, 0, SBD_AWAITING_DATA, false);
441         if (ret < 0)
442                 return ret;
443         ret = fd2buf(&cc->scc, &stdin_obj);
444         if (ret < 0)
445                 return ret;
446         query.size = arg_obj->size + stdin_obj.size;
447         query.data = para_malloc(query.size);
448         memcpy(query.data, arg_obj->data, arg_obj->size);
449         if (stdin_obj.size > 0)
450                 memcpy((char *)query.data + arg_obj->size, stdin_obj.data,
451                         stdin_obj.size);
452         free(stdin_obj.data);
453         ret = send_callback_request(f, &query, result_handler, private_result_data);
454         free(query.data);
455         return ret;
456 }
457
458 static int com_addblob(callback_function *f, struct command_context *cc)
459 {
460         struct osl_object arg_obj;
461
462         if (cc->argc != 2)
463                 return -E_BLOB_SYNTAX;
464         if (!*cc->argv[1]) /* empty name is reserved for the dummy row */
465                 return -E_BLOB_SYNTAX;
466         arg_obj.size = strlen(cc->argv[1]) + 1;
467         arg_obj.data = (char *)cc->argv[1];
468         return stdin_command(cc, &arg_obj, f, NULL, NULL);
469 }
470
471 /* FIXME: Print output to client, not to log file */
472 static void com_mvblob_callback(struct osl_table *table, __a_unused int fd,
473                 const struct osl_object *query)
474 {
475         char *src = (char *) query->data;
476         struct osl_object obj = {.data = src, .size = strlen(src) + 1};
477         char *dest = src + obj.size;
478         struct osl_row *row;
479         int ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
480
481         if (ret < 0)
482                 goto out;
483         obj.data = dest;
484         obj.size = strlen(dest) + 1;
485         ret = osl(osl_update_object(table, row, BLOBCOL_NAME, &obj));
486         if (ret < 0)
487                 goto out;
488         afs_event(BLOB_RENAME, NULL, table);
489 out:
490         if (ret < 0)
491                 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
492 }
493
494 static int com_mvblob(callback_function *f, struct command_context *cc)
495 {
496         if (cc->argc != 3)
497                 return -E_MOOD_SYNTAX;
498         return send_option_arg_callback_request(NULL, cc->argc - 1,
499                 cc->argv + 1, f, NULL, NULL);
500 }
501
502 #define DEFINE_BLOB_COMMAND(cmd_name, table_name, cmd_prefix) \
503         static void com_ ## cmd_name ## cmd_prefix ## _callback(int fd, const struct osl_object *query) \
504         { \
505                 return com_ ## cmd_name ## blob_callback(table_name ## _table, fd, query); \
506         } \
507         int com_ ## cmd_name ## cmd_prefix(struct command_context *cc) \
508         { \
509                 return com_ ## cmd_name ## blob(com_ ## cmd_name ## cmd_prefix ## _callback, cc); \
510         }
511
512 static int blob_get_name_by_id(struct osl_table *table, uint32_t id,
513                 char **name)
514 {
515         struct osl_row *row;
516         struct osl_object obj = {.data = &id, .size = sizeof(id)};
517         int ret;
518
519         *name = NULL;
520         if (!id)
521                 return 1;
522         ret = osl(osl_get_row(table, BLOBCOL_ID, &obj, &row));
523         if (ret < 0)
524                 return ret;
525         ret = osl(osl_get_object(table, row, BLOBCOL_NAME, &obj));
526         if (ret < 0)
527                 return ret;
528         *name = (char *)obj.data;
529         return 1;
530 }
531
532 /** Define the \p get_name_by_id function for this blob type. */
533 #define DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix) \
534         int cmd_prefix ## _get_name_by_id(uint32_t id, char **name) \
535         { \
536                 return blob_get_name_by_id(table_name ## _table, id, name); \
537         }
538
539
540 static int blob_get_def_by_name(struct osl_table *table, char *name,
541                 struct osl_object *def)
542 {
543         struct osl_row *row;
544         struct osl_object obj = {.data = name, .size = strlen(name) + 1};
545         int ret;
546
547         def->data = NULL;
548         if (!*name)
549                 return 1;
550         ret = osl(osl_get_row(table, BLOBCOL_NAME, &obj, &row));
551         if (ret < 0)
552                 return ret;
553         return osl(osl_open_disk_object(table, row, BLOBCOL_DEF, def));
554 }
555
556 /** Define the \p get_def_by_id function for this blob type. */
557 #define DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix) \
558         int cmd_prefix ## _get_def_by_name(char *name, struct osl_object *def) \
559         { \
560                 return blob_get_def_by_name(table_name ## _table, name, def); \
561         }
562
563 static int blob_get_def_by_id(struct osl_table *table, uint32_t id,
564                 struct osl_object *def)
565 {
566         struct osl_row *row;
567         struct osl_object obj = {.data = &id, .size = sizeof(id)};
568         int ret;
569
570         def->data = NULL;
571         if (!id)
572                 return 1;
573         ret = osl(osl_get_row(table, BLOBCOL_ID, &obj, &row));
574         if (ret < 0)
575                 return ret;
576         return osl(osl_open_disk_object(table, row, BLOBCOL_DEF, def));
577 }
578
579 /** Define the \p get_def_by_id function for this blob type. */
580 #define DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix) \
581         int cmd_prefix ## _get_def_by_id(uint32_t id, struct osl_object *def) \
582         { \
583                 return blob_get_def_by_id(table_name ## _table, id, def); \
584         }
585
586 static int blob_get_name_and_def_by_row(struct osl_table *table,
587                 const struct osl_row *row, char **name, struct osl_object *def)
588 {
589         struct osl_object obj;
590         int ret = osl(osl_get_object(table, row, BLOBCOL_NAME, &obj));
591         if (ret < 0)
592                 return ret;
593         *name = obj.data;
594         return osl(osl_open_disk_object(table, row, BLOBCOL_DEF, def));
595 }
596 /** Define the \p get_name_and_def_by_row function for this blob type. */
597 #define DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix) \
598         int cmd_prefix ## _get_name_and_def_by_row(const struct osl_row *row, \
599                 char **name, struct osl_object *def) \
600         { \
601                 return blob_get_name_and_def_by_row(table_name ## _table, \
602                         row, name, def); \
603         }
604
605 /** Define the \p close function for this blob type. */
606 #define DEFINE_BLOB_CLOSE(table_name) \
607         static void table_name ## _close(void) \
608         { \
609                 osl_close_table(table_name ## _table, OSL_MARK_CLEAN); \
610                 table_name ## _table = NULL; \
611         }
612
613 /** Define the \p create function for this blob type. */
614 #define DEFINE_BLOB_CREATE(table_name) \
615         static int table_name ## _create(const char *dir) \
616         { \
617                 table_name ## _table_desc.dir = dir; \
618                 return osl_create_table(&table_name ## _table_desc); \
619         }
620
621 static int blob_open(struct osl_table **table,
622                 struct osl_table_description *desc,
623                 const char *dir)
624 {
625         int ret;
626         desc->dir = dir;
627         ret = osl(osl_open_table(desc, table));
628         if (ret >= 0)
629                 return ret;
630         *table = NULL;
631         if (ret >= 0 || ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT))
632                 return 1;
633         return ret;
634 }
635
636 #define DEFINE_BLOB_OPEN(table_name) \
637         static int table_name ## _open(const char *dir) \
638         { \
639                 return blob_open(&table_name ## _table, \
640                         &table_name ## _table_desc, dir); \
641         }
642
643
644 /** Define the \p init function for this blob type. */
645 #define DEFINE_BLOB_INIT(table_name) \
646         void table_name ## _init(struct afs_table *t) \
647         { \
648                 t->open = table_name ## _open; \
649                 t->close = table_name ## _close; \
650                 t->create = table_name ## _create;\
651                 t->event_handler = table_name ##_event_handler; \
652                 table_name ## _table = NULL; \
653         }
654
655
656 /** Define all functions for this blob type. */
657 #define DEFINE_BLOB_FUNCTIONS(table_name, cmd_prefix) \
658         DEFINE_BLOB_OPEN(table_name) \
659         DEFINE_BLOB_CLOSE(table_name) \
660         DEFINE_BLOB_CREATE(table_name) \
661         DEFINE_BLOB_INIT(table_name) \
662         DEFINE_BLOB_COMMAND(ls, table_name, cmd_prefix) \
663         DEFINE_BLOB_COMMAND(cat, table_name, cmd_prefix) \
664         DEFINE_BLOB_COMMAND(add, table_name, cmd_prefix) \
665         DEFINE_BLOB_COMMAND(rm, table_name, cmd_prefix) \
666         DEFINE_BLOB_COMMAND(mv, table_name, cmd_prefix) \
667         DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix); \
668         DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix); \
669         DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix); \
670         DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix); \
671
672 /* doxygen isn't smart enough to recognize these */
673 /** \cond blob_function */
674 DEFINE_BLOB_FUNCTIONS(lyrics, lyr);
675 DEFINE_BLOB_FUNCTIONS(images, img);
676 DEFINE_BLOB_FUNCTIONS(moods, mood);
677 DEFINE_BLOB_FUNCTIONS(playlists, pl);
678 /** \endcond blob_function */