static void copy_labels_plus_unknown_commit_result (const bson_t *src, bson_t *dst) { bson_iter_t iter; bson_iter_t src_label; bson_t dst_labels; char str[16]; uint32_t i = 0; const char *key; BSON_APPEND_ARRAY_BEGIN (dst, "errorLabels", &dst_labels); BSON_APPEND_UTF8 (&dst_labels, "0", UNKNOWN_COMMIT_RESULT); /* append any other errorLabels already in "src" */ if (bson_iter_init_find (&iter, src, "errorLabels") && bson_iter_recurse (&iter, &src_label)) { while (bson_iter_next (&src_label) && BSON_ITER_HOLDS_UTF8 (&src_label)) { if (strcmp (bson_iter_utf8 (&src_label, NULL), UNKNOWN_COMMIT_RESULT) != 0) { i++; bson_uint32_to_string (i, &key, str, sizeof str); BSON_APPEND_UTF8 ( &dst_labels, key, bson_iter_utf8 (&src_label, NULL)); } } } bson_append_array_end (dst, &dst_labels); }
static void test_bson_iter_utf8 (void) { uint32_t len = 0; bson_iter_t iter; bson_t *b; char *s; b = bson_new(); assert(bson_append_utf8(b, "foo", -1, "bar", -1)); assert(bson_append_utf8(b, "bar", -1, "baz", -1)); assert(bson_iter_init(&iter, b)); assert(bson_iter_next(&iter)); assert(BSON_ITER_HOLDS_UTF8(&iter)); assert(!strcmp(bson_iter_key(&iter), "foo")); assert(!strcmp(bson_iter_utf8(&iter, NULL), "bar")); s = bson_iter_dup_utf8(&iter, &len); assert_cmpstr("bar", s); assert_cmpint(len, ==, 3); bson_free(s); assert(bson_iter_next(&iter)); assert(BSON_ITER_HOLDS_UTF8(&iter)); assert(!strcmp(bson_iter_key(&iter), "bar")); assert(!strcmp(bson_iter_utf8(&iter, NULL), "baz")); assert(!bson_iter_next(&iter)); bson_destroy(b); }
static void _mongoc_cursor_populate_error (mongoc_cursor_t *cursor, const bson_t *doc, bson_error_t *error) { bson_uint32_t code = MONGOC_ERROR_QUERY_FAILURE; bson_iter_t iter; const char *msg = "Unknown query failure"; BSON_ASSERT (cursor); BSON_ASSERT (doc); BSON_ASSERT (error); if (bson_iter_init_find (&iter, doc, "code") && BSON_ITER_HOLDS_INT32 (&iter)) { code = bson_iter_int32 (&iter); } if (bson_iter_init_find (&iter, doc, "$err") && BSON_ITER_HOLDS_UTF8 (&iter)) { msg = bson_iter_utf8 (&iter, NULL); } if (cursor->is_command && bson_iter_init_find (&iter, doc, "errmsg") && BSON_ITER_HOLDS_UTF8 (&iter)) { msg = bson_iter_utf8 (&iter, NULL); } bson_set_error(error, MONGOC_ERROR_QUERY, code, "%s", msg); }
void _mongoc_sasl_set_properties (mongoc_sasl_t *sasl, const mongoc_uri_t *uri) { const bson_t *options; bson_iter_t iter; bson_t properties; const char *service_name = NULL; bool canonicalize = false; _mongoc_sasl_set_pass(sasl, mongoc_uri_get_password(uri)); _mongoc_sasl_set_user(sasl, mongoc_uri_get_username(uri)); options = mongoc_uri_get_options (uri); if (!mongoc_uri_get_mechanism_properties (uri, &properties)) { bson_init (&properties); } if (bson_iter_init_find_case ( &iter, options, MONGOC_URI_GSSAPISERVICENAME) && BSON_ITER_HOLDS_UTF8 (&iter)) { service_name = bson_iter_utf8 (&iter, NULL); } if (bson_iter_init_find_case (&iter, &properties, "SERVICE_NAME") && BSON_ITER_HOLDS_UTF8 (&iter)) { /* newer "authMechanismProperties" URI syntax takes precedence */ service_name = bson_iter_utf8 (&iter, NULL); } _mongoc_sasl_set_service_name (sasl, service_name); /* * Driver Authentication Spec: "Drivers MAY allow the user to request * canonicalization of the hostname. This might be required when the hosts * report different hostnames than what is used in the kerberos database. * The default is "false". * * Some underlying GSSAPI layers will do this for us, but can be disabled in * their config (krb.conf). * * See CDRIVER-323 for more information. */ if (bson_iter_init_find_case ( &iter, options, MONGOC_URI_CANONICALIZEHOSTNAME) && BSON_ITER_HOLDS_BOOL (&iter)) { canonicalize = bson_iter_bool (&iter); } if (bson_iter_init_find_case ( &iter, &properties, "CANONICALIZE_HOST_NAME") && BSON_ITER_HOLDS_UTF8 (&iter)) { /* newer "authMechanismProperties" URI syntax takes precedence */ canonicalize = !strcasecmp (bson_iter_utf8 (&iter, NULL), "true"); } sasl->canonicalize_host_name = canonicalize; bson_destroy (&properties); }
static int mongo_get_oauth_key(const u08bits *kid, oauth_key_data_raw *key) { mongoc_collection_t * collection = mongo_get_collection("oauth_key"); if (!collection) return -1; bson_t query; bson_init(&query); BSON_APPEND_UTF8(&query, "kid", (const char *)kid); bson_t fields; bson_init(&fields); BSON_APPEND_INT32(&fields, "lifetime", 1); BSON_APPEND_INT32(&fields, "timestamp", 1); BSON_APPEND_INT32(&fields, "as_rs_alg", 1); BSON_APPEND_INT32(&fields, "ikm_key", 1); mongoc_cursor_t * cursor; cursor = mongoc_collection_find(collection, MONGOC_QUERY_NONE, 0, 1, 0, &query, &fields, NULL); int ret = -1; ns_bzero(key,sizeof(oauth_key_data_raw)); STRCPY(key->kid,kid); if (!cursor) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error querying MongoDB collection 'oauth_key'\n"); } else { const bson_t * item; uint32_t length; bson_iter_t iter; if (mongoc_cursor_next(cursor, &item)) { if (bson_iter_init(&iter, item) && bson_iter_find(&iter, "as_rs_alg") && BSON_ITER_HOLDS_UTF8(&iter)) { STRCPY(key->as_rs_alg,bson_iter_utf8(&iter, &length)); } if (bson_iter_init(&iter, item) && bson_iter_find(&iter, "ikm_key") && BSON_ITER_HOLDS_UTF8(&iter)) { STRCPY(key->ikm_key,bson_iter_utf8(&iter, &length)); } if (bson_iter_init(&iter, item) && bson_iter_find(&iter, "timestamp") && BSON_ITER_HOLDS_INT64(&iter)) { key->timestamp = (u64bits)bson_iter_int64(&iter); } if (bson_iter_init(&iter, item) && bson_iter_find(&iter, "lifetime") && BSON_ITER_HOLDS_INT32(&iter)) { key->lifetime = (u32bits)bson_iter_int32(&iter); } ret = 0; } mongoc_cursor_destroy(cursor); } mongoc_collection_destroy(collection); bson_destroy(&query); bson_destroy(&fields); return ret; }
/** * _mongoc_gridfs_file_new_from_bson: * * creates a gridfs file from a bson object * * This is only really useful for instantiating a gridfs file from a server * side object */ mongoc_gridfs_file_t * _mongoc_gridfs_file_new_from_bson (mongoc_gridfs_t *gridfs, const bson_t *data) { mongoc_gridfs_file_t *file; const char *key; bson_iter_t iter; const uint8_t *buf; uint32_t buf_len; ENTRY; BSON_ASSERT (gridfs); BSON_ASSERT (data); file = bson_malloc0 (sizeof *file); file->gridfs = gridfs; bson_copy_to (data, &file->bson); bson_iter_init (&iter, &file->bson); while (bson_iter_next (&iter)) { key = bson_iter_key (&iter); if (0 == strcmp (key, "_id")) { bson_oid_copy (bson_iter_oid (&iter), &file->files_id); } else if (0 == strcmp (key, "length")) { file->length = bson_iter_int64 (&iter); } else if (0 == strcmp (key, "chunkSize")) { file->chunk_size = bson_iter_int32 (&iter); } else if (0 == strcmp (key, "uploadDate")) { file->upload_date = bson_iter_date_time (&iter); } else if (0 == strcmp (key, "md5")) { file->bson_md5 = bson_iter_utf8 (&iter, NULL); } else if (0 == strcmp (key, "filename")) { file->bson_filename = bson_iter_utf8 (&iter, NULL); } else if (0 == strcmp (key, "contentType")) { file->bson_content_type = bson_iter_utf8 (&iter, NULL); } else if (0 == strcmp (key, "aliases")) { bson_iter_array (&iter, &buf_len, &buf); bson_init_static (&file->bson_aliases, buf, buf_len); } else if (0 == strcmp (key, "metadata")) { bson_iter_document (&iter, &buf_len, &buf); bson_init_static (&file->bson_metadata, buf, buf_len); } } /* TODO: is there are a minimal object we should be verifying that we * actually have here? */ RETURN (file); }
char *be_mongo_getuser(void *handle, const char *username) { struct mongo_backend *conf = (struct mongo_backend *)handle; mongoc_collection_t *collection; mongoc_cursor_t *cursor; bson_error_t error; const bson_t *doc; const char *collection_name = "passport"; bson_t query; char *str; char *result = NULL; bson_init (&query); bson_append_utf8 (&query, "username", -1, username, -1); collection = mongoc_client_get_collection (conf->client, "cas", collection_name); cursor = mongoc_collection_find (collection, MONGOC_QUERY_NONE, 0, 0, 0, &query, NULL, /* Fields, NULL for all. */ NULL); /* Read Prefs, NULL for default */ bson_iter_t iter; while (!mongoc_cursor_error (cursor, &error) && mongoc_cursor_more (cursor)) { if (mongoc_cursor_next (cursor, &doc)) { bson_iter_init(&iter, doc); bson_iter_find(&iter, "pwd"); fprintf (stdout, "%s\n", bson_iter_utf8(&iter, NULL)); str = bson_as_json (doc, NULL); fprintf (stdout, "%s\n", str); bson_free (str); result = bson_iter_utf8(&iter, NULL); } } if (mongoc_cursor_error (cursor, &error)) { fprintf (stderr, "Cursor Failure: %s\n", error.message); return EXIT_FAILURE; } bson_destroy (&query); mongoc_cursor_destroy (cursor); mongoc_collection_destroy (collection); return result; }
static int _score_tags (const bson_t *read_tags, const bson_t *node_tags) { uint32_t len; bson_iter_t iter; const char *key; const char *str; int count; int i; bson_return_val_if_fail(read_tags, -1); bson_return_val_if_fail(node_tags, -1); count = bson_count_keys(read_tags); if (!bson_empty(read_tags) && bson_iter_init(&iter, read_tags)) { for (i = count; bson_iter_next(&iter); i--) { if (BSON_ITER_HOLDS_UTF8(&iter)) { key = bson_iter_key(&iter); str = bson_iter_utf8(&iter, &len); if (_contains_tag(node_tags, key, str, len)) { return count; } } } return -1; } return 0; }
/* * If error is not set, set code from first document in array like * [{"code": 64, "errmsg": "duplicate"}, ...]. Format the error message * from all errors in array. */ static void _set_error_from_response (bson_t *bson_array, mongoc_error_domain_t domain, const char *error_type, bson_error_t *error /* OUT */) { bson_iter_t array_iter; bson_iter_t doc_iter; bson_string_t *compound_err; const char *errmsg = NULL; int32_t code = 0; uint32_t n_keys, i; compound_err = bson_string_new (NULL); n_keys = bson_count_keys (bson_array); if (n_keys > 1) { bson_string_append_printf ( compound_err, "Multiple %s errors: ", error_type); } if (!bson_empty0 (bson_array) && bson_iter_init (&array_iter, bson_array)) { /* get first code and all error messages */ i = 0; while (bson_iter_next (&array_iter)) { if (BSON_ITER_HOLDS_DOCUMENT (&array_iter) && bson_iter_recurse (&array_iter, &doc_iter)) { /* parse doc, which is like {"code": 64, "errmsg": "duplicate"} */ while (bson_iter_next (&doc_iter)) { /* use the first error code we find */ if (BSON_ITER_IS_KEY (&doc_iter, "code") && code == 0) { code = bson_iter_int32 (&doc_iter); } else if (BSON_ITER_IS_KEY (&doc_iter, "errmsg")) { errmsg = bson_iter_utf8 (&doc_iter, NULL); /* build message like 'Multiple write errors: "foo", "bar"' */ if (n_keys > 1) { bson_string_append_printf (compound_err, "\"%s\"", errmsg); if (i < n_keys - 1) { bson_string_append (compound_err, ", "); } } else { /* single error message */ bson_string_append (compound_err, errmsg); } } } i++; } } if (code && compound_err->len) { bson_set_error ( error, domain, (uint32_t) code, "%s", compound_err->str); } } bson_string_free (compound_err, true); }
bool bson_find_create_table (bson_t *bson_schema, const char *table_name, bson_iter_t *iter_col) { bson_iter_t iter_json, iter_ary, iter_sql, iter_table_prop; bson_iter_init_find (&iter_json, bson_schema, "json") || DIE; BSON_ITER_HOLDS_ARRAY (&iter_json) || DIE; bson_iter_recurse (&iter_json, &iter_ary) || DIE; while (bson_iter_next (&iter_ary)) { (BSON_ITER_HOLDS_DOCUMENT (&iter_ary) || DIE); bson_iter_recurse (&iter_ary, &iter_sql) || DIE; if (bson_iter_find (&iter_sql, "create_table") && (BSON_ITER_HOLDS_DOCUMENT (&iter_sql) || DIE) && (bson_iter_recurse (&iter_sql, &iter_table_prop) || DIE) && (bson_iter_find (&iter_table_prop, "table_name") || DIE) && (BSON_ITER_HOLDS_UTF8 (&iter_table_prop) || DIE) && (strcmp (bson_iter_utf8 (&iter_table_prop, NULL), table_name) == 0) && (bson_iter_find (&iter_table_prop, "columns") || DIE) && (BSON_ITER_HOLDS_ARRAY (&iter_table_prop) || DIE)) { bson_iter_recurse (&iter_table_prop, iter_col) || DIE; return true; } } return (false); }
int monary_load_size_value(const bson_iter_t* bsonit, monary_column_item* citem, int idx) { bson_type_t type; const uint8_t* discard; uint32_t size; uint32_t* dest; type = bson_iter_type(bsonit); switch (type) { case BSON_TYPE_UTF8: case BSON_TYPE_CODE: bson_iter_utf8(bsonit, &size); break; case BSON_TYPE_BINARY: bson_iter_binary(bsonit, NULL, &size, &discard); break; case BSON_TYPE_DOCUMENT: bson_iter_document(bsonit, &size, &discard); break; case BSON_TYPE_ARRAY: bson_iter_array(bsonit, &size, &discard); break; default: return 0; } dest = ((uint32_t*) citem->storage) + idx; memcpy(dest, &size, sizeof(uint32_t)); return 1; }
static bool mongoc_uri_finalize_auth (mongoc_uri_t *uri) { bson_iter_t iter; const char *source = NULL; const char *mechanism = mongoc_uri_get_auth_mechanism(uri); if (bson_iter_init_find_case(&iter, &uri->credentials, "authSource")) { source = bson_iter_utf8(&iter, NULL); } /* authSource with GSSAPI or X509 should always be external */ if (mechanism) { if (!strcasecmp(mechanism, "GSSAPI") || !strcasecmp(mechanism, "MONGODB-X509")) { if (source) { if (strcasecmp(source, "$external")) { return false; } } else { bson_append_utf8(&uri->credentials, "authsource", -1, "$external", -1); } } } return true; }
/* Test method that returns the server's version string */ static String HHVM_METHOD(MongoClient, getServerVersion) { bool result; bson_t buildInfo, reply; bson_error_t error; bson_iter_t iter; String retval; auto client = get_client(this_); bson_init(&buildInfo); bson_append_int32(&buildInfo, "buildInfo", 9, 1); result = mongoc_client_command_simple(client->get(), "test", &buildInfo, nullptr, &reply, &error); bson_destroy(&buildInfo); if ( ! result) { throw Exception("Command error: %s", error.message); } if (bson_iter_init_find(&iter, &reply, "version")) { retval = String(bson_iter_utf8(&iter, nullptr), CopyString); } bson_destroy(&reply); return retval; }
/* *-------------------------------------------------------------------------- * * mongoc_server_description_has_rs_member -- * * Return true if this address is included in server's list of rs * members, false otherwise. * * Returns: * true, false * * Side effects: * None * *-------------------------------------------------------------------------- */ bool mongoc_server_description_has_rs_member(mongoc_server_description_t *server, const char *address) { bson_iter_t member_iter; const bson_t *rs_members[3]; int i; if (server->type != MONGOC_SERVER_UNKNOWN) { rs_members[0] = &server->hosts; rs_members[1] = &server->arbiters; rs_members[2] = &server->passives; for (i = 0; i < 3; i++) { bson_iter_init (&member_iter, rs_members[i]); while (bson_iter_next (&member_iter)) { if (strcasecmp (address, bson_iter_utf8 (&member_iter, NULL)) == 0) { return true; } } } } return false; }
static void _mongoc_populate_query_error (const bson_t *doc, int32_t error_api_version, bson_error_t *error) { mongoc_error_domain_t domain = error_api_version >= MONGOC_ERROR_API_VERSION_2 ? MONGOC_ERROR_SERVER : MONGOC_ERROR_QUERY; uint32_t code = MONGOC_ERROR_QUERY_FAILURE; bson_iter_t iter; const char *msg = "Unknown query failure"; ENTRY; BSON_ASSERT (doc); if (bson_iter_init_find (&iter, doc, "code") && BSON_ITER_HOLDS_INT32 (&iter)) { code = (uint32_t) bson_iter_int32 (&iter); } if (bson_iter_init_find (&iter, doc, "$err") && BSON_ITER_HOLDS_UTF8 (&iter)) { msg = bson_iter_utf8 (&iter, NULL); } bson_set_error (error, domain, code, "%s", msg); EXIT; }
/* * Start iterating the reply to an "aggregate", "find", "getMore" etc. command: * * {cursor: {id: 1234, ns: "db.collection", firstBatch: [...]}} */ bool _mongoc_cursor_cursorid_start_batch (mongoc_cursor_t *cursor) { mongoc_cursor_cursorid_t *cid; bson_iter_t iter; bson_iter_t child; const char *ns; uint32_t nslen; cid = (mongoc_cursor_cursorid_t *)cursor->iface_data; BSON_ASSERT (cid); if (bson_iter_init_find (&iter, &cid->array, "cursor") && BSON_ITER_HOLDS_DOCUMENT (&iter) && bson_iter_recurse (&iter, &child)) { while (bson_iter_next (&child)) { if (BSON_ITER_IS_KEY (&child, "id")) { cursor->rpc.reply.cursor_id = bson_iter_as_int64 (&child); } else if (BSON_ITER_IS_KEY (&child, "ns")) { ns = bson_iter_utf8 (&child, &nslen); _mongoc_set_cursor_ns (cursor, ns, nslen); } else if (BSON_ITER_IS_KEY (&child, "firstBatch") || BSON_ITER_IS_KEY (&child, "nextBatch")) { if (BSON_ITER_HOLDS_ARRAY (&child) && bson_iter_recurse (&child, &cid->batch_iter)) { cid->in_batch = true; } } } } return cid->in_batch; }
static gboolean sim_parser_connect_version (bson_iter_t *piter, const char *key, SimCommand *cmd) { g_return_val_if_fail (piter != NULL, FALSE); g_return_val_if_fail (cmd != NULL, FALSE); g_return_val_if_fail (key != NULL, FALSE); cmd->data.connect.sensor_ver = g_new0 (SimVersion, 1); if (BSON_ITER_HOLDS_UTF8(piter)) { /* We need to split the version x.y.z.n */ const gchar *version = bson_iter_utf8 (piter, NULL); if (strlen (version) > 0) { sim_version_parse (version, &(cmd->data.connect.sensor_ver->major), &(cmd->data.connect.sensor_ver->minor), &(cmd->data.connect.sensor_ver->micro), &(cmd->data.connect.sensor_ver->nano)); } } return TRUE; }
static int mongo_get_admin_user(const u08bits *usname, u08bits *realm, password_t pwd) { mongoc_collection_t * collection = mongo_get_collection("admin_user"); if(!collection) return -1; realm[0]=0; pwd[0]=0; bson_t query; bson_init(&query); BSON_APPEND_UTF8(&query, "name", (const char *)usname); bson_t fields; bson_init(&fields); BSON_APPEND_INT32(&fields, "realm", 1); BSON_APPEND_INT32(&fields, "password", 1); mongoc_cursor_t * cursor; cursor = mongoc_collection_find(collection, MONGOC_QUERY_NONE, 0, 1, 0, &query, &fields, NULL); int ret = -1; if (!cursor) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error querying MongoDB collection 'admin_user'\n"); } else { const bson_t * item; uint32_t length; bson_iter_t iter; if (mongoc_cursor_next(cursor, &item)) { if (bson_iter_init(&iter, item) && bson_iter_find(&iter, "realm") && BSON_ITER_HOLDS_UTF8(&iter)) { strncpy((char*)realm,bson_iter_utf8(&iter, &length),STUN_MAX_REALM_SIZE); ret = 0; } if (bson_iter_init(&iter, item) && bson_iter_find(&iter, "password") && BSON_ITER_HOLDS_UTF8(&iter)) { strncpy((char*)pwd,bson_iter_utf8(&iter, &length),STUN_MAX_PWD_SIZE); ret = 0; } } mongoc_cursor_destroy(cursor); } mongoc_collection_destroy(collection); bson_destroy(&query); bson_destroy(&fields); return ret; }
types::b_utf8 element::get_utf8() const { CITER; uint32_t len; const char* val = bson_iter_utf8(&iter, &len); return types::b_utf8{string_or_literal{val, len}}; }
static struct ofields* get_data(struct results* res) { struct ofields* fields; const char* msg; const char* prog; const char* syslog_tag; int64_t date_r; bson_iter_t c; fields = malloc (sizeof (struct ofields)); bson_iter_init_find (&c, res->result, "msg"); if (!(msg = bson_iter_utf8 (&c, NULL))) { perror ("bson_cursor_get_string()"); exit (1); } bson_iter_init_find (&c, res->result, "sys"); if (!(prog = bson_iter_utf8 (&c, NULL))) { perror ("bson_cursor_get_string()"); exit (1); } bson_iter_init_find (&c, res->result, "syslog_tag"); if (!(syslog_tag = bson_iter_utf8 (&c, NULL))) { perror ("bson_cursor_get_string()"); exit (1); } bson_iter_init_find (&c, res->result, "time_rcvd"); if (!(date_r = bson_iter_date_time (&c))) { perror ("bson_cursor_get_utc_datetime()"); exit (1); } fields->msg = msg; fields->prog = prog; fields->syslog_tag = syslog_tag; fields->date_r = date_r; return fields; }
/* *----------------------------------------------------------------------- * * Run the JSON tests from the SDAM Monitoring spec. * *----------------------------------------------------------------------- */ static void test_sdam_monitoring_cb (bson_t *test) { mongoc_client_t *client; mongoc_topology_t *topology; bson_t phase; bson_t phases; bson_t outcome; bson_iter_t phase_iter; bson_iter_t phase_field_iter; bson_iter_t outcome_iter; bson_iter_t iter; bson_t events_expected; context_t context; /* parse out the uri and use it to create a client */ BSON_ASSERT (bson_iter_init_find (&iter, test, "uri")); client = mongoc_client_new (bson_iter_utf8 (&iter, NULL)); topology = client->topology; context_init (&context); client_set_topology_event_callbacks (client, &context); /* for each phase, parse and validate */ BSON_ASSERT (bson_iter_init_find (&iter, test, "phases")); bson_iter_bson (&iter, &phases); bson_iter_init (&phase_iter, &phases); while (bson_iter_next (&phase_iter)) { bson_iter_bson (&phase_iter, &phase); /* this test doesn't exercise this code path naturally, see below in * _test_topology_events for a non-hacky test of this event */ _mongoc_topology_description_monitor_opening (&topology->description); process_sdam_test_ismaster_responses (&phase, &client->topology->description); /* parse out "outcome" and validate */ BSON_ASSERT (bson_iter_init_find (&phase_field_iter, &phase, "outcome")); bson_iter_bson (&phase_field_iter, &outcome); bson_iter_init (&outcome_iter, &outcome); while (bson_iter_next (&outcome_iter)) { if (strcmp ("events", bson_iter_key (&outcome_iter)) == 0) { bson_iter_bson (&outcome_iter, &events_expected); check_json_apm_events (&context.events, &events_expected); } else { fprintf (stderr, "ERROR: unparsed test field %s\n", bson_iter_key (&outcome_iter)); BSON_ASSERT (false); } } } mongoc_client_destroy (client); context_destroy (&context); }
static void _test_bson_type_print_description (bson_iter_t *iter) { if (bson_iter_find (iter, "description") && BSON_ITER_HOLDS_UTF8 (iter)) { if (test_suite_debug_output ()) { fprintf (stderr, " - %s\n", bson_iter_utf8 (iter, NULL)); fflush (stderr); } } }
types::b_utf8 element::get_utf8() const { BSONCXX_TYPE_CHECK(k_utf8); BSONCXX_CITER; uint32_t len; const char* val = bson_iter_utf8(&iter, &len); return types::b_utf8{stdx::string_view{val, len}}; }
static int _score_tags (const bson_t *read_tags, const bson_t *node_tags) { uint32_t len; bson_iter_t iter; bson_iter_t sub_iter; const char *key; const char *str; int count; bool node_matches_set; bson_return_val_if_fail(read_tags, -1); bson_return_val_if_fail(node_tags, -1); count = bson_count_keys(read_tags); /* Execute this block if read tags were provided, else bail and return 0 (all nodes equal) */ if (!bson_empty(read_tags) && bson_iter_init(&iter, read_tags)) { /* * Iterate over array of read tag sets provided (each element is a tag set) * Tag sets are provided in order of preference so return the count of the * first set that matches the node or -1 if no set matched the node. */ while (count && bson_iter_next(&iter)) { if (BSON_ITER_HOLDS_DOCUMENT(&iter) && bson_iter_recurse(&iter, &sub_iter)) { node_matches_set = true; /* Iterate over the key/value pairs (tags) in the current set */ while (bson_iter_next(&sub_iter) && BSON_ITER_HOLDS_UTF8(&sub_iter)) { key = bson_iter_key(&sub_iter); str = bson_iter_utf8(&sub_iter, &len); /* If any of the tags do not match, this node cannot satisfy this tag set. */ if (!_contains_tag(node_tags, key, str, len)) { node_matches_set = false; break; } } /* This set matched, return the count as the score */ if (node_matches_set) { return count; } /* Decrement the score and try to match the next set. */ count--; } } return -1; } return 0; }
void ha_replica_set_wait_for_healthy (ha_replica_set_t *replica_set) { bson_iter_t iter; bson_iter_t ar; bson_iter_t member; const char *stateStr; bson_t status; again: sleep(1); if (!ha_replica_set_get_status(replica_set, &status)) { MONGOC_INFO("Failed to get replicaSet status. " "Sleeping 1 second."); goto again; } #if 0 { char *str; str = bson_as_json(&status, NULL); printf("%s\n", str); bson_free(str); } #endif if (!bson_iter_init_find(&iter, &status, "members") || !BSON_ITER_HOLDS_ARRAY(&iter) || !bson_iter_recurse(&iter, &ar)) { bson_destroy(&status); MONGOC_INFO("ReplicaSet has not yet come online. " "Sleeping 1 second."); goto again; } while (bson_iter_next(&ar)) { if (BSON_ITER_HOLDS_DOCUMENT(&ar) && bson_iter_recurse(&ar, &member) && bson_iter_find(&member, "stateStr") && (stateStr = bson_iter_utf8(&member, NULL))) { if (!!strcmp(stateStr, "PRIMARY") && !!strcmp(stateStr, "SECONDARY") && !!strcmp(stateStr, "ARBITER")) { bson_destroy(&status); MONGOC_INFO("Found unhealthy node. Sleeping 1 second."); goto again; } } } bson_destroy(&status); }
char * set_name (bson_t *ismaster_response) { bson_iter_t iter; if (bson_iter_init_find (&iter, ismaster_response, "setName")) { return bson_strdup (bson_iter_utf8 (&iter, NULL)); } else { return NULL; } }
/** * mongoc_database_has_collection: * @database: (in): A #mongoc_database_t. * @name: (in): The name of the collection to check for. * @error: (out) (allow-none): A location for a #bson_error_t, or %NULL. * * Checks to see if a collection exists within the database on the MongoDB * server. * * This will return %false if their was an error communicating with the * server, or if the collection does not exist. * * If @error is provided, it will first be zeroed. Upon error, error.domain * will be set. * * Returns: %true if @name exists, otherwise %false. @error may be set. */ bool mongoc_database_has_collection (mongoc_database_t *database, const char *name, bson_error_t *error) { mongoc_collection_t *collection; mongoc_read_prefs_t *read_prefs; mongoc_cursor_t *cursor; const bson_t *doc; bson_iter_t iter; bool ret = false; const char *cur_name; bson_t q = BSON_INITIALIZER; char ns[140]; ENTRY; BSON_ASSERT (database); BSON_ASSERT (name); if (error) { memset (error, 0, sizeof *error); } bson_snprintf (ns, sizeof ns, "%s.%s", database->name, name); ns[sizeof ns - 1] = '\0'; read_prefs = mongoc_read_prefs_new (MONGOC_READ_PRIMARY); collection = mongoc_client_get_collection (database->client, database->name, "system.namespaces"); cursor = mongoc_collection_find (collection, MONGOC_QUERY_NONE, 0, 0, 0, &q, NULL, read_prefs); while (!mongoc_cursor_error (cursor, error) && mongoc_cursor_more (cursor)) { while (mongoc_cursor_next (cursor, &doc) && bson_iter_init_find (&iter, doc, "name") && BSON_ITER_HOLDS_UTF8 (&iter)) { cur_name = bson_iter_utf8(&iter, NULL); if (!strcmp(cur_name, ns)) { ret = true; GOTO(cleanup); } } } cleanup: mongoc_cursor_destroy (cursor); mongoc_collection_destroy (collection); mongoc_read_prefs_destroy (read_prefs); RETURN(ret); }
static void _bson_to_error (const bson_t *b, bson_error_t *error) { bson_iter_t iter; int code = 0; BSON_ASSERT(b); if (!error) { return; } if (bson_iter_init_find(&iter, b, "code") && BSON_ITER_HOLDS_INT32(&iter)) { code = bson_iter_int32(&iter); } if (bson_iter_init_find(&iter, b, "$err") && BSON_ITER_HOLDS_UTF8(&iter)) { bson_set_error(error, MONGOC_ERROR_QUERY, code, "%s", bson_iter_utf8(&iter, NULL)); return; } if (bson_iter_init_find(&iter, b, "errmsg") && BSON_ITER_HOLDS_UTF8(&iter)) { bson_set_error(error, MONGOC_ERROR_QUERY, code, "%s", bson_iter_utf8(&iter, NULL)); return; } bson_set_error(error, MONGOC_ERROR_QUERY, MONGOC_ERROR_QUERY_FAILURE, "An unknown error ocurred on the server."); }
/** * mongoc_database_has_collection: * @database: (in): A #mongoc_database_t. * @name: (in): The name of the collection to check for. * @error: (out) (allow-none): A location for a #bson_error_t, or %NULL. * * Checks to see if a collection exists within the database on the MongoDB * server. * * This will return %false if their was an error communicating with the * server, or if the collection does not exist. * * If @error is provided, it will first be zeroed. Upon error, error.domain * will be set. * * Returns: %true if @name exists, otherwise %false. @error may be set. */ bool mongoc_database_has_collection (mongoc_database_t *database, const char *name, bson_error_t *error) { bson_iter_t col_iter; bool ret = false; const char *cur_name; bson_t filter = BSON_INITIALIZER; mongoc_cursor_t *cursor; const bson_t *doc; ENTRY; BSON_ASSERT (database); BSON_ASSERT (name); if (error) { memset (error, 0, sizeof *error); } BSON_APPEND_UTF8 (&filter, "name", name); cursor = mongoc_database_find_collections (database, &filter, error); if (!cursor) { return ret; } if (error && ((error->domain != 0) || (error->code != 0))) { GOTO (cleanup); } while (mongoc_cursor_next (cursor, &doc)) { if (bson_iter_init (&col_iter, doc) && bson_iter_find (&col_iter, "name") && BSON_ITER_HOLDS_UTF8 (&col_iter) && (cur_name = bson_iter_utf8 (&col_iter, NULL))) { if (!strcmp (cur_name, name)) { ret = true; GOTO (cleanup); } } } cleanup: mongoc_cursor_destroy (cursor); RETURN (ret); }
const char * mongoc_uri_get_auth_source (const mongoc_uri_t *uri) { bson_iter_t iter; BSON_ASSERT (uri); if (bson_iter_init_find_case(&iter, &uri->credentials, "authSource")) { return bson_iter_utf8(&iter, NULL); } return uri->database ? uri->database : "admin"; }