#include "common.h"
-GENERATE_BSON_APPEND_STRUCT(timespec,
- bson_append_long(out, "sec", data->tv_sec);
- bson_append_long(out, "nsec", data->tv_nsec);
- )
-
-int faultd_bson_iterator_timespec(const bson_iterator *i, struct timespec *t)
-{
- int ret = -EINVAL;
- bson_iterator it;
- bson_type bt;
- bool sec = false;
- bool nsec = false;
-
- bson_iterator_subiterator(i, &it);
- while ((bt = bson_iterator_next(&it)) != BSON_EOO) {
- if ((bt == BSON_LONG) &&
- (strcmp("sec", bson_iterator_key(&it)) == 0)) {
-
- sec = true;
- t->tv_sec = bson_iterator_long(&it);
-
- } else if ((bt == BSON_LONG) &&
- (strcmp("nsec", bson_iterator_key(&it)) == 0)) {
-
- nsec = true;
- t->tv_nsec = bson_iterator_long(&it);
-
- }
- }
-
- if (sec && nsec)
- ret = 0;
-
- return ret;
-}
-
-int bson_fill_empty(bson *b)
-{
- int ret;
-
- ret = faultd_bson_safe_init_size(b, BSON_EMPTY_SIZE);
- if (ret != BSON_OK)
- goto out;
-
- ret = bson_finish(b);
- if (ret != BSON_OK)
- goto destroy;
-
- return 0;
-destroy:
- bson_destroy(b);
-out:
- return -ENOMEM;
-}
-
-char *bson_get_string(bson *b, char *fieldpath)
-{
- bson_iterator it;
- int ret;
-
- BSON_ITERATOR_INIT(&it, b);
-
- ret = bson_find_fieldpath_value(fieldpath, &it);
- if (ret == BSON_EOO || ret != BSON_STRING)
- return NULL;
-
- return (char *)bson_iterator_string(&it);
-}
-
static const size_t data_size[] = {
[TYPE_OID] = sizeof(faultd_oid_t),
[TYPE_STRING] = sizeof(char *),
}
#define _cleanup_free_ _cleanup_(freep)
-#define DECLARE_BSON_APPEND_STRUCT(type) \
- void faultd_bson_append_##type(bson* out, const char *key, struct type *data)
-
-/* FIXME: bson_append_XXX can faile, check return codes */
-#define GENERATE_BSON_APPEND_STRUCT(type, code) \
- DECLARE_BSON_APPEND_STRUCT(type) \
- { \
- bson_append_start_object(out, key); \
- do { code } while(0); \
- bson_append_finish_object(out); \
- }
-
-DECLARE_BSON_APPEND_STRUCT(timespec);
-int faultd_bson_iterator_timespec(const bson_iterator *i, struct timespec *t);
-
-#define BSON_EMPTY_SIZE (4 /* len */ + 1 /* \0 */)
-
-#define BSON_INITIAL_BUFFER_SIZE 128
-
-static inline int faultd_bson_safe_init_size(bson *b, int size)
-{
- int ret;
- /*
- * TODO:
- * For now ejdb does not provide bson constructors
- * which return error if memory allocation fails.
- * This wrapper should be removed when such functions
- * become available.
- */
- bson_init_size(b, 0);
- ret = bson_ensure_space(b, size);
- if (ret != BSON_OK)
- bson_destroy(b);
-
- return ret;
-}
-
-#define faultd_bson_safe_init(b) \
- faultd_bson_safe_init_size(b, BSON_INITIAL_BUFFER_SIZE)
-
-int bson_fill_empty(bson *b);
-
-char *bson_get_string(bson *b, char *fieldpath);
-
enum faultd_type {
TYPE_OID,
TYPE_STRING,