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