From e35bf77938a036b059f926e3d00d23dd3d2f027c Mon Sep 17 00:00:00 2001 From: Andrew Bettison Date: Thu, 10 Jul 2014 21:19:42 +0930 Subject: [PATCH] Rhizome Java API: insert/update empty bundle --- rhizome_restful.c | 4 +++- rhizome_store.c | 14 +++++++---- tests/rhizomejava | 55 ++++++++++++++++++++++++++++++++++++++++++++ tests/rhizomerestful | 4 ++-- 4 files changed, 69 insertions(+), 8 deletions(-) diff --git a/rhizome_restful.c b/rhizome_restful.c index 8da5929e..6e83ac82 100644 --- a/rhizome_restful.c +++ b/rhizome_restful.c @@ -580,11 +580,13 @@ static int restful_rhizome_insert_end(struct http_request *hr) rhizome_manifest_set_filesize(r->manifest, r->u.insert.write.file_length); // fall through case RHIZOME_PAYLOAD_STATUS_STORED: + assert(r->manifest->filesize != RHIZOME_SIZE_UNSET); // TODO: check that stored hash matches received payload's hash // fall through case RHIZOME_PAYLOAD_STATUS_EMPTY: status_valid = 1; - assert(r->manifest->filesize != RHIZOME_SIZE_UNSET); + if (r->manifest->filesize == RHIZOME_SIZE_UNSET) + rhizome_manifest_set_filesize(r->manifest, 0); if (r->u.insert.payload_size == r->manifest->filesize) break; // fall through diff --git a/rhizome_store.c b/rhizome_store.c index 08154dd0..4967d21c 100644 --- a/rhizome_store.c +++ b/rhizome_store.c @@ -679,14 +679,18 @@ enum rhizome_payload_status rhizome_finish_write(struct rhizome_write *write) { enum rhizome_payload_status status = RHIZOME_PAYLOAD_STATUS_NEW; - // Once the whole file has been processed, we should finally know its. + // Once the whole file has been processed, we should finally know its length if (write->file_length == RHIZOME_SIZE_UNSET) { if (config.debug.rhizome_store) DEBUGF("Wrote %"PRIu64" bytes, set file_length", write->file_offset); write->file_length = write->file_offset; - status = store_make_space(write->file_length, NULL); - if (status!=RHIZOME_PAYLOAD_STATUS_NEW) - goto failure; + if (write->file_length == 0) + status = RHIZOME_PAYLOAD_STATUS_EMPTY; + else { + status = store_make_space(write->file_length, NULL); + if (status != RHIZOME_PAYLOAD_STATUS_NEW) + goto failure; + } } // flush out any remaining buffered pieces to disk @@ -709,7 +713,7 @@ enum rhizome_payload_status rhizome_finish_write(struct rhizome_write *write) } assert(write->file_offset == write->file_length); - if (write->file_length==0){ + if (write->file_length == 0) { // whoops, no payload, don't store anything if (config.debug.rhizome_store) DEBUGF("Ignoring empty write"); diff --git a/tests/rhizomejava b/tests/rhizomejava index bb07d3ff..7e3f4f24 100755 --- a/tests/rhizomejava +++ b/tests/rhizomejava @@ -393,4 +393,59 @@ test_RhizomeInsertAnon() { assert_rhizome_list --fromhere=0 --manifest=ifile2.manifest file2 } +doc_RhizomeInsertEmptyNew="Java API update existing Rhizome bundle to empty" +setup_RhizomeInsertEmptyNew() { + setup + >empty + assert [ ! -s empty ] +} +test_RhizomeInsertEmptyNew() { + executeJavaOk org.servalproject.test.Rhizome rhizome-insert '' '' empty empty.manifest "lucky" + tfw_cat --stdout --stderr -v empty.manifest + extract_manifest_id BID empty.manifest + assertStdoutGrep '^_status=NEW$' + assertStdoutGrep "^id=$BID\$" + assertStdoutGrep "^filesize=0\$" + assertStdoutGrep --matches=0 "^filehash=" + assertStderrGrep --matches=1 "^bundle_status_code=NEW$CR\$" + assertStderrGrep --matches=1 --ignore-case "^bundle_status_message=.*bundle new to store.*$CR\$" + assertStderrGrep --matches=1 "^payload_status_code=EMPTY$CR\$" + assertStderrGrep --matches=1 --ignore-case "^payload_status_message=.*payload empty.*$CR\$" + executeOk_servald rhizome list + assert_rhizome_list empty + executeOk_servald rhizome extract bundle "$BID" xempty.manifest xempty + assert [ ! -e xempty ] + assert diff xempty.manifest empty.manifest +} + +doc_RhizomeInsertEmptyUpdate="Java API insert new empty Rhizome bundle" +setup_RhizomeInsertEmptyUpdate() { + setup + create_file file1 1001 + executeOk_servald rhizome add file "$SIDA1" file1 file1.manifest + extract_stdout_manifestid BID + extract_stdout_BK BK + >empty + assert [ ! -s empty ] + echo "id=$BID" >iempty.manifest + echo "BK=$BK" >>iempty.manifest +} +test_RhizomeInsertEmptyUpdate() { + executeJavaOk org.servalproject.test.Rhizome rhizome-insert '' iempty.manifest empty empty.manifest + tfw_cat --stdout --stderr -v empty.manifest + assertStdoutGrep '^_status=NEW$' + assertStdoutGrep "^id=$BID\$" + assertStdoutGrep "^filesize=0\$" + assertStdoutGrep --matches=0 "^filehash=" + assertStderrGrep --matches=1 "^bundle_status_code=NEW$CR\$" + assertStderrGrep --matches=1 --ignore-case "^bundle_status_message=.*bundle new to store.*$CR\$" + assertStderrGrep --matches=1 "^payload_status_code=EMPTY$CR\$" + assertStderrGrep --matches=1 --ignore-case "^payload_status_message=.*payload empty.*$CR\$" + executeOk_servald rhizome list + assert_rhizome_list empty + executeOk_servald rhizome extract bundle "$BID" xempty.manifest xempty + assert [ ! -e xempty ] + assert diff xempty.manifest empty.manifest +} + runTests "$@" diff --git a/tests/rhizomerestful b/tests/rhizomerestful index 8fe06aa2..c5e895cd 100755 --- a/tests/rhizomerestful +++ b/tests/rhizomerestful @@ -650,8 +650,8 @@ test_RhizomeInsertEmpty() { assertStdoutIs 201 assertGrep --matches=1 --ignore-case http.header "^Serval-Rhizome-Result-Bundle-Status-Code: 0$CR\$" assertGrep --matches=1 --ignore-case http.header "^Serval-Rhizome-Result-Bundle-Status-Message: .*bundle new to store.*$CR\$" - assertGrep --matches=1 --ignore-case http.header "^Serval-Rhizome-Result-Payload-Status-Code: 1$CR\$" - assertGrep --matches=1 --ignore-case http.header "^Serval-Rhizome-Result-Payload-Status-Message: .*payload new to store.*$CR\$" + assertGrep --matches=1 --ignore-case http.header "^Serval-Rhizome-Result-Payload-Status-Code: 0$CR\$" + assertGrep --matches=1 --ignore-case http.header "^Serval-Rhizome-Result-Payload-Status-Message: .*payload empty.*$CR\$" extract_manifest_id BID empty.manifest executeOk_servald rhizome list assert_rhizome_list empty