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