mboxd: Add a backend abstraction layer to mboxd.

Introduce a backend abstraction, enabling multiple implementations to be
compiled in at once. This change formally abstracts the two existing
backends, mtd and vpnor.

With the backend abstraction in place, subsequent backends are easier to
implement.

This change is based of Evan's work and he retains authorship credit. I
(AJ) have reworked the patch to pass the vpnor tests, refactored some
parts to enable broader use of const structures and others to clarify
the initialisation sequences.

Due to the existing lack of abstraction the patch has unfortunately
wide-ranging impacts. I've whittled it down as much as I consider
reasonable.

Change-Id: I29984a36dae4ea86ec00b853d2a756f0b9afb3ec
Signed-off-by: Evan Lojewski <github@meklort.com>
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
diff --git a/vpnor/test/tmpd.hpp b/vpnor/test/tmpd.hpp
index 7386f2f..f6ea7c9 100644
--- a/vpnor/test/tmpd.hpp
+++ b/vpnor/test/tmpd.hpp
@@ -4,6 +4,7 @@
 #include "config.h"
 
 extern "C" {
+#include "backend.h"
 #include "mboxd.h"
 }
 
@@ -28,8 +29,9 @@
 {
   public:
     template <std::size_t N>
-    VpnorRoot(struct mbox_context* ctx, const std::string (&toc)[N],
-              size_t blockSize)
+    VpnorRoot(struct backend* backend, const std::string (&toc)[N],
+              size_t blockSize) :
+        backend(backend)
     {
         char tmplt[] = "/tmp/vpnor_root.XXXXXX";
         char* tmpdir = mkdtemp(tmplt);
@@ -58,14 +60,21 @@
             std::ofstream(tocFilePath, std::ofstream::app) << line << "\n";
         }
 
-        strncpy(ctx->paths.ro_loc, ro().c_str(), PATH_MAX - 1);
-        ctx->paths.ro_loc[PATH_MAX - 1] = '\0';
-        strncpy(ctx->paths.rw_loc, rw().c_str(), PATH_MAX - 1);
-        ctx->paths.rw_loc[PATH_MAX - 1] = '\0';
-        strncpy(ctx->paths.prsv_loc, prsv().c_str(), PATH_MAX - 1);
-        ctx->paths.prsv_loc[PATH_MAX - 1] = '\0';
-        strncpy(ctx->paths.patch_loc, patch().c_str(), PATH_MAX - 1);
-        ctx->paths.patch_loc[PATH_MAX - 1] = '\0';
+        vpnor_partition_paths paths{};
+
+        snprintf(paths.ro_loc, PATH_MAX - 1, "%s/ro", root.c_str());
+        paths.ro_loc[PATH_MAX - 1] = '\0';
+        snprintf(paths.rw_loc, PATH_MAX - 1, "%s/rw", root.c_str());
+        paths.rw_loc[PATH_MAX - 1] = '\0';
+        snprintf(paths.prsv_loc, PATH_MAX - 1, "%s/prsv", root.c_str());
+        paths.prsv_loc[PATH_MAX - 1] = '\0';
+        snprintf(paths.patch_loc, PATH_MAX - 1, "%s/patch", root.c_str());
+        paths.patch_loc[PATH_MAX - 1] = '\0';
+
+        if (backend_probe_vpnor(backend, &paths))
+        {
+            throw std::system_error(errno, std::system_category());
+        }
     }
 
     VpnorRoot(const VpnorRoot&) = delete;
@@ -75,6 +84,7 @@
 
     ~VpnorRoot()
     {
+        backend_free(backend);
         fs::remove_all(root);
     }
     fs::path ro()
@@ -97,6 +107,7 @@
     size_t patch(const std::string& name, const void* data, size_t len);
 
   private:
+    struct backend* backend;
     fs::path root;
     const std::string attributes[4] = {"ro", "rw", "prsv", "patch"};
 };