From 9a51c76dfb7cc477a72993a0ae8beebc673e5ec0 Mon Sep 17 00:00:00 2001 From: Andrew Bettison Date: Thu, 17 May 2012 16:09:57 +0930 Subject: [PATCH] Insist on 'service' field in all manifests The "rhizome file add" command assumes service=file if no manifest supplied or the manifest lacks a service field. The "rhizome extract manifest" command includes the service in its CLI output. --- commandline.c | 20 ++++++++++++++++---- rhizome.c | 3 +++ rhizome_database.c | 17 +++++++++++------ tests/dna_rhizome | 12 ++++++++++-- 4 files changed, 40 insertions(+), 12 deletions(-) diff --git a/commandline.c b/commandline.c index 985fcdfc..526759b8 100644 --- a/commandline.c +++ b/commandline.c @@ -1089,16 +1089,23 @@ int app_rhizome_add_file(int argc, const char *const *argv, struct command_line_ return WHY("Manifest struct could not be allocated -- not added to rhizome"); } /* Fill in a few missing manifest fields, to make it easier to use when adding new files: + - the default service is "file" - the current time for "date" - if service is "file", then the payload file's basename for "name" */ + const char *service = rhizome_manifest_get(m, "service", NULL, 0); + if (service == NULL) { + rhizome_manifest_set(m, "service", (service = "file")); + } if (rhizome_manifest_get(m, "date", NULL, 0) == NULL) { rhizome_manifest_set_ll(m, "date", gettime_ms()); } - if (rhizome_manifest_get(m, "name", NULL, 0) == NULL) { - const char *name = strrchr(filepath, '/'); - name = name ? name + 1 : filepath; - rhizome_manifest_set(m, "name", name); + if (strcasecmp("file", service) == 0) { + if (rhizome_manifest_get(m, "name", NULL, 0) == NULL) { + const char *name = strrchr(filepath, '/'); + name = name ? name + 1 : filepath; + rhizome_manifest_set(m, "name", name); + } } /* Add the manifest and its associated file to the Rhizome database, generating an "id" in the * process */ @@ -1121,6 +1128,11 @@ int app_rhizome_add_file(int argc, const char *const *argv, struct command_line_ invoking this command can read it to obtain feedback on the result. */ if (manifestpath[0] && rhizome_write_manifest_file(mout, manifestpath) == -1) ret = WHY("Could not overwrite manifest file."); + service = rhizome_manifest_get(mout, "service", NULL, 0); + if (service) { + cli_puts("service"); cli_delim(":"); + cli_puts(service); cli_delim("\n"); + } cli_puts("manifestid"); cli_delim(":"); cli_puts(rhizome_bytes_to_hex(mout->cryptoSignPublic, crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES)); cli_delim("\n"); cli_puts("filehash"); cli_delim(":"); diff --git a/rhizome.c b/rhizome.c index d1f85b01..a340d15e 100644 --- a/rhizome.c +++ b/rhizome.c @@ -147,6 +147,9 @@ int rhizome_add_manifest(rhizome_manifest *m_in, if (m_out) *m_out = NULL; /* Ensure manifest meets basic sanity checks. */ + const char *service = rhizome_manifest_get(m_in, "service", NULL, 0); + if (service == NULL || !service[0]) + return WHY("Manifest missing 'service' field"); const char *name = rhizome_manifest_get(m_in, "name", NULL, 0); if (name == NULL || !name[0]) return WHY("Manifest missing 'name' field"); diff --git a/rhizome_database.c b/rhizome_database.c index 4d0cd49a..d8b22d24 100644 --- a/rhizome_database.c +++ b/rhizome_database.c @@ -952,24 +952,29 @@ int rhizome_retrieve_manifest(const char *manifestid, rhizome_manifest **mp) } else { ret = 1; rhizome_hex_to_bytes(manifestid, m->cryptoSignPublic, crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES*2); + const char *blob_service = rhizome_manifest_get(m, "service", NULL, 0); + if (blob_service == NULL) + ret = WHY("Manifest is missing 'service' field"); const char *blob_filehash = rhizome_manifest_get(m, "filehash", NULL, 0); if (blob_filehash == NULL) - ret = WHY("Manifest is missing filehash line"); + ret = WHY("Manifest is missing 'filehash' field"); else { memcpy(m->fileHexHash, blob_filehash, SHA512_DIGEST_STRING_LENGTH); m->fileHashedP = 1; } long long blob_version = rhizome_manifest_get_ll(m, "version"); if (blob_version == -1) - ret = WHY("Manifest is missing version line"); + ret = WHY("Manifest is missing 'version' field"); else m->version = blob_version; - long long lengthq = rhizome_manifest_get_ll(m, "filesize"); - if (lengthq == -1) - ret = WHY("Manifest is missing filesize line"); + long long filesizeq = rhizome_manifest_get_ll(m, "filesize"); + if (filesizeq == -1) + ret = WHY("Manifest is missing 'filesize' field"); else - m->fileLength = lengthq; + m->fileLength = filesizeq; if (ret == 1) { + cli_puts("service"); cli_delim(":"); + cli_puts(blob_service); cli_delim("\n"); cli_puts("manifestid"); cli_delim(":"); cli_puts((const char *)sqlite3_column_text(statement, 0)); cli_delim("\n"); cli_puts("version"); cli_delim(":"); diff --git a/tests/dna_rhizome b/tests/dna_rhizome index 96c9c68e..f98f0810 100755 --- a/tests/dna_rhizome +++ b/tests/dna_rhizome @@ -45,7 +45,8 @@ assert_rhizome_list() { assert_stdout_add_file() { local filename="$1" unpack_manifest_for_grep "$filename" - assertStdoutLineCount '==' 4 + assertStdoutLineCount '==' 5 + assertStdoutGrep --matches=1 '^service:file$' assertStdoutGrep --matches=1 "^name:${2:-$re_name}\$" assertStdoutGrep --matches=1 "^manifestid:$re_manifestid\$" assertStdoutGrep --matches=1 "^filehash:$re_filehash\$" @@ -100,6 +101,10 @@ extract_manifest() { [ -n "$_var" ] && eval $_var=$_value } +extract_manifest_service() { + extract_manifest "$1" "$2" service '[0-9a-zA-Z]\+' +} + extract_manifest_id() { extract_manifest "$1" "$2" id '[0-9a-fA-F]\{64\}' } @@ -157,6 +162,7 @@ test_AddNonExistManifest() { assert_stdout_add_file file1 assert [ -r file1.manifest ] tfw_cat -v file1.manifest + assertGrep file1.manifest '^service=file$' assertGrep file1.manifest '^name=file1$' assertGrep file1.manifest '^date=[0-9]\+$' assertGrep file1.manifest '^version=[0-9]\+$' @@ -177,6 +183,7 @@ test_AddManifest() { executeOk $dna rhizome add file $sid '' file1 file1.manifest tfw_cat --stdout --stderr -v file1.manifest assert_stdout_add_file file1 wah + assertGrep file1.manifest '^service=file$' assertGrep file1.manifest '^name=wah$' assertGrep file1.manifest '^version=[0-9]\+$' assertGrep file1.manifest '^date=12345$' @@ -214,8 +221,9 @@ setup_AddThenExtractManifest() { test_AddThenExtractManifest() { executeOk $dna rhizome extract manifest $manifestid file1x.manifest assert cmp file1.manifest file1x.manifest - assertStdoutLineCount '==' 5 + assertStdoutLineCount '==' 6 local size=$(( $(cat file1 | wc -c) + 0 )) + assertStdoutGrep --matches=1 "^service:file$" assertStdoutGrep --matches=1 "^manifestid:$manifestid$" assertStdoutGrep --matches=1 "^version:$version$" assertStdoutGrep --matches=1 "^inserttime:[0-9]\+$"