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