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
);
339 } else { /* get id of the dummy row and increment it */
341 struct osl_object obj
= {.data
= "", .size
= 1};
342 ret
= osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
);
345 ret
= osl_get_object(table
, row
, BLOBCOL_ID
, &obj
);
348 id
= *(uint32_t *)obj
.data
+ 1;
350 ret
= osl_update_object(table
, row
, BLOBCOL_ID
, &obj
);
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
);
364 static int com_addblob(callback_function
*f
, int fd
, int argc
,
365 char * const * const argv
)
367 struct osl_object arg_obj
;
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
);
379 static int com_mvblob_callback(struct osl_table
*table
,
380 const struct osl_object
*query
,
381 __a_unused
struct osl_object
*result
)
383 char *src
= (char *) query
->data
;
384 struct osl_object obj
= {.data
= src
, .size
= strlen(src
) + 1};
385 char *dest
= src
+ obj
.size
;
387 int ret
= osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
);
392 obj
.size
= strlen(dest
) + 1;
393 return osl_update_object(table
, row
, BLOBCOL_NAME
, &obj
);
396 static int com_mvblob(callback_function
*f
, __a_unused
int fd
,
397 int argc
, char * const * const argv
)
400 return -E_MOOD_SYNTAX
;
401 return send_option_arg_callback_request(NULL
, argc
- 1, argv
+ 1, f
,
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) \
409 return com_ ## cmd_name ## blob_callback(table_name ## _table, query, output); \
411 int com_ ## cmd_name ## cmd_prefix(int fd, int argc, char * const * const argv) \
413 return com_ ## cmd_name ## blob(com_ ## cmd_name ## cmd_prefix ## _callback, fd, argc, argv); \
416 static int blob_get_name_by_id(struct osl_table
*table
, uint32_t id
,
420 struct osl_object obj
= {.data
= &id
, .size
= sizeof(id
)};
426 ret
= osl_get_row(table
, BLOBCOL_ID
, &obj
, &row
);
429 ret
= osl_get_object(table
, row
, BLOBCOL_NAME
, &obj
);
432 *name
= (char *)obj
.data
;
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) \
440 return blob_get_name_by_id(table_name ## _table, id, name); \
443 /** Define the \p shutdown function for this blob type. */
444 #define DEFINE_BLOB_SHUTDOWN(table_name) \
445 void table_name ## _shutdown(enum osl_close_flags flags) \
447 osl_close_table(table_name ## _table, flags); \
448 table_name ## _table = NULL; \
451 static int blob_init(struct osl_table
**table
,
452 struct osl_table_description
*desc
,
453 struct table_info
*ti
, const char *db
)
458 ret
= osl_open_table(ti
->desc
, &ti
->table
);
464 return ret
== -E_NOENT
? 1 : ret
;
467 /** Define the \p init function for this blob type. */
468 #define DEFINE_BLOB_INIT(table_name) \
469 int table_name ## _init(struct table_info *ti, const char *db) \
471 return blob_init(&table_name ## _table, \
472 &table_name ## _table_desc, ti, db); \
476 /** Define all functions for this blob type. */
477 #define DEFINE_BLOB_FUNCTIONS(table_name, cmd_prefix) \
478 DEFINE_BLOB_COMMAND(ls, table_name, cmd_prefix) \
479 DEFINE_BLOB_COMMAND(cat, table_name, cmd_prefix) \
480 DEFINE_BLOB_COMMAND(add, table_name, cmd_prefix) \
481 DEFINE_BLOB_COMMAND(rm, table_name, cmd_prefix) \
482 DEFINE_BLOB_COMMAND(mv, table_name, cmd_prefix) \
483 DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix); \
484 DEFINE_BLOB_SHUTDOWN(table_name); \
485 DEFINE_BLOB_INIT(table_name);
487 /** \cond doxygen isn't smart enough to recognize these */
488 DEFINE_BLOB_FUNCTIONS(lyrics
, lyr
);
489 DEFINE_BLOB_FUNCTIONS(images
, img
);
490 DEFINE_BLOB_FUNCTIONS(moods
, mood
);
491 DEFINE_BLOB_FUNCTIONS(playlists
, pl
);