From 53d5a8734b05a26cd0cf2deb740ba48618b4bc88 Mon Sep 17 00:00:00 2001
From: Andrew Bettison <andrew@servalproject.org>
Date: Tue, 18 Mar 2014 16:03:26 +1030
Subject: [PATCH] Fix Rhizome cleanup to remove temporary payloads

Fix test case to properly assert that all stale payloads are
deleted
---
 rhizome_database.c | 18 ++++++------------
 tests/rhizomeops   |  1 +
 2 files changed, 7 insertions(+), 12 deletions(-)

diff --git a/rhizome_database.c b/rhizome_database.c
index 2fffa672..bb18228a 100644
--- a/rhizome_database.c
+++ b/rhizome_database.c
@@ -1187,11 +1187,11 @@ int rhizome_database_filehash_from_id(const rhizome_bid_t *bidp, uint64_t versio
   OUT();
 }
 
-static int rhizome_delete_external(const rhizome_filehash_t *hashp)
+static int rhizome_delete_external(const char *id)
 {
   // attempt to remove any external blob
   char blob_path[1024];
-  if (!FORM_RHIZOME_DATASTORE_PATH(blob_path, "%s/%s", RHIZOME_BLOB_SUBDIR, alloca_tohex_rhizome_filehash_t(*hashp)))
+  if (!FORM_RHIZOME_DATASTORE_PATH(blob_path, "%s/%s", RHIZOME_BLOB_SUBDIR, id))
     return -1;
   if (unlink(blob_path) == -1) {
     if (errno != ENOENT)
@@ -1252,11 +1252,8 @@ int rhizome_cleanup(struct rhizome_cleanup_report *report)
   while (sqlite_step_retry(&retry, statement) == SQLITE_ROW) {
     candidates++;
     const char *id = (const char *) sqlite3_column_text(statement, 0);
-    rhizome_filehash_t hash;
-    if (str_to_rhizome_filehash_t(&hash, id) == -1)
-      WARNF("invalid field FILES.id=%s -- ignored", alloca_str_toprint(id));
-    else if (rhizome_delete_external(&hash) == 0 && report)
-	++report->deleted_stale_incoming_files;
+    if (rhizome_delete_external(id) == 0 && report)
+      ++report->deleted_stale_incoming_files;
   }
   sqlite3_finalize(statement);
 
@@ -1267,10 +1264,7 @@ int rhizome_cleanup(struct rhizome_cleanup_report *report)
   while (sqlite_step_retry(&retry, statement) == SQLITE_ROW) {
     candidates++;
     const char *id = (const char *) sqlite3_column_text(statement, 0);
-    rhizome_filehash_t hash;
-    if (str_to_rhizome_filehash_t(&hash, id) == -1)
-      WARNF("invalid field FILES.id=%s -- ignored", alloca_str_toprint(id));
-    else if (rhizome_delete_external(&hash) == 0 && report)
+    if (rhizome_delete_external(id) == 0 && report)
       ++report->deleted_orphan_files;
   }
   sqlite3_finalize(statement);
@@ -1981,7 +1975,7 @@ static int rhizome_delete_manifest_retry(sqlite_retry_state *retry, const rhizom
 static int rhizome_delete_file_retry(sqlite_retry_state *retry, const rhizome_filehash_t *hashp)
 {
   int ret = 0;
-  rhizome_delete_external(hashp);
+  rhizome_delete_external(alloca_tohex_rhizome_filehash_t(*hashp));
   sqlite3_stmt *statement = sqlite_prepare_bind(retry, "DELETE FROM files WHERE id = ?", RHIZOME_FILEHASH_T, hashp, END);
   if (!statement || sqlite_exec_retry(retry, statement) == -1)
     ret = -1;
diff --git a/tests/rhizomeops b/tests/rhizomeops
index 0f7c5415..077682e5 100755
--- a/tests/rhizomeops
+++ b/tests/rhizomeops
@@ -927,6 +927,7 @@ test_MeshMSAddGrow() {
       for ofilehash in "${ofilehashes[@]}"; do
          execute --exit-status=1 --stderr $servald rhizome export file "$ofilehash"
       done
+      filehash="$filehashx"
    done
 }