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