From 4c73f6ea41a43cae7d579b21722b621e5ef5c54c Mon Sep 17 00:00:00 2001 From: gardners Date: Fri, 18 Jan 2013 15:15:15 +1030 Subject: [PATCH] fixed root cause of return value problems, and generally reinstated rhizome_retrieve_manifest() functionality from master that got clobbered in the merge. Simple wrappers now exist for the rhizome_retrieve_manifest_by_*() functions. --- commandline.c | 9 +-- rhizome.h | 5 +- rhizome_bundle.c | 4 +- rhizome_database.c | 158 +++++++++++++++------------------------------ tests/rhizomeops | 2 + 5 files changed, 65 insertions(+), 113 deletions(-) diff --git a/commandline.c b/commandline.c index 790f43bf..e8e6f1ef 100644 --- a/commandline.c +++ b/commandline.c @@ -1288,11 +1288,12 @@ int app_rhizome_extract_bundle(int argc, const char *const *argv, const struct c if (bskhex && fromhexstr(bsk.binary, bskhex, RHIZOME_BUNDLE_KEY_BYTES) == -1) return WHYF("invalid bsk: \"%s\"", bskhex); - rhizome_manifest *m = NULL; - ret = rhizome_retrieve_manifest_bybidhex(manifestIdUpper, &m); + rhizome_manifest *m = rhizome_new_manifest(); + ret = rhizome_retrieve_manifest_by_bidhex(manifestIdUpper, m); + DEBUGF("ret=%d",ret); - if (ret==1){ - // ignore errors when extracting private key + if (ret==0){ + // ignore errors when extracting private key rhizome_extract_privatekey(m, NULL); const char *blob_service = rhizome_manifest_get(m, "service", NULL, 0); diff --git a/rhizome.h b/rhizome.h index 2752dabe..b071b46b 100644 --- a/rhizome.h +++ b/rhizome.h @@ -316,9 +316,8 @@ int rhizome_find_duplicate(const rhizome_manifest *m, rhizome_manifest **found, int rhizome_manifest_to_bar(rhizome_manifest *m,unsigned char *bar); long long rhizome_bar_version(unsigned char *bar); unsigned long long rhizome_bar_bidprefix_ll(unsigned char *bar); -int rhizome_retrieve_manifest_bybidhex(char *bidhex,rhizome_manifest **tmp); -int rhizome_retrieve_manifest_by_bar(unsigned char *bar, - rhizome_manifest **tmp); +int rhizome_retrieve_manifest_by_bidhex(char *bidhex,rhizome_manifest *m); +int rhizome_retrieve_manifest_by_bar(unsigned char *bar,rhizome_manifest *m); int rhizome_retrieve_file(const char *fileid, const char *filepath, const unsigned char *key); int rhizome_list_manifests(const char *service, const char *name, diff --git a/rhizome_bundle.c b/rhizome_bundle.c index 7351895b..426975c5 100644 --- a/rhizome_bundle.c +++ b/rhizome_bundle.c @@ -109,6 +109,8 @@ int rhizome_read_manifest_file(rhizome_manifest *m, const char *filename, int bu m->manifest_all_bytes=m->manifest_bytes; + dump("read manifest",m->manifestdata,m->manifest_all_bytes); + /* Parse out variables, signature etc */ int have_service = 0; int have_id = 0; @@ -751,4 +753,4 @@ int rhizome_fill_manifest(rhizome_manifest *m, const char *filepath, const sid_t } return 0; -} \ No newline at end of file +} diff --git a/rhizome_database.c b/rhizome_database.c index fe4506c9..ccb94516 100644 --- a/rhizome_database.c +++ b/rhizome_database.c @@ -834,6 +834,7 @@ int64_t rhizome_store_bundle(rhizome_manifest *m) filehash[0] = '\0'; } + dump("m->author",m->author,SID_SIZE); const char *author = is_sid_any(m->author) ? NULL : alloca_tohex_sid(m->author); const char *name = rhizome_manifest_get(m, "name", NULL, 0); const char *sender = rhizome_manifest_get(m, "sender", NULL, 0); @@ -1339,115 +1340,62 @@ int rhizome_find_duplicate(const rhizome_manifest *m, rhizome_manifest **found, /* Retrieve a manifest from the database, given its manifest ID. * - * Returns 1 if manifest is found (if mp != NULL then a new manifest struct is allocated, made - * finalisable and * assigned to *mp, caller is responsible for freeing). - * Returns 0 if manifest is not found (*mp is unchanged). - * Returns -1 on error (*mp is unchanged). + * Returns 0 if manifest is found + * Returns 1 if manifest is not found + * Returns -1 on error + * Caller is responsible for allocating and freeing rhizome_manifest */ -int rhizome_retrieve_manifest_by_bar(unsigned char *bar, - rhizome_manifest **tmp) +int rhizome_retrieve_manifest(const char *manifestpattern, rhizome_manifest *m){ + int ret=0; + + sqlite_retry_state retry = SQLITE_RETRY_STATE_DEFAULT; + + sqlite3_stmt *statement = sqlite_prepare(&retry, "SELECT manifest, version, inserttime, author FROM manifests WHERE id LIKE ?"); + if (!statement) + return -1; + + sqlite3_bind_text(statement, 1, manifestpattern, -1, SQLITE_STATIC); + if (sqlite_step_retry(&retry, statement) == SQLITE_ROW){ + const char *manifestblob = (char *) sqlite3_column_blob(statement, 0); + long long q_version = (long long) sqlite3_column_int64(statement, 1); + long long q_inserttime = (long long) sqlite3_column_int64(statement, 2); + const char *q_author = (const char *) sqlite3_column_text(statement, 3); + size_t manifestblobsize = sqlite3_column_bytes(statement, 0); // must call after sqlite3_column_blob() + + if (rhizome_read_manifest_file(m, manifestblob, manifestblobsize)){ + ret=WHYF("Manifest matching %s exists but is invalid", manifestpattern); + goto done; + } + + if (q_author){ + if (stowSid(m->author, 0, q_author) == -1) + WARNF("Manifest %s contains invalid author=%s -- ignored", manifestpattern, alloca_str_toprint(q_author)); + } + + if (m->version!=q_version) + WARNF("Version mismatch, manifest is %lld, database is %lld", m->version, q_version); + + m->inserttime = q_inserttime; + }else{ + INFOF("Manifest %s was not found", manifestpattern); + ret=1; + } + +done: + sqlite3_finalize(statement); + return ret; +} +int rhizome_retrieve_manifest_by_bar(unsigned char *bar,rhizome_manifest *m) { /* Obtain Manifest by prefix */ - char bid_low[RHIZOME_MANIFEST_ID_STRLEN+1]; - char bid_high[RHIZOME_MANIFEST_ID_STRLEN+1]; - int i; - tohex(bid_low,&bar[RHIZOME_BAR_PREFIX_OFFSET],RHIZOME_BAR_PREFIX_BYTES); - tohex(bid_high,&bar[RHIZOME_BAR_PREFIX_OFFSET],RHIZOME_BAR_PREFIX_BYTES); - for(i=RHIZOME_BAR_PREFIX_BYTES*2;i= 0 && sqlite3_blob_open(rhizome_db, "main", "manifests", "manifest", rowid, 0, &blob) != SQLITE_OK) - rowid = -1; - if (rowid == -1) { - DEBUGF("Row not found"); - return -1; - } - - // Extract manifest from blob - char buffer[1024]; - int bytes=sqlite3_blob_bytes(blob); - if (bytes>1024||bytes<1) { - sqlite3_blob_close(blob); - return -1; - } - - if (sqlite3_blob_read(blob,&buffer[0],bytes,0)!=SQLITE_OK) - { - sqlite3_blob_close(blob); - return -1; - } - - *tmp=rhizome_new_manifest(); - if (!*tmp) { - sqlite3_blob_close(blob); - return -1; - } - if (rhizome_read_manifest_file(*tmp,buffer,bytes)) - { - rhizome_manifest_free(*tmp); - *tmp=NULL; - sqlite3_blob_close(blob); - return -1; - } - - sqlite3_blob_close(blob); - return 1; + char bid_pattern[RHIZOME_MANIFEST_ID_STRLEN+1+1]; + tohex(bid_pattern,&bar[RHIZOME_BAR_PREFIX_OFFSET],RHIZOME_BAR_PREFIX_BYTES); + bid_pattern[RHIZOME_BAR_PREFIX_BYTES*2]='%'; + bid_pattern[RHIZOME_BAR_PREFIX_BYTES*2+1]=0; + return rhizome_retrieve_manifest(bid_pattern, m); } -/* Retrieve a manifest from the database, given its manifest ID. - * - * Returns 1 if manifest is found (if mp != NULL then a new manifest struct is allocated, made - * finalisable and * assigned to *mp, caller is responsible for freeing). - * Returns 0 if manifest is not found (*mp is unchanged). - * Returns -1 on error (*mp is unchanged). - */ -int rhizome_retrieve_manifest_bybidhex(char *bidhex,rhizome_manifest **tmp) +int rhizome_retrieve_manifest_by_bidhex(char *bidhex,rhizome_manifest *m) { - long long rowid = -1; - sqlite3_blob *blob=NULL; - sqlite_exec_int64(&rowid, "select rowid from manifests where id = '%s';", bidhex); - if (rowid >= 0 && sqlite3_blob_open(rhizome_db, "main", "manifests", "manifest", rowid, 0, &blob) != SQLITE_OK) - rowid = -1; - if (rowid == -1) { - DEBUGF("Row not found"); - return -1; - } - - // Extract manifest from blob - char buffer[1024]; - int bytes=sqlite3_blob_bytes(blob); - if (bytes>1024||bytes<1) { - sqlite3_blob_close(blob); - return -1; - } - - if (sqlite3_blob_read(blob,&buffer[0],bytes,0)!=SQLITE_OK) - { - sqlite3_blob_close(blob); - return -1; - } - - *tmp=rhizome_new_manifest(); - if (!*tmp) { - sqlite3_blob_close(blob); - return -1; - } - if (rhizome_read_manifest_file(*tmp,buffer,bytes)) - { - rhizome_manifest_free(*tmp); - *tmp=NULL; - sqlite3_blob_close(blob); - return -1; - } - - sqlite3_blob_close(blob); - return 1; + return rhizome_retrieve_manifest(bidhex, m); } diff --git a/tests/rhizomeops b/tests/rhizomeops index 2ff9f019..2edd1455 100755 --- a/tests/rhizomeops +++ b/tests/rhizomeops @@ -800,6 +800,7 @@ setup_ImportOwnBundle() { setup_rhizome echo "Hello from B" >fileB executeOk_servald rhizome add file $SIDB2 '' fileB fileB.manifest + assertGrep fileB.manifest "^BK=" assert_stdout_add_file fileB extract_manifest_id manifestid fileB.manifest extract_manifest_version version fileB.manifest @@ -810,6 +811,7 @@ setup_ImportOwnBundle() { } test_ImportOwnBundle() { executeOk_servald rhizome import bundle fileB fileB.manifest + tfw_cat --stderr assert_stdout_import_bundle fileB # Bundle author and sender are unknown, so appears not to be from here executeOk_servald rhizome list