2 * Copyright (C) 2007-2011 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. */
20 #include "portable_io.h"
23 * Compare two osl objects pointing to unsigned integers of 32 bit size.
25 * \param obj1 Pointer to the first integer.
26 * \param obj2 Pointer to the second integer.
28 * \return The values required for an osl compare function.
30 * \sa osl_compare_func, osl_hash_compare().
32 static int uint32_compare(const struct osl_object
*obj1
, const struct osl_object
*obj2
)
34 uint32_t d1
= read_u32((const char *)obj1
->data
);
35 uint32_t d2
= read_u32((const char *)obj2
->data
);
44 static struct osl_column_description blob_cols
[] = {
46 .storage_type
= OSL_MAPPED_STORAGE
,
47 .storage_flags
= OSL_RBTREE
| OSL_UNIQUE
| OSL_FIXED_SIZE
,
50 .compare_function
= uint32_compare
53 .storage_type
= OSL_MAPPED_STORAGE
,
54 .storage_flags
= OSL_RBTREE
| OSL_UNIQUE
,
56 .compare_function
= string_compare
59 .storage_type
= OSL_DISK_STORAGE
,
65 /** Define an osl table description for a blob table. */
66 #define DEFINE_BLOB_TABLE_DESC(table_name) \
67 struct osl_table_description table_name ## _table_desc = { \
68 .name = #table_name, \
69 .num_columns = NUM_BLOB_COLUMNS, \
70 .flags = OSL_LARGE_TABLE, \
71 .column_descriptions = blob_cols \
74 /** Define a pointer to an osl blob table with a canonical name. */
75 #define DEFINE_BLOB_TABLE_PTR(table_name) struct osl_table *table_name ## _table;
78 /** Define a blob table. */
79 #define INIT_BLOB_TABLE(table_name) \
80 DEFINE_BLOB_TABLE_DESC(table_name); \
81 DEFINE_BLOB_TABLE_PTR(table_name);
83 /* doxygen isn't smart enough to recognize these */
84 /** \cond blob_table */
85 INIT_BLOB_TABLE(lyrics
);
86 INIT_BLOB_TABLE(images
);
87 INIT_BLOB_TABLE(moods
);
88 INIT_BLOB_TABLE(playlists
);
89 /** \endcond blob_table */
91 /** Flags that may be passed to the \p ls functions of each blob type. */
93 /** List both id and name. */
94 BLOB_LS_FLAG_LONG
= 1,
95 /** Reverse sort order. */
96 BLOB_LS_FLAG_REVERSE
= 2,
97 /** Sort by id instead of name. */
98 BLOB_LS_FLAG_SORT_BY_ID
= 4,
101 /** Structure passed to the \p print_blob function. */
102 struct lsblob_action_data
{
103 /** The flags given at the command line. */
105 /** Message buffer. */
106 struct para_buffer pb
;
109 static int print_blob(struct osl_table
*table
, struct osl_row
*row
,
110 const char *name
, void *data
)
112 struct lsblob_action_data
*lbad
= data
;
113 struct osl_object obj
;
117 if (!(lbad
->flags
& BLOB_LS_FLAG_LONG
))
118 return para_printf(&lbad
->pb
, "%s\n", name
);
119 ret
= osl(osl_get_object(table
, row
, BLOBCOL_ID
, &obj
));
121 para_printf(&lbad
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
124 id
= *(uint32_t *)obj
.data
;
125 return para_printf(&lbad
->pb
, "%u\t%s\n", id
, name
);
128 static void com_lsblob_callback(struct osl_table
*table
,
129 int fd
, const struct osl_object
*query
)
131 struct lsblob_action_data lbad
= {
132 .flags
= *(uint32_t *)query
->data
,
134 .max_size
= shm_get_shmmax(),
136 .max_size_handler
= pass_buffer_as_shm
139 struct pattern_match_data pmd
= {
141 .patterns
= {.data
= (char *)query
->data
+ sizeof(uint32_t),
142 .size
= query
->size
- sizeof(uint32_t)},
143 .pm_flags
= PM_NO_PATTERN_MATCHES_EVERYTHING
| PM_SKIP_EMPTY_NAME
,
144 .match_col_num
= BLOBCOL_NAME
,
146 .action
= print_blob
,
150 if (lbad
.flags
& BLOB_LS_FLAG_REVERSE
)
151 pmd
.pm_flags
|= PM_REVERSE_LOOP
;
152 if (!(lbad
.flags
& BLOB_LS_FLAG_SORT_BY_ID
))
153 pmd
.loop_col_num
= BLOBCOL_NAME
;
155 pmd
.loop_col_num
= BLOBCOL_ID
;
156 ret
= for_each_matching_row(&pmd
);
158 para_printf(&lbad
.pb
, "%s\n", para_strerror(-ret
));
160 pass_buffer_as_shm(lbad
.pb
.buf
, lbad
.pb
.offset
, &fd
);
164 static int com_lsblob(callback_function
*f
, struct stream_cipher_context
*scc
, int argc
, char * const * const argv
)
167 struct osl_object options
= {.data
= &flags
, .size
= sizeof(flags
)};
170 for (i
= 1; i
< argc
; i
++) {
171 const char *arg
= argv
[i
];
174 if (!strcmp(arg
, "--")) {
178 if (!strcmp(arg
, "-l")) {
179 flags
|= BLOB_LS_FLAG_LONG
;
182 if (!strcmp(arg
, "-i")) {
183 flags
|= BLOB_LS_FLAG_SORT_BY_ID
;
186 if (!strcmp(arg
, "-r")) {
187 flags
|= BLOB_LS_FLAG_REVERSE
;
193 // return -E_BLOB_SYNTAX;
194 return send_option_arg_callback_request(&options
, argc
- i
,
195 argv
+ i
, f
, sc_send_result
, scc
);
198 static int cat_blob(struct osl_table
*table
, struct osl_row
*row
,
199 __a_unused
const char *name
, void *data
)
202 struct osl_object obj
;
204 ret
= osl(osl_open_disk_object(table
, row
, BLOBCOL_DEF
, &obj
));
208 ret
= pass_buffer_as_shm(obj
.data
, obj
.size
, data
);
209 ret2
= osl(osl_close_disk_object(&obj
));
210 return (ret
< 0)? ret
: ret2
;
213 static void com_catblob_callback(struct osl_table
*table
, int fd
,
214 const struct osl_object
*query
)
216 struct pattern_match_data pmd
= {
219 .loop_col_num
= BLOBCOL_NAME
,
220 .match_col_num
= BLOBCOL_NAME
,
221 .pm_flags
= PM_SKIP_EMPTY_NAME
,
225 for_each_matching_row(&pmd
);
228 static int com_catblob(callback_function
*f
, struct stream_cipher_context
*scc
, int argc
,
229 char * const * const argv
)
232 return -E_BLOB_SYNTAX
;
233 return send_standard_callback_request(argc
- 1, argv
+ 1, f
,
234 sc_send_result
, scc
);
237 /** Used for removing rows from a blob table. */
239 /** Message buffer. */
240 struct para_buffer pb
;
243 static int remove_blob(struct osl_table
*table
, struct osl_row
*row
,
244 const char *name
, void *data
)
246 struct rmblob_data
*rmbd
= data
;
247 int ret
= osl(osl_del_row(table
, row
));
249 para_printf(&rmbd
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
255 static void com_rmblob_callback(struct osl_table
*table
, int fd
,
256 const struct osl_object
*query
)
259 struct rmblob_data rmbd
= {
261 .max_size
= shm_get_shmmax(),
263 .max_size_handler
= pass_buffer_as_shm
266 struct pattern_match_data pmd
= {
269 .loop_col_num
= BLOBCOL_NAME
,
270 .match_col_num
= BLOBCOL_NAME
,
271 .pm_flags
= PM_SKIP_EMPTY_NAME
,
273 .action
= remove_blob
275 ret
= for_each_matching_row(&pmd
);
277 ret2
= para_printf(&rmbd
.pb
, "%s\n", para_strerror(-ret
));
281 if (pmd
.num_matches
== 0)
282 ret2
= para_printf(&rmbd
.pb
, "no matches, nothing removed\n");
284 ret2
= para_printf(&rmbd
.pb
, "removed %d blobs\n", pmd
.num_matches
);
285 afs_event(BLOB_RENAME
, NULL
, table
);
288 if (ret2
>= 0 && rmbd
.pb
.offset
)
289 pass_buffer_as_shm(rmbd
.pb
.buf
, rmbd
.pb
.offset
, &fd
);
293 static int com_rmblob(callback_function
*f
, struct stream_cipher_context
*scc
, int argc
,
294 char * const * const argv
)
297 return -E_MOOD_SYNTAX
;
298 return send_option_arg_callback_request(NULL
, argc
- 1, argv
+ 1, f
,
299 sc_send_result
, scc
);
302 static void com_addblob_callback(struct osl_table
*table
, __a_unused
int fd
,
303 const struct osl_object
*query
)
305 struct osl_object objs
[NUM_BLOB_COLUMNS
];
306 char *name
= query
->data
;
307 size_t name_len
= strlen(name
) + 1;
312 ret
= osl(osl_get_num_rows(table
, &num_rows
));
315 if (!num_rows
) { /* this is the first entry ever added */
316 /* insert dummy row containing the id */
317 id
= 2; /* this entry will be entry #1, so 2 is the next */
318 objs
[BLOBCOL_ID
].data
= &id
;
319 objs
[BLOBCOL_ID
].size
= sizeof(id
);
320 objs
[BLOBCOL_NAME
].data
= "";
321 objs
[BLOBCOL_NAME
].size
= 1;
322 objs
[BLOBCOL_DEF
].data
= "";
323 objs
[BLOBCOL_DEF
].size
= 1;
324 ret
= osl(osl_add_row(table
, objs
));
328 /* check if name already exists */
330 struct osl_object obj
= {.data
= name
, .size
= name_len
};
331 ret
= osl(osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
));
332 if (ret
< 0 && ret
!= -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND
))
334 if (ret
>= 0) { /* we already have a blob with this name */
335 obj
.data
= name
+ name_len
;
336 obj
.size
= query
->size
- name_len
;
337 ret
= osl(osl_update_object(table
, row
, BLOBCOL_DEF
, &obj
));
340 /* new blob, get id of the dummy row and increment it */
343 ret
= osl(osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
));
346 ret
= osl(osl_get_object(table
, row
, BLOBCOL_ID
, &obj
));
349 id
= *(uint32_t *)obj
.data
+ 1;
351 ret
= osl(osl_update_object(table
, row
, BLOBCOL_ID
, &obj
));
356 objs
[BLOBCOL_ID
].data
= &id
;
357 objs
[BLOBCOL_ID
].size
= sizeof(id
);
358 objs
[BLOBCOL_NAME
].data
= name
;
359 objs
[BLOBCOL_NAME
].size
= name_len
;
360 objs
[BLOBCOL_DEF
].data
= name
+ name_len
;
361 objs
[BLOBCOL_DEF
].size
= query
->size
- name_len
;
362 ret
= osl(osl_add_row(table
, objs
));
365 afs_event(BLOB_ADD
, NULL
, table
);
368 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
372 * write input from fd to dynamically allocated buffer,
373 * but maximal max_size byte.
375 static int fd2buf(struct stream_cipher_context
*scc
, unsigned max_size
, struct osl_object
*obj
)
377 const size_t chunk_size
= 1024;
378 size_t size
= 2048, received
= 0;
380 char *buf
= para_malloc(size
);
383 ret
= sc_recv_bin_buffer(scc
, buf
+ received
, chunk_size
);
387 if (received
+ chunk_size
>= size
) {
389 ret
= -E_INPUT_TOO_LARGE
;
392 buf
= para_realloc(buf
, size
);
396 obj
->size
= received
;
403 * Read data from a file descriptor, and send it to the afs process.
405 * \param scc crypt context containing the file descriptor to read data from.
406 * \param arg_obj Pointer to the arguments to \a f.
407 * \param f The callback function.
408 * \param max_len Don't read more than that many bytes from stdin.
409 * \param result_handler See \ref send_callback_request.
410 * \param private_result_data See \ref send_callback_request.
412 * This function is used by commands that wish to let para_server store
413 * arbitrary data specified by the user (for instance the add_blob family of
414 * commands). First, at most \a max_len bytes are read and decrypted from the
415 * file descriptor given by \a scc. The result is concatenated with the buffer
416 * given by \a arg_obj, and the combined buffer is made available to the afs
417 * process via the callback method. See \ref send_callback_request for details.
419 * \return Negative on errors, the return value of the underlying call to
420 * send_callback_request() otherwise.
422 static int stdin_command(struct stream_cipher_context
*scc
, struct osl_object
*arg_obj
,
423 callback_function
*f
, unsigned max_len
,
424 callback_result_handler
*result_handler
,
425 void *private_result_data
)
427 struct osl_object query
, stdin_obj
;
430 ret
= sc_send_buffer(scc
, AWAITING_DATA_MSG
);
433 ret
= fd2buf(scc
, max_len
, &stdin_obj
);
436 query
.size
= arg_obj
->size
+ stdin_obj
.size
;
437 query
.data
= para_malloc(query
.size
);
438 memcpy(query
.data
, arg_obj
->data
, arg_obj
->size
);
439 memcpy((char *)query
.data
+ arg_obj
->size
, stdin_obj
.data
, stdin_obj
.size
);
440 free(stdin_obj
.data
);
441 ret
= send_callback_request(f
, &query
, result_handler
, private_result_data
);
446 static int com_addblob(callback_function
*f
, struct stream_cipher_context
*scc
, int argc
,
447 char * const * const argv
)
449 struct osl_object arg_obj
;
452 return -E_BLOB_SYNTAX
;
453 if (!*argv
[1]) /* empty name is reserved for the dummy row */
454 return -E_BLOB_SYNTAX
;
455 arg_obj
.size
= strlen(argv
[1]) + 1;
456 arg_obj
.data
= (char *)argv
[1];
457 return stdin_command(scc
, &arg_obj
, f
, 10 * 1024 * 1024, NULL
, NULL
);
460 /* FIXME: Print output to client, not to log file */
461 static void com_mvblob_callback(struct osl_table
*table
, __a_unused
int fd
,
462 const struct osl_object
*query
)
464 char *src
= (char *) query
->data
;
465 struct osl_object obj
= {.data
= src
, .size
= strlen(src
) + 1};
466 char *dest
= src
+ obj
.size
;
468 int ret
= osl(osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
));
473 obj
.size
= strlen(dest
) + 1;
474 ret
= osl(osl_update_object(table
, row
, BLOBCOL_NAME
, &obj
));
477 afs_event(BLOB_RENAME
, NULL
, table
);
480 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
483 static int com_mvblob(callback_function
*f
, __a_unused
struct stream_cipher_context
*scc
,
484 int argc
, char * const * const argv
)
487 return -E_MOOD_SYNTAX
;
488 return send_option_arg_callback_request(NULL
, argc
- 1, argv
+ 1, f
,
492 #define DEFINE_BLOB_COMMAND(cmd_name, table_name, cmd_prefix) \
493 static void com_ ## cmd_name ## cmd_prefix ## _callback(int fd, const struct osl_object *query) \
495 return com_ ## cmd_name ## blob_callback(table_name ## _table, fd, query); \
497 int com_ ## cmd_name ## cmd_prefix(struct stream_cipher_context *scc, int argc, char * const * const argv) \
499 return com_ ## cmd_name ## blob(com_ ## cmd_name ## cmd_prefix ## _callback, scc, argc, argv); \
502 static int blob_get_name_by_id(struct osl_table
*table
, uint32_t id
,
506 struct osl_object obj
= {.data
= &id
, .size
= sizeof(id
)};
512 ret
= osl(osl_get_row(table
, BLOBCOL_ID
, &obj
, &row
));
515 ret
= osl(osl_get_object(table
, row
, BLOBCOL_NAME
, &obj
));
518 *name
= (char *)obj
.data
;
522 /** Define the \p get_name_by_id function for this blob type. */
523 #define DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix) \
524 int cmd_prefix ## _get_name_by_id(uint32_t id, char **name) \
526 return blob_get_name_by_id(table_name ## _table, id, name); \
530 static int blob_get_def_by_name(struct osl_table
*table
, char *name
,
531 struct osl_object
*def
)
534 struct osl_object obj
= {.data
= name
, .size
= strlen(name
) + 1};
540 ret
= osl(osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
));
543 return osl(osl_open_disk_object(table
, row
, BLOBCOL_DEF
, def
));
546 /** Define the \p get_def_by_id function for this blob type. */
547 #define DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix) \
548 int cmd_prefix ## _get_def_by_name(char *name, struct osl_object *def) \
550 return blob_get_def_by_name(table_name ## _table, name, def); \
553 static int blob_get_def_by_id(struct osl_table
*table
, uint32_t id
,
554 struct osl_object
*def
)
557 struct osl_object obj
= {.data
= &id
, .size
= sizeof(id
)};
563 ret
= osl(osl_get_row(table
, BLOBCOL_ID
, &obj
, &row
));
566 return osl(osl_open_disk_object(table
, row
, BLOBCOL_DEF
, def
));
569 /** Define the \p get_def_by_id function for this blob type. */
570 #define DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix) \
571 int cmd_prefix ## _get_def_by_id(uint32_t id, struct osl_object *def) \
573 return blob_get_def_by_id(table_name ## _table, id, def); \
576 static int blob_get_name_and_def_by_row(struct osl_table
*table
,
577 const struct osl_row
*row
, char **name
, struct osl_object
*def
)
579 struct osl_object obj
;
580 int ret
= osl(osl_get_object(table
, row
, BLOBCOL_NAME
, &obj
));
584 return osl(osl_open_disk_object(table
, row
, BLOBCOL_DEF
, def
));
586 /** Define the \p get_name_and_def_by_row function for this blob type. */
587 #define DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix) \
588 int cmd_prefix ## _get_name_and_def_by_row(const struct osl_row *row, \
589 char **name, struct osl_object *def) \
591 return blob_get_name_and_def_by_row(table_name ## _table, \
595 /** Define the \p close function for this blob type. */
596 #define DEFINE_BLOB_CLOSE(table_name) \
597 static void table_name ## _close(void) \
599 osl_close_table(table_name ## _table, OSL_MARK_CLEAN); \
600 table_name ## _table = NULL; \
603 /** Define the \p create function for this blob type. */
604 #define DEFINE_BLOB_CREATE(table_name) \
605 static int table_name ## _create(const char *dir) \
607 table_name ## _table_desc.dir = dir; \
608 return osl_create_table(&table_name ## _table_desc); \
611 static int blob_open(struct osl_table
**table
,
612 struct osl_table_description
*desc
,
617 ret
= osl(osl_open_table(desc
, table
));
621 if (ret
>= 0 || ret
== -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT
))
626 #define DEFINE_BLOB_OPEN(table_name) \
627 static int table_name ## _open(const char *dir) \
629 return blob_open(&table_name ## _table, \
630 &table_name ## _table_desc, dir); \
634 /** Define the \p init function for this blob type. */
635 #define DEFINE_BLOB_INIT(table_name) \
636 void table_name ## _init(struct afs_table *t) \
638 t->open = table_name ## _open; \
639 t->close = table_name ## _close; \
640 t->create = table_name ## _create;\
641 t->event_handler = table_name ##_event_handler; \
642 table_name ## _table = NULL; \
646 /** Define all functions for this blob type. */
647 #define DEFINE_BLOB_FUNCTIONS(table_name, cmd_prefix) \
648 DEFINE_BLOB_OPEN(table_name) \
649 DEFINE_BLOB_CLOSE(table_name) \
650 DEFINE_BLOB_CREATE(table_name) \
651 DEFINE_BLOB_INIT(table_name) \
652 DEFINE_BLOB_COMMAND(ls, table_name, cmd_prefix) \
653 DEFINE_BLOB_COMMAND(cat, table_name, cmd_prefix) \
654 DEFINE_BLOB_COMMAND(add, table_name, cmd_prefix) \
655 DEFINE_BLOB_COMMAND(rm, table_name, cmd_prefix) \
656 DEFINE_BLOB_COMMAND(mv, table_name, cmd_prefix) \
657 DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix); \
658 DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix); \
659 DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix); \
660 DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix); \
662 /* doxygen isn't smart enough to recognize these */
663 /** \cond blob_function */
664 DEFINE_BLOB_FUNCTIONS(lyrics
, lyr
);
665 DEFINE_BLOB_FUNCTIONS(images
, img
);
666 DEFINE_BLOB_FUNCTIONS(moods
, mood
);
667 DEFINE_BLOB_FUNCTIONS(playlists
, pl
);
668 /** \endcond blob_function */