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