bson_append_long(out, "sec", data->tv_sec);
bson_append_long(out, "nsec", data->tv_nsec);
)
+
+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);
+}
#include <ejdb/bson.h>
#include <stdlib.h>
#include <time.h>
+#include <errno.h>
#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*a))
DECLARE_BSON_APPEND_STRUCT(timespec);
+#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);
+
#endif /* FAULTD_COMMON_H */