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.
This commit is contained in:
Andrew Bettison 2012-05-17 16:09:57 +09:30
parent 1171e60704
commit 9a51c76dfb
4 changed files with 40 additions and 12 deletions

View File

@ -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(":");

View File

@ -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");

View File

@ -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(":");

View File

@ -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]\+$"