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