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