2 * Copyright (C) 2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file blob.c Macros and functions for blob handling. */
17 static struct osl_column_description blob_cols
[] = {
19 .storage_type
= OSL_MAPPED_STORAGE
,
20 .storage_flags
= OSL_RBTREE
| OSL_UNIQUE
| OSL_FIXED_SIZE
,
23 .compare_function
= uint32_compare
26 .storage_type
= OSL_MAPPED_STORAGE
,
27 .storage_flags
= OSL_RBTREE
| OSL_UNIQUE
,
29 .compare_function
= string_compare
32 .storage_type
= OSL_DISK_STORAGE
,
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
);
45 /** Flags that may be passed to the \p ls functions of each blob type. */
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,
55 /** Structure passed to the \p print_blob function. */
58 struct para_buffer pb
;
61 static int print_blob(struct osl_table
*table
, struct osl_row
*row
, const char *name
, void *data
)
63 struct osl_object obj
;
66 struct lsblob_data
*lbd
= data
;
68 if (!(lbd
->flags
& BLOB_LS_FLAG_LONG
)) {
69 para_printf(&lbd
->pb
, "%s\n", name
);
72 ret
= osl_get_object(table
, row
, BLOBCOL_ID
, &obj
);
75 id
= *(uint32_t *)obj
.data
;
76 para_printf(&lbd
->pb
, "%u\t%s\n", id
, name
);
80 enum blob_match_loop_flags
{
85 struct blob_match_data
{
86 struct osl_table
*table
;
92 int (*action
)(struct osl_table
*table
, struct osl_row
*row
, const char *name
, void *data
);
95 static int action_if_blob_matches(struct osl_row
*row
, void *data
)
97 struct blob_match_data
*bmd
= data
;
98 struct osl_object name_obj
;
100 int ret
= osl_get_object(bmd
->table
, row
, BLOBCOL_NAME
, &name_obj
);
103 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret
));
106 name
= (char *)name_obj
.data
;
107 if (!*name
) /* ignore dummy row */
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
)
118 return bmd
->action(bmd
->table
, row
, name
, bmd
->data
);
123 static int for_each_matching_blob(struct blob_match_data
*bmd
)
125 unsigned col
= (bmd
->loop_flags
& BM_NAME_LOOP
)?
126 BLOBCOL_NAME
: BLOBCOL_ID
;
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
);
133 int com_lsblob_callback(struct osl_table
*table
,
134 const struct osl_object
*query
, struct osl_object
*ls_output
)
136 struct lsblob_data lbd
= {.flags
= *(uint32_t *)query
};
137 struct blob_match_data bmd
= {
139 .patterns
= (char *)query
->data
+ sizeof(uint32_t),
140 .patterns_size
= query
->size
- sizeof(uint32_t),
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
);
152 ls_output
->data
= lbd
.pb
.buf
;
153 ls_output
->size
= lbd
.pb
.size
;
161 static int com_lsblob(callback_function
*f
, int fd
, int argc
, char * const * const argv
)
164 struct osl_object options
= {.data
= &flags
, .size
= sizeof(flags
)},
168 for (i
= 1; i
< argc
; i
++) {
169 const char *arg
= argv
[i
];
172 if (!strcmp(arg
, "--")) {
176 if (!strcmp(arg
, "-l")) {
177 flags
|= BLOB_LS_FLAG_LONG
;
180 if (!strcmp(arg
, "-i")) {
181 flags
|= BLOB_LS_FLAG_SORT_BY_ID
;
184 if (!strcmp(arg
, "-r")) {
185 flags
|= BLOB_LS_FLAG_REVERSE
;
190 // return -E_BLOB_SYNTAX;
191 ret
= send_option_arg_callback_request(&options
, argc
- i
,
192 argv
+ i
, f
, &result
);
194 send_buffer(fd
, (char *)result
.data
);
200 static int cat_blob(struct osl_table
*table
, struct osl_row
*row
,
201 __a_unused
const char *name
, void *data
)
204 struct osl_object
*blobs
= data
;
205 struct osl_object obj
;
207 ret
= osl_open_disk_object(table
, row
, BLOBCOL_DEF
, &obj
);
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
;
215 return osl_close_disk_object(&obj
);
218 static int com_catblob_callback(struct osl_table
*table
,
219 const struct osl_object
*query
, struct osl_object
*result
)
222 struct blob_match_data bmd
= {
224 .patterns
= (char *)query
->data
,
225 .patterns_size
= query
->size
,
230 ret
= for_each_matching_blob(&bmd
);
233 return (ret
> 0)? 0 : ret
;
236 static int com_catblob(callback_function
*f
, int fd
, int argc
,
237 char * const * const argv
)
239 struct osl_object result
;
243 return -E_BLOB_SYNTAX
;
244 ret
= send_standard_callback_request(argc
- 1, argv
+ 1, f
, &result
);
246 ret
= send_bin_buffer(fd
, (char *)result
.data
, result
.size
);
253 struct para_buffer pb
;
254 unsigned num_removed
;
257 static int remove_blob(struct osl_table
*table
, struct osl_row
*row
,
258 const char *name
, void *data
)
260 struct rmblob_data
*rmbd
= data
;
261 int ret
= osl_del_row(table
, row
);
263 para_printf(&rmbd
->pb
, "%s: %s\n", name
, PARA_STRERROR(-ret
));
267 return 1; /* return success to remove other matching blobs. */
270 static int com_rmblob_callback(struct osl_table
*table
,
271 const struct osl_object
*query
,
272 __a_unused
struct osl_object
*result
)
275 struct rmblob_data rmbd
= {.num_removed
= 0};
276 struct blob_match_data bmd
= {
278 .patterns
= (char *)query
->data
,
279 .patterns_size
= query
->size
,
281 .action
= remove_blob
284 ret
= for_each_matching_blob(&bmd
);
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");
290 para_printf(&rmbd
.pb
, "removed %d blobs\n", rmbd
.num_removed
);
291 result
->data
= rmbd
.pb
.buf
;
292 result
->size
= rmbd
.pb
.size
;
296 static int com_rmblob(callback_function
*f
, __a_unused
int fd
, int argc
,
297 char * const * const argv
)
300 struct osl_object result
;
303 return -E_MOOD_SYNTAX
;
304 ret
= send_option_arg_callback_request(NULL
, argc
- 1, argv
+ 1, f
,
307 send_buffer(fd
, (char *)result
.data
);
313 static int com_addblob_callback(struct osl_table
*table
,
314 const struct osl_object
*query
,
315 __a_unused
struct osl_object
*result
)
317 struct osl_object objs
[NUM_BLOB_COLUMNS
];
318 char *name
= query
->data
;
319 size_t name_len
= strlen(name
) + 1;
324 ret
= osl_get_num_rows(table
, &num_rows
);
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
);
340 /* check if name already exists */
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
)
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
);
351 /* new blob, get id of the dummy row and increment it */
354 ret
= osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
);
357 ret
= osl_get_object(table
, row
, BLOBCOL_ID
, &obj
);
360 id
= *(uint32_t *)obj
.data
+ 1;
362 ret
= osl_update_object(table
, row
, BLOBCOL_ID
, &obj
);
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
);
376 static int com_addblob(callback_function
*f
, int fd
, int argc
,
377 char * const * const argv
)
379 struct osl_object arg_obj
;
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
);
391 static int com_mvblob_callback(struct osl_table
*table
,
392 const struct osl_object
*query
,
393 __a_unused
struct osl_object
*result
)
395 char *src
= (char *) query
->data
;
396 struct osl_object obj
= {.data
= src
, .size
= strlen(src
) + 1};
397 char *dest
= src
+ obj
.size
;
399 int ret
= osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
);
404 obj
.size
= strlen(dest
) + 1;
405 return osl_update_object(table
, row
, BLOBCOL_NAME
, &obj
);
408 static int com_mvblob(callback_function
*f
, __a_unused
int fd
,
409 int argc
, char * const * const argv
)
412 return -E_MOOD_SYNTAX
;
413 return send_option_arg_callback_request(NULL
, argc
- 1, argv
+ 1, f
,
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) \
421 return com_ ## cmd_name ## blob_callback(table_name ## _table, query, output); \
423 int com_ ## cmd_name ## cmd_prefix(int fd, int argc, char * const * const argv) \
425 return com_ ## cmd_name ## blob(com_ ## cmd_name ## cmd_prefix ## _callback, fd, argc, argv); \
428 static int blob_get_name_by_id(struct osl_table
*table
, uint32_t id
,
432 struct osl_object obj
= {.data
= &id
, .size
= sizeof(id
)};
438 ret
= osl_get_row(table
, BLOBCOL_ID
, &obj
, &row
);
441 ret
= osl_get_object(table
, row
, BLOBCOL_NAME
, &obj
);
444 *name
= (char *)obj
.data
;
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) \
452 return blob_get_name_by_id(table_name ## _table, id, name); \
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
)
458 struct osl_object obj
;
459 int ret
= osl_get_object(table
, row
, BLOBCOL_NAME
, &obj
);
463 return osl_open_disk_object(table
, row
, BLOBCOL_DEF
, def
);
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) \
470 return blob_get_name_and_def_by_row(table_name ## _table, \
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) \
478 osl_close_table(table_name ## _table, flags); \
479 table_name ## _table = NULL; \
482 static int blob_init(struct osl_table
**table
,
483 struct osl_table_description
*desc
,
484 struct table_info
*ti
, const char *db
)
489 ret
= osl_open_table(ti
->desc
, &ti
->table
);
495 return ret
== -E_NOENT
? 1 : ret
;
498 /** Define the \p init function for this blob type. */
499 #define DEFINE_BLOB_INIT(table_name) \
500 int table_name ## _init(struct table_info *ti, const char *db) \
502 return blob_init(&table_name ## _table, \
503 &table_name ## _table_desc, ti, db); \
507 /** Define all functions for this blob type. */
508 #define DEFINE_BLOB_FUNCTIONS(table_name, cmd_prefix) \
509 DEFINE_BLOB_COMMAND(ls, table_name, cmd_prefix) \
510 DEFINE_BLOB_COMMAND(cat, table_name, cmd_prefix) \
511 DEFINE_BLOB_COMMAND(add, table_name, cmd_prefix) \
512 DEFINE_BLOB_COMMAND(rm, table_name, cmd_prefix) \
513 DEFINE_BLOB_COMMAND(mv, table_name, cmd_prefix) \
514 DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix); \
515 DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix); \
516 DEFINE_BLOB_SHUTDOWN(table_name); \
517 DEFINE_BLOB_INIT(table_name);
519 /** \cond doxygen isn't smart enough to recognize these */
520 DEFINE_BLOB_FUNCTIONS(lyrics
, lyr
);
521 DEFINE_BLOB_FUNCTIONS(images
, img
);
522 DEFINE_BLOB_FUNCTIONS(moods
, mood
);
523 DEFINE_BLOB_FUNCTIONS(playlists
, pl
);