8 /** \file blob.c Macros and functions for blob handling. */
10 static struct osl_column_description blob_cols
[] = {
12 .storage_type
= OSL_MAPPED_STORAGE
,
13 .storage_flags
= OSL_RBTREE
| OSL_UNIQUE
| OSL_FIXED_SIZE
,
16 .compare_function
= uint32_compare
19 .storage_type
= OSL_MAPPED_STORAGE
,
20 .storage_flags
= OSL_RBTREE
| OSL_UNIQUE
,
22 .compare_function
= string_compare
25 .storage_type
= OSL_DISK_STORAGE
,
31 /** \cond doxygen isn't smart enough to recognize these */
32 INIT_BLOB_TABLE(lyrics
);
33 INIT_BLOB_TABLE(images
);
34 INIT_BLOB_TABLE(moods
);
35 INIT_BLOB_TABLE(playlists
);
38 /** Flags that may be passed to the \p ls functions of each blob type. */
40 /** List both id and name. */
41 BLOB_LS_FLAG_LONG
= 1,
42 /** Reverse sort order. */
43 BLOB_LS_FLAG_REVERSE
= 2,
44 /** Sort by id instead of name. */
45 BLOB_LS_FLAG_SORT_BY_ID
= 4,
48 /** Data passed to \p com_lsbob_callback(). */
49 struct com_lsblob_options
{
50 /** Given flags for the ls command. */
54 /** Structure passed to the \p print_blob loop function. */
55 struct lsblob_loop_data
{
56 struct com_lsblob_options
*opts
;
57 struct para_buffer
*pb
;
58 struct osl_table
*table
;
61 static int print_blob(struct osl_row
*row
, void *loop_data
)
63 struct osl_object obj
;
67 struct lsblob_loop_data
*lld
= loop_data
;
69 ret
= osl_get_object(lld
->table
, row
, BLOBCOL_NAME
, &obj
);
73 if (!*name
) /* ignore dummy row */
75 ret
= osl_get_object(lld
->table
, row
, BLOBCOL_ID
, &obj
);
78 id
= *(uint32_t *)obj
.data
;
79 if (lld
->opts
->flags
& BLOB_LS_FLAG_LONG
)
80 para_printf(lld
->pb
, "%u\t%s\n", id
, name
);
82 para_printf(lld
->pb
, "%s\n", name
);
86 int com_lsblob_callback(struct osl_table
*table
,
87 const struct osl_object
*query
, struct osl_object
*ls_output
)
89 struct para_buffer pb
= {.buf
= NULL
};
90 struct lsblob_loop_data lld
= {.opts
= query
->data
, .pb
= &pb
, .table
= table
};
93 if (lld
.opts
->flags
& BLOB_LS_FLAG_REVERSE
) {
94 if (lld
.opts
->flags
& BLOB_LS_FLAG_SORT_BY_ID
)
95 ret
= osl_rbtree_loop(lld
.table
, BLOBCOL_ID
, &lld
, print_blob
);
97 ret
= osl_rbtree_loop_reverse(lld
.table
, BLOBCOL_NAME
, &lld
, print_blob
);
99 if (lld
.opts
->flags
& BLOB_LS_FLAG_SORT_BY_ID
)
100 ret
= osl_rbtree_loop_reverse(lld
.table
, BLOBCOL_ID
, &lld
, print_blob
);
102 ret
= osl_rbtree_loop(lld
.table
, BLOBCOL_NAME
, &lld
, print_blob
);
104 ls_output
->data
= pb
.buf
;
105 ls_output
->size
= pb
.size
;
109 static int com_lsblob(callback_function
*f
, int fd
, int argc
, char * const * const argv
)
111 struct com_lsblob_options clbo
= {.flags
= 0};
112 struct osl_object query
= {.data
= &clbo
, .size
= sizeof(clbo
)},
116 for (i
= 1; i
< argc
; i
++) {
117 const char *arg
= argv
[i
];
120 if (!strcmp(arg
, "--")) {
124 if (!strcmp(arg
, "-l")) {
125 clbo
.flags
|= BLOB_LS_FLAG_LONG
;
128 if (!strcmp(arg
, "-i")) {
129 clbo
.flags
|= BLOB_LS_FLAG_SORT_BY_ID
;
132 if (!strcmp(arg
, "-r")) {
133 clbo
.flags
|= BLOB_LS_FLAG_REVERSE
;
138 return -E_BLOB_SYNTAX
;
139 ret
= send_option_arg_callback_request(&query
, argc
- i
,
140 argv
+ i
, f
, &ls_output
);
142 send_buffer(fd
, (char *)ls_output
.data
);
143 free(ls_output
.data
);
148 static int com_catblob_callback(struct osl_table
*table
,
149 const struct osl_object
*query
, struct osl_object
*output
)
151 struct osl_object obj
;
155 ret
= osl_get_row(table
, BLOBCOL_NAME
, query
, &row
);
158 ret
= osl_open_disk_object(table
, row
, BLOBCOL_DEF
, &obj
);
161 output
->data
= para_malloc(obj
.size
);
162 output
->size
= obj
.size
;
163 memcpy(output
->data
, obj
.data
, obj
.size
);
164 return osl_close_disk_object(&obj
);
166 static int com_catblob(callback_function
*f
, int fd
, int argc
,
167 char * const * const argv
)
169 struct osl_object cat_output
= {.data
= NULL
};
173 return -E_BLOB_SYNTAX
;
174 if (!*argv
[1]) /* empty name is reserved of the dummy row */
175 return -E_BLOB_SYNTAX
;
176 ret
= send_standard_callback_request(1, argv
+ 1, f
, &cat_output
);
177 if (ret
>= 0 && cat_output
.data
)
178 ret
= send_buffer(fd
, (char *)cat_output
.data
);
179 free(cat_output
.data
);
184 static int com_addblob_callback(struct osl_table
*table
,
185 const struct osl_object
*query
,
186 __a_unused
struct osl_object
*result
)
188 struct osl_object objs
[NUM_BLOB_COLUMNS
];
189 char *name
= query
->data
;
190 size_t name_len
= strlen(name
) + 1;
195 ret
= osl_get_num_rows(table
, &num_rows
);
198 if (!num_rows
) { /* this is the first entry ever added */
199 /* insert dummy row containing the id */
200 id
= 2; /* this entry will be entry #1, so 2 is the next */
201 objs
[BLOBCOL_ID
].data
= &id
;
202 objs
[BLOBCOL_ID
].size
= sizeof(id
);
203 objs
[BLOBCOL_NAME
].data
= "";
204 objs
[BLOBCOL_NAME
].size
= 1;
205 objs
[BLOBCOL_DEF
].data
= "";
206 objs
[BLOBCOL_DEF
].size
= 1;
207 ret
= osl_add_row(table
, objs
);
210 } else { /* get id of the dummy row and increment it */
212 struct osl_object obj
= {.data
= "", .size
= 1};
213 ret
= osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
);
216 ret
= osl_get_object(table
, row
, BLOBCOL_ID
, &obj
);
219 id
= *(uint32_t *)obj
.data
+ 1;
221 ret
= osl_update_object(table
, row
, BLOBCOL_ID
, &obj
);
226 objs
[BLOBCOL_ID
].data
= &id
;
227 objs
[BLOBCOL_ID
].size
= sizeof(id
);
228 objs
[BLOBCOL_NAME
].data
= name
;
229 objs
[BLOBCOL_NAME
].size
= name_len
;
230 objs
[BLOBCOL_DEF
].data
= name
+ name_len
;
231 objs
[BLOBCOL_DEF
].size
= query
->size
- name_len
;
232 return osl_add_row(table
, objs
);
235 static int com_addblob(callback_function
*f
, __a_unused
int fd
, int argc
,
236 char * const * const argv
)
238 struct osl_object arg_obj
;
241 return -E_BLOB_SYNTAX
;
242 if (!*argv
[1]) /* empty name is reserved for the dummy row */
243 return -E_BLOB_SYNTAX
;
244 PARA_NOTICE_LOG("argv[1]: %s\n", argv
[1]);
245 arg_obj
.size
= strlen(argv
[1]) + 1;
246 arg_obj
.data
= (char *)argv
[1];
247 return stdin_command(&arg_obj
, f
, 10 * 1024 * 1024, NULL
);
250 static int com_rmblob_callback(struct osl_table
*table
,
251 const struct osl_object
*query
,
252 __a_unused
struct osl_object
*result
)
254 char *p
= query
->data
;
258 for (; p
< (char *)query
->data
+ query
->size
; p
+= len
+ 1) {
260 struct osl_object obj
;
265 ret
= osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
);
268 ret
= osl_del_row(table
, row
);
275 static int com_rmblob(callback_function
*f
, __a_unused
int fd
, int argc
,
276 char * const * const argv
)
279 return -E_MOOD_SYNTAX
;
280 return send_option_arg_callback_request(NULL
, argc
- 1, argv
+ 1, f
,
284 static int com_mvblob_callback(struct osl_table
*table
,
285 const struct osl_object
*query
,
286 __a_unused
struct osl_object
*result
)
288 char *src
= (char *) query
->data
;
289 struct osl_object obj
= {.data
= src
, .size
= strlen(src
) + 1};
290 char *dest
= src
+ obj
.size
;
292 int ret
= osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
);
297 obj
.size
= strlen(dest
) + 1;
298 return osl_update_object(table
, row
, BLOBCOL_NAME
, &obj
);
301 static int com_mvblob(callback_function
*f
, __a_unused
int fd
,
302 int argc
, char * const * const argv
)
305 return -E_MOOD_SYNTAX
;
306 return send_option_arg_callback_request(NULL
, argc
- 1, argv
+ 1, f
,
310 #define DEFINE_BLOB_COMMAND(cmd_name, table_name, cmd_prefix) \
311 static int com_ ## cmd_name ## cmd_prefix ## _callback(const struct osl_object *query, \
312 struct osl_object *output) \
314 return com_ ## cmd_name ## blob_callback(table_name ## _table, query, output); \
316 int com_ ## cmd_name ## cmd_prefix(int fd, int argc, char * const * const argv) \
318 return com_ ## cmd_name ## blob(com_ ## cmd_name ## cmd_prefix ## _callback, fd, argc, argv); \
321 static int blob_get_name_by_id(struct osl_table
*table
, uint32_t id
,
325 struct osl_object obj
= {.data
= &id
, .size
= sizeof(id
)};
331 ret
= osl_get_row(table
, BLOBCOL_ID
, &obj
, &row
);
334 ret
= osl_get_object(table
, row
, BLOBCOL_NAME
, &obj
);
337 *name
= (char *)obj
.data
;
340 /** Define the \p get_name_by_id function for this blob type. */
341 #define DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix) \
342 int cmd_prefix ## _get_name_by_id(uint32_t id, char **name) \
344 return blob_get_name_by_id(table_name ## _table, id, name); \
347 /** Define the \p shutdown function for this blob type. */
348 #define DEFINE_BLOB_SHUTDOWN(table_name) \
349 void table_name ## _shutdown(enum osl_close_flags flags) \
351 osl_close_table(table_name ## _table, flags); \
352 table_name ## _table = NULL; \
355 static int blob_init(struct osl_table
**table
,
356 struct osl_table_description
*desc
,
357 struct table_info
*ti
, const char *db
)
362 ret
= osl_open_table(ti
->desc
, &ti
->table
);
368 return ret
== -E_NOENT
? 1 : ret
;
371 /** Define the \p init function for this blob type. */
372 #define DEFINE_BLOB_INIT(table_name) \
373 int table_name ## _init(struct table_info *ti, const char *db) \
375 return blob_init(&table_name ## _table, \
376 &table_name ## _table_desc, ti, db); \
380 /** Define all functions for this blob type. */
381 #define DEFINE_BLOB_FUNCTIONS(table_name, cmd_prefix) \
382 DEFINE_BLOB_COMMAND(ls, table_name, cmd_prefix) \
383 DEFINE_BLOB_COMMAND(cat, table_name, cmd_prefix) \
384 DEFINE_BLOB_COMMAND(add, table_name, cmd_prefix) \
385 DEFINE_BLOB_COMMAND(rm, table_name, cmd_prefix) \
386 DEFINE_BLOB_COMMAND(mv, table_name, cmd_prefix) \
387 DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix); \
388 DEFINE_BLOB_SHUTDOWN(table_name); \
389 DEFINE_BLOB_INIT(table_name);
391 /** \cond doxygen isn't smart enough to recognize these */
392 DEFINE_BLOB_FUNCTIONS(lyrics
, lyr
);
393 DEFINE_BLOB_FUNCTIONS(images
, img
);
394 DEFINE_BLOB_FUNCTIONS(moods
, mood
);
395 DEFINE_BLOB_FUNCTIONS(playlists
, pl
);