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