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 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 afs_event(BLOB_RENAME
, NULL
, table
);
251 result
->data
= rmbd
.pb
.buf
;
252 result
->size
= rmbd
.pb
.size
;
256 static int com_rmblob(callback_function
*f
, int fd
, int argc
,
257 char * const * const argv
)
260 struct osl_object result
;
263 return -E_MOOD_SYNTAX
;
264 ret
= send_option_arg_callback_request(NULL
, argc
- 1, argv
+ 1, f
,
267 send_buffer(fd
, (char *)result
.data
);
273 static int com_addblob_callback(struct osl_table
*table
,
274 const struct osl_object
*query
,
275 __a_unused
struct osl_object
*result
)
277 struct osl_object objs
[NUM_BLOB_COLUMNS
];
278 char *name
= query
->data
;
279 size_t name_len
= strlen(name
) + 1;
284 ret
= osl_get_num_rows(table
, &num_rows
);
287 if (!num_rows
) { /* this is the first entry ever added */
288 /* insert dummy row containing the id */
289 id
= 2; /* this entry will be entry #1, so 2 is the next */
290 objs
[BLOBCOL_ID
].data
= &id
;
291 objs
[BLOBCOL_ID
].size
= sizeof(id
);
292 objs
[BLOBCOL_NAME
].data
= "";
293 objs
[BLOBCOL_NAME
].size
= 1;
294 objs
[BLOBCOL_DEF
].data
= "";
295 objs
[BLOBCOL_DEF
].size
= 1;
296 ret
= osl_add_row(table
, objs
);
300 /* check if name already exists */
302 struct osl_object obj
= {.data
= name
, .size
= name_len
};
303 ret
= osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
);
304 if (ret
< 0 && ret
!= -E_RB_KEY_NOT_FOUND
)
306 if (ret
>= 0) { /* we already have a blob with this name */
307 obj
.data
= name
+ name_len
;
308 obj
.size
= query
->size
- name_len
;
309 return osl_update_object(table
, row
, BLOBCOL_DEF
, &obj
);
311 /* new blob, get id of the dummy row and increment it */
314 ret
= osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
);
317 ret
= osl_get_object(table
, row
, BLOBCOL_ID
, &obj
);
320 id
= *(uint32_t *)obj
.data
+ 1;
322 ret
= osl_update_object(table
, row
, BLOBCOL_ID
, &obj
);
327 objs
[BLOBCOL_ID
].data
= &id
;
328 objs
[BLOBCOL_ID
].size
= sizeof(id
);
329 objs
[BLOBCOL_NAME
].data
= name
;
330 objs
[BLOBCOL_NAME
].size
= name_len
;
331 objs
[BLOBCOL_DEF
].data
= name
+ name_len
;
332 objs
[BLOBCOL_DEF
].size
= query
->size
- name_len
;
333 ret
= osl_add_row(table
, objs
);
336 afs_event(BLOB_ADD
, NULL
, table
);
340 static int com_addblob(callback_function
*f
, int fd
, int argc
,
341 char * const * const argv
)
343 struct osl_object arg_obj
;
346 return -E_BLOB_SYNTAX
;
347 if (!*argv
[1]) /* empty name is reserved for the dummy row */
348 return -E_BLOB_SYNTAX
;
349 PARA_NOTICE_LOG("argv[1]: %s\n", argv
[1]);
350 arg_obj
.size
= strlen(argv
[1]) + 1;
351 arg_obj
.data
= (char *)argv
[1];
352 return stdin_command(fd
, &arg_obj
, f
, 10 * 1024 * 1024, NULL
);
355 static int com_mvblob_callback(struct osl_table
*table
,
356 const struct osl_object
*query
,
357 __a_unused
struct osl_object
*result
)
359 char *src
= (char *) query
->data
;
360 struct osl_object obj
= {.data
= src
, .size
= strlen(src
) + 1};
361 char *dest
= src
+ obj
.size
;
363 int ret
= osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
);
368 obj
.size
= strlen(dest
) + 1;
369 ret
= osl_update_object(table
, row
, BLOBCOL_NAME
, &obj
);
372 afs_event(BLOB_RENAME
, NULL
, table
);
376 static int com_mvblob(callback_function
*f
, __a_unused
int fd
,
377 int argc
, char * const * const argv
)
380 return -E_MOOD_SYNTAX
;
381 return send_option_arg_callback_request(NULL
, argc
- 1, argv
+ 1, f
,
385 #define DEFINE_BLOB_COMMAND(cmd_name, table_name, cmd_prefix) \
386 static int com_ ## cmd_name ## cmd_prefix ## _callback(const struct osl_object *query, \
387 struct osl_object *output) \
389 return com_ ## cmd_name ## blob_callback(table_name ## _table, query, output); \
391 int com_ ## cmd_name ## cmd_prefix(int fd, int argc, char * const * const argv) \
393 return com_ ## cmd_name ## blob(com_ ## cmd_name ## cmd_prefix ## _callback, fd, argc, argv); \
396 static int blob_get_name_by_id(struct osl_table
*table
, uint32_t id
,
400 struct osl_object obj
= {.data
= &id
, .size
= sizeof(id
)};
406 ret
= osl_get_row(table
, BLOBCOL_ID
, &obj
, &row
);
409 ret
= osl_get_object(table
, row
, BLOBCOL_NAME
, &obj
);
412 *name
= (char *)obj
.data
;
416 /** Define the \p get_name_by_id function for this blob type. */
417 #define DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix) \
418 int cmd_prefix ## _get_name_by_id(uint32_t id, char **name) \
420 return blob_get_name_by_id(table_name ## _table, id, name); \
424 static int blob_get_def_by_name(struct osl_table
*table
, char *name
,
425 struct osl_object
*def
)
428 struct osl_object obj
= {.data
= name
, .size
= strlen(name
) + 1};
434 ret
= osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
);
437 return osl_open_disk_object(table
, row
, BLOBCOL_DEF
, def
);
440 /** Define the \p get_def_by_id function for this blob type. */
441 #define DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix) \
442 int cmd_prefix ## _get_def_by_name(char *name, struct osl_object *def) \
444 return blob_get_def_by_name(table_name ## _table, name, def); \
447 static int blob_get_def_by_id(struct osl_table
*table
, uint32_t id
,
448 struct osl_object
*def
)
451 struct osl_object obj
= {.data
= &id
, .size
= sizeof(id
)};
457 ret
= osl_get_row(table
, BLOBCOL_ID
, &obj
, &row
);
460 return osl_open_disk_object(table
, row
, BLOBCOL_DEF
, def
);
463 /** Define the \p get_def_by_id function for this blob type. */
464 #define DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix) \
465 int cmd_prefix ## _get_def_by_id(uint32_t id, struct osl_object *def) \
467 return blob_get_def_by_id(table_name ## _table, id, def); \
470 static int blob_get_name_and_def_by_row(struct osl_table
*table
,
471 const struct osl_row
*row
, char **name
, struct osl_object
*def
)
473 struct osl_object obj
;
474 int ret
= osl_get_object(table
, row
, BLOBCOL_NAME
, &obj
);
478 return osl_open_disk_object(table
, row
, BLOBCOL_DEF
, def
);
480 /** Define the \p get_name_and_def_by_row function for this blob type. */
481 #define DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix) \
482 int cmd_prefix ## _get_name_and_def_by_row(const struct osl_row *row, \
483 char **name, struct osl_object *def) \
485 return blob_get_name_and_def_by_row(table_name ## _table, \
489 /** Define the \p close function for this blob type. */
490 #define DEFINE_BLOB_CLOSE(table_name) \
491 void table_name ## _close(void) \
493 osl_close_table(table_name ## _table, OSL_MARK_CLEAN); \
494 table_name ## _table = NULL; \
497 /** Define the \p create function for this blob type. */
498 #define DEFINE_BLOB_CREATE(table_name) \
499 int table_name ## _create(const char *dir) \
501 table_name ## _table_desc.dir = dir; \
502 return osl_create_table(&table_name ## _table_desc); \
505 static int blob_open(struct osl_table
**table
,
506 struct osl_table_description
*desc
,
511 ret
= osl_open_table(desc
, table
);
515 if (ret
>= 0 || is_errno(-ret
, ENOENT
))
520 #define DEFINE_BLOB_OPEN(table_name) \
521 int table_name ## _open(const char *dir) \
523 return blob_open(&table_name ## _table, \
524 &table_name ## _table_desc, dir); \
528 /** Define the \p init function for this blob type. */
529 #define DEFINE_BLOB_INIT(table_name) \
530 void table_name ## _init(struct afs_table *t) \
532 t->name = table_name ## _table_desc.name; \
533 t->open = table_name ## _open; \
534 t->close = table_name ## _close; \
535 t->create = table_name ## _create;\
536 t->event_handler = table_name ##_event_handler; \
540 /** Define all functions for this blob type. */
541 #define DEFINE_BLOB_FUNCTIONS(table_name, cmd_prefix) \
542 DEFINE_BLOB_OPEN(table_name) \
543 DEFINE_BLOB_CLOSE(table_name) \
544 DEFINE_BLOB_CREATE(table_name) \
545 DEFINE_BLOB_INIT(table_name) \
546 DEFINE_BLOB_COMMAND(ls, table_name, cmd_prefix) \
547 DEFINE_BLOB_COMMAND(cat, table_name, cmd_prefix) \
548 DEFINE_BLOB_COMMAND(add, table_name, cmd_prefix) \
549 DEFINE_BLOB_COMMAND(rm, table_name, cmd_prefix) \
550 DEFINE_BLOB_COMMAND(mv, table_name, cmd_prefix) \
551 DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix); \
552 DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix); \
553 DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix); \
554 DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix); \
556 /** \cond doxygen isn't smart enough to recognize these */
557 DEFINE_BLOB_FUNCTIONS(lyrics
, lyr
);
558 DEFINE_BLOB_FUNCTIONS(images
, img
);
559 DEFINE_BLOB_FUNCTIONS(moods
, mood
);
560 DEFINE_BLOB_FUNCTIONS(playlists
, pl
);