PUBLIC int r_io_repo_data_file_fd(struct ccnr_handle *h, unsigned repofile, int output) { if (repofile != 1) return(-1); if (output) return(-1); if (h->repofile1_fd > 0) return(h->repofile1_fd); h->repofile1_fd = r_io_open_repo_data_file(h, "repoFile1", 0); return(h->repofile1_fd); }
int r_init_map_and_process_file(struct ndnr_handle *h, struct ndn_charbuf *filename, int add_content) { int res = 0; int dres; struct stat statbuf; unsigned char *mapped_file = MAP_FAILED; unsigned char *msg; size_t size; int fd = -1; struct content_entry *content; struct ndn_skeleton_decoder *d; struct fdholder *fdholder; fd = r_io_open_repo_data_file(h, ndn_charbuf_as_string(filename), 0); if (fd == -1) // Normal exit return(1); res = fstat(fd, &statbuf); if (res != 0) { ndnr_msg(h, "stat failed for %s (fd=%d), %s (errno=%d)", ndn_charbuf_as_string(filename), fd, strerror(errno), errno); res = -errno; goto Bail; } if (statbuf.st_size == 0) goto Bail; mapped_file = mmap(NULL, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0); if (mapped_file == MAP_FAILED) { ndnr_msg(h, "mmap failed for %s (fd=%d), %s (errno=%d)", ndn_charbuf_as_string(filename), fd, strerror(errno), errno); res = -errno; goto Bail; } fdholder = r_io_fdholder_from_fd(h, fd); d = &fdholder->decoder; msg = mapped_file; size = statbuf.st_size; while (d->index < size) { dres = ndn_skeleton_decode(d, msg + d->index, size - d->index); if (!NDN_FINAL_DSTATE(d->state)) break; if (add_content) { content = process_incoming_content(h, fdholder, msg + d->index - dres, dres, NULL); if (content != NULL) r_store_commit_content(h, content); } } if (d->index != size || !NDN_FINAL_DSTATE(d->state)) { ndnr_msg(h, "protocol error on fdholder %u (state %d), discarding %d bytes", fdholder->filedesc, d->state, (int)(size - d->index)); res = -1; goto Bail; } Bail: if (mapped_file != MAP_FAILED) munmap(mapped_file, statbuf.st_size); r_io_shutdown_client_fd(h, fd); return (res); }
PUBLIC void r_store_init(struct ccnr_handle *h) { struct ccn_btree *btree = NULL; struct ccn_btree_node *node = NULL; struct hashtb_param param = {0}; int i; int j; int res; struct ccn_charbuf *path = NULL; struct ccn_charbuf *msgs = NULL; off_t offset; path = ccn_charbuf_create(); param.finalize_data = h; param.finalize = 0; h->cob_limit = r_init_confval(h, "CCNR_CONTENT_CACHE", 16, 2000000, 4201); h->cookie_limit = choose_limit(h->cob_limit, (ccnr_cookie)(~0U)); h->content_by_cookie = calloc(h->cookie_limit, sizeof(h->content_by_cookie[0])); CHKPTR(h->content_by_cookie); h->content_by_accession_tab = hashtb_create(sizeof(struct content_by_accession_entry), NULL); CHKPTR(h->content_by_accession_tab); h->btree = btree = ccn_btree_create(); CHKPTR(btree); FAILIF(btree->nextnodeid != 1); ccn_charbuf_putf(path, "%s/index", h->directory); res = mkdir(ccn_charbuf_as_string(path), 0700); if (res != 0 && errno != EEXIST) r_init_fail(h, __LINE__, ccn_charbuf_as_string(path), errno); else { msgs = ccn_charbuf_create(); btree->io = ccn_btree_io_from_directory(ccn_charbuf_as_string(path), msgs); if (btree->io == NULL) res = errno; if (msgs->length != 0 && CCNSHOULDLOG(h, sffdsdf, CCNL_WARNING)) { ccnr_msg(h, "while initializing %s - %s", ccn_charbuf_as_string(path), ccn_charbuf_as_string(msgs)); } ccn_charbuf_destroy(&msgs); if (btree->io == NULL) r_init_fail(h, __LINE__, ccn_charbuf_as_string(path), res); } node = ccn_btree_getnode(btree, 1, 0); if (btree->io != NULL) btree->nextnodeid = btree->io->maxnodeid + 1; CHKPTR(node); if (node->buf->length == 0) { res = ccn_btree_init_node(node, 0, 'R', 0); CHKSYS(res); } ccn_charbuf_destroy(&path); if (h->running == -1) return; r_store_read_stable_point(h); h->active_in_fd = -1; h->active_out_fd = r_io_open_repo_data_file(h, "repoFile1", 1); /* output */ offset = lseek(h->active_out_fd, 0, SEEK_END); h->startupbytes = offset; if (offset != h->stable || node->corrupt != 0) { ccnr_msg(h, "Index not current - resetting"); ccn_btree_init_node(node, 0, 'R', 0); node = NULL; ccn_btree_destroy(&h->btree); path = ccn_charbuf_create(); /* Remove old index files to avoid confusion */ for (i = 1, j = 0; i > 0 && j < 3; i++) { path->length = 0; res = ccn_charbuf_putf(path, "%s/index/%d", h->directory, i); if (res >= 0) res = unlink(ccn_charbuf_as_string(path)); if (res < 0) j++; } h->btree = btree = ccn_btree_create(); path->length = 0; ccn_charbuf_putf(path, "%s/index", h->directory); btree->io = ccn_btree_io_from_directory(ccn_charbuf_as_string(path), msgs); CHKPTR(btree->io); btree->io->maxnodeid = 0; btree->nextnodeid = 1; node = ccn_btree_getnode(btree, 1, 0); btree->nextnodeid = btree->io->maxnodeid + 1; ccn_btree_init_node(node, 0, 'R', 0); h->stable = 0; h->active_in_fd = r_io_open_repo_data_file(h, "repoFile1", 0); /* input */ ccn_charbuf_destroy(&path); if (CCNSHOULDLOG(h, dfds, CCNL_INFO)) ccn_schedule_event(h->sched, 50000, r_store_reindexing, NULL, 0); } if (CCNSHOULDLOG(h, weuyg, CCNL_FINEST)) { FILE *dumpfile = NULL; path = ccn_charbuf_create(); ccn_charbuf_putf(path, "%s/index/btree_check.out", h->directory); dumpfile = fopen(ccn_charbuf_as_string(path), "w"); res = ccn_btree_check(btree, dumpfile); if (dumpfile != NULL) { fclose(dumpfile); dumpfile = NULL; } else path->length = 0; ccnr_msg(h, "ccn_btree_check returned %d (%s)", res, ccn_charbuf_as_string(path)); ccn_charbuf_destroy(&path); if (res < 0) r_init_fail(h, __LINE__, "index is corrupt", res); } btree->full = r_init_confval(h, "CCNR_BTREE_MAX_FANOUT", 4, 9999, 1999); btree->full0 = r_init_confval(h, "CCNR_BTREE_MAX_LEAF_ENTRIES", 4, 9999, 1999); btree->nodebytes = r_init_confval(h, "CCNR_BTREE_MAX_NODE_BYTES", 1024, 8388608, 2097152); btree->nodepool = r_init_confval(h, "CCNR_BTREE_NODE_POOL", 16, 2000000, 512); if (h->running != -1) r_store_index_needs_cleaning(h); }