bool my_fileset_get( struct my_fileset *fs, size_t i, const char **fname_out, void **ptr_out) { if (i < entry_vec_size(fs->entries)) { *fname_out = entry_vec_value(fs->entries, i)->fname; *ptr_out = entry_vec_value(fs->entries, i)->ptr; return (true); } return (false); }
void my_fileset_destroy(struct my_fileset **fs) { if (*fs != NULL) { for (size_t i = 0; i < entry_vec_size((*fs)->entries); i++) { struct fileset_entry *ent = entry_vec_value((*fs)->entries, i); if ((*fs)->unload) (*fs)->unload(*fs, ent->fname, ent->ptr); free(ent->fname); free(ent); } entry_vec_destroy(&(*fs)->entries); free((*fs)->setdir); free((*fs)->setfile); free(*fs); *fs = NULL; } }
void mtbl_sorter_destroy(struct mtbl_sorter **s) { if (*s) { for (unsigned i = 0; i < entry_vec_size((*s)->vec); i++) { struct entry *ent = entry_vec_value((*s)->vec, i); free(ent); } entry_vec_destroy(&((*s)->vec)); for (unsigned i = 0; i < chunk_vec_size((*s)->chunks); i++) { struct chunk *c = chunk_vec_value((*s)->chunks, i); close(c->fd); free(c); } chunk_vec_destroy(&((*s)->chunks)); free((*s)->opt.tmp_dname); free(*s); *s = NULL; } }
void my_fileset_reload(struct my_fileset *fs) { assert(fs != NULL); struct fileset_entry *ent, **entptr; entry_vec *new_entries; FILE *fp; char *fname, *line = NULL; size_t len = 0; ubuf *u; if (!setfile_updated(fs)) return; fp = fopen(fs->setfile, "r"); if (fp == NULL) return; u = ubuf_init(64); new_entries = entry_vec_init(1); while (getline(&line, &len, fp) != -1) { ubuf_clip(u, 0); if (line[0] != '/') { /* if not absolute path, prepend fileset file's path */ ubuf_add_cstr(u, fs->setdir); ubuf_add(u, '/'); } ubuf_add_cstr(u, line); ubuf_rstrip(u, '\n'); fname = ubuf_cstr(u); if (path_exists(fname)) { entptr = fetch_entry(fs->entries, fname); if (entptr == NULL) { ent = my_calloc(1, sizeof(*ent)); ent->fname = my_strdup(fname); if (fs->load) ent->ptr = fs->load(fs, fname); entry_vec_add(new_entries, ent); } else { ent = my_calloc(1, sizeof(*ent)); ent->fname = my_strdup(fname); ent->ptr = (*entptr)->ptr; (*entptr)->keep = true; entry_vec_add(new_entries, ent); } } } free(line); fclose(fp); qsort(entry_vec_data(new_entries), entry_vec_size(new_entries), sizeof(void *), cmp_fileset_entry); for (size_t i = 0; i < entry_vec_size(fs->entries); i++) { ent = entry_vec_value(fs->entries, i); assert(ent != NULL); if (ent->keep == false && fs->unload) fs->unload(fs, ent->fname, ent->ptr); free(ent->fname); free(ent); } entry_vec_destroy(&fs->entries); fs->entries = new_entries; ubuf_destroy(&u); }
ubuf_append(tmp_fname, (const uint8_t *) "\x00", 1); c->fd = mkstemp((char *) ubuf_data(tmp_fname)); assert(c->fd >= 0); int unlink_ret = unlink((char *) ubuf_data(tmp_fname)); assert(unlink_ret == 0); ubuf_destroy(&tmp_fname); struct mtbl_writer_options *wopt = mtbl_writer_options_init(); mtbl_writer_options_set_compression(wopt, MTBL_COMPRESSION_SNAPPY); struct mtbl_writer *w = mtbl_writer_init_fd(c->fd, wopt); mtbl_writer_options_destroy(&wopt); size_t entries_written = 0; struct entry **array = entry_vec_data(s->vec); qsort(array, entry_vec_size(s->vec), sizeof(void *), _mtbl_sorter_compare); for (unsigned i = 0; i < entry_vec_size(s->vec); i++) { struct entry *ent = entry_vec_value(s->vec, i); if (i + 1 < entry_vec_size(s->vec)) { struct entry *next_ent = entry_vec_value(s->vec, i + 1); struct entry *merge_ent = NULL; if (_mtbl_sorter_compare(&ent, &next_ent) == 0) { assert(s->opt.merge != NULL); uint8_t *merge_val = NULL; size_t len_merge_val = 0; s->opt.merge(s->opt.merge_clos, entry_key(ent), ent->len_key, entry_val(ent), ent->len_val, entry_val(next_ent), next_ent->len_val,