int main(void) { PureDBW dbw; if (puredbw_open(&dbw, "puredb.index", "puredb.data", "puredb.pdb") != 0) { perror("Can't create the database"); goto end; } if (puredbw_add_s(&dbw, "key", "content") != 0 || puredbw_add_s(&dbw, "key2", "content2") != 0 || puredbw_add_s(&dbw, "key42", "content42") != 0) { perror("Error while inserting key/data pairs"); goto end; } if (puredbw_close(&dbw) != 0) { perror("Error while closing the database"); } end: puredbw_free(&dbw); return 0; }
int main(void) { char key[42]; char data[42]; unsigned long long curkey = 0ULL; unsigned long long nbrec = 0ULL; PureDBW dbw; PureDB db; off_t retpos; size_t retlen; char *founddata; int pass = 0; unsigned int seed = 0U; unsigned int randomrounds = 42000; printf("Starting regression tests\n\nDatabase creation (wait) ... "); fflush(stdout); if (puredbw_open(&dbw, "puredb.index", "puredb.data", "puredb.pdb") != 0) { perror("Can't create the database"); goto end; } seed = (unsigned int) time(NULL); srand(seed); do { curkey += (rand() & 0x4fff); snprintf(key, sizeof key, "%llu", curkey); snprintf(data, sizeof data, "%llu", curkey ^ 0x12345678abcdefULL); if (puredbw_add_s(&dbw, key, data) != 0) { goto end; } nbrec++; } while (curkey < (unsigned long long) 0xfffffff0); if (puredbw_close(&dbw) != 0) { goto end; } pass++; end: puredbw_free(&dbw); if (pass == 0) { puts("Failure :("); unlink("puredb.index"); unlink("puredb.data"); unlink("puredb.pdb"); return -1; } else { printf("Success! %llu records have been written\n", nbrec); pass = 0; } printf("Database lookups (wait) ... "); fflush(stdout); if (puredb_open(&db, "puredb.pdb") != 0) { perror("Can't open the database"); goto end2; } curkey = 0ULL; srand(seed); do { curkey += (rand() & 0x4fff); snprintf(key, sizeof key, "%llu", curkey); snprintf(data, sizeof data, "%llu", curkey ^ 0x12345678abcdefULL); if (puredb_find_s(&db, key, &retpos, &retlen) != 0) { fprintf(stderr, "The key wasn't found\n"); goto end2; } if ((founddata = puredb_read(&db, retpos, retlen)) != NULL) { if (strcmp(founddata, data) != 0) { fprintf(stderr, "Wrong data\n"); goto end2; } puredb_read_free(founddata); } } while (curkey < 0xfffffff0); printf("also trying non-existent data ... "); fflush(stdout); do { curkey <<= 1; curkey ^= (unsigned long long) rand(); snprintf(key, sizeof key, "%llu", curkey); if (puredb_find_s(&db, key, &retpos, &retlen) == 0) { founddata = puredb_read(&db, retpos, retlen); puredb_read_free(founddata); } randomrounds--; } while (randomrounds > 0U); pass++; end2: if (puredb_close(&db) != 0) { perror("The database couldn't be properly closed"); } unlink("puredb.pdb"); if (pass == 0) { puts("Failure :("); return -1; } else { puts("Success!"); } return 0; }
static int do_mkdb(const char *dbfile, const char * const file) { FILE *fp; char *index_dbfile; size_t sizeof_index_dbfile; char *data_dbfile; size_t sizeof_data_dbfile; char *s; PureDBW dbw; int ret = PW_ERROR_UNEXPECTED_ERROR; char line[LINE_MAX]; if (dbfile == NULL || *dbfile == 0) { char *dbfile_; if ((dbfile_ = getenv(ENV_DEFAULT_PW_DB)) != NULL && *dbfile_ != 0) { dbfile = dbfile_; } else { dbfile = DEFAULT_PW_DB; } } if (file == NULL) { fprintf(stderr, "Missing passwd file\n"); return PW_ERROR_MISSING_PASSWD_FILE; } if ((fp = fopen(file, "r")) == NULL) { perror("Unable to open the passwd file"); return PW_ERROR_MISSING_PASSWD_FILE; } sizeof_index_dbfile = strlen(dbfile) + sizeof NEWPASSWD_INDEX_SUFFIX; if ((index_dbfile = ALLOCA(sizeof_index_dbfile)) == NULL) { fclose(fp); no_mem(); } sizeof_data_dbfile = strlen(dbfile) + sizeof NEWPASSWD_DATA_SUFFIX; if ((data_dbfile = ALLOCA(sizeof_data_dbfile)) == NULL) { fclose(fp); ALLOCA_FREE(index_dbfile); no_mem(); } snprintf(index_dbfile, sizeof_index_dbfile, "%s%s", dbfile, NEWPASSWD_INDEX_SUFFIX); snprintf(data_dbfile, sizeof_data_dbfile, "%s%s", dbfile, NEWPASSWD_DATA_SUFFIX); if (puredbw_open(&dbw, index_dbfile, data_dbfile, dbfile) != 0) { perror("Unable to create the database"); goto err; } while (fgets(line, (int) sizeof line - 1U, fp) != NULL) { strip_lf(line); if (*line == PW_LINE_COMMENT) { continue; } if (*line == 0 || (s = strchr(line, *PW_LINE_SEP)) == NULL || s[1] == 0) { continue; } *s++ = 0; if (puredbw_add_s(&dbw, line, s) != 0) { perror("Error while indexing a new entry"); goto err; } } if (puredbw_close(&dbw) != 0) { perror("Unable to close the database"); } else { ret = 0; } err: puredbw_free(&dbw); ALLOCA_FREE(index_dbfile); ALLOCA_FREE(data_dbfile); fclose(fp); return ret; }