From 7b427d48ee14b338a4d50bc08fee682c5177088a Mon Sep 17 00:00:00 2001 From: Andrew Bettison Date: Fri, 4 Oct 2013 04:50:37 +0930 Subject: [PATCH] A few more uses of rhizome_bid_t Now the TEXT_TOUPPER SQL bind action is only used for filehash hex strings; all Bundle ID parameters are bound using RHIZOME_BID_T. --- rhizome.h | 4 ++-- rhizome_crypto.c | 10 ++++++---- rhizome_database.c | 40 +++++++++++++++++++++++----------------- rhizome_direct_http.c | 11 ++++------- rhizome_store.c | 4 +--- 5 files changed, 36 insertions(+), 33 deletions(-) diff --git a/rhizome.h b/rhizome.h index b97e82a8..ac240cae 100644 --- a/rhizome.h +++ b/rhizome.h @@ -259,7 +259,7 @@ sqlite_retry_state sqlite_retry_state_init(int serverLimit, int serverSleep, int int rhizome_write_manifest_file(rhizome_manifest *m, const char *filename, char append); int rhizome_manifest_selfsign(rhizome_manifest *m); int rhizome_drop_stored_file(const char *id,int maximum_priority); -int rhizome_manifest_priority(sqlite_retry_state *retry, const char *id); +int rhizome_manifest_priority(sqlite_retry_state *retry, const rhizome_bid_t *bidp); int rhizome_read_manifest_file(rhizome_manifest *m, const char *filename, int bufferPAndSize); int rhizome_hash_file(rhizome_manifest *m, const char *filename,char *hash_out); char *rhizome_manifest_get(const rhizome_manifest *m, const char *var, char *out, int maxlen); @@ -773,7 +773,7 @@ int rhizome_read_cached(const rhizome_bid_t *bid, uint64_t version, time_ms_t ti uint64_t fileOffset, unsigned char *buffer, int length); int rhizome_cache_close(); -int rhizome_database_filehash_from_id(const char *id, uint64_t version, char hash[SHA512_DIGEST_STRING_LENGTH]); +int rhizome_database_filehash_from_id(const rhizome_bid_t *bidp, uint64_t version, char hash[SHA512_DIGEST_STRING_LENGTH]); int overlay_mdp_service_rhizome_sync(struct overlay_frame *frame, overlay_mdp_frame *mdp); diff --git a/rhizome_crypto.c b/rhizome_crypto.c index 6e528649..74577a5f 100644 --- a/rhizome_crypto.c +++ b/rhizome_crypto.c @@ -368,10 +368,12 @@ int rhizome_find_bundle_author(rhizome_manifest *m) DEBUGF("found bundle author sid=%s", alloca_tohex_sid(m->author)); // if this bundle is already in the database, update the author. - if (m->inserttime) { - const char *id = rhizome_manifest_get(m, "id", NULL, 0); - sqlite_exec_void_loglevel(LOG_LEVEL_WARN, "UPDATE MANIFESTS SET author = ? WHERE id = ?;", SID_T, (sid_t*)m->author, TEXT_TOUPPER, id, END); - } + if (m->inserttime) + sqlite_exec_void_loglevel(LOG_LEVEL_WARN, + "UPDATE MANIFESTS SET author = ? WHERE id = ?;", + SID_T, (sid_t*)m->author, + RHIZOME_BID_T, &m->cryptoSignPublic, + END); } RETURN(0); // bingo diff --git a/rhizome_database.c b/rhizome_database.c index c8421abe..2848e7f4 100644 --- a/rhizome_database.c +++ b/rhizome_database.c @@ -78,7 +78,7 @@ int create_rhizome_datastore_dir() sqlite3 *rhizome_db=NULL; /* XXX Requires a messy join that might be slow. */ -int rhizome_manifest_priority(sqlite_retry_state *retry, const char *id) +int rhizome_manifest_priority(sqlite_retry_state *retry, const rhizome_bid_t *bidp) { int64_t result = 0; if (sqlite_exec_int64_retry(retry, &result, @@ -86,7 +86,8 @@ int rhizome_manifest_priority(sqlite_retry_state *retry, const char *id) " WHERE MANIFESTS.id = ?" " AND GROUPLIST.id = GROUPMEMBERSHIPS.groupid" " AND GROUPMEMBERSHIPS.manifestid = MANIFESTS.id;", - TEXT_TOUPPER, id, END + RHIZOME_BID_T, bidp, + END ) == -1 ) return -1; @@ -933,12 +934,12 @@ int64_t rhizome_database_used_bytes() return db_page_size * (db_page_count - db_free_page_count); } -int rhizome_database_filehash_from_id(const char *id, uint64_t version, char hash[SHA512_DIGEST_STRING_LENGTH]) +int rhizome_database_filehash_from_id(const rhizome_bid_t *bidp, uint64_t version, char hash[SHA512_DIGEST_STRING_LENGTH]) { IN(); strbuf hash_sb = strbuf_local(hash, SHA512_DIGEST_STRING_LENGTH); - RETURN(sqlite_exec_strbuf(hash_sb, "SELECT filehash FROM MANIFESTS WHERE manifests.version = ? AND manifests.id = ?;", - INT64, version, TEXT_TOUPPER, id, END)); + RETURN(sqlite_exec_strbuf(hash_sb, "SELECT filehash FROM MANIFESTS WHERE version = ? AND id = ?;", + INT64, version, RHIZOME_BID_T, bidp, END)); OUT(); } @@ -1091,10 +1092,10 @@ int rhizome_make_space(int group_priority, long long bytes) return WHY("Incomplete"); } -/* Drop the specified file from storage, and any manifests that reference it, - provided that none of those manifests are being retained at a higher priority - than the maximum specified here. */ -int rhizome_drop_stored_file(const char *id,int maximum_priority) +/* Drop the specified file from storage, and any manifests that reference it, provided that none of + * those manifests are being retained at a higher priority than the maximum specified here. + */ +int rhizome_drop_stored_file(const char *id, int maximum_priority) { if (!rhizome_str_is_file_hash(id)) return WHYF("invalid file hash id=%s", alloca_str_toprint(id)); @@ -1109,23 +1110,28 @@ int rhizome_drop_stored_file(const char *id,int maximum_priority) WHYF("Incorrect type in id column of manifests table"); break; } - const char *manifestId = (char *) sqlite3_column_text(statement, 0); + const char *q_id = (char *) sqlite3_column_text(statement, 0); + rhizome_bid_t bid; + if (str_to_rhizome_bid_t(&bid, q_id) == -1) { + WARNF("malformed column value MANIFESTS.id = %s -- skipped", alloca_str_toprint(q_id)); + continue; + } /* Check that manifest is not part of a higher priority group. If so, we cannot drop the manifest or the file. However, we will keep iterating, as we can still drop any other manifests pointing to this file that are lower priority, and thus free up a little space. */ - int priority = rhizome_manifest_priority(&retry, manifestId); + int priority = rhizome_manifest_priority(&retry, &bid); if (priority == -1) - WHYF("Cannot drop fileid=%s due to error, manifestId=%s", id, manifestId); + WHYF("Cannot drop fileid=%s due to error, bid=%s", id, alloca_tohex_rhizome_bid_t(bid)); else if (priority > maximum_priority) { - WHYF("Cannot drop fileid=%s due to manifest priority, manifestId=%s", id, manifestId); + WHYF("Cannot drop fileid=%s due to manifest priority, bid=%s", id, alloca_tohex_rhizome_bid_t(bid)); can_drop = 0; } else { if (config.debug.rhizome) DEBUGF("removing stale manifests, groupmemberships"); - sqlite_exec_void_retry(&retry, "delete from manifests where id = ?;", TEXT_TOUPPER, manifestId, END); - sqlite_exec_void_retry(&retry, "delete from keypairs where public = ?;", TEXT_TOUPPER, manifestId, END); - sqlite_exec_void_retry(&retry, "delete from groupmemberships where manifestid = ?;", TEXT_TOUPPER, manifestId, END); + sqlite_exec_void_retry(&retry, "DELETE FROM MANIFESTS WHERE id = ?;", RHIZOME_BID_T, &bid, END); + sqlite_exec_void_retry(&retry, "DELETE FROM KEYPAIRS WHERE public = ?;", RHIZOME_BID_T, &bid, END); + sqlite_exec_void_retry(&retry, "DELETE FROM GROUPMEMBERSHIPS WHERE manifestid = ?;", RHIZOME_BID_T, &bid, END); } } sqlite3_finalize(statement); @@ -1795,7 +1801,7 @@ static int is_interesting(const char *id_hex, int64_t version) // do we have this bundle [or later]? sqlite_retry_state retry = SQLITE_RETRY_STATE_DEFAULT; - sqlite3_stmt *statement = sqlite_prepare_bind(&retry, + sqlite3_stmt *statement = sqlite_prepare_bind(&retry, "SELECT filehash FROM manifests WHERE id like ? and version >= ?", TEXT_TOUPPER, id_hex, INT64, version, diff --git a/rhizome_direct_http.c b/rhizome_direct_http.c index 8abf1671..99de479f 100644 --- a/rhizome_direct_http.c +++ b/rhizome_direct_http.c @@ -884,17 +884,14 @@ void rhizome_direct_http_dispatch(rhizome_direct_sync_request *r) /* Start by getting the manifest, which is the main thing we need, and also gives us the information we need for sending any associated file. */ - rhizome_manifest - *m=rhizome_direct_get_manifest(&actionlist[i+1], - RHIZOME_BAR_PREFIX_BYTES); - if (!m) { + rhizome_manifest *m = rhizome_direct_get_manifest(&actionlist[i+1], RHIZOME_BAR_PREFIX_BYTES); + if (m == NULL) { WHY("This should never happen. The manifest exists, but when I went looking for it, it doesn't appear to be there."); goto next_item; } /* Get filehash and size from manifest if present */ - const char *id = rhizome_manifest_get(m, "id", NULL, 0); - DEBUGF("bundle id = '%s'",id); + DEBUGF("bundle id = %s", alloca_tohex_rhizome_bid_t(m->cryptoSignPublic)); const char *hash = rhizome_manifest_get(m, "filehash", NULL, 0); DEBUGF("bundle file hash = '%s'",hash); long long filesize = rhizome_manifest_get_ll(m, "filesize"); @@ -965,7 +962,7 @@ void rhizome_direct_http_dispatch(rhizome_direct_sync_request *r) /* send file contents */ { char filehash[SHA512_DIGEST_STRING_LENGTH]; - if (rhizome_database_filehash_from_id(id, version, filehash)<=0) + if (rhizome_database_filehash_from_id(&m->cryptoSignPublic, version, filehash) == -1) goto closeit; struct rhizome_read read; diff --git a/rhizome_store.c b/rhizome_store.c index 1b5688bf..7d7c0807 100644 --- a/rhizome_store.c +++ b/rhizome_store.c @@ -989,10 +989,8 @@ int rhizome_read_cached(const rhizome_bid_t *bidp, uint64_t version, time_ms_t t // if we don't have one yet, create one and open it if (!entry){ - char *id_str = alloca_tohex_rhizome_bid_t(*bidp); - char filehash[SHA512_DIGEST_STRING_LENGTH]; - if (rhizome_database_filehash_from_id(id_str, version, filehash)<=0) + if (rhizome_database_filehash_from_id(bidp, version, filehash) == -1) return -1; entry = emalloc_zero(sizeof(struct cache_entry));