api-94-expand-during-parse.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* We use direct access to hand to the parse function: what if db expands? */
  2. #include "config.h"
  3. #include "../ntdb.h"
  4. #include "tap-interface.h"
  5. #include "logging.h"
  6. #include "../private.h" /* To establish size, esp. for NTDB_INTERNAL dbs */
  7. #include "helpapi-external-agent.h"
  8. static struct ntdb_context *ntdb;
  9. static off_t ntdb_size(void)
  10. {
  11. return ntdb->file->map_size;
  12. }
  13. struct parse_info {
  14. unsigned int depth;
  15. NTDB_DATA expected;
  16. };
  17. static enum NTDB_ERROR parse(NTDB_DATA key, NTDB_DATA data,
  18. struct parse_info *pinfo)
  19. {
  20. off_t flen;
  21. unsigned int i;
  22. if (!ntdb_deq(data, pinfo->expected))
  23. return NTDB_ERR_EINVAL;
  24. flen = ntdb_size();
  25. for (i = 0; ntdb_size() == flen; i++) {
  26. NTDB_DATA add = ntdb_mkdata(&i, sizeof(i));
  27. /* This is technically illegal parse(), which is why we
  28. * grabbed allrecord lock.*/
  29. ntdb_store(ntdb, add, add, NTDB_INSERT);
  30. }
  31. /* Access the record again. */
  32. if (!ntdb_deq(data, pinfo->expected))
  33. return NTDB_ERR_EINVAL;
  34. /* Recurse! Woot! */
  35. if (pinfo->depth != 0) {
  36. enum NTDB_ERROR ecode;
  37. pinfo->depth--;
  38. ecode = ntdb_parse_record(ntdb, key, parse, pinfo);
  39. if (ecode) {
  40. return ecode;
  41. }
  42. }
  43. /* Access the record one more time. */
  44. if (!ntdb_deq(data, pinfo->expected))
  45. return NTDB_ERR_EINVAL;
  46. return NTDB_SUCCESS;
  47. }
  48. int main(int argc, char *argv[])
  49. {
  50. unsigned int i;
  51. int flags[] = { NTDB_INTERNAL, NTDB_DEFAULT, NTDB_NOMMAP,
  52. NTDB_INTERNAL|NTDB_CONVERT, NTDB_CONVERT,
  53. NTDB_NOMMAP|NTDB_CONVERT };
  54. struct parse_info pinfo;
  55. NTDB_DATA key = ntdb_mkdata("hello", 5), data = ntdb_mkdata("world", 5);
  56. plan_tests(sizeof(flags) / sizeof(flags[0]) * 3 + 1);
  57. for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
  58. ntdb = ntdb_open("api-94-expand-during-parse.ntdb",
  59. flags[i]|MAYBE_NOSYNC,
  60. O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
  61. ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == NTDB_SUCCESS);
  62. ok1(ntdb_lockall(ntdb) == NTDB_SUCCESS);
  63. pinfo.expected = data;
  64. pinfo.depth = 3;
  65. ok1(ntdb_parse_record(ntdb, key, parse, &pinfo) == NTDB_SUCCESS);
  66. ntdb_unlockall(ntdb);
  67. ntdb_close(ntdb);
  68. }
  69. ok1(tap_log_messages == 0);
  70. return exit_status();
  71. }