2 * Copyright (C) 2007 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file blob.c Macros and functions for blob handling. */
20 #include "portable_io.h"
25 * Compare two osl objects pointing to unsigned integers of 32 bit size.
27 * \param obj1 Pointer to the first integer.
28 * \param obj2 Pointer to the second integer.
30 * \return The values required for an osl compare function.
32 * \sa osl_compare_func, osl_hash_compare().
34 static int uint32_compare(const struct osl_object
*obj1
, const struct osl_object
*obj2
)
36 uint32_t d1
= read_u32((const char *)obj1
->data
);
37 uint32_t d2
= read_u32((const char *)obj2
->data
);
46 static struct osl_column_description blob_cols
[] = {
48 .storage_type
= OSL_MAPPED_STORAGE
,
49 .storage_flags
= OSL_RBTREE
| OSL_UNIQUE
| OSL_FIXED_SIZE
,
52 .compare_function
= uint32_compare
55 .storage_type
= OSL_MAPPED_STORAGE
,
56 .storage_flags
= OSL_RBTREE
| OSL_UNIQUE
,
58 .compare_function
= string_compare
61 .storage_type
= OSL_DISK_STORAGE
,
67 /** Define an osl table description for a blob table. */
68 #define DEFINE_BLOB_TABLE_DESC(table_name) \
69 static struct osl_table_description table_name ## _table_desc = { \
70 .name = #table_name, \
71 .num_columns = NUM_BLOB_COLUMNS, \
72 .flags = OSL_LARGE_TABLE, \
73 .column_descriptions = blob_cols \
76 /** Define a pointer to an osl blob table with a canonical name. */
77 #define DEFINE_BLOB_TABLE_PTR(table_name) struct osl_table *table_name ## _table;
80 /** Define a blob table. */
81 #define INIT_BLOB_TABLE(table_name) \
82 DEFINE_BLOB_TABLE_DESC(table_name); \
83 DEFINE_BLOB_TABLE_PTR(table_name);
85 /* doxygen isn't smart enough to recognize these */
86 /** \cond blob_table */
87 INIT_BLOB_TABLE(lyrics
);
88 INIT_BLOB_TABLE(images
);
89 INIT_BLOB_TABLE(moods
);
90 INIT_BLOB_TABLE(playlists
);
91 /** \endcond blob_table */
93 /** Flags that may be passed to the \p ls functions of each blob type. */
95 /** List both id and name. */
96 BLOB_LS_FLAG_LONG
= 1,
97 /** Reverse sort order. */
98 BLOB_LS_FLAG_REVERSE
= 2,
99 /** Sort by id instead of name. */
100 BLOB_LS_FLAG_SORT_BY_ID
= 4,
103 static int print_blob(struct osl_table
*table
, struct osl_row
*row
,
104 const char *name
, void *data
)
106 struct afs_callback_arg
*aca
= data
;
107 uint32_t flags
= *(uint32_t *)aca
->query
.data
;
108 struct osl_object obj
;
112 if (!(flags
& BLOB_LS_FLAG_LONG
)) {
113 para_printf(&aca
->pbout
, "%s\n", name
);
116 ret
= osl(osl_get_object(table
, row
, BLOBCOL_ID
, &obj
));
118 para_printf(&aca
->pbout
, "cannot list %s\n", name
);
121 id
= *(uint32_t *)obj
.data
;
122 para_printf(&aca
->pbout
, "%u\t%s\n", id
, name
);
126 static int com_lsblob_callback(struct osl_table
*table
,
127 struct afs_callback_arg
*aca
)
129 uint32_t flags
= *(uint32_t *)aca
->query
.data
;
130 struct pattern_match_data pmd
= {
132 .patterns
= {.data
= (char *)aca
->query
.data
+ sizeof(uint32_t),
133 .size
= aca
->query
.size
- sizeof(uint32_t)},
134 .pm_flags
= PM_NO_PATTERN_MATCHES_EVERYTHING
| PM_SKIP_EMPTY_NAME
,
135 .match_col_num
= BLOBCOL_NAME
,
137 .action
= print_blob
,
141 if (flags
& BLOB_LS_FLAG_REVERSE
)
142 pmd
.pm_flags
|= PM_REVERSE_LOOP
;
143 if (!(flags
& BLOB_LS_FLAG_SORT_BY_ID
))
144 pmd
.loop_col_num
= BLOBCOL_NAME
;
146 pmd
.loop_col_num
= BLOBCOL_ID
;
147 ret
= for_each_matching_row(&pmd
);
150 if (pmd
.num_matches
== 0 && pmd
.patterns
.size
> 0)
156 static int com_lsblob(afs_callback
*f
, struct command_context
*cc
)
159 struct osl_object options
= {.data
= &flags
, .size
= sizeof(flags
)};
162 for (i
= 1; i
< cc
->argc
; i
++) {
163 const char *arg
= cc
->argv
[i
];
166 if (!strcmp(arg
, "--")) {
170 if (!strcmp(arg
, "-l")) {
171 flags
|= BLOB_LS_FLAG_LONG
;
174 if (!strcmp(arg
, "-i")) {
175 flags
|= BLOB_LS_FLAG_SORT_BY_ID
;
178 if (!strcmp(arg
, "-r")) {
179 flags
|= BLOB_LS_FLAG_REVERSE
;
185 // return -E_BLOB_SYNTAX;
186 return send_option_arg_callback_request(&options
, cc
->argc
- i
,
187 cc
->argv
+ i
, f
, afs_cb_result_handler
, cc
);
190 static int cat_blob(struct osl_table
*table
, struct osl_row
*row
,
191 __a_unused
const char *name
, void *data
)
193 int ret
= 0, ret2
, fd
= *(int *)data
;
194 struct osl_object obj
;
196 ret
= osl(osl_open_disk_object(table
, row
, BLOBCOL_DEF
, &obj
));
198 return (ret
== osl(-E_OSL_EMPTY
))? 0 : ret
;
199 assert(obj
.size
> 0);
200 ret
= pass_buffer_as_shm(fd
, SBD_OUTPUT
, obj
.data
, obj
.size
);
201 ret2
= osl(osl_close_disk_object(&obj
));
202 return (ret
< 0)? ret
: ret2
;
205 static int com_catblob_callback(struct osl_table
*table
,
206 struct afs_callback_arg
*aca
)
209 struct pattern_match_data pmd
= {
211 .patterns
= aca
->query
,
212 .loop_col_num
= BLOBCOL_NAME
,
213 .match_col_num
= BLOBCOL_NAME
,
214 .pm_flags
= PM_SKIP_EMPTY_NAME
,
218 ret
= for_each_matching_row(&pmd
);
221 if (pmd
.num_matches
== 0)
226 static int com_catblob(afs_callback
*f
, struct command_context
*cc
)
229 return -E_BLOB_SYNTAX
;
230 return send_standard_callback_request(cc
->argc
- 1, cc
->argv
+ 1, f
,
231 afs_cb_result_handler
, cc
);
234 static int remove_blob(struct osl_table
*table
, struct osl_row
*row
,
235 const char *name
, void *data
)
237 struct afs_callback_arg
*aca
= data
;
238 int ret
= osl(osl_del_row(table
, row
));
240 para_printf(&aca
->pbout
, "cannot remove %s\n", name
);
246 static int com_rmblob_callback(struct osl_table
*table
,
247 struct afs_callback_arg
*aca
)
250 struct pattern_match_data pmd
= {
252 .patterns
= aca
->query
,
253 .loop_col_num
= BLOBCOL_NAME
,
254 .match_col_num
= BLOBCOL_NAME
,
255 .pm_flags
= PM_SKIP_EMPTY_NAME
,
257 .action
= remove_blob
259 ret
= for_each_matching_row(&pmd
);
262 if (pmd
.num_matches
== 0)
265 para_printf(&aca
->pbout
, "removed %d blob(s)\n",
267 ret
= afs_event(BLOB_REMOVE
, NULL
, table
);
273 static int com_rmblob(afs_callback
*f
, struct command_context
*cc
)
276 return -E_MOOD_SYNTAX
;
277 return send_option_arg_callback_request(NULL
, cc
->argc
- 1, cc
->argv
+ 1, f
,
278 afs_cb_result_handler
, cc
);
281 static int com_addblob_callback(struct osl_table
*table
,
282 struct afs_callback_arg
*aca
)
284 struct osl_object objs
[NUM_BLOB_COLUMNS
];
285 char *name
= aca
->query
.data
;
286 size_t name_len
= strlen(name
) + 1;
291 ret
= osl(osl_get_num_rows(table
, &num_rows
));
294 if (!num_rows
) { /* this is the first entry ever added */
295 /* insert dummy row containing the id */
296 id
= 2; /* this entry will be entry #1, so 2 is the next */
297 objs
[BLOBCOL_ID
].data
= &id
;
298 objs
[BLOBCOL_ID
].size
= sizeof(id
);
299 objs
[BLOBCOL_NAME
].data
= "";
300 objs
[BLOBCOL_NAME
].size
= 1;
301 objs
[BLOBCOL_DEF
].data
= "";
302 objs
[BLOBCOL_DEF
].size
= 1;
303 ret
= osl(osl_add_row(table
, objs
));
307 /* check if name already exists */
309 struct osl_object obj
= {.data
= name
, .size
= name_len
};
310 ret
= osl(osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
));
311 if (ret
< 0 && ret
!= -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND
))
313 if (ret
>= 0) { /* we already have a blob with this name */
314 ret
= osl(osl_get_object(table
, row
, BLOBCOL_ID
, &obj
));
317 id
= *(uint32_t *)obj
.data
;
318 obj
.data
= name
+ name_len
;
319 obj
.size
= aca
->query
.size
- name_len
;
320 ret
= osl(osl_update_object(table
, row
, BLOBCOL_DEF
, &obj
));
323 /* new blob, get id of the dummy row and increment it */
326 ret
= osl(osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
));
329 ret
= osl(osl_get_object(table
, row
, BLOBCOL_ID
, &obj
));
332 id
= *(uint32_t *)obj
.data
+ 1;
334 ret
= osl(osl_update_object(table
, row
, BLOBCOL_ID
, &obj
));
339 objs
[BLOBCOL_ID
].data
= &id
;
340 objs
[BLOBCOL_ID
].size
= sizeof(id
);
341 objs
[BLOBCOL_NAME
].data
= name
;
342 objs
[BLOBCOL_NAME
].size
= name_len
;
343 objs
[BLOBCOL_DEF
].data
= name
+ name_len
;
344 objs
[BLOBCOL_DEF
].size
= aca
->query
.size
- name_len
;
345 ret
= osl(osl_add_row(table
, objs
));
348 ret
= afs_event(BLOB_ADD
, NULL
, table
);
351 para_printf(&aca
->pbout
, "cannot add %s\n", name
);
353 para_printf(&aca
->pbout
, "added %s as id %u\n", name
, id
);
357 /* Write input from fd to dynamically allocated buffer, but maximal 10M. */
358 static int fd2buf(struct stream_cipher_context
*scc
, struct osl_object
*obj
)
360 size_t max_size
= 10 * 1024 * 1024;
368 ret
= recv_sb(scc
, SBD_BLOB_DATA
, max_size
, &iov
);
377 if (iov
.iov_len
== 0) /* end of blob */
380 obj
->data
= iov
.iov_base
;
381 obj
->size
= iov
.iov_len
;
383 obj
->data
= para_realloc(obj
->data
, obj
->size
+ iov
.iov_len
);
384 memcpy(obj
->data
+ obj
->size
, iov
.iov_base
, iov
.iov_len
);
385 obj
->size
+= iov
.iov_len
;
387 max_size
-= iov
.iov_len
;
394 * Read blob from a file descriptor and send it to afs.
396 * This function is called from the addblob command handlers to instruct the
397 * afs process to store the input in a blob table. Input is read and decrypted
398 * from the file descriptor given by cc and appended to arg_obj, which contains
399 * the name of the blob to create. The combined buffer is made available to the
400 * afs process via the callback method.
402 static int stdin_command(struct command_context
*cc
, struct osl_object
*arg_obj
,
405 struct osl_object query
, stdin_obj
;
408 ret
= send_sb(&cc
->scc
, NULL
, 0, SBD_AWAITING_DATA
, false);
411 ret
= fd2buf(&cc
->scc
, &stdin_obj
);
414 query
.size
= arg_obj
->size
+ stdin_obj
.size
;
415 query
.data
= para_malloc(query
.size
);
416 memcpy(query
.data
, arg_obj
->data
, arg_obj
->size
);
417 if (stdin_obj
.size
> 0)
418 memcpy((char *)query
.data
+ arg_obj
->size
, stdin_obj
.data
,
420 free(stdin_obj
.data
);
421 ret
= send_callback_request(f
, &query
, afs_cb_result_handler
, cc
);
426 static int com_addblob(afs_callback
*f
, struct command_context
*cc
)
428 struct osl_object arg_obj
;
431 return -E_BLOB_SYNTAX
;
432 if (!*cc
->argv
[1]) /* empty name is reserved for the dummy row */
433 return -E_BLOB_SYNTAX
;
434 arg_obj
.size
= strlen(cc
->argv
[1]) + 1;
435 arg_obj
.data
= (char *)cc
->argv
[1];
436 return stdin_command(cc
, &arg_obj
, f
);
439 static int com_mvblob_callback(struct osl_table
*table
,
440 struct afs_callback_arg
*aca
)
442 char *src
= (char *)aca
->query
.data
;
443 struct osl_object obj
= {.data
= src
, .size
= strlen(src
) + 1};
444 char *dest
= src
+ obj
.size
;
446 int ret
= osl(osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
));
449 para_printf(&aca
->pbout
, "cannot find source blob %s\n", src
);
453 obj
.size
= strlen(dest
) + 1;
454 ret
= osl(osl_update_object(table
, row
, BLOBCOL_NAME
, &obj
));
456 para_printf(&aca
->pbout
, "cannot rename blob %s to %s\n",
460 ret
= afs_event(BLOB_RENAME
, NULL
, table
);
465 static int com_mvblob(afs_callback
*f
, struct command_context
*cc
)
468 return -E_MOOD_SYNTAX
;
469 return send_option_arg_callback_request(NULL
, cc
->argc
- 1,
470 cc
->argv
+ 1, f
, afs_cb_result_handler
, cc
);
473 #define DEFINE_BLOB_COMMAND(cmd_name, table_name, cmd_prefix) \
474 static int com_ ## cmd_name ## cmd_prefix ## _callback(struct afs_callback_arg *aca) \
476 return com_ ## cmd_name ## blob_callback(table_name ## _table, aca); \
478 int com_ ## cmd_name ## cmd_prefix(struct command_context *cc) \
480 return com_ ## cmd_name ## blob(com_ ## cmd_name ## cmd_prefix ## _callback, cc); \
483 static int blob_get_name_by_id(struct osl_table
*table
, uint32_t id
,
487 struct osl_object obj
= {.data
= &id
, .size
= sizeof(id
)};
493 ret
= osl(osl_get_row(table
, BLOBCOL_ID
, &obj
, &row
));
496 ret
= osl(osl_get_object(table
, row
, BLOBCOL_NAME
, &obj
));
499 *name
= (char *)obj
.data
;
503 /** Define the \p get_name_by_id function for this blob type. */
504 #define DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix) \
505 int cmd_prefix ## _get_name_by_id(uint32_t id, char **name) \
507 return blob_get_name_by_id(table_name ## _table, id, name); \
511 static int blob_get_def_by_name(struct osl_table
*table
, char *name
,
512 struct osl_object
*def
)
515 struct osl_object obj
= {.data
= name
, .size
= strlen(name
) + 1};
521 ret
= osl(osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
));
524 return osl(osl_open_disk_object(table
, row
, BLOBCOL_DEF
, def
));
527 /** Define the \p get_def_by_id function for this blob type. */
528 #define DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix) \
529 int cmd_prefix ## _get_def_by_name(char *name, struct osl_object *def) \
531 return blob_get_def_by_name(table_name ## _table, name, def); \
534 static int blob_get_def_by_id(struct osl_table
*table
, uint32_t id
,
535 struct osl_object
*def
)
538 struct osl_object obj
= {.data
= &id
, .size
= sizeof(id
)};
544 ret
= osl(osl_get_row(table
, BLOBCOL_ID
, &obj
, &row
));
547 return osl(osl_open_disk_object(table
, row
, BLOBCOL_DEF
, def
));
550 /** Define the \p get_def_by_id function for this blob type. */
551 #define DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix) \
552 int cmd_prefix ## _get_def_by_id(uint32_t id, struct osl_object *def) \
554 return blob_get_def_by_id(table_name ## _table, id, def); \
557 static int blob_get_name_and_def_by_row(struct osl_table
*table
,
558 const struct osl_row
*row
, char **name
, struct osl_object
*def
)
560 struct osl_object obj
;
561 int ret
= osl(osl_get_object(table
, row
, BLOBCOL_NAME
, &obj
));
565 return osl(osl_open_disk_object(table
, row
, BLOBCOL_DEF
, def
));
567 /** Define the \p get_name_and_def_by_row function for this blob type. */
568 #define DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix) \
569 int cmd_prefix ## _get_name_and_def_by_row(const struct osl_row *row, \
570 char **name, struct osl_object *def) \
572 return blob_get_name_and_def_by_row(table_name ## _table, \
576 /** Define the \p close function for this blob type. */
577 #define DEFINE_BLOB_CLOSE(table_name) \
578 static void table_name ## _close(void) \
580 osl_close_table(table_name ## _table, OSL_MARK_CLEAN); \
581 table_name ## _table = NULL; \
584 /** Define the \p create function for this blob type. */
585 #define DEFINE_BLOB_CREATE(table_name) \
586 static int table_name ## _create(const char *dir) \
588 table_name ## _table_desc.dir = dir; \
589 return osl(osl_create_table(&table_name ## _table_desc)); \
592 static int blob_open(struct osl_table
**table
,
593 struct osl_table_description
*desc
,
598 ret
= osl(osl_open_table(desc
, table
));
602 if (ret
>= 0 || ret
== -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT
))
607 #define DEFINE_BLOB_OPEN(table_name) \
608 static int table_name ## _open(const char *dir) \
610 return blob_open(&table_name ## _table, \
611 &table_name ## _table_desc, dir); \
615 /** Define the \p init function for this blob type. */
616 #define DEFINE_BLOB_INIT(table_name) \
617 void table_name ## _init(struct afs_table *t) \
619 t->open = table_name ## _open; \
620 t->close = table_name ## _close; \
621 t->create = table_name ## _create;\
622 t->event_handler = table_name ##_event_handler; \
623 table_name ## _table = NULL; \
627 /** Define all functions for this blob type. */
628 #define DEFINE_BLOB_FUNCTIONS(table_name, cmd_prefix) \
629 DEFINE_BLOB_OPEN(table_name) \
630 DEFINE_BLOB_CLOSE(table_name) \
631 DEFINE_BLOB_CREATE(table_name) \
632 DEFINE_BLOB_INIT(table_name) \
633 DEFINE_BLOB_COMMAND(ls, table_name, cmd_prefix) \
634 DEFINE_BLOB_COMMAND(cat, table_name, cmd_prefix) \
635 DEFINE_BLOB_COMMAND(add, table_name, cmd_prefix) \
636 DEFINE_BLOB_COMMAND(rm, table_name, cmd_prefix) \
637 DEFINE_BLOB_COMMAND(mv, table_name, cmd_prefix) \
638 DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix); \
639 DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix); \
640 DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix); \
641 DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix); \
643 /* doxygen isn't smart enough to recognize these */
644 /** \cond blob_function */
645 DEFINE_BLOB_FUNCTIONS(lyrics
, lyr
);
646 DEFINE_BLOB_FUNCTIONS(images
, img
);
647 DEFINE_BLOB_FUNCTIONS(moods
, mood
);
648 DEFINE_BLOB_FUNCTIONS(playlists
, pl
);
649 /** \endcond blob_function */