pnor_partition_table: Refactor to allow tests to specify patch location

The Table class was unhelpful for testing in a couple of ways:

1. It attempted to access files on the filesystem whilst parsing ToC
   entries
2. It incorrectly assumed the location of the files it was accessing

Both of these issues come down to handling of patch files and the
configuration of the 'actual' member of the partition struct.

Hoist the handling of the partition entry's data size out of the ToC
parser, and rework the Table constructor to only require a struct
mbox_context pointer.  We can then use the paths member of mbox_context
to find the patch location rather than hard-code the value generated by
the configure script.

This prompts a rework and rename of the wrapper functions in
mboxd_pnor_partition_table.{cpp,h} to better align with the new
behaviour of the Table constructor. Reworking the wrappers has knock-on
effects in the tests, but the changes are straight-forward.

Change-Id: I87e63daf0d28b93566f7e5cb565cbf0790428479
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
diff --git a/pnor_partition_table.cpp b/pnor_partition_table.cpp
index 50d7d59..376b191 100644
--- a/pnor_partition_table.cpp
+++ b/pnor_partition_table.cpp
@@ -22,11 +22,11 @@
 namespace partition
 {
 
-Table::Table(fs::path&& directory, size_t blockSize, size_t pnorSize) :
-    szBytes(sizeof(pnor_partition_table)), directory(std::move(directory)),
-    numParts(0), blockSize(blockSize), pnorSize(pnorSize)
+Table::Table(const struct mbox_context* ctx) :
+    szBytes(sizeof(pnor_partition_table)), numParts(0),
+    blockSize(1 << ctx->erase_size_shift), pnorSize(ctx->flash_size)
 {
-    preparePartitions();
+    preparePartitions(ctx);
     prepareHeader();
     hostTbl = endianFixup(tbl);
 }
@@ -68,10 +68,11 @@
     tbl.resize(capacity());
 }
 
-void Table::preparePartitions()
+void Table::preparePartitions(const struct mbox_context* ctx)
 {
-    fs::path tocFile = directory;
-    tocFile /= PARTITION_TOC_FILE;
+    const fs::path roDir = ctx->paths.ro_loc;
+    const fs::path patchDir = ctx->paths.patch_loc;
+    fs::path tocFile = roDir / PARTITION_TOC_FILE;
     allocateMemory(tocFile);
 
     std::ifstream file(tocFile.c_str());
@@ -81,6 +82,7 @@
     while (std::getline(file, line))
     {
         pnor_partition& part = table.partitions[numParts];
+        fs::path patch;
         fs::path file;
 
         // The ToC file presented in the vpnor squashfs looks like:
@@ -124,7 +126,7 @@
             }
         }
 
-        file = directory / part.data.name;
+        file = roDir / part.data.name;
         if (!fs::exists(file))
         {
             std::stringstream err;
@@ -132,6 +134,14 @@
             throw InvalidTocEntry(err.str());
         }
 
+        patch = patchDir / part.data.name;
+        if (fs::is_regular_file(patch))
+        {
+            const size_t size = part.data.size * blockSize;
+            part.data.actual =
+                std::min(size, static_cast<size_t>(fs::file_size(patch)));
+        }
+
         ++numParts;
     }
 }
@@ -226,20 +236,7 @@
     part.data.base = align_up(start, blockSize) / blockSize;
     size_t sizeInBlocks = align_up(size, blockSize) / blockSize;
     part.data.size = sizeInBlocks;
-
-    // If a a patch partition file exists, populate actual size with its file
-    // size if it is smaller than the total size.
-    fs::path patchFile(PARTITION_FILES_PATCH_LOC);
-    patchFile /= part.data.name;
-    if (fs::is_regular_file(patchFile))
-    {
-        part.data.actual =
-            std::min(size, static_cast<size_t>(fs::file_size(patchFile)));
-    }
-    else
-    {
-        part.data.actual = size;
-    }
+    part.data.actual = size;
 }
 
 static inline void writeUserdata(pnor_partition& part, uint32_t version,