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 /** \cond doxygen isn't smart enough to recognize these */
84 INIT_BLOB_TABLE(lyrics
);
85 INIT_BLOB_TABLE(images
);
86 INIT_BLOB_TABLE(moods
);
87 INIT_BLOB_TABLE(playlists
);
90 /** Flags that may be passed to the \p ls functions of each blob type. */
92 /** List both id and name. */
93 BLOB_LS_FLAG_LONG
= 1,
94 /** Reverse sort order. */
95 BLOB_LS_FLAG_REVERSE
= 2,
96 /** Sort by id instead of name. */
97 BLOB_LS_FLAG_SORT_BY_ID
= 4,
100 /** Structure passed to the \p print_blob function. */
101 struct lsblob_action_data
{
102 /** The flags given at the command line. */
104 /** Message buffer. */
105 struct para_buffer pb
;
108 static int print_blob(struct osl_table
*table
, struct osl_row
*row
,
109 const char *name
, void *data
)
111 struct lsblob_action_data
*lbad
= data
;
112 struct osl_object obj
;
116 if (!(lbad
->flags
& BLOB_LS_FLAG_LONG
))
117 return para_printf(&lbad
->pb
, "%s\n", name
);
118 ret
= osl(osl_get_object(table
, row
, BLOBCOL_ID
, &obj
));
120 para_printf(&lbad
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
123 id
= *(uint32_t *)obj
.data
;
124 return para_printf(&lbad
->pb
, "%u\t%s\n", id
, name
);
127 static void com_lsblob_callback(struct osl_table
*table
,
128 int fd
, const struct osl_object
*query
)
130 struct lsblob_action_data lbad
= {
131 .flags
= *(uint32_t *)query
->data
,
135 .max_size_handler
= pass_buffer_as_shm
138 struct pattern_match_data pmd
= {
140 .patterns
= {.data
= (char *)query
->data
+ sizeof(uint32_t),
141 .size
= query
->size
- sizeof(uint32_t)},
142 .pm_flags
= PM_NO_PATTERN_MATCHES_EVERYTHING
| PM_SKIP_EMPTY_NAME
,
143 .match_col_num
= BLOBCOL_NAME
,
145 .action
= print_blob
,
149 if (lbad
.flags
& BLOB_LS_FLAG_REVERSE
)
150 pmd
.pm_flags
|= PM_REVERSE_LOOP
;
151 if (!(lbad
.flags
& BLOB_LS_FLAG_SORT_BY_ID
))
152 pmd
.loop_col_num
= BLOBCOL_NAME
;
154 pmd
.loop_col_num
= BLOBCOL_ID
;
155 ret
= for_each_matching_row(&pmd
);
157 para_printf(&lbad
.pb
, "%s\n", para_strerror(-ret
));
159 pass_buffer_as_shm(lbad
.pb
.buf
, lbad
.pb
.offset
, &fd
);
163 static int com_lsblob(callback_function
*f
, struct stream_cipher_context
*scc
, int argc
, char * const * const argv
)
166 struct osl_object options
= {.data
= &flags
, .size
= sizeof(flags
)};
169 for (i
= 1; i
< argc
; i
++) {
170 const char *arg
= argv
[i
];
173 if (!strcmp(arg
, "--")) {
177 if (!strcmp(arg
, "-l")) {
178 flags
|= BLOB_LS_FLAG_LONG
;
181 if (!strcmp(arg
, "-i")) {
182 flags
|= BLOB_LS_FLAG_SORT_BY_ID
;
185 if (!strcmp(arg
, "-r")) {
186 flags
|= BLOB_LS_FLAG_REVERSE
;
192 // return -E_BLOB_SYNTAX;
193 return send_option_arg_callback_request(&options
, argc
- i
,
194 argv
+ i
, f
, sc_send_result
, scc
);
197 static int cat_blob(struct osl_table
*table
, struct osl_row
*row
,
198 __a_unused
const char *name
, void *data
)
201 struct osl_object obj
;
203 ret
= osl(osl_open_disk_object(table
, row
, BLOBCOL_DEF
, &obj
));
207 ret
= pass_buffer_as_shm(obj
.data
, obj
.size
, data
);
208 ret2
= osl(osl_close_disk_object(&obj
));
209 return (ret
< 0)? ret
: ret2
;
212 static void com_catblob_callback(struct osl_table
*table
, int fd
,
213 const struct osl_object
*query
)
215 struct pattern_match_data pmd
= {
218 .loop_col_num
= BLOBCOL_NAME
,
219 .match_col_num
= BLOBCOL_NAME
,
220 .pm_flags
= PM_SKIP_EMPTY_NAME
,
224 for_each_matching_row(&pmd
);
227 static int com_catblob(callback_function
*f
, struct stream_cipher_context
*scc
, int argc
,
228 char * const * const argv
)
231 return -E_BLOB_SYNTAX
;
232 return send_standard_callback_request(argc
- 1, argv
+ 1, f
,
233 sc_send_result
, scc
);
236 /** Used for removing rows from a blob table. */
238 /** Message buffer. */
239 struct para_buffer pb
;
240 /** Number of removed blobs. */
241 unsigned num_removed
;
244 static int remove_blob(struct osl_table
*table
, struct osl_row
*row
,
245 const char *name
, void *data
)
247 struct rmblob_data
*rmbd
= data
;
248 int ret
= osl(osl_del_row(table
, row
));
250 para_printf(&rmbd
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
257 static void com_rmblob_callback(struct osl_table
*table
, int fd
,
258 const struct osl_object
*query
)
261 struct rmblob_data rmbd
= {
266 .max_size_handler
= pass_buffer_as_shm
269 struct pattern_match_data pmd
= {
272 .loop_col_num
= BLOBCOL_NAME
,
273 .match_col_num
= BLOBCOL_NAME
,
274 .pm_flags
= PM_SKIP_EMPTY_NAME
,
276 .action
= remove_blob
278 ret
= for_each_matching_row(&pmd
);
280 ret2
= para_printf(&rmbd
.pb
, "%s\n", para_strerror(-ret
));
284 if (!rmbd
.num_removed
)
285 ret2
= para_printf(&rmbd
.pb
, "no matches, nothing removed\n");
287 ret2
= para_printf(&rmbd
.pb
, "removed %d blobs\n", rmbd
.num_removed
);
288 afs_event(BLOB_RENAME
, NULL
, table
);
291 if (ret2
>= 0 && rmbd
.pb
.offset
)
292 pass_buffer_as_shm(rmbd
.pb
.buf
, rmbd
.pb
.offset
, &fd
);
296 static int com_rmblob(callback_function
*f
, struct stream_cipher_context
*scc
, int argc
,
297 char * const * const argv
)
300 return -E_MOOD_SYNTAX
;
301 return send_option_arg_callback_request(NULL
, argc
- 1, argv
+ 1, f
,
302 sc_send_result
, scc
);
305 static void com_addblob_callback(struct osl_table
*table
, __a_unused
int fd
,
306 const struct osl_object
*query
)
308 struct osl_object objs
[NUM_BLOB_COLUMNS
];
309 char *name
= query
->data
;
310 size_t name_len
= strlen(name
) + 1;
315 ret
= osl(osl_get_num_rows(table
, &num_rows
));
318 if (!num_rows
) { /* this is the first entry ever added */
319 /* insert dummy row containing the id */
320 id
= 2; /* this entry will be entry #1, so 2 is the next */
321 objs
[BLOBCOL_ID
].data
= &id
;
322 objs
[BLOBCOL_ID
].size
= sizeof(id
);
323 objs
[BLOBCOL_NAME
].data
= "";
324 objs
[BLOBCOL_NAME
].size
= 1;
325 objs
[BLOBCOL_DEF
].data
= "";
326 objs
[BLOBCOL_DEF
].size
= 1;
327 ret
= osl(osl_add_row(table
, objs
));
331 /* check if name already exists */
333 struct osl_object obj
= {.data
= name
, .size
= name_len
};
334 ret
= osl(osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
));
335 if (ret
< 0 && ret
!= -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND
))
337 if (ret
>= 0) { /* we already have a blob with this name */
338 obj
.data
= name
+ name_len
;
339 obj
.size
= query
->size
- name_len
;
340 ret
= osl(osl_update_object(table
, row
, BLOBCOL_DEF
, &obj
));
343 /* new blob, get id of the dummy row and increment it */
346 ret
= osl(osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
));
349 ret
= osl(osl_get_object(table
, row
, BLOBCOL_ID
, &obj
));
352 id
= *(uint32_t *)obj
.data
+ 1;
354 ret
= osl(osl_update_object(table
, row
, BLOBCOL_ID
, &obj
));
359 objs
[BLOBCOL_ID
].data
= &id
;
360 objs
[BLOBCOL_ID
].size
= sizeof(id
);
361 objs
[BLOBCOL_NAME
].data
= name
;
362 objs
[BLOBCOL_NAME
].size
= name_len
;
363 objs
[BLOBCOL_DEF
].data
= name
+ name_len
;
364 objs
[BLOBCOL_DEF
].size
= query
->size
- name_len
;
365 ret
= osl(osl_add_row(table
, objs
));
368 afs_event(BLOB_ADD
, NULL
, table
);
371 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
375 * write input from fd to dynamically allocated buffer,
376 * but maximal max_size byte.
378 static int fd2buf(struct stream_cipher_context
*scc
, unsigned max_size
, struct osl_object
*obj
)
380 const size_t chunk_size
= 1024;
381 size_t size
= 2048, received
= 0;
383 char *buf
= para_malloc(size
);
386 ret
= sc_recv_bin_buffer(scc
, buf
+ received
, chunk_size
);
390 if (received
+ chunk_size
>= size
) {
392 ret
= -E_INPUT_TOO_LARGE
;
395 buf
= para_realloc(buf
, size
);
399 obj
->size
= received
;
406 * Read data from a file descriptor, and send it to the afs process.
408 * \param scc crypt context containing the file descriptor to read data from.
409 * \param arg_obj Pointer to the arguments to \a f.
410 * \param f The callback function.
411 * \param max_len Don't read more than that many bytes from stdin.
412 * \param result_handler See \ref send_callback_request.
413 * \param private_result_data See \ref send_callback_request.
415 * This function is used by commands that wish to let para_server store
416 * arbitrary data specified by the user (for instance the add_blob family of
417 * commands). First, at most \a max_len bytes are read and decrypted from the
418 * file descriptor given by \a scc. The result is concatenated with the buffer
419 * given by \a arg_obj, and the combined buffer is made available to the afs
420 * process via the callback method. See \ref send_callback_request for details.
422 * \return Negative on errors, the return value of the underlying call to
423 * send_callback_request() otherwise.
425 static int stdin_command(struct stream_cipher_context
*scc
, struct osl_object
*arg_obj
,
426 callback_function
*f
, unsigned max_len
,
427 callback_result_handler
*result_handler
,
428 void *private_result_data
)
430 struct osl_object query
, stdin_obj
;
433 ret
= sc_send_buffer(scc
, AWAITING_DATA_MSG
);
436 ret
= fd2buf(scc
, max_len
, &stdin_obj
);
439 query
.size
= arg_obj
->size
+ stdin_obj
.size
;
440 query
.data
= para_malloc(query
.size
);
441 memcpy(query
.data
, arg_obj
->data
, arg_obj
->size
);
442 memcpy((char *)query
.data
+ arg_obj
->size
, stdin_obj
.data
, stdin_obj
.size
);
443 free(stdin_obj
.data
);
444 ret
= send_callback_request(f
, &query
, result_handler
, private_result_data
);
449 static int com_addblob(callback_function
*f
, struct stream_cipher_context
*scc
, int argc
,
450 char * const * const argv
)
452 struct osl_object arg_obj
;
455 return -E_BLOB_SYNTAX
;
456 if (!*argv
[1]) /* empty name is reserved for the dummy row */
457 return -E_BLOB_SYNTAX
;
458 arg_obj
.size
= strlen(argv
[1]) + 1;
459 arg_obj
.data
= (char *)argv
[1];
460 return stdin_command(scc
, &arg_obj
, f
, 10 * 1024 * 1024, NULL
, NULL
);
463 /* FIXME: Print output to client, not to log file */
464 static void com_mvblob_callback(struct osl_table
*table
, __a_unused
int fd
,
465 const struct osl_object
*query
)
467 char *src
= (char *) query
->data
;
468 struct osl_object obj
= {.data
= src
, .size
= strlen(src
) + 1};
469 char *dest
= src
+ obj
.size
;
471 int ret
= osl(osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
));
476 obj
.size
= strlen(dest
) + 1;
477 ret
= osl(osl_update_object(table
, row
, BLOBCOL_NAME
, &obj
));
480 afs_event(BLOB_RENAME
, NULL
, table
);
483 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
486 static int com_mvblob(callback_function
*f
, __a_unused
struct stream_cipher_context
*scc
,
487 int argc
, char * const * const argv
)
490 return -E_MOOD_SYNTAX
;
491 return send_option_arg_callback_request(NULL
, argc
- 1, argv
+ 1, f
,
495 #define DEFINE_BLOB_COMMAND(cmd_name, table_name, cmd_prefix) \
496 static void com_ ## cmd_name ## cmd_prefix ## _callback(int fd, const struct osl_object *query) \
498 return com_ ## cmd_name ## blob_callback(table_name ## _table, fd, query); \
500 int com_ ## cmd_name ## cmd_prefix(struct stream_cipher_context *scc, int argc, char * const * const argv) \
502 return com_ ## cmd_name ## blob(com_ ## cmd_name ## cmd_prefix ## _callback, scc, argc, argv); \
505 static int blob_get_name_by_id(struct osl_table
*table
, uint32_t id
,
509 struct osl_object obj
= {.data
= &id
, .size
= sizeof(id
)};
515 ret
= osl(osl_get_row(table
, BLOBCOL_ID
, &obj
, &row
));
518 ret
= osl(osl_get_object(table
, row
, BLOBCOL_NAME
, &obj
));
521 *name
= (char *)obj
.data
;
525 /** Define the \p get_name_by_id function for this blob type. */
526 #define DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix) \
527 int cmd_prefix ## _get_name_by_id(uint32_t id, char **name) \
529 return blob_get_name_by_id(table_name ## _table, id, name); \
533 static int blob_get_def_by_name(struct osl_table
*table
, char *name
,
534 struct osl_object
*def
)
537 struct osl_object obj
= {.data
= name
, .size
= strlen(name
) + 1};
543 ret
= osl(osl_get_row(table
, BLOBCOL_NAME
, &obj
, &row
));
546 return osl(osl_open_disk_object(table
, row
, BLOBCOL_DEF
, def
));
549 /** Define the \p get_def_by_id function for this blob type. */
550 #define DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix) \
551 int cmd_prefix ## _get_def_by_name(char *name, struct osl_object *def) \
553 return blob_get_def_by_name(table_name ## _table, name, def); \
556 static int blob_get_def_by_id(struct osl_table
*table
, uint32_t id
,
557 struct osl_object
*def
)
560 struct osl_object obj
= {.data
= &id
, .size
= sizeof(id
)};
566 ret
= osl(osl_get_row(table
, BLOBCOL_ID
, &obj
, &row
));
569 return osl(osl_open_disk_object(table
, row
, BLOBCOL_DEF
, def
));
572 /** Define the \p get_def_by_id function for this blob type. */
573 #define DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix) \
574 int cmd_prefix ## _get_def_by_id(uint32_t id, struct osl_object *def) \
576 return blob_get_def_by_id(table_name ## _table, id, def); \
579 static int blob_get_name_and_def_by_row(struct osl_table
*table
,
580 const struct osl_row
*row
, char **name
, struct osl_object
*def
)
582 struct osl_object obj
;
583 int ret
= osl(osl_get_object(table
, row
, BLOBCOL_NAME
, &obj
));
587 return osl(osl_open_disk_object(table
, row
, BLOBCOL_DEF
, def
));
589 /** Define the \p get_name_and_def_by_row function for this blob type. */
590 #define DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix) \
591 int cmd_prefix ## _get_name_and_def_by_row(const struct osl_row *row, \
592 char **name, struct osl_object *def) \
594 return blob_get_name_and_def_by_row(table_name ## _table, \
598 /** Define the \p close function for this blob type. */
599 #define DEFINE_BLOB_CLOSE(table_name) \
600 static void table_name ## _close(void) \
602 osl_close_table(table_name ## _table, OSL_MARK_CLEAN); \
603 table_name ## _table = NULL; \
606 /** Define the \p create function for this blob type. */
607 #define DEFINE_BLOB_CREATE(table_name) \
608 static int table_name ## _create(const char *dir) \
610 table_name ## _table_desc.dir = dir; \
611 return osl_create_table(&table_name ## _table_desc); \
614 static int blob_open(struct osl_table
**table
,
615 struct osl_table_description
*desc
,
620 ret
= osl(osl_open_table(desc
, table
));
624 if (ret
>= 0 || ret
== -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT
))
629 #define DEFINE_BLOB_OPEN(table_name) \
630 static int table_name ## _open(const char *dir) \
632 return blob_open(&table_name ## _table, \
633 &table_name ## _table_desc, dir); \
637 /** Define the \p init function for this blob type. */
638 #define DEFINE_BLOB_INIT(table_name) \
639 void table_name ## _init(struct afs_table *t) \
641 t->open = table_name ## _open; \
642 t->close = table_name ## _close; \
643 t->create = table_name ## _create;\
644 t->event_handler = table_name ##_event_handler; \
645 table_name ## _table = NULL; \
649 /** Define all functions for this blob type. */
650 #define DEFINE_BLOB_FUNCTIONS(table_name, cmd_prefix) \
651 DEFINE_BLOB_OPEN(table_name) \
652 DEFINE_BLOB_CLOSE(table_name) \
653 DEFINE_BLOB_CREATE(table_name) \
654 DEFINE_BLOB_INIT(table_name) \
655 DEFINE_BLOB_COMMAND(ls, table_name, cmd_prefix) \
656 DEFINE_BLOB_COMMAND(cat, table_name, cmd_prefix) \
657 DEFINE_BLOB_COMMAND(add, table_name, cmd_prefix) \
658 DEFINE_BLOB_COMMAND(rm, table_name, cmd_prefix) \
659 DEFINE_BLOB_COMMAND(mv, table_name, cmd_prefix) \
660 DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix); \
661 DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix); \
662 DEFINE_GET_DEF_BY_NAME(table_name, cmd_prefix); \
663 DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix); \
665 /** \cond doxygen isn't smart enough to recognize these */
666 DEFINE_BLOB_FUNCTIONS(lyrics
, lyr
);
667 DEFINE_BLOB_FUNCTIONS(images
, img
);
668 DEFINE_BLOB_FUNCTIONS(moods
, mood
);
669 DEFINE_BLOB_FUNCTIONS(playlists
, pl
);