0

I am writing a C function to read a bson encoded file. I'm trying to find the key to an array. I know the key exists, because bsondump will print it and the array out. bson_iter_find() returns false when I search for "vol".

Here's the function:

static int nas_initalize(const char *path)
{
  int error = 0;
  bson_reader_t *reader = bson_reader_new_from_file(path, &bson_error);
  if (!reader) {
    fprintf (stderr, "ERROR: %d.%d: %s\n",
             bson_error.domain, bson_error.code, bson_error.message);
  }
  super_block = bson_reader_read(reader, NULL);
  bson_iter_init(&iter, super_block);

  if (bson_iter_find(&iter, "max_dir"))
    {
      max_dir = bson_iter_int32(&iter);
    }
  else
    {
      error = -1;
      bson_reader_destroy(reader);
    }

  if (bson_iter_find(&iter, "raid_lv"))
    {
      raid_lv = bson_iter_int32(&iter);
    }
  else
    {
      error = -1;
      bson_reader_destroy(reader);
    }

  if(bson_iter_find(&iter, "vol"))
    {
      int count = 0;
      bson_iter_recurse(&iter, sub_iter);
      while(bson_iter_next(sub_iter) == true)
        {
          if (bson_iter_find_descendant(&iter, "vol.0", sub_iter))
            {
              vol[count++] = bson_iter_utf8 (sub_iter, NULL);
            }
        }
    }
  else
    {
      error = -1;
      bson_reader_destroy(reader);
    }

  bson_reader_destroy(reader);

  return error;
}
Community
  • 1
  • 1
Ironlenny
  • 634
  • 7
  • 10

1 Answers1

0

you are doing multiple bson_iter_find() commands and there is a chance that you're hopping right over the data you want.

You need to reinitialize the iter object for every find or be ABSOLUTELY sure that you're calling the find's in the exact order as they are serialized into the bson document.

See: Does order matter in bson_iter_find in mongo c driver

Community
  • 1
  • 1
bauman.space
  • 1,993
  • 13
  • 15