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. */
56 struct lsblob_action_data
{
58 struct para_buffer pb
;
61 static int print_blob(struct osl_table
*table
, struct osl_row
*row
,
62 const char *name
, void *data
)
64 struct lsblob_action_data
*lbad
= data
;
65 struct osl_object obj
;
69 if (!(lbad
->flags
& BLOB_LS_FLAG_LONG
)) {
70 para_printf(&lbad
->pb
, "%s\n", name
);
73 ret
= osl_get_object(table
, row
, BLOBCOL_ID
, &obj
);
75 para_printf(&lbad
->pb
, "%s: %s\n", name
, PARA_STRERROR(-ret
));
78 id
= *(uint32_t *)obj
.data
;
79 para_printf(&lbad
->pb
, "%u\t%s\n", id
, name
);
83 static int com_lsblob_callback(struct osl_table
*table
,
84 const struct osl_object
*query
, struct osl_object
*result
)
86 struct lsblob_action_data lbad
= {.flags
= *(uint32_t *)query
->data
};
87 struct pattern_match_data pmd
= {
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
,
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
;
103 pmd
.loop_col_num
= BLOBCOL_NAME
;
104 ret
= for_each_matching_row(&pmd
);
106 para_printf(&lbad
.pb
, "%s\n", PARA_STRERROR(-ret
));
109 result
->data
= lbad
.pb
.buf
;
110 result
->size
= lbad
.pb
.size
;
114 static int com_lsblob(callback_function
*f
, int fd
, int argc
, char * const * const argv
)
117 struct osl_object options
= {.data
= &flags
, .size
= sizeof(flags
)},
121 for (i
= 1; i
< argc
; i
++) {
122 const char *arg
= argv
[i
];
125 if (!strcmp(arg
, "--")) {
129 if (!strcmp(arg
, "-l")) {
130 flags
|= BLOB_LS_FLAG_LONG
;
133 if (!strcmp(arg
, "-i")) {
134 flags
|= BLOB_LS_FLAG_SORT_BY_ID
;
137 if (!strcmp(arg
, "-r")) {
138 flags
|= BLOB_LS_FLAG_REVERSE
;
144 // return -E_BLOB_SYNTAX;
145 ret
= send_option_arg_callback_request(&options
, argc
- i
,
146 argv
+ i
, f
, &result
);
148 send_buffer(fd
, (char *)result
.data
);
154 static int cat_blob(struct osl_table
*table
, struct osl_row
*row
,
155 __a_unused
const char *name
, void *data
)
158 struct osl_object
*blobs
= data
;
159 struct osl_object obj
;
161 ret
= osl_open_disk_object(table
, row
, BLOBCOL_DEF
, &obj
);
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
;
169 return osl_close_disk_object(&obj
);
172 static int com_catblob_callback(struct osl_table
*table
,
173 const struct osl_object
*query
, struct osl_object
*result
)
176 struct pattern_match_data pmd
= {
179 .loop_col_num
= BLOBCOL_NAME
,
180 .match_col_num
= BLOBCOL_NAME
,
181 .pm_flags
= PM_SKIP_EMPTY_NAME
,
186 ret
= for_each_matching_row(&pmd
);
189 return (ret
> 0)? 0 : ret
;
192 static int com_catblob(callback_function
*f
, int fd
, int argc
,
193 char * const * const argv
)
195 struct osl_object result
;
199 return -E_BLOB_SYNTAX
;
200 ret
= send_standard_callback_request(argc
- 1, argv
+ 1, f
, &result
);
202 ret
= send_bin_buffer(fd
, (char *)result
.data
, result
.size
);
209 struct para_buffer pb
;
210 unsigned num_removed
;
213 static int remove_blob(struct osl_table
*table
, struct osl_row
*row
,
214 const char *name
, void *data
)
216 struct rmblob_data
*rmbd
= data
;
217 int ret
= osl_del_row(table
, row
);
219 para_printf(&rmbd
->pb
, "%s: %s\n", name
, PARA_STRERROR(-ret
));
223 return 1; /* return success to remove other matching blobs. */
226 static int com_rmblob_callback(struct osl_table
*table
,
227 const struct osl_object
*query
,
228 __a_unused
struct osl_object
*result
)
231 struct rmblob_data rmbd
= {.num_removed
= 0};
232 struct pattern_match_data pmd
= {
235 .loop_col_num
= BLOBCOL_NAME
,
236 .match_col_num
= BLOBCOL_NAME
,
237 .pm_flags
= PM_SKIP_EMPTY_NAME
,
239 .action
= remove_blob
242 ret
= for_each_matching_row(&pmd
);
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");
248 para_printf(&rmbd
.pb
, "removed %d blobs\n", rmbd
.num_removed
);
249 result
->data
= rmbd
.pb
.buf
;
250 result
->size
= rmbd
.pb
.size
;
254 static int com_rmblob(callback_function
*f
, __a_unused
int fd
, int argc
,
255 char * const * const argv
)
258 struct osl_object result
;
261 return -E_MOOD_SYNTAX
;
262 ret
= send_option_arg_callback_request(NULL
, argc
- 1, argv
+ 1, f
,
265 send_buffer(fd
, (char *)result
.data
);
271 static int com_addblob_callback(struct osl_table
*table
,
272 const struct osl_object
*query
,
273 __a_unused
struct osl_object
*result
)
275 struct osl_object objs
[NUM_BLOB_COLUMNS
];
276 char *name
= query
->data
;
277 size_t name_len
= strlen(name
) + 1;
282 ret
= osl_get_num_rows(table
, &num_rows
);
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
);
298 /* check if name already exists */
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
)
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
);
309 /* new blob, get id of the dummy row and increment it */
312 ret
= osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
);
315 ret
= osl_get_object(table
, row
, BLOBCOL_ID
, &obj
);
318 id
= *(uint32_t *)obj
.data
+ 1;
320 ret
= osl_update_object(table
, row
, BLOBCOL_ID
, &obj
);
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
);
334 static int com_addblob(callback_function
*f
, int fd
, int argc
,
335 char * const * const argv
)
337 struct osl_object arg_obj
;
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
);
349 static int com_mvblob_callback(struct osl_table
*table
,
350 const struct osl_object
*query
,
351 __a_unused
struct osl_object
*result
)
353 char *src
= (char *) query
->data
;
354 struct osl_object obj
= {.data
= src
, .size
= strlen(src
) + 1};
355 char *dest
= src
+ obj
.size
;
357 int ret
= osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
);
362 obj
.size
= strlen(dest
) + 1;
363 return osl_update_object(table
, row
, BLOBCOL_NAME
, &obj
);
366 static int com_mvblob(callback_function
*f
, __a_unused
int fd
,
367 int argc
, char * const * const argv
)
370 return -E_MOOD_SYNTAX
;
371 return send_option_arg_callback_request(NULL
, argc
- 1, argv
+ 1, f
,
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) \
379 return com_ ## cmd_name ## blob_callback(table_name ## _table, query, output); \
381 int com_ ## cmd_name ## cmd_prefix(int fd, int argc, char * const * const argv) \
383 return com_ ## cmd_name ## blob(com_ ## cmd_name ## cmd_prefix ## _callback, fd, argc, argv); \
386 static int blob_get_name_by_id(struct osl_table
*table
, uint32_t id
,
390 struct osl_object obj
= {.data
= &id
, .size
= sizeof(id
)};
396 ret
= osl_get_row(table
, BLOBCOL_ID
, &obj
, &row
);
399 ret
= osl_get_object(table
, row
, BLOBCOL_NAME
, &obj
);
402 *name
= (char *)obj
.data
;
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) \
410 return blob_get_name_by_id(table_name ## _table, id, name); \
413 static int blob_get_def_by_id(struct osl_table
*table
, uint32_t id
,
414 struct osl_object
*def
)
417 struct osl_object obj
= {.data
= &id
, .size
= sizeof(id
)};
423 ret
= osl_get_row(table
, BLOBCOL_ID
, &obj
, &row
);
426 return osl_open_disk_object(table
, row
, BLOBCOL_DEF
, def
);
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) \
433 return blob_get_def_by_id(table_name ## _table, id, def); \
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
)
439 struct osl_object obj
;
440 int ret
= osl_get_object(table
, row
, BLOBCOL_NAME
, &obj
);
444 return osl_open_disk_object(table
, row
, BLOBCOL_DEF
, def
);
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) \
451 return blob_get_name_and_def_by_row(table_name ## _table, \
455 /** Define the \p shutdown function for this blob type. */
456 #define DEFINE_BLOB_SHUTDOWN(table_name) \
457 void table_name ## _shutdown(enum osl_close_flags flags) \
459 osl_close_table(table_name ## _table, flags); \
460 table_name ## _table = NULL; \
463 static int blob_init(struct osl_table
**table
,
464 struct osl_table_description
*desc
,
465 struct table_info
*ti
, const char *db
)
470 ret
= osl_open_table(ti
->desc
, table
);
474 return ret
== -E_NOENT
? 1 : ret
;
477 /** Define the \p init function for this blob type. */
478 #define DEFINE_BLOB_INIT(table_name) \
479 int table_name ## _init(struct table_info *ti, const char *db) \
481 return blob_init(&table_name ## _table, \
482 &table_name ## _table_desc, ti, db); \
486 /** Define all functions for this blob type. */
487 #define DEFINE_BLOB_FUNCTIONS(table_name, cmd_prefix) \
488 DEFINE_BLOB_COMMAND(ls, table_name, cmd_prefix) \
489 DEFINE_BLOB_COMMAND(cat, table_name, cmd_prefix) \
490 DEFINE_BLOB_COMMAND(add, table_name, cmd_prefix) \
491 DEFINE_BLOB_COMMAND(rm, table_name, cmd_prefix) \
492 DEFINE_BLOB_COMMAND(mv, table_name, cmd_prefix) \
493 DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix); \
494 DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix); \
495 DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix); \
496 DEFINE_BLOB_SHUTDOWN(table_name); \
497 DEFINE_BLOB_INIT(table_name);
499 /** \cond doxygen isn't smart enough to recognize these */
500 DEFINE_BLOB_FUNCTIONS(lyrics
, lyr
);
501 DEFINE_BLOB_FUNCTIONS(images
, img
);
502 DEFINE_BLOB_FUNCTIONS(moods
, mood
);
503 DEFINE_BLOB_FUNCTIONS(playlists
, pl
);