pnor_partition_table: Hoist partition existence check from parseTocLine()

Enforce some separation of concerns: Parsing the text of a ToC entry is
an operation independent of testing whether context derived from the
parsed data is valid.

Specifically, testing whether a file at a path derived from the ToC
entry is valid inside the parser is unhelpful when we try to reuse the
parser for test code, where we want to set up an environment as
described by the ToC entry. Under this condition the file is of course
not going to exist - we're parsing the ToC entry in order to create it.

So, lets hoist the test out to the caller, enabling re-use of the parser
in the test code.

Change-Id: Ibc9b62c00b95b8296b48ccf45630cb5344347bd7
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
diff --git a/pnor_partition_table.cpp b/pnor_partition_table.cpp
index 9c9fb9c..8ee43c0 100644
--- a/pnor_partition_table.cpp
+++ b/pnor_partition_table.cpp
@@ -151,8 +151,7 @@
     part.data.id = std::stoul(id);
 }
 
-bool Table::parseTocLine(fs::path& dir, const std::string& line,
-                         pnor_partition& part)
+bool Table::parseTocLine(const std::string& line, pnor_partition& part)
 {
     static constexpr auto ID_MATCH = 1;
     static constexpr auto NAME_MATCH = 2;
@@ -175,14 +174,6 @@
         return false;
     }
 
-    fs::path partitionFile = dir;
-    partitionFile /= match[NAME_MATCH].str();
-    if (!fs::exists(partitionFile))
-    {
-        MSG_ERR("Partition file %s does not exist", partitionFile.c_str());
-        return false;
-    }
-
     writeNameAndId(part, match[NAME_MATCH].str(), match[ID_MATCH].str());
     writeDefaults(part);
 
@@ -211,10 +202,24 @@
 
     while (std::getline(file, line))
     {
-        if (parseTocLine(directory, line, table.partitions[numParts]))
+        pnor_partition& part = table.partitions[numParts];
+        fs::path file;
+
+        if (!parseTocLine(line, part))
+        {
+            continue;
+        }
+
+        file = directory;
+        file /= part.data.name;
+        if (fs::exists(file))
         {
             ++numParts;
         }
+        else
+        {
+            MSG_ERR("Partition file %s does not exist", file.c_str());
+        }
     }
 }
 
diff --git a/pnor_partition_table.hpp b/pnor_partition_table.hpp
index 9d254d8..a333b5f 100644
--- a/pnor_partition_table.hpp
+++ b/pnor_partition_table.hpp
@@ -147,16 +147,13 @@
     /** @brief Parse a ToC line (entry) into the corresponding FFS partition
      * object.
      *
-     * @param[in] dir - The parent directory of the FFS partition files
-     *                  described in the ToC.
      * @param[in] line - The ToC line to parse
      * @param[out] part - The partition object to populate with the information
      *                    parsed from the provided ToC line
      *
      * @returns True on success, false on failure.
      */
-    bool parseTocLine(fs::path& dir, const std::string& line,
-                      pnor_partition& part);
+    bool parseTocLine(const std::string& line, pnor_partition& part);
 
     /** @brief Prepares a vector of PNOR partition structures.
      */