2 * Copyright (C) 2007-2009 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. */
10 #include <openssl/rc4.h>
21 #include "portable_io.h"
24 * Compare two osl objects pointing to unsigned integers of 32 bit size.
26 * \param obj1 Pointer to the first integer.
27 * \param obj2 Pointer to the second integer.
29 * \return The values required for an osl compare function.
31 * \sa osl_compare_func, osl_hash_compare().
33 static int uint32_compare(const struct osl_object
*obj1
, const struct osl_object
*obj2
)
35 uint32_t d1
= read_u32((const char *)obj1
->data
);
36 uint32_t d2
= read_u32((const char *)obj2
->data
);
45 static struct osl_column_description blob_cols
[] = {
47 .storage_type
= OSL_MAPPED_STORAGE
,
48 .storage_flags
= OSL_RBTREE
| OSL_UNIQUE
| OSL_FIXED_SIZE
,
51 .compare_function
= uint32_compare
54 .storage_type
= OSL_MAPPED_STORAGE
,
55 .storage_flags
= OSL_RBTREE
| OSL_UNIQUE
,
57 .compare_function
= string_compare
60 .storage_type
= OSL_DISK_STORAGE
,
66 /** \cond doxygen isn't smart enough to recognize these */
67 INIT_BLOB_TABLE(lyrics
);
68 INIT_BLOB_TABLE(images
);
69 INIT_BLOB_TABLE(moods
);
70 INIT_BLOB_TABLE(playlists
);
73 /** Flags that may be passed to the \p ls functions of each blob type. */
75 /** List both id and name. */
76 BLOB_LS_FLAG_LONG
= 1,
77 /** Reverse sort order. */
78 BLOB_LS_FLAG_REVERSE
= 2,
79 /** Sort by id instead of name. */
80 BLOB_LS_FLAG_SORT_BY_ID
= 4,
83 /** Structure passed to the \p print_blob function. */
84 struct lsblob_action_data
{
85 /** The flags given at the command line. */
87 /** Message buffer. */
88 struct para_buffer pb
;
91 static int print_blob(struct osl_table
*table
, struct osl_row
*row
,
92 const char *name
, void *data
)
94 struct lsblob_action_data
*lbad
= data
;
95 struct osl_object obj
;
99 if (!(lbad
->flags
& BLOB_LS_FLAG_LONG
))
100 return para_printf(&lbad
->pb
, "%s\n", name
);
101 ret
= osl(osl_get_object(table
, row
, BLOBCOL_ID
, &obj
));
103 para_printf(&lbad
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
106 id
= *(uint32_t *)obj
.data
;
107 return para_printf(&lbad
->pb
, "%u\t%s\n", id
, name
);
110 static void com_lsblob_callback(struct osl_table
*table
,
111 int fd
, const struct osl_object
*query
)
113 struct lsblob_action_data lbad
= {
114 .flags
= *(uint32_t *)query
->data
,
118 .max_size_handler
= pass_buffer_as_shm
121 struct pattern_match_data pmd
= {
123 .patterns
= {.data
= (char *)query
->data
+ sizeof(uint32_t),
124 .size
= query
->size
- sizeof(uint32_t)},
125 .pm_flags
= PM_NO_PATTERN_MATCHES_EVERYTHING
| PM_SKIP_EMPTY_NAME
,
126 .match_col_num
= BLOBCOL_NAME
,
128 .action
= print_blob
,
132 if (lbad
.flags
& BLOB_LS_FLAG_REVERSE
)
133 pmd
.pm_flags
|= PM_REVERSE_LOOP
;
134 if (!(lbad
.flags
& BLOB_LS_FLAG_SORT_BY_ID
))
135 pmd
.loop_col_num
= BLOBCOL_NAME
;
137 pmd
.loop_col_num
= BLOBCOL_ID
;
138 ret
= for_each_matching_row(&pmd
);
140 para_printf(&lbad
.pb
, "%s\n", para_strerror(-ret
));
142 pass_buffer_as_shm(lbad
.pb
.buf
, lbad
.pb
.offset
, &fd
);
146 static int com_lsblob(callback_function
*f
, struct rc4_context
*rc4c
, int argc
, char * const * const argv
)
149 struct osl_object options
= {.data
= &flags
, .size
= sizeof(flags
)};
152 for (i
= 1; i
< argc
; i
++) {
153 const char *arg
= argv
[i
];
156 if (!strcmp(arg
, "--")) {
160 if (!strcmp(arg
, "-l")) {
161 flags
|= BLOB_LS_FLAG_LONG
;
164 if (!strcmp(arg
, "-i")) {
165 flags
|= BLOB_LS_FLAG_SORT_BY_ID
;
168 if (!strcmp(arg
, "-r")) {
169 flags
|= BLOB_LS_FLAG_REVERSE
;
175 // return -E_BLOB_SYNTAX;
176 return send_option_arg_callback_request(&options
, argc
- i
,
177 argv
+ i
, f
, rc4_send_result
, rc4c
);
180 static int cat_blob(struct osl_table
*table
, struct osl_row
*row
,
181 __a_unused
const char *name
, void *data
)
184 struct osl_object obj
;
186 ret
= osl(osl_open_disk_object(table
, row
, BLOBCOL_DEF
, &obj
));
190 ret
= pass_buffer_as_shm(obj
.data
, obj
.size
, data
);
191 ret2
= osl(osl_close_disk_object(&obj
));
192 return (ret
< 0)? ret
: ret2
;
195 static void com_catblob_callback(struct osl_table
*table
, int fd
,
196 const struct osl_object
*query
)
198 struct pattern_match_data pmd
= {
201 .loop_col_num
= BLOBCOL_NAME
,
202 .match_col_num
= BLOBCOL_NAME
,
203 .pm_flags
= PM_SKIP_EMPTY_NAME
,
207 for_each_matching_row(&pmd
);
210 static int com_catblob(callback_function
*f
, struct rc4_context
*rc4c
, int argc
,
211 char * const * const argv
)
214 return -E_BLOB_SYNTAX
;
215 return send_standard_callback_request(argc
- 1, argv
+ 1, f
,
216 rc4_send_result
, rc4c
);
219 /** Used for removing rows from a blob table. */
221 /** Message buffer. */
222 struct para_buffer pb
;
223 /** Number of removed blobs. */
224 unsigned num_removed
;
227 static int remove_blob(struct osl_table
*table
, struct osl_row
*row
,
228 const char *name
, void *data
)
230 struct rmblob_data
*rmbd
= data
;
231 int ret
= osl(osl_del_row(table
, row
));
233 para_printf(&rmbd
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
240 static void com_rmblob_callback(struct osl_table
*table
, int fd
,
241 const struct osl_object
*query
)
244 struct rmblob_data rmbd
= {
249 .max_size_handler
= pass_buffer_as_shm
252 struct pattern_match_data pmd
= {
255 .loop_col_num
= BLOBCOL_NAME
,
256 .match_col_num
= BLOBCOL_NAME
,
257 .pm_flags
= PM_SKIP_EMPTY_NAME
,
259 .action
= remove_blob
261 ret
= for_each_matching_row(&pmd
);
263 ret2
= para_printf(&rmbd
.pb
, "%s\n", para_strerror(-ret
));
267 if (!rmbd
.num_removed
)
268 ret2
= para_printf(&rmbd
.pb
, "no matches, nothing removed\n");
270 ret2
= para_printf(&rmbd
.pb
, "removed %d blobs\n", rmbd
.num_removed
);
271 afs_event(BLOB_RENAME
, NULL
, table
);
274 if (ret2
>= 0 && rmbd
.pb
.offset
)
275 pass_buffer_as_shm(rmbd
.pb
.buf
, rmbd
.pb
.offset
, &fd
);
279 static int com_rmblob(callback_function
*f
, struct rc4_context
*rc4c
, int argc
,
280 char * const * const argv
)
283 return -E_MOOD_SYNTAX
;
284 return send_option_arg_callback_request(NULL
, argc
- 1, argv
+ 1, f
,
285 rc4_send_result
, rc4c
);
288 static void com_addblob_callback(struct osl_table
*table
, __a_unused
int fd
,
289 const struct osl_object
*query
)
291 struct osl_object objs
[NUM_BLOB_COLUMNS
];
292 char *name
= query
->data
;
293 size_t name_len
= strlen(name
) + 1;
298 ret
= osl(osl_get_num_rows(table
, &num_rows
));
301 if (!num_rows
) { /* this is the first entry ever added */
302 /* insert dummy row containing the id */
303 id
= 2; /* this entry will be entry #1, so 2 is the next */
304 objs
[BLOBCOL_ID
].data
= &id
;
305 objs
[BLOBCOL_ID
].size
= sizeof(id
);
306 objs
[BLOBCOL_NAME
].data
= "";
307 objs
[BLOBCOL_NAME
].size
= 1;
308 objs
[BLOBCOL_DEF
].data
= "";
309 objs
[BLOBCOL_DEF
].size
= 1;
310 ret
= osl(osl_add_row(table
, objs
));
314 /* check if name already exists */
316 struct osl_object obj
= {.data
= name
, .size
= name_len
};
317 ret
= osl(osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
));
318 if (ret
< 0 && ret
!= -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND
))
320 if (ret
>= 0) { /* we already have a blob with this name */
321 obj
.data
= name
+ name_len
;
322 obj
.size
= query
->size
- name_len
;
323 ret
= osl(osl_update_object(table
, row
, BLOBCOL_DEF
, &obj
));
326 /* new blob, get id of the dummy row and increment it */
329 ret
= osl(osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
));
332 ret
= osl(osl_get_object(table
, row
, BLOBCOL_ID
, &obj
));
335 id
= *(uint32_t *)obj
.data
+ 1;
337 ret
= osl(osl_update_object(table
, row
, BLOBCOL_ID
, &obj
));
342 objs
[BLOBCOL_ID
].data
= &id
;
343 objs
[BLOBCOL_ID
].size
= sizeof(id
);
344 objs
[BLOBCOL_NAME
].data
= name
;
345 objs
[BLOBCOL_NAME
].size
= name_len
;
346 objs
[BLOBCOL_DEF
].data
= name
+ name_len
;
347 objs
[BLOBCOL_DEF
].size
= query
->size
- name_len
;
348 ret
= osl(osl_add_row(table
, objs
));
351 afs_event(BLOB_ADD
, NULL
, table
);
354 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
358 * write input from fd to dynamically allocated buffer,
359 * but maximal max_size byte.
361 static int fd2buf(struct rc4_context
*rc4c
, unsigned max_size
, struct osl_object
*obj
)
363 const size_t chunk_size
= 1024;
364 size_t size
= 2048, received
= 0;
366 char *buf
= para_malloc(size
);
369 ret
= rc4_recv_bin_buffer(rc4c
, buf
+ received
, chunk_size
);
373 if (received
+ chunk_size
>= size
) {
375 ret
= -E_INPUT_TOO_LARGE
;
378 buf
= para_realloc(buf
, size
);
382 obj
->size
= received
;
389 * Read data from a file descriptor, and send it to the afs process.
391 * \param rc4c crypt context containing the file descriptor to read data from.
392 * \param arg_obj Pointer to the arguments to \a f.
393 * \param f The callback function.
394 * \param max_len Don't read more than that many bytes from stdin.
395 * \param result_handler See \ref send_callback_request.
396 * \param private_result_data See \ref send_callback_request.
398 * This function is used by commands that wish to let para_server store
399 * arbitrary data specified by the user (for instance the add_blob family of
400 * commands). First, at most \a max_len bytes are read and decrypted from the
401 * file descriptor given by \a rc4c. The result is concatenated with the buffer
402 * given by \a arg_obj, and the combined buffer is made available to the afs
403 * process via the callback method. See \ref send_callback_request for details.
405 * \return Negative on errors, the return value of the underlying call to
406 * send_callback_request() otherwise.
408 static int stdin_command(struct rc4_context
*rc4c
, struct osl_object
*arg_obj
,
409 callback_function
*f
, unsigned max_len
,
410 callback_result_handler
*result_handler
,
411 void *private_result_data
)
413 struct osl_object query
, stdin_obj
;
416 ret
= rc4_send_buffer(rc4c
, AWAITING_DATA_MSG
);
419 ret
= fd2buf(rc4c
, max_len
, &stdin_obj
);
422 query
.size
= arg_obj
->size
+ stdin_obj
.size
;
423 query
.data
= para_malloc(query
.size
);
424 memcpy(query
.data
, arg_obj
->data
, arg_obj
->size
);
425 memcpy((char *)query
.data
+ arg_obj
->size
, stdin_obj
.data
, stdin_obj
.size
);
426 free(stdin_obj
.data
);
427 ret
= send_callback_request(f
, &query
, result_handler
, private_result_data
);
432 static int com_addblob(callback_function
*f
, struct rc4_context
*rc4c
, int argc
,
433 char * const * const argv
)
435 struct osl_object arg_obj
;
438 return -E_BLOB_SYNTAX
;
439 if (!*argv
[1]) /* empty name is reserved for the dummy row */
440 return -E_BLOB_SYNTAX
;
441 arg_obj
.size
= strlen(argv
[1]) + 1;
442 arg_obj
.data
= (char *)argv
[1];
443 return stdin_command(rc4c
, &arg_obj
, f
, 10 * 1024 * 1024, NULL
, NULL
);
446 /* FIXME: Print output to client, not to log file */
447 static void com_mvblob_callback(struct osl_table
*table
, __a_unused
int fd
,
448 const struct osl_object
*query
)
450 char *src
= (char *) query
->data
;
451 struct osl_object obj
= {.data
= src
, .size
= strlen(src
) + 1};
452 char *dest
= src
+ obj
.size
;
454 int ret
= osl(osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
));
459 obj
.size
= strlen(dest
) + 1;
460 ret
= osl(osl_update_object(table
, row
, BLOBCOL_NAME
, &obj
));
463 afs_event(BLOB_RENAME
, NULL
, table
);
466 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
469 static int com_mvblob(callback_function
*f
, __a_unused
struct rc4_context
*rc4c
,
470 int argc
, char * const * const argv
)
473 return -E_MOOD_SYNTAX
;
474 return send_option_arg_callback_request(NULL
, argc
- 1, argv
+ 1, f
,
478 #define DEFINE_BLOB_COMMAND(cmd_name, table_name, cmd_prefix) \
479 static void com_ ## cmd_name ## cmd_prefix ## _callback(int fd, const struct osl_object *query) \
481 return com_ ## cmd_name ## blob_callback(table_name ## _table, fd, query); \
483 int com_ ## cmd_name ## cmd_prefix(struct rc4_context *rc4c, int argc, char * const * const argv) \
485 return com_ ## cmd_name ## blob(com_ ## cmd_name ## cmd_prefix ## _callback, rc4c, argc, argv); \
488 static int blob_get_name_by_id(struct osl_table
*table
, uint32_t id
,
492 struct osl_object obj
= {.data
= &id
, .size
= sizeof(id
)};
498 ret
= osl(osl_get_row(table
, BLOBCOL_ID
, &obj
, &row
));
501 ret
= osl(osl_get_object(table
, row
, BLOBCOL_NAME
, &obj
));
504 *name
= (char *)obj
.data
;
508 /** Define the \p get_name_by_id function for this blob type. */
509 #define DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix) \
510 int cmd_prefix ## _get_name_by_id(uint32_t id, char **name) \
512 return blob_get_name_by_id(table_name ## _table, id, name); \
516 static int blob_get_def_by_name(struct osl_table
*table
, char *name
,
517 struct osl_object
*def
)
520 struct osl_object obj
= {.data
= name
, .size
= strlen(name
) + 1};
526 ret
= osl(osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
));
529 return osl(osl_open_disk_object(table
, row
, BLOBCOL_DEF
, def
));
532 /** Define the \p get_def_by_id function for this blob type. */
533 #define DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix) \
534 int cmd_prefix ## _get_def_by_name(char *name, struct osl_object *def) \
536 return blob_get_def_by_name(table_name ## _table, name, def); \
539 static int blob_get_def_by_id(struct osl_table
*table
, uint32_t id
,
540 struct osl_object
*def
)
543 struct osl_object obj
= {.data
= &id
, .size
= sizeof(id
)};
549 ret
= osl(osl_get_row(table
, BLOBCOL_ID
, &obj
, &row
));
552 return osl(osl_open_disk_object(table
, row
, BLOBCOL_DEF
, def
));
555 /** Define the \p get_def_by_id function for this blob type. */
556 #define DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix) \
557 int cmd_prefix ## _get_def_by_id(uint32_t id, struct osl_object *def) \
559 return blob_get_def_by_id(table_name ## _table, id, def); \
562 static int blob_get_name_and_def_by_row(struct osl_table
*table
,
563 const struct osl_row
*row
, char **name
, struct osl_object
*def
)
565 struct osl_object obj
;
566 int ret
= osl(osl_get_object(table
, row
, BLOBCOL_NAME
, &obj
));
570 return osl(osl_open_disk_object(table
, row
, BLOBCOL_DEF
, def
));
572 /** Define the \p get_name_and_def_by_row function for this blob type. */
573 #define DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix) \
574 int cmd_prefix ## _get_name_and_def_by_row(const struct osl_row *row, \
575 char **name, struct osl_object *def) \
577 return blob_get_name_and_def_by_row(table_name ## _table, \
581 /** Define the \p close function for this blob type. */
582 #define DEFINE_BLOB_CLOSE(table_name) \
583 void table_name ## _close(void) \
585 osl_close_table(table_name ## _table, OSL_MARK_CLEAN); \
586 table_name ## _table = NULL; \
589 /** Define the \p create function for this blob type. */
590 #define DEFINE_BLOB_CREATE(table_name) \
591 int table_name ## _create(const char *dir) \
593 table_name ## _table_desc.dir = dir; \
594 return osl_create_table(&table_name ## _table_desc); \
597 static int blob_open(struct osl_table
**table
,
598 struct osl_table_description
*desc
,
603 ret
= osl(osl_open_table(desc
, table
));
607 if (ret
>= 0 || ret
== -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT
))
612 #define DEFINE_BLOB_OPEN(table_name) \
613 int table_name ## _open(const char *dir) \
615 return blob_open(&table_name ## _table, \
616 &table_name ## _table_desc, dir); \
620 /** Define the \p init function for this blob type. */
621 #define DEFINE_BLOB_INIT(table_name) \
622 void table_name ## _init(struct afs_table *t) \
624 t->name = table_name ## _table_desc.name; \
625 t->open = table_name ## _open; \
626 t->close = table_name ## _close; \
627 t->create = table_name ## _create;\
628 t->event_handler = table_name ##_event_handler; \
629 table_name ## _table = NULL; \
633 /** Define all functions for this blob type. */
634 #define DEFINE_BLOB_FUNCTIONS(table_name, cmd_prefix) \
635 DEFINE_BLOB_OPEN(table_name) \
636 DEFINE_BLOB_CLOSE(table_name) \
637 DEFINE_BLOB_CREATE(table_name) \
638 DEFINE_BLOB_INIT(table_name) \
639 DEFINE_BLOB_COMMAND(ls, table_name, cmd_prefix) \
640 DEFINE_BLOB_COMMAND(cat, table_name, cmd_prefix) \
641 DEFINE_BLOB_COMMAND(add, table_name, cmd_prefix) \
642 DEFINE_BLOB_COMMAND(rm, table_name, cmd_prefix) \
643 DEFINE_BLOB_COMMAND(mv, table_name, cmd_prefix) \
644 DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix); \
645 DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix); \
646 DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix); \
647 DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix); \
649 /** \cond doxygen isn't smart enough to recognize these */
650 DEFINE_BLOB_FUNCTIONS(lyrics
, lyr
);
651 DEFINE_BLOB_FUNCTIONS(images
, img
);
652 DEFINE_BLOB_FUNCTIONS(moods
, mood
);
653 DEFINE_BLOB_FUNCTIONS(playlists
, pl
);