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 int com_lsblob_callback(struct osl_table
*table
,
81 const struct osl_object
*query
, struct osl_object
*ls_output
)
83 struct lsblob_data lbd
= {.flags
= *(uint32_t *)query
};
84 struct pattern_match_data pmd
= {
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
,
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
;
100 pmd
.loop_col_num
= BLOBCOL_NAME
;
101 ret
= for_each_matching_row(&pmd
);
103 ls_output
->data
= lbd
.pb
.buf
;
104 ls_output
->size
= lbd
.pb
.size
;
112 static int com_lsblob(callback_function
*f
, int fd
, int argc
, char * const * const argv
)
115 struct osl_object options
= {.data
= &flags
, .size
= sizeof(flags
)},
119 for (i
= 1; i
< argc
; i
++) {
120 const char *arg
= argv
[i
];
123 if (!strcmp(arg
, "--")) {
127 if (!strcmp(arg
, "-l")) {
128 flags
|= BLOB_LS_FLAG_LONG
;
131 if (!strcmp(arg
, "-i")) {
132 flags
|= BLOB_LS_FLAG_SORT_BY_ID
;
135 if (!strcmp(arg
, "-r")) {
136 flags
|= BLOB_LS_FLAG_REVERSE
;
141 // return -E_BLOB_SYNTAX;
142 ret
= send_option_arg_callback_request(&options
, argc
- i
,
143 argv
+ i
, f
, &result
);
145 send_buffer(fd
, (char *)result
.data
);
151 static int cat_blob(struct osl_table
*table
, struct osl_row
*row
,
152 __a_unused
const char *name
, void *data
)
155 struct osl_object
*blobs
= data
;
156 struct osl_object obj
;
158 ret
= osl_open_disk_object(table
, row
, BLOBCOL_DEF
, &obj
);
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
;
166 return osl_close_disk_object(&obj
);
169 static int com_catblob_callback(struct osl_table
*table
,
170 const struct osl_object
*query
, struct osl_object
*result
)
173 struct pattern_match_data pmd
= {
176 .loop_col_num
= BLOBCOL_NAME
,
177 .match_col_num
= BLOBCOL_NAME
,
178 .pm_flags
= PM_SKIP_EMPTY_NAME
,
183 ret
= for_each_matching_row(&pmd
);
186 return (ret
> 0)? 0 : ret
;
189 static int com_catblob(callback_function
*f
, int fd
, int argc
,
190 char * const * const argv
)
192 struct osl_object result
;
196 return -E_BLOB_SYNTAX
;
197 ret
= send_standard_callback_request(argc
- 1, argv
+ 1, f
, &result
);
199 ret
= send_bin_buffer(fd
, (char *)result
.data
, result
.size
);
206 struct para_buffer pb
;
207 unsigned num_removed
;
210 static int remove_blob(struct osl_table
*table
, struct osl_row
*row
,
211 const char *name
, void *data
)
213 struct rmblob_data
*rmbd
= data
;
214 int ret
= osl_del_row(table
, row
);
216 para_printf(&rmbd
->pb
, "%s: %s\n", name
, PARA_STRERROR(-ret
));
220 return 1; /* return success to remove other matching blobs. */
223 static int com_rmblob_callback(struct osl_table
*table
,
224 const struct osl_object
*query
,
225 __a_unused
struct osl_object
*result
)
228 struct rmblob_data rmbd
= {.num_removed
= 0};
229 struct pattern_match_data pmd
= {
232 .loop_col_num
= BLOBCOL_NAME
,
233 .match_col_num
= BLOBCOL_NAME
,
234 .pm_flags
= PM_SKIP_EMPTY_NAME
,
236 .action
= remove_blob
239 ret
= for_each_matching_row(&pmd
);
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");
245 para_printf(&rmbd
.pb
, "removed %d blobs\n", rmbd
.num_removed
);
246 result
->data
= rmbd
.pb
.buf
;
247 result
->size
= rmbd
.pb
.size
;
251 static int com_rmblob(callback_function
*f
, __a_unused
int fd
, int argc
,
252 char * const * const argv
)
255 struct osl_object result
;
258 return -E_MOOD_SYNTAX
;
259 ret
= send_option_arg_callback_request(NULL
, argc
- 1, argv
+ 1, f
,
262 send_buffer(fd
, (char *)result
.data
);
268 static int com_addblob_callback(struct osl_table
*table
,
269 const struct osl_object
*query
,
270 __a_unused
struct osl_object
*result
)
272 struct osl_object objs
[NUM_BLOB_COLUMNS
];
273 char *name
= query
->data
;
274 size_t name_len
= strlen(name
) + 1;
279 ret
= osl_get_num_rows(table
, &num_rows
);
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
);
295 /* check if name already exists */
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
)
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
);
306 /* new blob, get id of the dummy row and increment it */
309 ret
= osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
);
312 ret
= osl_get_object(table
, row
, BLOBCOL_ID
, &obj
);
315 id
= *(uint32_t *)obj
.data
+ 1;
317 ret
= osl_update_object(table
, row
, BLOBCOL_ID
, &obj
);
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
);
331 static int com_addblob(callback_function
*f
, int fd
, int argc
,
332 char * const * const argv
)
334 struct osl_object arg_obj
;
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
);
346 static int com_mvblob_callback(struct osl_table
*table
,
347 const struct osl_object
*query
,
348 __a_unused
struct osl_object
*result
)
350 char *src
= (char *) query
->data
;
351 struct osl_object obj
= {.data
= src
, .size
= strlen(src
) + 1};
352 char *dest
= src
+ obj
.size
;
354 int ret
= osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
);
359 obj
.size
= strlen(dest
) + 1;
360 return osl_update_object(table
, row
, BLOBCOL_NAME
, &obj
);
363 static int com_mvblob(callback_function
*f
, __a_unused
int fd
,
364 int argc
, char * const * const argv
)
367 return -E_MOOD_SYNTAX
;
368 return send_option_arg_callback_request(NULL
, argc
- 1, argv
+ 1, f
,
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) \
376 return com_ ## cmd_name ## blob_callback(table_name ## _table, query, output); \
378 int com_ ## cmd_name ## cmd_prefix(int fd, int argc, char * const * const argv) \
380 return com_ ## cmd_name ## blob(com_ ## cmd_name ## cmd_prefix ## _callback, fd, argc, argv); \
383 static int blob_get_name_by_id(struct osl_table
*table
, uint32_t id
,
387 struct osl_object obj
= {.data
= &id
, .size
= sizeof(id
)};
393 ret
= osl_get_row(table
, BLOBCOL_ID
, &obj
, &row
);
396 ret
= osl_get_object(table
, row
, BLOBCOL_NAME
, &obj
);
399 *name
= (char *)obj
.data
;
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) \
407 return blob_get_name_by_id(table_name ## _table, id, name); \
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
)
413 struct osl_object obj
;
414 int ret
= osl_get_object(table
, row
, BLOBCOL_NAME
, &obj
);
418 return osl_open_disk_object(table
, row
, BLOBCOL_DEF
, def
);
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) \
425 return blob_get_name_and_def_by_row(table_name ## _table, \
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) \
433 osl_close_table(table_name ## _table, flags); \
434 table_name ## _table = NULL; \
437 static int blob_init(struct osl_table
**table
,
438 struct osl_table_description
*desc
,
439 struct table_info
*ti
, const char *db
)
444 ret
= osl_open_table(ti
->desc
, table
);
448 return ret
== -E_NOENT
? 1 : ret
;
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) \
455 return blob_init(&table_name ## _table, \
456 &table_name ## _table_desc, ti, db); \
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);
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
);