struct mtbl_sorter * mtbl_sorter_init(struct mtbl_sorter_options *opt) { struct mtbl_sorter *s; s = my_calloc(1, sizeof(*s)); if (opt != NULL) { memcpy(&s->opt, opt, sizeof(*opt)); s->opt.tmp_dname = strdup(opt->tmp_dname); } s->vec = entry_vec_init(INITIAL_SORTER_VEC_SIZE); s->chunks = chunk_vec_init(1); return (s); }
struct my_fileset * my_fileset_init( const char *setfile, my_fileset_load_func load, my_fileset_unload_func unload, void *user) { assert(path_exists(setfile)); struct my_fileset *fs = my_calloc(1, sizeof(*fs)); char *t = my_strdup(setfile); fs->setdir = my_strdup(dirname(t)); free(t); fs->setfile = my_strdup(setfile); fs->load = load; fs->unload = unload; fs->user = user; fs->entries = entry_vec_init(1); return (fs); }
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); }