74b1c4f5a3bbe8cec2d6df5ebcac477025bb9c18
[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 { /* get id of the dummy row and increment it */
340 struct osl_row *row;
341 struct osl_object obj = {.data = "", .size = 1};
342 ret = osl_get_row(table, BLOBCOL_NAME, &obj, &row);
343 if (ret < 0)
344 return ret;
345 ret = osl_get_object(table, row, BLOBCOL_ID, &obj);
346 if (ret < 0)
347 return ret;
348 id = *(uint32_t *)obj.data + 1;
349 obj.data = &id;
350 ret = osl_update_object(table, row, BLOBCOL_ID, &obj);
351 if (ret < 0)
352 return ret;
353 }
354 id--;
355 objs[BLOBCOL_ID].data = &id;
356 objs[BLOBCOL_ID].size = sizeof(id);
357 objs[BLOBCOL_NAME].data = name;
358 objs[BLOBCOL_NAME].size = name_len;
359 objs[BLOBCOL_DEF].data = name + name_len;
360 objs[BLOBCOL_DEF].size = query->size - name_len;
361 return osl_add_row(table, objs);
362 }
363
364 static int com_addblob(callback_function *f, int fd, int argc,
365 char * const * const argv)
366 {
367 struct osl_object arg_obj;
368
369 if (argc != 2)
370 return -E_BLOB_SYNTAX;
371 if (!*argv[1]) /* empty name is reserved for the dummy row */
372 return -E_BLOB_SYNTAX;
373 PARA_NOTICE_LOG("argv[1]: %s\n", argv[1]);
374 arg_obj.size = strlen(argv[1]) + 1;
375 arg_obj.data = (char *)argv[1];
376 return stdin_command(fd, &arg_obj, f, 10 * 1024 * 1024, NULL);
377 }
378
379 static int com_mvblob_callback(struct osl_table *table,
380 const struct osl_object *query,
381 __a_unused struct osl_object *result)
382 {
383 char *src = (char *) query->data;
384 struct osl_object obj = {.data = src, .size = strlen(src) + 1};
385 char *dest = src + obj.size;
386 struct osl_row *row;
387 int ret = osl_get_row(table, BLOBCOL_NAME, &obj, &row);
388
389 if (ret < 0)
390 return ret;
391 obj.data = dest;
392 obj.size = strlen(dest) + 1;
393 return osl_update_object(table, row, BLOBCOL_NAME, &obj);
394 }
395
396 static int com_mvblob(callback_function *f, __a_unused int fd,
397 int argc, char * const * const argv)
398 {
399 if (argc != 3)
400 return -E_MOOD_SYNTAX;
401 return send_option_arg_callback_request(NULL, argc - 1, argv + 1, f,
402 NULL);
403 }
404
405 #define DEFINE_BLOB_COMMAND(cmd_name, table_name, cmd_prefix) \
406 static int com_ ## cmd_name ## cmd_prefix ## _callback(const struct osl_object *query, \
407 struct osl_object *output) \
408 { \
409 return com_ ## cmd_name ## blob_callback(table_name ## _table, query, output); \
410 } \
411 int com_ ## cmd_name ## cmd_prefix(int fd, int argc, char * const * const argv) \
412 { \
413 return com_ ## cmd_name ## blob(com_ ## cmd_name ## cmd_prefix ## _callback, fd, argc, argv); \
414 }
415
416 static int blob_get_name_by_id(struct osl_table *table, uint32_t id,
417 char **name)
418 {
419 struct osl_row *row;
420 struct osl_object obj = {.data = &id, .size = sizeof(id)};
421 int ret;
422
423 *name = NULL;
424 if (!id)
425 return 1;
426 ret = osl_get_row(table, BLOBCOL_ID, &obj, &row);
427 if (ret < 0)
428 return ret;
429 ret = osl_get_object(table, row, BLOBCOL_NAME, &obj);
430 if (ret < 0)
431 return ret;
432 *name = (char *)obj.data;
433 return 1;
434 }
435
436 /** Define the \p get_name_by_id function for this blob type. */
437 #define DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix) \
438 int cmd_prefix ## _get_name_by_id(uint32_t id, char **name) \
439 { \
440 return blob_get_name_by_id(table_name ## _table, id, name); \
441 }
442
443 static int blob_get_name_and_def_by_row(struct osl_table *table,
444 const struct osl_row *row, char **name, struct osl_object *def)
445 {
446 struct osl_object obj;
447 int ret = osl_get_object(table, row, BLOBCOL_NAME, &obj);
448 if (ret < 0)
449 return ret;
450 *name = obj.data;
451 return osl_open_disk_object(table, row, BLOBCOL_DEF, def);
452 }
453 /** Define the \p get_name_and_def_by_row function for this blob type. */
454 #define DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix) \
455 int cmd_prefix ## _get_name_and_def_by_row(const struct osl_row *row, \
456 char **name, struct osl_object *def) \
457 { \
458 return blob_get_name_and_def_by_row(table_name ## _table, \
459 row, name, def); \
460 }
461
462 /** Define the \p shutdown function for this blob type. */
463 #define DEFINE_BLOB_SHUTDOWN(table_name) \
464 void table_name ## _shutdown(enum osl_close_flags flags) \
465 { \
466 osl_close_table(table_name ## _table, flags); \
467 table_name ## _table = NULL; \
468 }
469
470 static int blob_init(struct osl_table **table,
471 struct osl_table_description *desc,
472 struct table_info *ti, const char *db)
473 {
474 int ret;
475 desc->dir = db;
476 ti->desc = desc;
477 ret = osl_open_table(ti->desc, &ti->table);
478 if (ret >= 0) {
479 *table = ti->table;
480 return ret;
481 }
482 *table = NULL;
483 return ret == -E_NOENT? 1 : ret;
484 }
485
486 /** Define the \p init function for this blob type. */
487 #define DEFINE_BLOB_INIT(table_name) \
488 int table_name ## _init(struct table_info *ti, const char *db) \
489 { \
490 return blob_init(&table_name ## _table, \
491 &table_name ## _table_desc, ti, db); \
492 }
493
494
495 /** Define all functions for this blob type. */
496 #define DEFINE_BLOB_FUNCTIONS(table_name, cmd_prefix) \
497 DEFINE_BLOB_COMMAND(ls, table_name, cmd_prefix) \
498 DEFINE_BLOB_COMMAND(cat, table_name, cmd_prefix) \
499 DEFINE_BLOB_COMMAND(add, table_name, cmd_prefix) \
500 DEFINE_BLOB_COMMAND(rm, table_name, cmd_prefix) \
501 DEFINE_BLOB_COMMAND(mv, table_name, cmd_prefix) \
502 DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix); \
503 DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix); \
504 DEFINE_BLOB_SHUTDOWN(table_name); \
505 DEFINE_BLOB_INIT(table_name);
506
507 /** \cond doxygen isn't smart enough to recognize these */
508 DEFINE_BLOB_FUNCTIONS(lyrics, lyr);
509 DEFINE_BLOB_FUNCTIONS(images, img);
510 DEFINE_BLOB_FUNCTIONS(moods, mood);
511 DEFINE_BLOB_FUNCTIONS(playlists, pl);
512 /** \endcond */