Add 'rhizome add file --bundle=BID' option

A convenience to re-use an existing manifest, instead of having
to extract the manifest, erase some fields, then pass the result
back into the 'rhizome add file' command
This commit is contained in:
Andrew Bettison 2015-03-28 05:01:43 +10:30
parent b17848a438
commit da22816784
5 changed files with 38 additions and 25 deletions

View File

@ -109,6 +109,8 @@ int rhizome_fetch_delay_ms()
*
* - 'mout' must point to a manifest pointer which is updated to hold the constructed manifest.
*
* - 'bid' must point to a supplied bundle id parameter, or NULL if none was supplied.
*
* - 'bsk' must point to a supplied bundle secret parameter, or NULL if none was supplied.
*
* - 'author' must point to a supplied author parameter, or NULL if none was supplied.
@ -145,6 +147,7 @@ int rhizome_fetch_delay_ms()
enum rhizome_add_file_result rhizome_manifest_add_file(int appending,
rhizome_manifest *m,
rhizome_manifest **mout,
const rhizome_bid_t *bid,
const rhizome_bk_t *bsk,
const sid_t *author,
const char *file_path,
@ -161,14 +164,14 @@ enum rhizome_add_file_result rhizome_manifest_add_file(int appending,
// Caller must not supply a malformed manifest (but an invalid one is okay because missing
// fields will be filled in, so we don't check validity here).
assert(!m->malformed);
if (m->has_id) {
if (bid) {
if (config.debug.rhizome)
DEBUGF("Reading manifest from database: id=%s", alloca_tohex_rhizome_bid_t(m->cryptoSignPublic));
DEBUGF("Reading manifest from database: id=%s", alloca_tohex_rhizome_bid_t(*bid));
if ((existing_manifest = rhizome_new_manifest()) == NULL) {
WHY(cause = "Manifest struct could not be allocated");
goto error;
}
enum rhizome_bundle_status status = rhizome_retrieve_manifest(&m->cryptoSignPublic, existing_manifest);
enum rhizome_bundle_status status = rhizome_retrieve_manifest(bid, existing_manifest);
switch (status) {
case RHIZOME_BUNDLE_STATUS_NEW:
// No manifest with that bundle ID exists in the store, so we are building a bundle from

View File

@ -463,6 +463,7 @@ enum rhizome_add_file_result {
enum rhizome_add_file_result rhizome_manifest_add_file(int appending,
rhizome_manifest *m,
rhizome_manifest **mout,
const rhizome_bid_t *bid,
const rhizome_bk_t *bsk,
const sid_t *author,
const char *file_path,

View File

@ -104,22 +104,22 @@ static int app_rhizome_hash_file(const struct cli_parsed *parsed, struct cli_con
DEFINE_CMD(app_rhizome_add_file, 0,
"Add a file to Rhizome and optionally write its manifest to the given path",
"rhizome","add","file" KEYRING_PIN_OPTIONS,"[--force-new]","<author_sid>","<filepath>","[<manifestpath>]","[<bsk>]","...");
"rhizome","add","file" KEYRING_PIN_OPTIONS,"[--bundle=<bundleid>]","[--force-new]","<author_sid>","<filepath>","[<manifestpath>]","[<bsk>]","...");
DEFINE_CMD(app_rhizome_add_file, 0,
"Append content to a journal bundle",
"rhizome", "journal", "append" KEYRING_PIN_OPTIONS, "<author_sid>", "<manifestid>", "<filepath>", "[<bsk>]");
"rhizome", "journal", "append" KEYRING_PIN_OPTIONS, "<author_sid>", "<bundleid>", "<filepath>", "[<bsk>]");
static int app_rhizome_add_file(const struct cli_parsed *parsed, struct cli_context *context)
{
if (config.debug.verbose)
DEBUG_cli_parsed(parsed);
const char *filepath, *manifestpath, *manifestIdHex, *authorSidHex, *bsktext;
const char *filepath, *manifestpath, *bundleIdHex, *authorSidHex, *bsktext;
int force_new = 0 == cli_arg(parsed, "--force-new", NULL, NULL, NULL);
cli_arg(parsed, "filepath", &filepath, NULL, "");
if (cli_arg(parsed, "author_sid", &authorSidHex, cli_optional_sid, "") == -1)
return -1;
cli_arg(parsed, "manifestpath", &manifestpath, NULL, "");
cli_arg(parsed, "manifestid", &manifestIdHex, NULL, "");
cli_arg(parsed, "--bundle", &bundleIdHex, cli_bid, "") == 0 || cli_arg(parsed, "bundleid", &bundleIdHex, cli_optional_bid, "");
if (cli_arg(parsed, "bsk", &bsktext, cli_optional_bundle_secret_key, NULL) == -1)
return -1;
@ -130,10 +130,10 @@ static int app_rhizome_add_file(const struct cli_parsed *parsed, struct cli_cont
return WHYF("invalid author_sid: %s", authorSidHex);
rhizome_bid_t bid;
if (!manifestIdHex || !*manifestIdHex)
manifestIdHex = NULL;
else if (str_to_rhizome_bid_t(&bid, manifestIdHex) == -1)
return WHYF("Invalid bundle ID: %s", alloca_str_toprint(manifestIdHex));
if (!bundleIdHex || !*bundleIdHex)
bundleIdHex = NULL;
else if (str_to_rhizome_bid_t(&bid, bundleIdHex) == -1)
return WHYF("Invalid bundle ID: %s", alloca_str_toprint(bundleIdHex));
rhizome_bk_t bsk;
if (!bsktext || !*bsktext)
@ -184,7 +184,7 @@ static int app_rhizome_add_file(const struct cli_parsed *parsed, struct cli_cont
if (rhizome_opendb() == -1)
goto finish;
/* Create a manifest in memory that to accompany the added file. Initially the manifest is blank.
/* Create a manifest in memory that to describe the added file. Initially the manifest is blank.
* If a manifest file is supplied, then read and parse it, barfing if it contains any duplicate
* fields or invalid values. If it successfully parses, then overwrite it with any command-line
* manifest field settings, overriding the values parsed from the file. Barf if any of these new
@ -204,23 +204,11 @@ static int app_rhizome_add_file(const struct cli_parsed *parsed, struct cli_cont
}
}
/* If a manifest ID (bundle ID) was supplied on the command line, first ensure it does not
* contradict any manifest ID present in the supplied manifest file, then insert it into the
* manifest.
*/
if (manifestIdHex) {
if (!m->has_id)
rhizome_manifest_set_id(m, &bid);
else if (cmp_rhizome_bid_t(&m->cryptoSignPublic, &bid) != 0) {
ret = WHYF("manifestid=%s does not match manifest id=%s", manifestIdHex, alloca_tohex_rhizome_bid_t(m->cryptoSignPublic));
goto finish;
}
}
/* Create an in-memory manifest for the file being added.
*/
rhizome_manifest *mout = NULL;
enum rhizome_add_file_result result = rhizome_manifest_add_file(appending, m, &mout,
bundleIdHex ? &bid : NULL,
bsktext ? &bsk : NULL,
authorSidHex ? &authorSid : NULL,
filepath,

View File

@ -403,6 +403,7 @@ static int insert_make_manifest(httpd_request *r)
rhizome_manifest *mout = NULL;
char message[150];
enum rhizome_add_file_result result = rhizome_manifest_add_file(r->u.insert.appending, r->manifest, &mout,
NULL,
r->u.insert.received_secret ? &r->u.insert.bundle_secret : NULL,
r->u.insert.received_author ? &r->u.insert.author: NULL,
NULL, 0, NULL, strbuf_local(message, sizeof message));

View File

@ -999,6 +999,26 @@ test_BroadcastNotEncrypted() {
assert diff file1 file1y
}
doc_AddReuseManifest="Add --bundle copies fields from existing manifest"
setup_AddReuseManifest() {
B_IDENTITY_COUNT=1
setup_servald
setup_rhizome
echo "First content" >file1
echo "Second content" >file1a
echo -e "service=wazoo\nunexpected=true\nrecipient=$SIDB1\nnothing=here" >file1.manifest
executeOk_servald rhizome add file $SIDA file1 file1.manifest
tfw_cat --stderr
extract_stdout_manifestid BID
}
test_AddReuseManifest() {
executeOk_servald rhizome add file --bundle=$BID $SIDA file1a file1a.manifest '' !nothing something=there
tfw_cat --stderr
assert_manifest_fields file1a.manifest service=wazoo unexpected=true recipient=$SIDB1 !nothing something=there
executeOk_servald rhizome export bundle $BID file1ax.manifest file1ax
assert diff file1a file1ax
}
doc_JournalAppend="Create and append to a journal"
setup_JournalAppend() {
setup_servald