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