7 /** \file blob.c Macros and functions for blob handling. */
9 static struct osl_column_description blob_cols
[] = {
11 .storage_type
= OSL_MAPPED_STORAGE
,
12 .storage_flags
= OSL_RBTREE
| OSL_UNIQUE
| OSL_FIXED_SIZE
,
15 .compare_function
= uint32_compare
18 .storage_type
= OSL_MAPPED_STORAGE
,
19 .storage_flags
= OSL_RBTREE
| OSL_UNIQUE
,
21 .compare_function
= string_compare
24 .storage_type
= OSL_DISK_STORAGE
,
30 /** \cond doxygen isn't smart enough to recognize these */
31 INIT_BLOB_TABLE(lyrics
);
32 INIT_BLOB_TABLE(images
);
33 INIT_BLOB_TABLE(moods
);
34 INIT_BLOB_TABLE(playlists
);
37 /** Flags that may be passed to the \p ls functions of each blob type. */
39 /** List both id and name. */
40 BLOB_LS_FLAG_LONG
= 1,
41 /** Reverse sort order. */
42 BLOB_LS_FLAG_REVERSE
= 2,
43 /** Sort by id instead of name. */
44 BLOB_LS_FLAG_SORT_BY_ID
= 4,
47 /** Data passed to \p com_lsbob_callback(). */
48 struct com_lsblob_options
{
49 /** Given flags for the ls command. */
53 /** Structure passed to the \p print_blob loop function. */
54 struct lsblob_loop_data
{
55 struct com_lsblob_options
*opts
;
56 struct para_buffer
*pb
;
57 struct osl_table
*table
;
60 static int print_blob(struct osl_row
*row
, void *loop_data
)
62 struct osl_object obj
;
66 struct lsblob_loop_data
*lld
= loop_data
;
68 ret
= osl_get_object(lld
->table
, row
, BLOBCOL_NAME
, &obj
);
72 if (!*name
) /* ignore dummy row */
74 ret
= osl_get_object(lld
->table
, row
, BLOBCOL_ID
, &obj
);
77 id
= *(uint32_t *)obj
.data
;
78 if (lld
->opts
->flags
& BLOB_LS_FLAG_LONG
)
79 para_printf(lld
->pb
, "%u\t%s\n", id
, name
);
81 para_printf(lld
->pb
, "%s\n", name
);
85 int com_lsblob_callback(struct osl_table
*table
,
86 const struct osl_object
*query
, struct osl_object
*ls_output
)
88 struct para_buffer pb
= {.buf
= NULL
};
89 struct lsblob_loop_data lld
= {.opts
= query
->data
, .pb
= &pb
, .table
= table
};
92 if (lld
.opts
->flags
& BLOB_LS_FLAG_REVERSE
) {
93 if (lld
.opts
->flags
& BLOB_LS_FLAG_SORT_BY_ID
)
94 ret
= osl_rbtree_loop(lld
.table
, BLOBCOL_ID
, &lld
, print_blob
);
96 ret
= osl_rbtree_loop_reverse(lld
.table
, BLOBCOL_NAME
, &lld
, print_blob
);
98 if (lld
.opts
->flags
& BLOB_LS_FLAG_SORT_BY_ID
)
99 ret
= osl_rbtree_loop_reverse(lld
.table
, BLOBCOL_ID
, &lld
, print_blob
);
101 ret
= osl_rbtree_loop(lld
.table
, BLOBCOL_NAME
, &lld
, print_blob
);
103 ls_output
->data
= pb
.buf
;
104 ls_output
->size
= pb
.size
;
108 static int com_lsblob(callback_function
*f
, __a_unused
int fd
, int argc
, const char **argv
)
110 struct com_lsblob_options clbo
= {.flags
= 0};
111 struct osl_object query
= {.data
= &clbo
, .size
= sizeof(clbo
)},
115 for (i
= 1; i
< argc
; i
++) {
116 const char *arg
= argv
[i
];
119 if (!strcmp(arg
, "--")) {
123 if (!strcmp(arg
, "-l")) {
124 clbo
.flags
|= BLOB_LS_FLAG_LONG
;
127 if (!strcmp(arg
, "-i")) {
128 clbo
.flags
|= BLOB_LS_FLAG_SORT_BY_ID
;
131 if (!strcmp(arg
, "-r")) {
132 clbo
.flags
|= BLOB_LS_FLAG_REVERSE
;
137 return -E_BLOB_SYNTAX
;
138 ret
= send_option_arg_callback_request(&query
, argc
- i
,
139 argv
+ i
, f
, &ls_output
);
140 if (ret
>= 0 && ls_output
.data
)
141 printf("%s\n", (char *)ls_output
.data
);
142 free(ls_output
.data
);
146 static int com_catblob_callback(struct osl_table
*table
,
147 const struct osl_object
*query
, struct osl_object
*output
)
149 struct osl_object obj
;
153 ret
= osl_get_row(table
, BLOBCOL_NAME
, query
, &row
);
156 ret
= osl_open_disk_object(table
, row
, BLOBCOL_DEF
, &obj
);
159 output
->data
= para_malloc(obj
.size
);
160 output
->size
= obj
.size
;
161 memcpy(output
->data
, obj
.data
, obj
.size
);
162 return osl_close_disk_object(&obj
);
164 static int com_catblob(callback_function
*f
, __a_unused
int fd
, int argc
,
167 struct osl_object cat_output
= {.data
= NULL
};
171 return -E_BLOB_SYNTAX
;
172 if (!*argv
[1]) /* empty name is reserved of the dummy row */
173 return -E_BLOB_SYNTAX
;
174 ret
= send_standard_callback_request(1, argv
+ 1, f
, &cat_output
);
175 if (ret
>= 0 && cat_output
.data
)
176 printf("%s\n", (char *)cat_output
.data
);
177 free(cat_output
.data
);
182 static int com_addblob_callback(struct osl_table
*table
,
183 const struct osl_object
*query
,
184 __a_unused
struct osl_object
*result
)
186 struct osl_object objs
[NUM_BLOB_COLUMNS
];
187 char *name
= query
->data
;
188 size_t name_len
= strlen(name
) + 1;
193 ret
= osl_get_num_rows(table
, &num_rows
);
196 if (!num_rows
) { /* this is the first entry ever added */
197 /* insert dummy row containing the id */
198 id
= 2; /* this entry will be entry #1, so 2 is the next */
199 objs
[BLOBCOL_ID
].data
= &id
;
200 objs
[BLOBCOL_ID
].size
= sizeof(id
);
201 objs
[BLOBCOL_NAME
].data
= "";
202 objs
[BLOBCOL_NAME
].size
= 1;
203 objs
[BLOBCOL_DEF
].data
= "";
204 objs
[BLOBCOL_DEF
].size
= 1;
205 ret
= osl_add_row(table
, objs
);
208 } else { /* get id of the dummy row and increment it */
210 struct osl_object obj
= {.data
= "", .size
= 1};
211 ret
= osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
);
214 ret
= osl_get_object(table
, row
, BLOBCOL_ID
, &obj
);
217 id
= *(uint32_t *)obj
.data
+ 1;
219 ret
= osl_update_object(table
, row
, BLOBCOL_ID
, &obj
);
224 objs
[BLOBCOL_ID
].data
= &id
;
225 objs
[BLOBCOL_ID
].size
= sizeof(id
);
226 objs
[BLOBCOL_NAME
].data
= name
;
227 objs
[BLOBCOL_NAME
].size
= name_len
;
228 objs
[BLOBCOL_DEF
].data
= name
+ name_len
;
229 objs
[BLOBCOL_DEF
].size
= query
->size
- name_len
;
230 return osl_add_row(table
, objs
);
233 static int com_addblob(callback_function
*f
, __a_unused
int fd
, int argc
,
236 struct osl_object arg_obj
;
239 return -E_BLOB_SYNTAX
;
240 if (!*argv
[1]) /* empty name is reserved for the dummy row */
241 return -E_BLOB_SYNTAX
;
242 PARA_NOTICE_LOG("argv[1]: %s\n", argv
[1]);
243 arg_obj
.size
= strlen(argv
[1]) + 1;
244 arg_obj
.data
= (char *)argv
[1];
245 return stdin_command(&arg_obj
, f
, 10 * 1024 * 1024, NULL
);
248 static int com_rmblob_callback(struct osl_table
*table
,
249 const struct osl_object
*query
,
250 __a_unused
struct osl_object
*result
)
252 char *p
= query
->data
;
256 for (; p
< (char *)query
->data
+ query
->size
; p
+= len
+ 1) {
258 struct osl_object obj
;
263 ret
= osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
);
266 ret
= osl_del_row(table
, row
);
273 static int com_rmblob(callback_function
*f
, __a_unused
int fd
, int argc
,
277 return -E_MOOD_SYNTAX
;
278 return send_option_arg_callback_request(NULL
, argc
- 1, argv
+ 1, f
,
282 static int com_mvblob_callback(struct osl_table
*table
,
283 const struct osl_object
*query
,
284 __a_unused
struct osl_object
*result
)
286 char *src
= (char *) query
->data
;
287 struct osl_object obj
= {.data
= src
, .size
= strlen(src
) + 1};
288 char *dest
= src
+ obj
.size
;
290 int ret
= osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
);
295 obj
.size
= strlen(dest
) + 1;
296 return osl_update_object(table
, row
, BLOBCOL_NAME
, &obj
);
299 static int com_mvblob(callback_function
*f
, __a_unused
int fd
,
300 int argc
, const char **argv
)
303 return -E_MOOD_SYNTAX
;
304 return send_option_arg_callback_request(NULL
, argc
- 1, argv
+ 1, f
,
308 #define DEFINE_BLOB_COMMAND(cmd_name, table_name, cmd_prefix) \
309 static int com_ ## cmd_name ## cmd_prefix ## _callback(const struct osl_object *query, \
310 struct osl_object *output) \
312 return com_ ## cmd_name ## blob_callback(table_name ## _table, query, output); \
314 int com_ ## cmd_name ## cmd_prefix(__a_unused int fd, int argc, const char **argv) \
316 return com_ ## cmd_name ## blob(com_ ## cmd_name ## cmd_prefix ## _callback, fd, argc, argv); \
319 static int blob_get_name_by_id(struct osl_table
*table
, uint32_t id
,
323 struct osl_object obj
= {.data
= &id
, .size
= sizeof(id
)};
329 ret
= osl_get_row(table
, BLOBCOL_ID
, &obj
, &row
);
332 ret
= osl_get_object(table
, row
, BLOBCOL_NAME
, &obj
);
335 *name
= (char *)obj
.data
;
338 /** Define the \p get_name_by_id function for this blob type. */
339 #define DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix) \
340 int cmd_prefix ## _get_name_by_id(uint32_t id, char **name) \
342 return blob_get_name_by_id(table_name ## _table, id, name); \
345 /** Define the \p shutdown function for this blob type. */
346 #define DEFINE_BLOB_SHUTDOWN(table_name) \
347 void table_name ## _shutdown(enum osl_close_flags flags) \
349 osl_close_table(table_name ## _table, flags); \
350 table_name ## _table = NULL; \
353 static int blob_init(struct osl_table
**table
,
354 const struct osl_table_description
*desc
,
355 struct table_info
*ti
)
360 ret
= osl_open_table(ti
->desc
, &ti
->table
);
366 return ret
== -E_NOENT
? 1 : ret
;
369 /** Define the \p init function for this blob type. */
370 #define DEFINE_BLOB_INIT(table_name) \
371 int table_name ## _init(struct table_info *ti) \
373 return blob_init(&table_name ## _table, \
374 &table_name ## _table_desc, ti); \
378 /** Define all functions for this blob type. */
379 #define DEFINE_BLOB_FUNCTIONS(table_name, cmd_prefix) \
380 DEFINE_BLOB_COMMAND(ls, table_name, cmd_prefix) \
381 DEFINE_BLOB_COMMAND(cat, table_name, cmd_prefix) \
382 DEFINE_BLOB_COMMAND(add, table_name, cmd_prefix) \
383 DEFINE_BLOB_COMMAND(rm, table_name, cmd_prefix) \
384 DEFINE_BLOB_COMMAND(mv, table_name, cmd_prefix) \
385 DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix); \
386 DEFINE_BLOB_SHUTDOWN(table_name); \
387 DEFINE_BLOB_INIT(table_name);
389 /** \cond doxygen isn't smart enough to recognize these */
390 DEFINE_BLOB_FUNCTIONS(lyrics
, lyr
);
391 DEFINE_BLOB_FUNCTIONS(images
, img
);
392 DEFINE_BLOB_FUNCTIONS(moods
, mood
);
393 DEFINE_BLOB_FUNCTIONS(playlists
, pl
);