/* Initializes a kvserver. Will return 0 if successful, or a negative error * code if not. DIRNAME is the directory which should be used to store entries * for this server. The server's cache will have NUM_SETS cache sets, each * with ELEM_PER_SET elements. HOSTNAME and PORT indicate where SERVER will be * made available for requests. USE_TPC indicates whether this server should * use TPC logic (for PUTs and DELs) or not. */ int kvserver_init(kvserver_t *server, char *dirname, unsigned int num_sets, unsigned int elem_per_set, unsigned int max_threads, const char *hostname, int port, bool use_tpc) { int ret; ret = kvcache_init(&server->cache, num_sets, elem_per_set); if (ret < 0) return ret; ret = kvstore_init(&server->store, dirname); if (ret < 0) return ret; if (use_tpc) { ret = tpclog_init(&server->log, dirname); if (ret < 0) return ret; } server->hostname = malloc(strlen(hostname) + 1); if (server->hostname == NULL) return ENOMEM; strcpy(server->hostname, hostname); server->port = port; server->use_tpc = use_tpc; server->max_threads = max_threads; server->handle = kvserver_handle; // OUR CODE HERE server->msg = NULL; server->state = TPC_READY; return 0; }
int kvstore_get_put_with_hash_conflicts(void) { /* hash("abD") == hash("aae") == hash("ac#") */ char *retval, *key1 = "abD", *key2 = "aae", *key3 = "ac#"; int ret; ret = kvstore_put(&teststore, key1, "value1"); ret += kvstore_put(&teststore, key2, "value2"); ret += kvstore_put(&teststore, key3, "value3"); ret += kvstore_get(&teststore, key1, &retval); ASSERT_STRING_EQUAL(retval, "value1"); free(retval); ret += kvstore_get(&teststore, key2, &retval); ASSERT_STRING_EQUAL(retval, "value2"); free(retval); ret += kvstore_get(&teststore, key3, &retval); ASSERT_STRING_EQUAL(retval, "value3"); free(retval); /* Clean the store and do it again in a different order to ensure that the above success wasn't just because of a lucky ordering. */ kvstore_clean(&teststore); kvstore_init(&teststore, KVSTORE_DIRNAME); ret = kvstore_put(&teststore, key2, "value2"); ret += kvstore_put(&teststore, key3, "value3"); ret += kvstore_put(&teststore, key1, "value1"); ret += kvstore_get(&teststore, key1, &retval); ASSERT_STRING_EQUAL(retval, "value1"); free(retval); ret += kvstore_get(&teststore, key2, &retval); ASSERT_STRING_EQUAL(retval, "value2"); free(retval); ret += kvstore_get(&teststore, key3, &retval); ASSERT_STRING_EQUAL(retval, "value3"); free(retval); ASSERT_EQUAL(ret, 0); return 1; }
/* Initializes a tpcfollower. Will return 0 if successful, or a negative error * code if not. DIRNAME is the directory which should be used to store entries * for this server. HOSTNAME and PORT indicate where SERVER will be * made available for requests. */ int tpcfollower_init(tpcfollower_t *server, char *dirname, unsigned int max_threads, const char *hostname, int port) { int ret; ret = kvstore_init(&server->store, dirname); if (ret < 0) return ret; ret = tpclog_init(&server->log, dirname); if (ret < 0) return ret; strcpy(server->hostname, hostname); server->port = port; server->max_threads = max_threads; server->state = TPC_INIT; /* Rebuild TPC state. */ tpcfollower_rebuild_state(server); return 0; }
int kvstore_del_hash_conflicts(void) { /* hash("abD") == hash("aae") == hash("ac#") */ char *retval = NULL, *key1 = "abD", *key2 = "aae", *key3 = "ac#"; int ret; ret = kvstore_put(&teststore, key1, "value1"); ret += kvstore_put(&teststore, key2, "value2"); ret += kvstore_put(&teststore, key3, "value3"); ret += kvstore_del(&teststore, key2); ASSERT_EQUAL(ret, 0); ret = kvstore_get(&teststore, key2, &retval); ASSERT_PTR_NULL(retval); ASSERT_EQUAL(ret, ERRNOKEY); ret = kvstore_get(&teststore, key1, &retval); ASSERT_STRING_EQUAL(retval, "value1"); free(retval); ret += kvstore_get(&teststore, key3, &retval); ASSERT_STRING_EQUAL(retval, "value3"); free(retval); ASSERT_EQUAL(ret, 0); /* Clean store and do operations again with a different insertion order to * help ensure that success wasn't due to a lucky ordering. */ kvstore_clean(&teststore); kvstore_init(&teststore, KVSTORE_DIRNAME); ret = kvstore_put(&teststore, key2, "value2"); ret += kvstore_put(&teststore, key1, "value1"); ret += kvstore_put(&teststore, key3, "value3"); ret += kvstore_del(&teststore, key2); ASSERT_EQUAL(ret, 0); retval = NULL; ret = kvstore_get(&teststore, key2, &retval); ASSERT_PTR_NULL(retval); ASSERT_EQUAL(ret, ERRNOKEY); ret = kvstore_get(&teststore, key1, &retval); ASSERT_STRING_EQUAL(retval, "value1"); free(retval); ret += kvstore_get(&teststore, key3, &retval); ASSERT_STRING_EQUAL(retval, "value3"); free(retval); ASSERT_EQUAL(ret, 0); return 1; }
void showtime_init(void) { int r; hts_mutex_init(&gconf.state_mutex); hts_cond_init(&gconf.state_cond, &gconf.state_mutex); gconf.exit_code = 1; unicode_init(); /* Initialize property tree */ prop_init(); init_global_info(); /* Initiailize logging */ trace_init(); /* Callout framework */ callout_init(); /* Initialize htsmsg_store() */ htsmsg_store_init(); /* Notification framework */ notifications_init(); /* Initialize settings */ settings_init(); TRACE(TRACE_DEBUG, "core", "Loading resources from %s", showtime_dataroot()); /* Try to create cache path */ if(gconf.cache_path != NULL && (r = makedirs(gconf.cache_path)) != 0) { TRACE(TRACE_ERROR, "cache", "Unable to create cache path %s -- %s", gconf.cache_path, strerror(r)); gconf.cache_path = NULL; } /* Initialize sqlite3 */ db_init(); /* Initializte blob cache */ blobcache_init(); /* Try to create settings path */ if(gconf.persistent_path != NULL && (r = makedirs(gconf.persistent_path)) != 0) { TRACE(TRACE_ERROR, "settings", "Unable to create path for persistent storage %s -- %s", gconf.persistent_path, strerror(r)); gconf.persistent_path = NULL; } /* Metadata init */ metadata_init(); metadb_init(); kvstore_init(); /* Metadata decoration init */ decoration_init(); /* Initialize keyring */ keyring_init(); #if ENABLE_LIBAV /* Initialize libavcodec & libavformat */ av_lockmgr_register(fflockmgr); av_log_set_callback(fflog); av_register_all(); TRACE(TRACE_INFO, "libav", LIBAVFORMAT_IDENT", "LIBAVCODEC_IDENT", "LIBAVUTIL_IDENT); #endif /* Freetype */ #if ENABLE_LIBFREETYPE freetype_init(); rasterizer_ft_init(); #endif #if ENABLE_GLW glw_settings_init(); #endif fontstash_init(); /* Global keymapper */ keymapper_init(); /* Initialize media subsystem */ media_init(); /* Service handling */ service_init(); /* Initialize backend content handlers */ backend_init(); /* Initialize navigator */ nav_init(); /* Initialize audio subsystem */ audio_init(); /* Initialize plugin manager */ plugins_init(gconf.devplugin); /* Start software installer thread (plugins, upgrade, etc) */ hts_thread_create_detached("swinst", swthread, NULL, THREAD_PRIO_LOW); /* Internationalization */ i18n_init(); /* Video settings */ video_settings_init(); if(gconf.load_jsfile) js_load(gconf.load_jsfile); /* Various interprocess communication stuff (D-Bus on Linux, etc) */ init_group(INIT_GROUP_IPC); /* Service discovery. Must be after ipc_init() (d-bus and threads, etc) */ if(!gconf.disable_sd) sd_init(); /* Initialize various external APIs */ init_group(INIT_GROUP_API); /* HTTP server and UPNP */ #if ENABLE_HTTPSERVER http_server_init(); if(!gconf.disable_upnp) upnp_init(); #endif runcontrol_init(); }
void main_init(void) { char errbuf[512]; hts_mutex_init(&gconf.state_mutex); hts_cond_init(&gconf.state_cond, &gconf.state_mutex); gconf.exit_code = 1; asyncio_init_early(); init_group(INIT_GROUP_NET); unicode_init(); /* Initialize property tree */ prop_init(); init_global_info(); /* Initiailize logging */ trace_init(); /* Callout framework */ callout_init(); prop_init_late(); /* Initialize htsmsg_store() */ htsmsg_store_init(); /* Notification framework */ notifications_init(); /* Initialize settings */ settings_init(); TRACE(TRACE_DEBUG, "core", "Loading resources from %s", app_dataroot()); TRACE(TRACE_DEBUG, "core", "Cache path: %s", gconf.cache_path); /* Try to create cache path */ if(gconf.cache_path != NULL && fa_makedirs(gconf.cache_path, errbuf, sizeof(errbuf))) { TRACE(TRACE_ERROR, "core", "Unable to create cache path %s -- %s", gconf.cache_path, errbuf); gconf.cache_path = NULL; } /* Initialize sqlite3 */ #if ENABLE_SQLITE db_init(); #endif /* Initializte blob cache */ blobcache_init(); TRACE(TRACE_DEBUG, "core", "Persistent path: %s", gconf.persistent_path); /* Try to create settings path */ if(gconf.persistent_path != NULL && fa_makedirs(gconf.persistent_path, errbuf, sizeof(errbuf))) { TRACE(TRACE_ERROR, "core", "Unable to create path for persistent storage %s -- %s", gconf.persistent_path, errbuf); gconf.persistent_path = NULL; } /* Per-item key/value store */ kvstore_init(); /* Metadata init */ #if ENABLE_METADATA metadata_init(); metadb_init(); decoration_init(); #endif subtitles_init(); /* Initialize keyring */ keyring_init(); #if ENABLE_LIBAV /* Initialize libavcodec & libavformat */ av_lockmgr_register(fflockmgr); av_log_set_callback(fflog); av_register_all(); TRACE(TRACE_INFO, "libav", LIBAVFORMAT_IDENT", "LIBAVCODEC_IDENT", "LIBAVUTIL_IDENT" cpuflags:0x%x", av_get_cpu_flags()); #endif init_group(INIT_GROUP_GRAPHICS); #if ENABLE_GLW glw_settings_init(); #endif /* Global keymapper */ keymapper_init(); /* Initialize media subsystem */ media_init(); /* Service handling */ service_init(); /* Initialize backend content handlers */ backend_init(); /* Initialize navigator */ nav_init(); /* Initialize audio subsystem */ audio_init(); /* Initialize plugin manager */ plugins_init(gconf.devplugins); /* Start software installer thread (plugins, upgrade, etc) */ hts_thread_create_detached("swinst", swthread, NULL, THREAD_PRIO_BGTASK); /* Internationalization */ i18n_init(); /* Video settings */ video_settings_init(); /* Various interprocess communication stuff (D-Bus on Linux, etc) */ init_group(INIT_GROUP_IPC); /* Service discovery. Must be after ipc_init() (d-bus and threads, etc) */ if(!gconf.disable_sd) sd_init(); /* Initialize various external APIs */ init_group(INIT_GROUP_API); /* Asynchronous IO (Used by HTTP server, etc) */ asyncio_start(); runcontrol_init(); TRACE(TRACE_DEBUG, "SYSTEM", "Hashed device ID: %s", gconf.device_id); if(gconf.device_type[0]) TRACE(TRACE_DEBUG, "SYSTEM", "Device type: %s", gconf.device_type); }
int kvstore_test_init(void) { return kvstore_init(&teststore, KVSTORE_DIRNAME); }