2 * Copyright (C) 2007-2010 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. */
11 #include <openssl/rc4.h>
22 #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 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 /** \cond doxygen isn't smart enough to recognize these */
86 INIT_BLOB_TABLE(lyrics
);
87 INIT_BLOB_TABLE(images
);
88 INIT_BLOB_TABLE(moods
);
89 INIT_BLOB_TABLE(playlists
);
92 /** Flags that may be passed to the \p ls functions of each blob type. */
94 /** List both id and name. */
95 BLOB_LS_FLAG_LONG
= 1,
96 /** Reverse sort order. */
97 BLOB_LS_FLAG_REVERSE
= 2,
98 /** Sort by id instead of name. */
99 BLOB_LS_FLAG_SORT_BY_ID
= 4,
102 /** Structure passed to the \p print_blob function. */
103 struct lsblob_action_data
{
104 /** The flags given at the command line. */
106 /** Message buffer. */
107 struct para_buffer pb
;
110 static int print_blob(struct osl_table
*table
, struct osl_row
*row
,
111 const char *name
, void *data
)
113 struct lsblob_action_data
*lbad
= data
;
114 struct osl_object obj
;
118 if (!(lbad
->flags
& BLOB_LS_FLAG_LONG
))
119 return para_printf(&lbad
->pb
, "%s\n", name
);
120 ret
= osl(osl_get_object(table
, row
, BLOBCOL_ID
, &obj
));
122 para_printf(&lbad
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
125 id
= *(uint32_t *)obj
.data
;
126 return para_printf(&lbad
->pb
, "%u\t%s\n", id
, name
);
129 static void com_lsblob_callback(struct osl_table
*table
,
130 int fd
, const struct osl_object
*query
)
132 struct lsblob_action_data lbad
= {
133 .flags
= *(uint32_t *)query
->data
,
137 .max_size_handler
= pass_buffer_as_shm
140 struct pattern_match_data pmd
= {
142 .patterns
= {.data
= (char *)query
->data
+ sizeof(uint32_t),
143 .size
= query
->size
- sizeof(uint32_t)},
144 .pm_flags
= PM_NO_PATTERN_MATCHES_EVERYTHING
| PM_SKIP_EMPTY_NAME
,
145 .match_col_num
= BLOBCOL_NAME
,
147 .action
= print_blob
,
151 if (lbad
.flags
& BLOB_LS_FLAG_REVERSE
)
152 pmd
.pm_flags
|= PM_REVERSE_LOOP
;
153 if (!(lbad
.flags
& BLOB_LS_FLAG_SORT_BY_ID
))
154 pmd
.loop_col_num
= BLOBCOL_NAME
;
156 pmd
.loop_col_num
= BLOBCOL_ID
;
157 ret
= for_each_matching_row(&pmd
);
159 para_printf(&lbad
.pb
, "%s\n", para_strerror(-ret
));
161 pass_buffer_as_shm(lbad
.pb
.buf
, lbad
.pb
.offset
, &fd
);
165 static int com_lsblob(callback_function
*f
, struct rc4_context
*rc4c
, int argc
, char * const * const argv
)
168 struct osl_object options
= {.data
= &flags
, .size
= sizeof(flags
)};
171 for (i
= 1; i
< argc
; i
++) {
172 const char *arg
= argv
[i
];
175 if (!strcmp(arg
, "--")) {
179 if (!strcmp(arg
, "-l")) {
180 flags
|= BLOB_LS_FLAG_LONG
;
183 if (!strcmp(arg
, "-i")) {
184 flags
|= BLOB_LS_FLAG_SORT_BY_ID
;
187 if (!strcmp(arg
, "-r")) {
188 flags
|= BLOB_LS_FLAG_REVERSE
;
194 // return -E_BLOB_SYNTAX;
195 return send_option_arg_callback_request(&options
, argc
- i
,
196 argv
+ i
, f
, rc4_send_result
, rc4c
);
199 static int cat_blob(struct osl_table
*table
, struct osl_row
*row
,
200 __a_unused
const char *name
, void *data
)
203 struct osl_object obj
;
205 ret
= osl(osl_open_disk_object(table
, row
, BLOBCOL_DEF
, &obj
));
209 ret
= pass_buffer_as_shm(obj
.data
, obj
.size
, data
);
210 ret2
= osl(osl_close_disk_object(&obj
));
211 return (ret
< 0)? ret
: ret2
;
214 static void com_catblob_callback(struct osl_table
*table
, int fd
,
215 const struct osl_object
*query
)
217 struct pattern_match_data pmd
= {
220 .loop_col_num
= BLOBCOL_NAME
,
221 .match_col_num
= BLOBCOL_NAME
,
222 .pm_flags
= PM_SKIP_EMPTY_NAME
,
226 for_each_matching_row(&pmd
);
229 static int com_catblob(callback_function
*f
, struct rc4_context
*rc4c
, int argc
,
230 char * const * const argv
)
233 return -E_BLOB_SYNTAX
;
234 return send_standard_callback_request(argc
- 1, argv
+ 1, f
,
235 rc4_send_result
, rc4c
);
238 /** Used for removing rows from a blob table. */
240 /** Message buffer. */
241 struct para_buffer pb
;
242 /** Number of removed blobs. */
243 unsigned num_removed
;
246 static int remove_blob(struct osl_table
*table
, struct osl_row
*row
,
247 const char *name
, void *data
)
249 struct rmblob_data
*rmbd
= data
;
250 int ret
= osl(osl_del_row(table
, row
));
252 para_printf(&rmbd
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
259 static void com_rmblob_callback(struct osl_table
*table
, int fd
,
260 const struct osl_object
*query
)
263 struct rmblob_data rmbd
= {
268 .max_size_handler
= pass_buffer_as_shm
271 struct pattern_match_data pmd
= {
274 .loop_col_num
= BLOBCOL_NAME
,
275 .match_col_num
= BLOBCOL_NAME
,
276 .pm_flags
= PM_SKIP_EMPTY_NAME
,
278 .action
= remove_blob
280 ret
= for_each_matching_row(&pmd
);
282 ret2
= para_printf(&rmbd
.pb
, "%s\n", para_strerror(-ret
));
286 if (!rmbd
.num_removed
)
287 ret2
= para_printf(&rmbd
.pb
, "no matches, nothing removed\n");
289 ret2
= para_printf(&rmbd
.pb
, "removed %d blobs\n", rmbd
.num_removed
);
290 afs_event(BLOB_RENAME
, NULL
, table
);
293 if (ret2
>= 0 && rmbd
.pb
.offset
)
294 pass_buffer_as_shm(rmbd
.pb
.buf
, rmbd
.pb
.offset
, &fd
);
298 static int com_rmblob(callback_function
*f
, struct rc4_context
*rc4c
, int argc
,
299 char * const * const argv
)
302 return -E_MOOD_SYNTAX
;
303 return send_option_arg_callback_request(NULL
, argc
- 1, argv
+ 1, f
,
304 rc4_send_result
, rc4c
);
307 static void com_addblob_callback(struct osl_table
*table
, __a_unused
int fd
,
308 const struct osl_object
*query
)
310 struct osl_object objs
[NUM_BLOB_COLUMNS
];
311 char *name
= query
->data
;
312 size_t name_len
= strlen(name
) + 1;
317 ret
= osl(osl_get_num_rows(table
, &num_rows
));
320 if (!num_rows
) { /* this is the first entry ever added */
321 /* insert dummy row containing the id */
322 id
= 2; /* this entry will be entry #1, so 2 is the next */
323 objs
[BLOBCOL_ID
].data
= &id
;
324 objs
[BLOBCOL_ID
].size
= sizeof(id
);
325 objs
[BLOBCOL_NAME
].data
= "";
326 objs
[BLOBCOL_NAME
].size
= 1;
327 objs
[BLOBCOL_DEF
].data
= "";
328 objs
[BLOBCOL_DEF
].size
= 1;
329 ret
= osl(osl_add_row(table
, objs
));
333 /* check if name already exists */
335 struct osl_object obj
= {.data
= name
, .size
= name_len
};
336 ret
= osl(osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
));
337 if (ret
< 0 && ret
!= -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND
))
339 if (ret
>= 0) { /* we already have a blob with this name */
340 obj
.data
= name
+ name_len
;
341 obj
.size
= query
->size
- name_len
;
342 ret
= osl(osl_update_object(table
, row
, BLOBCOL_DEF
, &obj
));
345 /* new blob, get id of the dummy row and increment it */
348 ret
= osl(osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
));
351 ret
= osl(osl_get_object(table
, row
, BLOBCOL_ID
, &obj
));
354 id
= *(uint32_t *)obj
.data
+ 1;
356 ret
= osl(osl_update_object(table
, row
, BLOBCOL_ID
, &obj
));
361 objs
[BLOBCOL_ID
].data
= &id
;
362 objs
[BLOBCOL_ID
].size
= sizeof(id
);
363 objs
[BLOBCOL_NAME
].data
= name
;
364 objs
[BLOBCOL_NAME
].size
= name_len
;
365 objs
[BLOBCOL_DEF
].data
= name
+ name_len
;
366 objs
[BLOBCOL_DEF
].size
= query
->size
- name_len
;
367 ret
= osl(osl_add_row(table
, objs
));
370 afs_event(BLOB_ADD
, NULL
, table
);
373 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
377 * write input from fd to dynamically allocated buffer,
378 * but maximal max_size byte.
380 static int fd2buf(struct rc4_context
*rc4c
, unsigned max_size
, struct osl_object
*obj
)
382 const size_t chunk_size
= 1024;
383 size_t size
= 2048, received
= 0;
385 char *buf
= para_malloc(size
);
388 ret
= rc4_recv_bin_buffer(rc4c
, buf
+ received
, chunk_size
);
392 if (received
+ chunk_size
>= size
) {
394 ret
= -E_INPUT_TOO_LARGE
;
397 buf
= para_realloc(buf
, size
);
401 obj
->size
= received
;
408 * Read data from a file descriptor, and send it to the afs process.
410 * \param rc4c crypt context containing the file descriptor to read data from.
411 * \param arg_obj Pointer to the arguments to \a f.
412 * \param f The callback function.
413 * \param max_len Don't read more than that many bytes from stdin.
414 * \param result_handler See \ref send_callback_request.
415 * \param private_result_data See \ref send_callback_request.
417 * This function is used by commands that wish to let para_server store
418 * arbitrary data specified by the user (for instance the add_blob family of
419 * commands). First, at most \a max_len bytes are read and decrypted from the
420 * file descriptor given by \a rc4c. The result is concatenated with the buffer
421 * given by \a arg_obj, and the combined buffer is made available to the afs
422 * process via the callback method. See \ref send_callback_request for details.
424 * \return Negative on errors, the return value of the underlying call to
425 * send_callback_request() otherwise.
427 static int stdin_command(struct rc4_context
*rc4c
, struct osl_object
*arg_obj
,
428 callback_function
*f
, unsigned max_len
,
429 callback_result_handler
*result_handler
,
430 void *private_result_data
)
432 struct osl_object query
, stdin_obj
;
435 ret
= rc4_send_buffer(rc4c
, AWAITING_DATA_MSG
);
438 ret
= fd2buf(rc4c
, max_len
, &stdin_obj
);
441 query
.size
= arg_obj
->size
+ stdin_obj
.size
;
442 query
.data
= para_malloc(query
.size
);
443 memcpy(query
.data
, arg_obj
->data
, arg_obj
->size
);
444 memcpy((char *)query
.data
+ arg_obj
->size
, stdin_obj
.data
, stdin_obj
.size
);
445 free(stdin_obj
.data
);
446 ret
= send_callback_request(f
, &query
, result_handler
, private_result_data
);
451 static int com_addblob(callback_function
*f
, struct rc4_context
*rc4c
, int argc
,
452 char * const * const argv
)
454 struct osl_object arg_obj
;
457 return -E_BLOB_SYNTAX
;
458 if (!*argv
[1]) /* empty name is reserved for the dummy row */
459 return -E_BLOB_SYNTAX
;
460 arg_obj
.size
= strlen(argv
[1]) + 1;
461 arg_obj
.data
= (char *)argv
[1];
462 return stdin_command(rc4c
, &arg_obj
, f
, 10 * 1024 * 1024, NULL
, NULL
);
465 /* FIXME: Print output to client, not to log file */
466 static void com_mvblob_callback(struct osl_table
*table
, __a_unused
int fd
,
467 const struct osl_object
*query
)
469 char *src
= (char *) query
->data
;
470 struct osl_object obj
= {.data
= src
, .size
= strlen(src
) + 1};
471 char *dest
= src
+ obj
.size
;
473 int ret
= osl(osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
));
478 obj
.size
= strlen(dest
) + 1;
479 ret
= osl(osl_update_object(table
, row
, BLOBCOL_NAME
, &obj
));
482 afs_event(BLOB_RENAME
, NULL
, table
);
485 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
488 static int com_mvblob(callback_function
*f
, __a_unused
struct rc4_context
*rc4c
,
489 int argc
, char * const * const argv
)
492 return -E_MOOD_SYNTAX
;
493 return send_option_arg_callback_request(NULL
, argc
- 1, argv
+ 1, f
,
497 #define DEFINE_BLOB_COMMAND(cmd_name, table_name, cmd_prefix) \
498 static void com_ ## cmd_name ## cmd_prefix ## _callback(int fd, const struct osl_object *query) \
500 return com_ ## cmd_name ## blob_callback(table_name ## _table, fd, query); \
502 int com_ ## cmd_name ## cmd_prefix(struct rc4_context *rc4c, int argc, char * const * const argv) \
504 return com_ ## cmd_name ## blob(com_ ## cmd_name ## cmd_prefix ## _callback, rc4c, argc, argv); \
507 static int blob_get_name_by_id(struct osl_table
*table
, uint32_t id
,
511 struct osl_object obj
= {.data
= &id
, .size
= sizeof(id
)};
517 ret
= osl(osl_get_row(table
, BLOBCOL_ID
, &obj
, &row
));
520 ret
= osl(osl_get_object(table
, row
, BLOBCOL_NAME
, &obj
));
523 *name
= (char *)obj
.data
;
527 /** Define the \p get_name_by_id function for this blob type. */
528 #define DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix) \
529 int cmd_prefix ## _get_name_by_id(uint32_t id, char **name) \
531 return blob_get_name_by_id(table_name ## _table, id, name); \
535 static int blob_get_def_by_name(struct osl_table
*table
, char *name
,
536 struct osl_object
*def
)
539 struct osl_object obj
= {.data
= name
, .size
= strlen(name
) + 1};
545 ret
= osl(osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
));
548 return osl(osl_open_disk_object(table
, row
, BLOBCOL_DEF
, def
));
551 /** Define the \p get_def_by_id function for this blob type. */
552 #define DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix) \
553 int cmd_prefix ## _get_def_by_name(char *name, struct osl_object *def) \
555 return blob_get_def_by_name(table_name ## _table, name, def); \
558 static int blob_get_def_by_id(struct osl_table
*table
, uint32_t id
,
559 struct osl_object
*def
)
562 struct osl_object obj
= {.data
= &id
, .size
= sizeof(id
)};
568 ret
= osl(osl_get_row(table
, BLOBCOL_ID
, &obj
, &row
));
571 return osl(osl_open_disk_object(table
, row
, BLOBCOL_DEF
, def
));
574 /** Define the \p get_def_by_id function for this blob type. */
575 #define DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix) \
576 int cmd_prefix ## _get_def_by_id(uint32_t id, struct osl_object *def) \
578 return blob_get_def_by_id(table_name ## _table, id, def); \
581 static int blob_get_name_and_def_by_row(struct osl_table
*table
,
582 const struct osl_row
*row
, char **name
, struct osl_object
*def
)
584 struct osl_object obj
;
585 int ret
= osl(osl_get_object(table
, row
, BLOBCOL_NAME
, &obj
));
589 return osl(osl_open_disk_object(table
, row
, BLOBCOL_DEF
, def
));
591 /** Define the \p get_name_and_def_by_row function for this blob type. */
592 #define DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix) \
593 int cmd_prefix ## _get_name_and_def_by_row(const struct osl_row *row, \
594 char **name, struct osl_object *def) \
596 return blob_get_name_and_def_by_row(table_name ## _table, \
600 /** Define the \p close function for this blob type. */
601 #define DEFINE_BLOB_CLOSE(table_name) \
602 static void table_name ## _close(void) \
604 osl_close_table(table_name ## _table, OSL_MARK_CLEAN); \
605 table_name ## _table = NULL; \
608 /** Define the \p create function for this blob type. */
609 #define DEFINE_BLOB_CREATE(table_name) \
610 static int table_name ## _create(const char *dir) \
612 table_name ## _table_desc.dir = dir; \
613 return osl_create_table(&table_name ## _table_desc); \
616 static int blob_open(struct osl_table
**table
,
617 struct osl_table_description
*desc
,
622 ret
= osl(osl_open_table(desc
, table
));
626 if (ret
>= 0 || ret
== -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT
))
631 #define DEFINE_BLOB_OPEN(table_name) \
632 static int table_name ## _open(const char *dir) \
634 return blob_open(&table_name ## _table, \
635 &table_name ## _table_desc, dir); \
639 /** Define the \p init function for this blob type. */
640 #define DEFINE_BLOB_INIT(table_name) \
641 void table_name ## _init(struct afs_table *t) \
643 t->open = table_name ## _open; \
644 t->close = table_name ## _close; \
645 t->create = table_name ## _create;\
646 t->event_handler = table_name ##_event_handler; \
647 table_name ## _table = NULL; \
651 /** Define all functions for this blob type. */
652 #define DEFINE_BLOB_FUNCTIONS(table_name, cmd_prefix) \
653 DEFINE_BLOB_OPEN(table_name) \
654 DEFINE_BLOB_CLOSE(table_name) \
655 DEFINE_BLOB_CREATE(table_name) \
656 DEFINE_BLOB_INIT(table_name) \
657 DEFINE_BLOB_COMMAND(ls, table_name, cmd_prefix) \
658 DEFINE_BLOB_COMMAND(cat, table_name, cmd_prefix) \
659 DEFINE_BLOB_COMMAND(add, table_name, cmd_prefix) \
660 DEFINE_BLOB_COMMAND(rm, table_name, cmd_prefix) \
661 DEFINE_BLOB_COMMAND(mv, table_name, cmd_prefix) \
662 DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix); \
663 DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix); \
664 DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix); \
665 DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix); \
667 /** \cond doxygen isn't smart enough to recognize these */
668 DEFINE_BLOB_FUNCTIONS(lyrics
, lyr
);
669 DEFINE_BLOB_FUNCTIONS(images
, img
);
670 DEFINE_BLOB_FUNCTIONS(moods
, mood
);
671 DEFINE_BLOB_FUNCTIONS(playlists
, pl
);