Set partition actual size to file size if patch file present

A patch partition file may be of smaller size than the size allocated
for that partition. If that's the case then set the actual size value
to the smaller file size.
This mimics the pflash implementation of flashing an individual
partition, which updates the actual size in the toc to the size
of the provided file.

Change-Id: I7653f5570e9e2b32b095ee6d7be4557cf59d2de7
Signed-off-by: Adriana Kobylak <anoo@us.ibm.com>
diff --git a/pnor_partition_table.cpp b/pnor_partition_table.cpp
index 98bba46..58a750e 100644
--- a/pnor_partition_table.cpp
+++ b/pnor_partition_table.cpp
@@ -87,7 +87,20 @@
     size_t sizeInBlocks = align_up(size, blockSize) / blockSize;
     imgBlocks += sizeInBlocks;
     part.data.size = sizeInBlocks;
-    part.data.actual = size;
+
+    // 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;
+    }
 }
 
 inline void Table::writeUserdata(pnor_partition& part,