void blobcache_init(void) { sqlite3 *db; extern char *showtime_cache_path; char buf[256]; blobcache_prune_old(); snprintf(buf, sizeof(buf), "%s/cachedb", showtime_cache_path); mkdir(buf, 0770); snprintf(buf, sizeof(buf), "%s/cachedb/cache.db", showtime_cache_path); // unlink(buf); cachedb_pool = db_pool_create(buf, 4); db = db_pool_get(cachedb_pool); if(db == NULL) return; int r = db_upgrade_schema(db, "bundle://resources/cachedb", "cachedb"); db_pool_put(cachedb_pool, db); if(r) cachedb_pool = NULL; else callout_arm(&blobcache_callout, blobcache_do_prune, NULL, 1); }
static void blobcache_do_prune(struct callout *c, void *opaque) { sqlite3 *db = db_pool_get(cachedb_pool); if(db != NULL) blobcache_prune(db); db_pool_put(cachedb_pool, db); }
int my_con(db_con_t* con) { struct my_con* ptr; struct my_uri* uri; /* First try to lookup the connection in the connection pool and * re-use it if a match is found */ ptr = (struct my_con*)db_pool_get(con->uri); if (ptr) { DBG("mysql: Connection to %.*s:%.*s found in connection pool\n", con->uri->scheme.len, ZSW(con->uri->scheme.s), con->uri->body.len, ZSW(con->uri->body.s)); goto found; } ptr = (struct my_con*)pkg_malloc(sizeof(struct my_con)); if (!ptr) { LOG(L_ERR, "mysql: No memory left\n"); goto error; } memset(ptr, '\0', sizeof(struct my_con)); if (db_pool_entry_init(&ptr->gen, my_con_free, con->uri) < 0) goto error; ptr->con = (MYSQL*)pkg_malloc(sizeof(MYSQL)); if (!ptr->con) { LOG(L_ERR, "mysql: No enough memory\n"); goto error; } mysql_init(ptr->con); uri = DB_GET_PAYLOAD(con->uri); DBG("mysql: Creating new connection to: %.*s:%.*s\n", con->uri->scheme.len, ZSW(con->uri->scheme.s), con->uri->body.len, ZSW(con->uri->body.s)); /* Put the newly created mysql connection into the pool */ db_pool_put((struct db_pool_entry*)ptr); DBG("mysql: Connection stored in connection pool\n"); found: /* Attach driver payload to the db_con structure and set connect and * disconnect functions */ DB_SET_PAYLOAD(con, ptr); con->connect = my_con_connect; con->disconnect = my_con_disconnect; return 0; error: if (ptr) { db_pool_entry_free(&ptr->gen); if (ptr->con) pkg_free(ptr->con); pkg_free(ptr); } return 0; }
int bdb_con(db_con_t* con) { bdb_con_t* bcon; bdb_uri_t* buri; buri = DB_GET_PAYLOAD(con->uri); /* First try to lookup the connection in the connection pool and * re-use it if a match is found */ bcon = (bdb_con_t*)db_pool_get(con->uri); if (bcon) { DBG("bdb: Connection to %s found in connection pool\n", buri->uri); goto found; } bcon = (bdb_con_t*)pkg_malloc(sizeof(bdb_con_t)); if (!bcon) { ERR("bdb: No memory left\n"); goto error; } memset(bcon, '\0', sizeof(bdb_con_t)); if (db_pool_entry_init(&bcon->gen, bdb_con_free, con->uri) < 0) goto error; DBG("bdb: Preparing new connection to %s\n", buri->uri); if(bdb_is_database(buri->path.s)!=0) { ERR("bdb: database [%.*s] does not exists!\n", buri->path.len, buri->path.s); goto error; } /* Put the newly created BDB connection into the pool */ db_pool_put((struct db_pool_entry*)bcon); DBG("bdb: Connection stored in connection pool\n"); found: /* Attach driver payload to the db_con structure and set connect and * disconnect functions */ DB_SET_PAYLOAD(con, bcon); con->connect = bdb_con_connect; con->disconnect = bdb_con_disconnect; return 0; error: if (bcon) { db_pool_entry_free(&bcon->gen); pkg_free(bcon); } return -1; }
void blobcache_put(const char *key, const char *stash, const void *data, size_t size, int maxage, const char *etag, time_t mtime) { sqlite3 *db = db_pool_get(cachedb_pool); if(db == NULL) return; blobcache_put0(db, key, stash, data, size, maxage, etag, mtime); db_pool_put(cachedb_pool, db); }
int pg_con(db_con_t *con) { struct pg_con *pcon; /* First try to lookup the connection in the connection pool and * re-use it if a match is found */ pcon = (struct pg_con *)db_pool_get(con->uri); if(pcon) { DBG("postgres: Connection to %.*s:%.*s found in connection pool\n", con->uri->scheme.len, ZSW(con->uri->scheme.s), con->uri->body.len, ZSW(con->uri->body.s)); goto found; } pcon = (struct pg_con *)pkg_malloc(sizeof(struct pg_con)); if(!pcon) { LOG(L_ERR, "postgres: No memory left\n"); goto error; } memset(pcon, '\0', sizeof(struct pg_con)); if(db_pool_entry_init(&pcon->gen, pg_con_free, con->uri) < 0) goto error; DBG("postgres: Preparing new connection to: %.*s:%.*s\n", con->uri->scheme.len, ZSW(con->uri->scheme.s), con->uri->body.len, ZSW(con->uri->body.s)); /* Put the newly created postgres connection into the pool */ db_pool_put((struct db_pool_entry *)pcon); DBG("postgres: Connection stored in connection pool\n"); found: /* Attach driver payload to the db_con structure and set connect and * disconnect functions */ DB_SET_PAYLOAD(con, pcon); con->connect = pg_con_connect; con->disconnect = pg_con_disconnect; return 0; error: if(pcon) { db_pool_entry_free(&pcon->gen); pkg_free(pcon); } return -1; }
int ld_con(db_con_t* con) { struct ld_con* lcon; struct ld_uri* luri; luri = DB_GET_PAYLOAD(con->uri); /* First try to lookup the connection in the connection pool and * re-use it if a match is found */ lcon = (struct ld_con*)db_pool_get(con->uri); if (lcon) { DBG("ldap: Connection to %s found in connection pool\n", luri->uri); goto found; } lcon = (struct ld_con*)pkg_malloc(sizeof(struct ld_con)); if (!lcon) { ERR("ldap: No memory left\n"); goto error; } memset(lcon, '\0', sizeof(struct ld_con)); if (db_pool_entry_init(&lcon->gen, ld_con_free, con->uri) < 0) goto error; DBG("ldap: Preparing new connection to %s\n", luri->uri); /* Put the newly created LDAP connection into the pool */ db_pool_put((struct db_pool_entry*)lcon); DBG("ldap: Connection stored in connection pool\n"); found: /* Attach driver payload to the db_con structure and set connect and * disconnect functions */ DB_SET_PAYLOAD(con, lcon); con->connect = ld_con_connect; con->disconnect = ld_con_disconnect; return 0; error: if (lcon) { db_pool_entry_free(&lcon->gen); pkg_free(lcon); } return -1; }
int blobcache_get_meta(const char *key, const char *stash, char **etagp, time_t *mtimep) { sqlite3 *db = db_pool_get(cachedb_pool); *etagp = NULL; *mtimep = 0; if(db == NULL) return -1; int r = blobcache_get_meta0(db, key, stash, etagp, mtimep); db_pool_put(cachedb_pool, db); return r; }
void * blobcache_get(const char *key, const char *stash, size_t *sizep, int pad, int *ignore_expiry, char **etagp, time_t *mtimep) { sqlite3 *db = db_pool_get(cachedb_pool); if(etagp != NULL) *etagp = NULL; if(mtimep != NULL) *mtimep = 0; if(db == NULL) return NULL; void *r = blobcache_get0(db, key, stash, sizep, pad, ignore_expiry, etagp, mtimep); db_pool_put(cachedb_pool, db); return r; }
/* Queue: pairing */ static int pairing_request_cb(struct http_connection *c, struct http_request *req, struct http_response *r, void *arg) { struct remote_info *ri; struct evbuffer *body; char guid[17]; const char *reason; uint8_t *response; size_t bodylen; int code; int len; int i; int ret; ri = (struct remote_info *)arg; code = http_response_get_status(r, &reason); if (code != HTTP_OK) { DPRINTF(E_LOG, L_REMOTE, "Pairing failed with Remote %s/%s, HTTP response code %d (%s)\n", ri->pi.remote_id, ri->pi.name, code, reason); goto cleanup; } body = http_response_get_body(r); if (!body || (EVBUFFER_LENGTH(body) < 8)) { DPRINTF(E_LOG, L_REMOTE, "Remote %s/%s: pairing response too short\n", ri->pi.remote_id, ri->pi.name); goto cleanup; } bodylen = EVBUFFER_LENGTH(body); response = EVBUFFER_DATA(body); if ((response[0] != 'c') || (response[1] != 'm') || (response[2] != 'p') || (response[3] != 'a')) { DPRINTF(E_LOG, L_REMOTE, "Remote %s/%s: unknown pairing response, expected cmpa\n", ri->pi.remote_id, ri->pi.name); goto cleanup; } len = (response[4] << 24) | (response[5] << 16) | (response[6] << 8) | (response[7]); if (bodylen < 8 + len) { DPRINTF(E_LOG, L_REMOTE, "Remote %s/%s: pairing response truncated (got %d expected %d)\n", ri->pi.remote_id, ri->pi.name, (int)bodylen, len + 8); goto cleanup; } response += 8; for (; len > 0; len--, response++) { if ((response[0] != 'c') || (response[1] != 'm') || (response[2] != 'p') || (response[3] != 'g')) continue; else { len -= 8; response += 8; break; } } if (len < 8) { DPRINTF(E_LOG, L_REMOTE, "Remote %s/%s: cmpg truncated in pairing response\n", ri->pi.remote_id, ri->pi.name); goto cleanup; } for (i = 0; i < 8; i++) sprintf(guid + (2 * i), "%02X", response[i]); ri->pi.guid = strdup(guid); DPRINTF(E_INFO, L_REMOTE, "Pairing succeeded with Remote '%s' (id %s), GUID: %s\n", ri->pi.name, ri->pi.remote_id, guid); ret = db_pool_get(); if (ret < 0) { DPRINTF(E_LOG, L_REMOTE, "Could not acquire database connection; cannot register pairing with %s\n", ri->pi.name); goto cleanup; } ret = db_pairing_add(&ri->pi); db_pool_release(); if (ret < 0) { DPRINTF(E_LOG, L_REMOTE, "Failed to register pairing!\n"); goto cleanup; } cleanup: http_request_free(req); http_client_free(c); return 0; }