static struct io_watch *watch_new(struct io *io, GIOCondition cond, io_callback_func_t callback, void *user_data, io_destroy_func_t destroy) { struct io_watch *watch; watch = g_try_new0(struct io_watch, 1); if (!watch) return NULL; watch->io = io_ref(io); watch->callback = callback; watch->destroy = destroy; watch->user_data = user_data; watch->id = g_io_add_watch_full(io->channel, G_PRIORITY_DEFAULT, cond | G_IO_ERR | G_IO_NVAL, watch_callback, watch, watch_destroy); if (watch->id == 0) { watch_destroy(watch); return NULL; } return watch; }
lib_t *lib_create(const char *dbfile, const char *libdir) { lib_t *lib=malloc(sizeof(lib_t)); if(!lib) return NULL; lib->watch=watch_create(lib); if(!lib->watch) { error(ll, "failed to create watcher."); goto err; } lib->watch->event=lib_watch_event; empty=string_create_unique(""); ll=get_logger("lib"); lib->entries=chunked_list_create(512, sizeof(lib_entry), &lib_entry_destructor); if(!lib->entries) { error(ll, "failed to allocate lib."); goto err; } int s=strlen(libdir); if(libdir[s-1]!='/') { char *tmp=malloc(s+2); strcpy(tmp, libdir); tmp[s++]='/'; tmp[s]=0; libdir=tmp; } else libdir=strdup(libdir); lib->base_path=libdir; lib->base_path_size=s; filecount=0; lib->dbfile=strdup(dbfile); lib_read(lib); // pthread_create(&lib->check_thread, NULL, check_lib, lib); // pthread_join(lib->check_thread, NULL); check_lib(lib); return lib; err: if(lib) { if(lib->entries) chunked_list_destroy(lib->entries); if(lib->watch) watch_destroy(lib->watch); free(lib); } return NULL; }