Small cleanups and documentation improvements.
[paraslash.git] / blob.c
1 /*
2  * Copyright (C) 2007 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 <fnmatch.h>
10 #include "para.h"
11 #include "error.h"
12 #include "string.h"
13 #include "afh.h"
14 #include "afs.h"
15 #include "net.h"
16
17 static struct osl_column_description blob_cols[] = {
18         [BLOBCOL_ID] = {
19                 .storage_type = OSL_MAPPED_STORAGE,
20                 .storage_flags = OSL_RBTREE | OSL_UNIQUE | OSL_FIXED_SIZE,
21                 .name = "id",
22                 .data_size = 4,
23                 .compare_function = uint32_compare
24         },
25         [BLOBCOL_NAME] = {
26                 .storage_type = OSL_MAPPED_STORAGE,
27                 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
28                 .name = "name",
29                 .compare_function = string_compare
30         },
31         [BLOBCOL_DEF] = {
32                 .storage_type = OSL_DISK_STORAGE,
33                 .storage_flags = 0,
34                 .name = "definition"
35         }
36 };
37
38 /** \cond doxygen isn't smart enough to recognize these */
39 INIT_BLOB_TABLE(lyrics);
40 INIT_BLOB_TABLE(images);
41 INIT_BLOB_TABLE(moods);
42 INIT_BLOB_TABLE(playlists);
43 /** \endcond */
44
45 /** Flags that may be passed to the \p ls functions of each blob  type. */
46 enum blob_ls_flags {
47         /** List both id and name. */
48         BLOB_LS_FLAG_LONG = 1,
49         /** Reverse sort order. */
50         BLOB_LS_FLAG_REVERSE = 2,
51         /** Sort by id instead of name. */
52         BLOB_LS_FLAG_SORT_BY_ID = 4,
53 };
54
55 /** Structure passed to the \p print_blob function. */
56 struct lsblob_action_data {
57         uint32_t flags;
58         struct para_buffer pb;
59 };
60
61 static int print_blob(struct osl_table *table, struct osl_row *row,
62                 const char *name, void *data)
63 {
64         struct lsblob_action_data *lbad = data;
65         struct osl_object obj;
66         uint32_t id;
67         int ret;
68
69         if (!(lbad->flags & BLOB_LS_FLAG_LONG)) {
70                 para_printf(&lbad->pb, "%s\n", name);
71                 return 1;
72         }
73         ret = osl_get_object(table, row, BLOBCOL_ID, &obj);
74         if (ret < 0) {
75                 para_printf(&lbad->pb, "%s: %s\n", name, PARA_STRERROR(-ret));
76                 return ret;
77         }
78         id = *(uint32_t *)obj.data;
79         para_printf(&lbad->pb, "%u\t%s\n", id, name);
80         return 1;
81 }
82
83 static int com_lsblob_callback(struct osl_table *table,
84                 const struct osl_object *query, struct osl_object *result)
85 {
86         struct lsblob_action_data lbad = {.flags = *(uint32_t *)query->data};
87         struct pattern_match_data pmd = {
88                 .table = table,
89                 .patterns = {.data = (char *)query->data + sizeof(uint32_t),
90                         .size = query->size - sizeof(uint32_t)},
91                 .pm_flags = PM_NO_PATTERN_MATCHES_EVERYTHING | PM_SKIP_EMPTY_NAME,
92                 .match_col_num = BLOBCOL_NAME,
93                 .data = &lbad,
94                 .action = print_blob,
95         };
96         int ret;
97
98         if (lbad.flags & BLOB_LS_FLAG_REVERSE)
99                 pmd.pm_flags |= PM_REVERSE_LOOP;
100         if (!(lbad.flags & BLOB_LS_FLAG_SORT_BY_ID))
101                 pmd.loop_col_num = BLOBCOL_ID;
102         else
103                 pmd.loop_col_num = BLOBCOL_NAME;
104         ret = for_each_matching_row(&pmd);
105         if (ret < 0)
106                 para_printf(&lbad.pb, "%s\n", PARA_STRERROR(-ret));
107         if (!lbad.pb.buf)
108                 return 0;
109         result->data = lbad.pb.buf;
110         result->size = lbad.pb.size;
111         return 1;
112 }
113
114 static int com_lsblob(callback_function *f, int fd, int argc, char * const * const argv)
115 {
116         uint32_t flags = 0;
117         struct osl_object options = {.data = &flags, .size = sizeof(flags)},
118                 result;
119         int i, ret;
120
121         for (i = 1; i < argc; i++) {
122                 const char *arg = argv[i];
123                 if (arg[0] != '-')
124                         break;
125                 if (!strcmp(arg, "--")) {
126                         i++;
127                         break;
128                 }
129                 if (!strcmp(arg, "-l")) {
130                         flags |= BLOB_LS_FLAG_LONG;
131                         continue;
132                 }
133                 if (!strcmp(arg, "-i")) {
134                         flags |= BLOB_LS_FLAG_SORT_BY_ID;
135                         continue;
136                 }
137                 if (!strcmp(arg, "-r")) {
138                         flags |= BLOB_LS_FLAG_REVERSE;
139                         continue;
140                 }
141                 break;
142         }
143 //      if (argc > i)
144 //              return -E_BLOB_SYNTAX;
145         ret = send_option_arg_callback_request(&options, argc - i,
146                 argv + i, f, &result);
147         if (ret > 0) {
148                 send_buffer(fd, (char *)result.data);
149                 free(result.data);
150         }
151         return ret;
152 }
153
154 static int cat_blob(struct osl_table *table, struct osl_row *row,
155                 __a_unused const char *name, void *data)
156 {
157         int ret;
158         struct osl_object *blobs = data;
159         struct osl_object obj;
160
161         ret = osl_open_disk_object(table, row, BLOBCOL_DEF, &obj);
162         if (ret < 0)
163                 return ret;
164         if (obj.size) {
165                 blobs->data = para_realloc(blobs->data, blobs->size + obj.size);
166                 memcpy(blobs->data + blobs->size, obj.data, obj.size);
167                 blobs->size += obj.size;
168         }
169         return osl_close_disk_object(&obj);
170 }
171
172 static int com_catblob_callback(struct osl_table *table,
173                 const struct osl_object *query, struct osl_object *result)
174 {
175         int ret;
176         struct pattern_match_data pmd = {
177                 .table = table,
178                 .patterns = *query,
179                 .loop_col_num = BLOBCOL_NAME,
180                 .match_col_num = BLOBCOL_NAME,
181                 .pm_flags = PM_SKIP_EMPTY_NAME,
182                 .data = result,
183                 .action = cat_blob
184         };
185         result->data = NULL;
186         ret = for_each_matching_row(&pmd);
187         if (result->data)
188                 return 1;
189         return (ret > 0)? 0 : ret;
190 }
191
192 static int com_catblob(callback_function *f, int fd, int argc,
193                 char * const * const argv)
194 {
195         struct osl_object result;
196         int ret;
197
198         if (argc < 2)
199                 return -E_BLOB_SYNTAX;
200         ret = send_standard_callback_request(argc - 1, argv + 1, f, &result);
201         if (ret > 0) {
202                 ret = send_bin_buffer(fd, (char *)result.data, result.size);
203                 free(result.data);
204         }
205         return ret;
206 }
207
208 struct rmblob_data {
209         struct para_buffer pb;
210         unsigned num_removed;
211 };
212
213 static int remove_blob(struct osl_table *table, struct osl_row *row,
214                 const char *name, void *data)
215 {
216         struct rmblob_data *rmbd = data;
217         int ret = osl_del_row(table, row);
218         if (ret < 0) {
219                 para_printf(&rmbd->pb, "%s: %s\n", name, PARA_STRERROR(-ret));
220                 return ret;
221         }
222         rmbd->num_removed++;
223         return 1; /* return success to remove other matching blobs. */
224 }
225
226 static int com_rmblob_callback(struct osl_table *table,
227                 const struct osl_object *query,
228                 struct osl_object *result)
229 {
230         int ret;
231         struct rmblob_data rmbd = {.num_removed = 0};
232         struct pattern_match_data pmd = {
233                 .table = table,
234                 .patterns = *query,
235                 .loop_col_num = BLOBCOL_NAME,
236                 .match_col_num = BLOBCOL_NAME,
237                 .pm_flags = PM_SKIP_EMPTY_NAME,
238                 .data = &rmbd,
239                 .action = remove_blob
240         };
241         result->data = NULL;
242         ret = for_each_matching_row(&pmd);
243         if (ret < 0)
244                 para_printf(&rmbd.pb, "%s\n", PARA_STRERROR(-ret));
245         if (!rmbd.num_removed)
246                 para_printf(&rmbd.pb, "no matches, nothing removed\n");
247         else {
248                 para_printf(&rmbd.pb, "removed %d blobs\n", rmbd.num_removed);
249                 afs_event(BLOB_RENAME, NULL, table);
250         }
251         result->data = rmbd.pb.buf;
252         result->size = rmbd.pb.size;
253         return 1;
254 }
255
256 static int com_rmblob(callback_function *f, int fd, int argc,
257                 char * const * const argv)
258 {
259         int ret;
260         struct osl_object result;
261
262         if (argc < 2)
263                 return -E_MOOD_SYNTAX;
264         ret = send_option_arg_callback_request(NULL, argc - 1, argv + 1, f,
265                 &result);
266         if (ret > 0) {
267                 send_buffer(fd, (char *)result.data);
268                 free(result.data);
269         }
270         return ret;
271 }
272
273 static int com_addblob_callback(struct osl_table *table,
274                 const struct osl_object *query,
275                 __a_unused struct osl_object *result)
276 {
277         struct osl_object objs[NUM_BLOB_COLUMNS];
278         char *name = query->data;
279         size_t name_len = strlen(name) + 1;
280         uint32_t id;
281         unsigned num_rows;
282         int ret;
283
284         ret = osl_get_num_rows(table, &num_rows);
285         if (ret < 0)
286                 return ret;
287         if (!num_rows) { /* this is the first entry ever added */
288                 /* insert dummy row containing the id */
289                 id = 2; /* this entry will be entry #1, so 2 is the next */
290                 objs[BLOBCOL_ID].data = &id;
291                 objs[BLOBCOL_ID].size = sizeof(id);
292                 objs[BLOBCOL_NAME].data = "";
293                 objs[BLOBCOL_NAME].size = 1;
294                 objs[BLOBCOL_DEF].data = "";
295                 objs[BLOBCOL_DEF].size = 1;
296                 ret = osl_add_row(table, objs);
297                 if (ret < 0)
298                         return ret;
299         } else {
300                 /* check if name already exists */
301                 struct osl_row *row;
302                 struct osl_object obj = {.data = name, .size = name_len};
303                 ret = osl_get_row(table, BLOBCOL_NAME, &obj, &row);
304                 if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
305                         return ret;
306                 if (ret >= 0) { /* we already have a blob with this name */
307                         obj.data = name + name_len;
308                         obj.size = query->size - name_len;
309                         return osl_update_object(table, row, BLOBCOL_DEF, &obj);
310                 }
311                 /* new blob, get id of the dummy row and increment it */
312                 obj.data = "";
313                 obj.size = 1;
314                 ret = osl_get_row(table, BLOBCOL_NAME, &obj, &row);
315                 if (ret < 0)
316                         return ret;
317                 ret = osl_get_object(table, row, BLOBCOL_ID, &obj);
318                 if (ret < 0)
319                         return ret;
320                 id = *(uint32_t *)obj.data + 1;
321                 obj.data = &id;
322                 ret = osl_update_object(table, row, BLOBCOL_ID, &obj);
323                 if (ret < 0)
324                         return ret;
325         }
326         id--;
327         objs[BLOBCOL_ID].data = &id;
328         objs[BLOBCOL_ID].size = sizeof(id);
329         objs[BLOBCOL_NAME].data = name;
330         objs[BLOBCOL_NAME].size = name_len;
331         objs[BLOBCOL_DEF].data = name + name_len;
332         objs[BLOBCOL_DEF].size = query->size - name_len;
333         ret = osl_add_row(table, objs);
334         if (ret < 0)
335                 return ret;
336         afs_event(BLOB_ADD, NULL, table);
337         return 1;
338 }
339
340 static int com_addblob(callback_function *f, int fd, int argc,
341                 char * const * const argv)
342 {
343         struct osl_object arg_obj;
344
345         if (argc != 2)
346                 return -E_BLOB_SYNTAX;
347         if (!*argv[1]) /* empty name is reserved for the dummy row */
348                 return -E_BLOB_SYNTAX;
349         PARA_NOTICE_LOG("argv[1]: %s\n", argv[1]);
350         arg_obj.size = strlen(argv[1]) + 1;
351         arg_obj.data = (char *)argv[1];
352         return stdin_command(fd, &arg_obj, f, 10 * 1024 * 1024, NULL);
353 }
354
355 static int com_mvblob_callback(struct osl_table *table,
356                 const struct osl_object *query,
357                 __a_unused struct osl_object *result)
358 {
359         char *src = (char *) query->data;
360         struct osl_object obj = {.data = src, .size = strlen(src) + 1};
361         char *dest = src + obj.size;
362         struct osl_row *row;
363         int ret = osl_get_row(table, BLOBCOL_NAME, &obj, &row);
364
365         if (ret < 0)
366                 return ret;
367         obj.data = dest;
368         obj.size = strlen(dest) + 1;
369         ret = osl_update_object(table, row, BLOBCOL_NAME, &obj);
370         if (ret < 0)
371                 return ret;
372         afs_event(BLOB_RENAME, NULL, table);
373         return 1;
374 }
375
376 static int com_mvblob(callback_function *f, __a_unused int fd,
377                 int argc, char * const * const argv)
378 {
379         if (argc != 3)
380                 return -E_MOOD_SYNTAX;
381         return send_option_arg_callback_request(NULL, argc - 1, argv + 1, f,
382                 NULL);
383 }
384
385 #define DEFINE_BLOB_COMMAND(cmd_name, table_name, cmd_prefix) \
386         static int com_ ## cmd_name ## cmd_prefix ## _callback(const struct osl_object *query, \
387                         struct osl_object *output) \
388         { \
389                 return com_ ## cmd_name ## blob_callback(table_name ## _table, query, output); \
390         } \
391         int com_ ## cmd_name ## cmd_prefix(int fd, int argc, char * const * const argv) \
392         { \
393                 return com_ ## cmd_name ## blob(com_ ## cmd_name ## cmd_prefix ## _callback, fd, argc, argv); \
394         }
395
396 static int blob_get_name_by_id(struct osl_table *table, uint32_t id,
397                 char **name)
398 {
399         struct osl_row *row;
400         struct osl_object obj = {.data = &id, .size = sizeof(id)};
401         int ret;
402
403         *name = NULL;
404         if (!id)
405                 return 1;
406         ret = osl_get_row(table, BLOBCOL_ID, &obj, &row);
407         if (ret < 0)
408                 return ret;
409         ret = osl_get_object(table, row, BLOBCOL_NAME, &obj);
410         if (ret < 0)
411                 return ret;
412         *name = (char *)obj.data;
413         return 1;
414 }
415
416 /** Define the \p get_name_by_id function for this blob type. */
417 #define DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix) \
418         int cmd_prefix ## _get_name_by_id(uint32_t id, char **name) \
419         { \
420                 return blob_get_name_by_id(table_name ## _table, id, name); \
421         }
422
423
424 static int blob_get_def_by_name(struct osl_table *table, char *name,
425                 struct osl_object *def)
426 {
427         struct osl_row *row;
428         struct osl_object obj = {.data = name, .size = strlen(name) + 1};
429         int ret;
430
431         def->data = NULL;
432         if (!*name)
433                 return 1;
434         ret = osl_get_row(table, BLOBCOL_NAME, &obj, &row);
435         if (ret < 0)
436                 return ret;
437         return osl_open_disk_object(table, row, BLOBCOL_DEF, def);
438 }
439
440 /** Define the \p get_def_by_id function for this blob type. */
441 #define DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix) \
442         int cmd_prefix ## _get_def_by_name(char *name, struct osl_object *def) \
443         { \
444                 return blob_get_def_by_name(table_name ## _table, name, def); \
445         }
446
447 static int blob_get_def_by_id(struct osl_table *table, uint32_t id,
448                 struct osl_object *def)
449 {
450         struct osl_row *row;
451         struct osl_object obj = {.data = &id, .size = sizeof(id)};
452         int ret;
453
454         def->data = NULL;
455         if (!id)
456                 return 1;
457         ret = osl_get_row(table, BLOBCOL_ID, &obj, &row);
458         if (ret < 0)
459                 return ret;
460         return osl_open_disk_object(table, row, BLOBCOL_DEF, def);
461 }
462
463 /** Define the \p get_def_by_id function for this blob type. */
464 #define DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix) \
465         int cmd_prefix ## _get_def_by_id(uint32_t id, struct osl_object *def) \
466         { \
467                 return blob_get_def_by_id(table_name ## _table, id, def); \
468         }
469
470 static int blob_get_name_and_def_by_row(struct osl_table *table,
471                 const struct osl_row *row, char **name, struct osl_object *def)
472 {
473         struct osl_object obj;
474         int ret = osl_get_object(table, row, BLOBCOL_NAME, &obj);
475         if (ret < 0)
476                 return ret;
477         *name = obj.data;
478         return osl_open_disk_object(table, row, BLOBCOL_DEF, def);
479 }
480 /** Define the \p get_name_and_def_by_row function for this blob type. */
481 #define DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix) \
482         int cmd_prefix ## _get_name_and_def_by_row(const struct osl_row *row, \
483                 char **name, struct osl_object *def) \
484         { \
485                 return blob_get_name_and_def_by_row(table_name ## _table, \
486                         row, name, def); \
487         }
488
489 /** Define the \p close function for this blob type. */
490 #define DEFINE_BLOB_CLOSE(table_name) \
491         void table_name ## _close(void) \
492         { \
493                 osl_close_table(table_name ## _table, OSL_MARK_CLEAN); \
494                 table_name ## _table = NULL; \
495         }
496
497 /** Define the \p create function for this blob type. */
498 #define DEFINE_BLOB_CREATE(table_name) \
499         int table_name ## _create(const char *dir) \
500         { \
501                 table_name ## _table_desc.dir = dir; \
502                 return osl_create_table(&table_name ## _table_desc); \
503         }
504
505 static int blob_open(struct osl_table **table,
506                 struct osl_table_description *desc,
507                 const char *dir)
508 {
509         int ret;
510         desc->dir = dir;
511         ret = osl_open_table(desc, table);
512         if (ret >= 0)
513                 return ret;
514         *table = NULL;
515         if (ret >= 0 || is_errno(-ret, ENOENT))
516                 return 1;
517         return ret;
518 }
519
520 #define DEFINE_BLOB_OPEN(table_name) \
521         int table_name ## _open(const char *dir) \
522         { \
523                 return blob_open(&table_name ## _table, \
524                         &table_name ## _table_desc, dir); \
525         }
526
527
528 /** Define the \p init function for this blob type. */
529 #define DEFINE_BLOB_INIT(table_name) \
530         void table_name ## _init(struct afs_table *t) \
531         { \
532                 t->name = table_name ## _table_desc.name; \
533                 t->open = table_name ## _open; \
534                 t->close = table_name ## _close; \
535                 t->create = table_name ## _create;\
536                 t->event_handler = table_name ##_event_handler; \
537                 table_name ## _table = NULL; \
538         }
539
540
541 /** Define all functions for this blob type. */
542 #define DEFINE_BLOB_FUNCTIONS(table_name, cmd_prefix) \
543         DEFINE_BLOB_OPEN(table_name) \
544         DEFINE_BLOB_CLOSE(table_name) \
545         DEFINE_BLOB_CREATE(table_name) \
546         DEFINE_BLOB_INIT(table_name) \
547         DEFINE_BLOB_COMMAND(ls, table_name, cmd_prefix) \
548         DEFINE_BLOB_COMMAND(cat, table_name, cmd_prefix) \
549         DEFINE_BLOB_COMMAND(add, table_name, cmd_prefix) \
550         DEFINE_BLOB_COMMAND(rm, table_name, cmd_prefix) \
551         DEFINE_BLOB_COMMAND(mv, table_name, cmd_prefix) \
552         DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix); \
553         DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix); \
554         DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix); \
555         DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix); \
556
557 /** \cond doxygen isn't smart enough to recognize these */
558 DEFINE_BLOB_FUNCTIONS(lyrics, lyr);
559 DEFINE_BLOB_FUNCTIONS(images, img);
560 DEFINE_BLOB_FUNCTIONS(moods, mood);
561 DEFINE_BLOB_FUNCTIONS(playlists, pl);
562 /** \endcond */