Issue #9, rewrite rhizome_import_bundle()

Objective is to avoid having to call system("servald rhizome import ...") to
handle a Rhizome direct POST /rhizome/bundle request.  Antiquated code in and
around rhizome_import_bundle() needs much cleaning up, as indicated by some
TODO comments.  Invocations must unnecessarily write the manifest into a file,
when they already have it in memory, ready to pass to the function.

All the 'rhizomeops' tests pass, but two 'rhizomeprotocol' tests are broken
by the changes in this commit.
This commit is contained in:
Andrew Bettison 2012-09-28 17:55:43 +09:30
parent e51745e39d
commit 17ef2249e4
3 changed files with 27 additions and 61 deletions

View File

@ -48,64 +48,53 @@ int rhizome_enabled()
file and object buffers and lifetimes.
*/
int rhizome_bundle_import(rhizome_manifest *m_in, rhizome_manifest **m_out,
const char *bundle, int ttl)
int rhizome_bundle_import(rhizome_manifest *m_in, rhizome_manifest **m_out,
const char *manifest_path, int ttl)
{
if (debug & DEBUG_RHIZOME)
DEBUGF("rhizome_bundle_import(m_in=%p, m_out=%p, bundle=%s, ttl=%d)",
m_in, m_out, bundle ? bundle : "(null)", ttl);
if (m_out) *m_out = NULL;
char filename[1024];
char manifestname[1024];
/* make sure import path exists */
if (create_rhizome_import_dir() == -1)
return -1;
if (!FORM_RHIZOME_IMPORT_PATH(filename, "file.%s", bundle)
|| !FORM_RHIZOME_IMPORT_PATH(manifestname, "manifest.%s", bundle))
return WHY("Manifest bundle name too long");
DEBUGF("rhizome_bundle_import(m_in=%p, m_out=%p, manifest_path=%s, ttl=%d)",
m_in, m_out,
manifest_path ? alloca_str_toprint(manifest_path) : "NULL",
ttl
);
if (m_out)
*m_out = NULL;
/* Read manifest file if no manifest was given */
rhizome_manifest *m = m_in;
if (!m_in) {
if (!manifest_path)
return WHY("No manifest supplied");
m = rhizome_new_manifest();
if (!m)
return WHY("Out of manifests.");
if (rhizome_read_manifest_file(m, manifestname, 0 /* file not buffer */) == -1) {
return WHY("Out of manifests");
if (rhizome_read_manifest_file(m, manifest_path, 0 /* file not buffer */) == -1) {
rhizome_manifest_free(m);
return WHY("Could not read manifest file.");
return WHY("Could not read manifest file");
} else if (rhizome_manifest_verify(m)) {
rhizome_manifest_free(m);
return WHY("Could not verify manifest file.");
return WHY("Could not verify manifest file");
}
}
/* Add the manifest and its associated file to the Rhizome database. */
m->dataFileName = strdup(filename);
/* Add the manifest and its payload to the Rhizome database. */
if (!(m->dataFileName && m->dataFileName[0]))
return WHY("Missing data file name");
if (rhizome_manifest_check_file(m))
return WHY("File does not belong to manifest");
int ret=rhizome_manifest_check_duplicate(m,NULL);
if (!ret) rhizome_add_manifest(m, ttl);
unlink(filename);
int ret = rhizome_manifest_check_duplicate(m, NULL);
if (ret == 0)
ret = rhizome_add_manifest(m, ttl);
if (ret == -1) {
WHY("rhizome_add_manifest() failed");
unlink(manifestname);
} else {
/* >>> For testing, write manifest file back to disk and leave it there */
// unlink(manifestname);
if (rhizome_write_manifest_file(m, manifestname))
ret = WHY("Could not write manifest file.");
if (manifest_path && rhizome_write_manifest_file(m, manifest_path))
ret = WHYF("Could not write %s", manifest_path);
}
/* If the manifest structure was allocated in this function, and it is not being returned to the
caller, then this function is responsible for freeing it */
if (m_out)
*m_out = m;
else if (!m_in)
rhizome_manifest_free(m);
return ret;
}

View File

@ -209,7 +209,7 @@ int rhizome_store_bundle(rhizome_manifest *m);
int rhizome_manifest_add_group(rhizome_manifest *m,char *groupid);
int rhizome_clean_payload(const char *fileidhex);
int rhizome_store_file(rhizome_manifest *m,const unsigned char *key);
int rhizome_bundle_import(rhizome_manifest *m_in, rhizome_manifest **m_out, const char *bundle, int ttl);
int rhizome_bundle_import(rhizome_manifest *m_in, rhizome_manifest **m_out, const char *manifest_path, int ttl);
int rhizome_manifest_verify(rhizome_manifest *m);
int rhizome_manifest_check_sanity(rhizome_manifest *m_in);

View File

@ -382,27 +382,11 @@ int rhizome_position_candidate(int position)
void rhizome_import_received_bundle(struct rhizome_manifest *m)
{
// TODO: We already have the manifest struct in memory, should import the bundle
// directly from that, not by writing it to a file and re-reading it!
const char *id = rhizome_manifest_get(m, "id", NULL, 0);
if (id == NULL) {
WHY("Manifest missing ID");
return;
}
if (create_rhizome_import_dir() == -1)
return;
char filename[1024];
if (!FORM_RHIZOME_IMPORT_PATH(filename, "manifest.%s", id))
return;
/* Do really write the manifest unchanged */
m->finalised = 1;
m->manifest_bytes = m->manifest_all_bytes;
if (debug & DEBUG_RHIZOME_RX) {
DEBUGF("manifest bid=%s len=%d has %d signatories", id, m->manifest_bytes, m->sig_count);
DEBUGF("manifest len=%d has %d signatories", m->manifest_bytes, m->sig_count);
dump("manifest", m->manifestdata, m->manifest_all_bytes);
}
if (rhizome_write_manifest_file(m, filename) != -1)
rhizome_bundle_import(m, NULL, id, m->ttl - 1 /* TTL */);
rhizome_bundle_import(m, NULL, NULL, m->ttl - 1 /* TTL */);
}
/* Verifies manifests as late as possible to avoid wasting time. */
@ -723,14 +707,7 @@ int rhizome_queue_manifest_import(rhizome_manifest *m, struct sockaddr_in *peeri
{
if (debug & DEBUG_RHIZOME_RX)
DEBUGF("We already have the file for this manifest; importing from manifest alone.");
if (create_rhizome_import_dir() == -1)
return -1;
char filename[1024];
if (!FORM_RHIZOME_IMPORT_PATH(filename, "manifest.%s", bid))
return -1;
if (!rhizome_write_manifest_file(m, filename)) {
rhizome_bundle_import(m, NULL, bid, m->ttl-1);
}
rhizome_bundle_import(m, NULL, NULL, m->ttl-1);
}
}