2 * Copyright (C) 2007-2013 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"
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 /* 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 /** Structure passed to the \p print_blob function. */
104 struct lsblob_action_data
{
105 /** The flags given at the command line. */
107 /** Message buffer. */
108 struct para_buffer pb
;
111 static int print_blob(struct osl_table
*table
, struct osl_row
*row
,
112 const char *name
, void *data
)
114 struct lsblob_action_data
*lbad
= data
;
115 struct osl_object obj
;
119 if (!(lbad
->flags
& BLOB_LS_FLAG_LONG
))
120 return para_printf(&lbad
->pb
, "%s\n", name
);
121 ret
= osl(osl_get_object(table
, row
, BLOBCOL_ID
, &obj
));
123 para_printf(&lbad
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
126 id
= *(uint32_t *)obj
.data
;
127 return para_printf(&lbad
->pb
, "%u\t%s\n", id
, name
);
130 static void com_lsblob_callback(struct osl_table
*table
,
131 int fd
, const struct osl_object
*query
)
133 struct lsblob_action_data lbad
= {
134 .flags
= *(uint32_t *)query
->data
,
136 .max_size
= shm_get_shmmax(),
137 .private_data
= &(struct afs_max_size_handler_data
) {
141 .max_size_handler
= afs_max_size_handler
,
144 struct pattern_match_data pmd
= {
146 .patterns
= {.data
= (char *)query
->data
+ sizeof(uint32_t),
147 .size
= query
->size
- sizeof(uint32_t)},
148 .pm_flags
= PM_NO_PATTERN_MATCHES_EVERYTHING
| PM_SKIP_EMPTY_NAME
,
149 .match_col_num
= BLOBCOL_NAME
,
151 .action
= print_blob
,
155 if (lbad
.flags
& BLOB_LS_FLAG_REVERSE
)
156 pmd
.pm_flags
|= PM_REVERSE_LOOP
;
157 if (!(lbad
.flags
& BLOB_LS_FLAG_SORT_BY_ID
))
158 pmd
.loop_col_num
= BLOBCOL_NAME
;
160 pmd
.loop_col_num
= BLOBCOL_ID
;
161 ret
= for_each_matching_row(&pmd
);
163 para_printf(&lbad
.pb
, "%s\n", para_strerror(-ret
));
164 else if (pmd
.num_matches
== 0 && pmd
.patterns
.size
> 0)
165 para_printf(&lbad
.pb
, "no matches\n");
167 pass_buffer_as_shm(fd
, SBD_OUTPUT
, lbad
.pb
.buf
, lbad
.pb
.offset
);
171 static int com_lsblob(callback_function
*f
, struct command_context
*cc
)
174 struct osl_object options
= {.data
= &flags
, .size
= sizeof(flags
)};
177 for (i
= 1; i
< cc
->argc
; i
++) {
178 const char *arg
= cc
->argv
[i
];
181 if (!strcmp(arg
, "--")) {
185 if (!strcmp(arg
, "-l")) {
186 flags
|= BLOB_LS_FLAG_LONG
;
189 if (!strcmp(arg
, "-i")) {
190 flags
|= BLOB_LS_FLAG_SORT_BY_ID
;
193 if (!strcmp(arg
, "-r")) {
194 flags
|= BLOB_LS_FLAG_REVERSE
;
200 // return -E_BLOB_SYNTAX;
201 return send_option_arg_callback_request(&options
, cc
->argc
- i
,
202 cc
->argv
+ i
, f
, afs_cb_result_handler
, cc
);
205 static int cat_blob(struct osl_table
*table
, struct osl_row
*row
,
206 __a_unused
const char *name
, void *data
)
208 int ret
= 0, ret2
, fd
= *(int *)data
;
209 struct osl_object obj
;
211 ret
= osl(osl_open_disk_object(table
, row
, BLOBCOL_DEF
, &obj
));
213 return (ret
== osl(-E_OSL_EMPTY
))? 0 : ret
;
214 assert(obj
.size
> 0);
215 ret
= pass_buffer_as_shm(fd
, SBD_OUTPUT
, obj
.data
, obj
.size
);
216 ret2
= osl(osl_close_disk_object(&obj
));
217 return (ret
< 0)? ret
: ret2
;
220 static void com_catblob_callback(struct osl_table
*table
, int fd
,
221 const struct osl_object
*query
)
223 struct pattern_match_data pmd
= {
226 .loop_col_num
= BLOBCOL_NAME
,
227 .match_col_num
= BLOBCOL_NAME
,
228 .pm_flags
= PM_SKIP_EMPTY_NAME
,
232 for_each_matching_row(&pmd
);
233 if (pmd
.num_matches
== 0) {
234 char err_msg
[] = "no matches\n";
235 pass_buffer_as_shm(fd
, SBD_OUTPUT
, err_msg
, sizeof(err_msg
));
239 static int com_catblob(callback_function
*f
, struct command_context
*cc
)
242 return -E_BLOB_SYNTAX
;
243 return send_standard_callback_request(cc
->argc
- 1, cc
->argv
+ 1, f
,
244 afs_cb_result_handler
, cc
);
247 /** Used for removing rows from a blob table. */
249 /** Message buffer. */
250 struct para_buffer pb
;
253 static int remove_blob(struct osl_table
*table
, struct osl_row
*row
,
254 const char *name
, void *data
)
256 struct rmblob_data
*rmbd
= data
;
257 int ret
= osl(osl_del_row(table
, row
));
259 para_printf(&rmbd
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
265 static void com_rmblob_callback(struct osl_table
*table
, int fd
,
266 const struct osl_object
*query
)
269 struct rmblob_data rmbd
= {
271 .max_size
= shm_get_shmmax(),
272 .private_data
= &(struct afs_max_size_handler_data
) {
276 .max_size_handler
= afs_max_size_handler
,
279 struct pattern_match_data pmd
= {
282 .loop_col_num
= BLOBCOL_NAME
,
283 .match_col_num
= BLOBCOL_NAME
,
284 .pm_flags
= PM_SKIP_EMPTY_NAME
,
286 .action
= remove_blob
288 ret
= for_each_matching_row(&pmd
);
290 ret2
= para_printf(&rmbd
.pb
, "%s\n", para_strerror(-ret
));
294 if (pmd
.num_matches
== 0)
295 ret2
= para_printf(&rmbd
.pb
, "no matches, nothing removed\n");
297 ret2
= para_printf(&rmbd
.pb
, "removed %d blobs\n", pmd
.num_matches
);
298 afs_event(BLOB_REMOVE
, NULL
, table
);
301 if (ret2
>= 0 && rmbd
.pb
.offset
)
302 pass_buffer_as_shm(fd
, SBD_OUTPUT
, rmbd
.pb
.buf
, rmbd
.pb
.offset
);
306 static int com_rmblob(callback_function
*f
, struct command_context
*cc
)
309 return -E_MOOD_SYNTAX
;
310 return send_option_arg_callback_request(NULL
, cc
->argc
- 1, cc
->argv
+ 1, f
,
311 afs_cb_result_handler
, cc
);
314 static void com_addblob_callback(struct osl_table
*table
, __a_unused
int fd
,
315 const struct osl_object
*query
)
317 struct osl_object objs
[NUM_BLOB_COLUMNS
];
318 char *name
= query
->data
;
319 size_t name_len
= strlen(name
) + 1;
324 ret
= osl(osl_get_num_rows(table
, &num_rows
));
327 if (!num_rows
) { /* this is the first entry ever added */
328 /* insert dummy row containing the id */
329 id
= 2; /* this entry will be entry #1, so 2 is the next */
330 objs
[BLOBCOL_ID
].data
= &id
;
331 objs
[BLOBCOL_ID
].size
= sizeof(id
);
332 objs
[BLOBCOL_NAME
].data
= "";
333 objs
[BLOBCOL_NAME
].size
= 1;
334 objs
[BLOBCOL_DEF
].data
= "";
335 objs
[BLOBCOL_DEF
].size
= 1;
336 ret
= osl(osl_add_row(table
, objs
));
340 /* check if name already exists */
342 struct osl_object obj
= {.data
= name
, .size
= name_len
};
343 ret
= osl(osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
));
344 if (ret
< 0 && ret
!= -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND
))
346 if (ret
>= 0) { /* we already have a blob with this name */
347 obj
.data
= name
+ name_len
;
348 obj
.size
= query
->size
- name_len
;
349 ret
= osl(osl_update_object(table
, row
, BLOBCOL_DEF
, &obj
));
352 /* new blob, get id of the dummy row and increment it */
355 ret
= osl(osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
));
358 ret
= osl(osl_get_object(table
, row
, BLOBCOL_ID
, &obj
));
361 id
= *(uint32_t *)obj
.data
+ 1;
363 ret
= osl(osl_update_object(table
, row
, BLOBCOL_ID
, &obj
));
368 objs
[BLOBCOL_ID
].data
= &id
;
369 objs
[BLOBCOL_ID
].size
= sizeof(id
);
370 objs
[BLOBCOL_NAME
].data
= name
;
371 objs
[BLOBCOL_NAME
].size
= name_len
;
372 objs
[BLOBCOL_DEF
].data
= name
+ name_len
;
373 objs
[BLOBCOL_DEF
].size
= query
->size
- name_len
;
374 ret
= osl(osl_add_row(table
, objs
));
377 afs_event(BLOB_ADD
, NULL
, table
);
380 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
384 * write input from fd to dynamically allocated buffer,
385 * but maximal max_size byte.
387 static int fd2buf(struct stream_cipher_context
*scc
, unsigned max_size
, struct osl_object
*obj
)
389 const size_t chunk_size
= 1024;
390 size_t size
= 2048, received
= 0;
392 char *buf
= para_malloc(size
);
395 ret
= sc_recv_bin_buffer(scc
, buf
+ received
, chunk_size
);
399 if (received
+ chunk_size
>= size
) {
401 ret
= -E_INPUT_TOO_LARGE
;
404 buf
= para_realloc(buf
, size
);
408 obj
->size
= received
;
415 * Read data from a file descriptor, and send it to the afs process.
417 * \param scc crypt context containing the file descriptor to read data from.
418 * \param arg_obj Pointer to the arguments to \a f.
419 * \param f The callback function.
420 * \param max_len Don't read more than that many bytes from stdin.
421 * \param result_handler See \ref send_callback_request.
422 * \param private_result_data See \ref send_callback_request.
424 * This function is used by commands that wish to let para_server store
425 * arbitrary data specified by the user (for instance the add_blob family of
426 * commands). First, at most \a max_len bytes are read and decrypted from the
427 * file descriptor given by \a scc. The result is concatenated with the buffer
428 * given by \a arg_obj, and the combined buffer is made available to the afs
429 * process via the callback method. See \ref send_callback_request for details.
431 * \return Negative on errors, the return value of the underlying call to
432 * send_callback_request() otherwise.
434 static int stdin_command(struct command_context
*cc
, struct osl_object
*arg_obj
,
435 callback_function
*f
, unsigned max_len
,
436 callback_result_handler
*result_handler
,
437 void *private_result_data
)
439 struct osl_object query
, stdin_obj
;
442 if (cc
->use_sideband
)
443 ret
= send_sb(&cc
->scc
, NULL
, 0, SBD_AWAITING_DATA
, false);
445 ret
= sc_send_buffer(&cc
->scc
, AWAITING_DATA_MSG
);
448 ret
= fd2buf(&cc
->scc
, max_len
, &stdin_obj
);
451 query
.size
= arg_obj
->size
+ stdin_obj
.size
;
452 query
.data
= para_malloc(query
.size
);
453 memcpy(query
.data
, arg_obj
->data
, arg_obj
->size
);
454 memcpy((char *)query
.data
+ arg_obj
->size
, stdin_obj
.data
, stdin_obj
.size
);
455 free(stdin_obj
.data
);
456 ret
= send_callback_request(f
, &query
, result_handler
, private_result_data
);
461 static int com_addblob(callback_function
*f
, struct command_context
*cc
)
463 struct osl_object arg_obj
;
466 return -E_BLOB_SYNTAX
;
467 if (!*cc
->argv
[1]) /* empty name is reserved for the dummy row */
468 return -E_BLOB_SYNTAX
;
469 arg_obj
.size
= strlen(cc
->argv
[1]) + 1;
470 arg_obj
.data
= (char *)cc
->argv
[1];
471 return stdin_command(cc
, &arg_obj
, f
, 10 * 1024 * 1024, NULL
, NULL
);
474 /* FIXME: Print output to client, not to log file */
475 static void com_mvblob_callback(struct osl_table
*table
, __a_unused
int fd
,
476 const struct osl_object
*query
)
478 char *src
= (char *) query
->data
;
479 struct osl_object obj
= {.data
= src
, .size
= strlen(src
) + 1};
480 char *dest
= src
+ obj
.size
;
482 int ret
= osl(osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
));
487 obj
.size
= strlen(dest
) + 1;
488 ret
= osl(osl_update_object(table
, row
, BLOBCOL_NAME
, &obj
));
491 afs_event(BLOB_RENAME
, NULL
, table
);
494 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
497 static int com_mvblob(callback_function
*f
, struct command_context
*cc
)
500 return -E_MOOD_SYNTAX
;
501 return send_option_arg_callback_request(NULL
, cc
->argc
- 1,
502 cc
->argv
+ 1, f
, NULL
, NULL
);
505 #define DEFINE_BLOB_COMMAND(cmd_name, table_name, cmd_prefix) \
506 static void com_ ## cmd_name ## cmd_prefix ## _callback(int fd, const struct osl_object *query) \
508 return com_ ## cmd_name ## blob_callback(table_name ## _table, fd, query); \
510 int com_ ## cmd_name ## cmd_prefix(struct command_context *cc) \
512 return com_ ## cmd_name ## blob(com_ ## cmd_name ## cmd_prefix ## _callback, cc); \
515 static int blob_get_name_by_id(struct osl_table
*table
, uint32_t id
,
519 struct osl_object obj
= {.data
= &id
, .size
= sizeof(id
)};
525 ret
= osl(osl_get_row(table
, BLOBCOL_ID
, &obj
, &row
));
528 ret
= osl(osl_get_object(table
, row
, BLOBCOL_NAME
, &obj
));
531 *name
= (char *)obj
.data
;
535 /** Define the \p get_name_by_id function for this blob type. */
536 #define DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix) \
537 int cmd_prefix ## _get_name_by_id(uint32_t id, char **name) \
539 return blob_get_name_by_id(table_name ## _table, id, name); \
543 static int blob_get_def_by_name(struct osl_table
*table
, char *name
,
544 struct osl_object
*def
)
547 struct osl_object obj
= {.data
= name
, .size
= strlen(name
) + 1};
553 ret
= osl(osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
));
556 return osl(osl_open_disk_object(table
, row
, BLOBCOL_DEF
, def
));
559 /** Define the \p get_def_by_id function for this blob type. */
560 #define DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix) \
561 int cmd_prefix ## _get_def_by_name(char *name, struct osl_object *def) \
563 return blob_get_def_by_name(table_name ## _table, name, def); \
566 static int blob_get_def_by_id(struct osl_table
*table
, uint32_t id
,
567 struct osl_object
*def
)
570 struct osl_object obj
= {.data
= &id
, .size
= sizeof(id
)};
576 ret
= osl(osl_get_row(table
, BLOBCOL_ID
, &obj
, &row
));
579 return osl(osl_open_disk_object(table
, row
, BLOBCOL_DEF
, def
));
582 /** Define the \p get_def_by_id function for this blob type. */
583 #define DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix) \
584 int cmd_prefix ## _get_def_by_id(uint32_t id, struct osl_object *def) \
586 return blob_get_def_by_id(table_name ## _table, id, def); \
589 static int blob_get_name_and_def_by_row(struct osl_table
*table
,
590 const struct osl_row
*row
, char **name
, struct osl_object
*def
)
592 struct osl_object obj
;
593 int ret
= osl(osl_get_object(table
, row
, BLOBCOL_NAME
, &obj
));
597 return osl(osl_open_disk_object(table
, row
, BLOBCOL_DEF
, def
));
599 /** Define the \p get_name_and_def_by_row function for this blob type. */
600 #define DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix) \
601 int cmd_prefix ## _get_name_and_def_by_row(const struct osl_row *row, \
602 char **name, struct osl_object *def) \
604 return blob_get_name_and_def_by_row(table_name ## _table, \
608 /** Define the \p close function for this blob type. */
609 #define DEFINE_BLOB_CLOSE(table_name) \
610 static void table_name ## _close(void) \
612 osl_close_table(table_name ## _table, OSL_MARK_CLEAN); \
613 table_name ## _table = NULL; \
616 /** Define the \p create function for this blob type. */
617 #define DEFINE_BLOB_CREATE(table_name) \
618 static int table_name ## _create(const char *dir) \
620 table_name ## _table_desc.dir = dir; \
621 return osl(osl_create_table(&table_name ## _table_desc)); \
624 static int blob_open(struct osl_table
**table
,
625 struct osl_table_description
*desc
,
630 ret
= osl(osl_open_table(desc
, table
));
634 if (ret
>= 0 || ret
== -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT
))
639 #define DEFINE_BLOB_OPEN(table_name) \
640 static int table_name ## _open(const char *dir) \
642 return blob_open(&table_name ## _table, \
643 &table_name ## _table_desc, dir); \
647 /** Define the \p init function for this blob type. */
648 #define DEFINE_BLOB_INIT(table_name) \
649 void table_name ## _init(struct afs_table *t) \
651 t->open = table_name ## _open; \
652 t->close = table_name ## _close; \
653 t->create = table_name ## _create;\
654 t->event_handler = table_name ##_event_handler; \
655 table_name ## _table = NULL; \
659 /** Define all functions for this blob type. */
660 #define DEFINE_BLOB_FUNCTIONS(table_name, cmd_prefix) \
661 DEFINE_BLOB_OPEN(table_name) \
662 DEFINE_BLOB_CLOSE(table_name) \
663 DEFINE_BLOB_CREATE(table_name) \
664 DEFINE_BLOB_INIT(table_name) \
665 DEFINE_BLOB_COMMAND(ls, table_name, cmd_prefix) \
666 DEFINE_BLOB_COMMAND(cat, table_name, cmd_prefix) \
667 DEFINE_BLOB_COMMAND(add, table_name, cmd_prefix) \
668 DEFINE_BLOB_COMMAND(rm, table_name, cmd_prefix) \
669 DEFINE_BLOB_COMMAND(mv, table_name, cmd_prefix) \
670 DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix); \
671 DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix); \
672 DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix); \
673 DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix); \
675 /* doxygen isn't smart enough to recognize these */
676 /** \cond blob_function */
677 DEFINE_BLOB_FUNCTIONS(lyrics
, lyr
);
678 DEFINE_BLOB_FUNCTIONS(images
, img
);
679 DEFINE_BLOB_FUNCTIONS(moods
, mood
);
680 DEFINE_BLOB_FUNCTIONS(playlists
, pl
);
681 /** \endcond blob_function */