oem-ibm : Support for VPD record PSPD via file I/O with offset

This commits allows transfer of PSPD record data via PLDM with
offset included. Host can now send the keyword data with
non zero offset in request message.
Tested by using pldmtool to send read request command to PLDM

Change-Id: Ibdceedd24714135adf8f106ff08eb09da9bdcaa5
Signed-off-by: Varsha Kaverappa <vkaverap@in.ibm.com>
diff --git a/oem/ibm/libpldmresponder/file_io_type_vpd.cpp b/oem/ibm/libpldmresponder/file_io_type_vpd.cpp
index 07530a8..0614ccc 100644
--- a/oem/ibm/libpldmresponder/file_io_type_vpd.cpp
+++ b/oem/ibm/libpldmresponder/file_io_type_vpd.cpp
@@ -63,8 +63,8 @@
                         fs::perms::others_read | fs::perms::owner_write);
     }
 
-    std::ofstream keywrdFile("vpdKeywrd.bin");
-    keywrdFile.open(keywrdFilePath, std::ios::out | std::ofstream::binary);
+    std::ofstream keywrdFile(keywrdFilePath);
+    auto fd = open(keywrdFilePath, std::ios::out | std::ofstream::binary);
     if (!keywrdFile)
     {
         std::cerr << "VPD keyword file open error: " << keywrdFilePath
@@ -74,6 +74,26 @@
             pldm::PelSeverity::ERROR);
         return PLDM_ERROR;
     }
+
+    if (offset > keywrdSize)
+    {
+        std::cerr << "Offset exceeds file size, OFFSET=" << offset
+                  << " FILE_SIZE=" << keywrdSize << std::endl;
+        return PLDM_DATA_OUT_OF_RANGE;
+    }
+
+    // length of keyword data should be same as keyword data size in dbus object
+    length = static_cast<uint32_t>(keywrdSize) - offset;
+
+    auto returnCode = lseek(fd, offset, SEEK_SET);
+    if (returnCode == -1)
+    {
+        std::cerr
+            << "Could not find keyword data at given offset. File Seek failed"
+            << std::endl;
+        return PLDM_ERROR;
+    }
+
     keywrdFile.write((const char*)std::get<std::vector<byte>>(keywrd).data(),
                      keywrdSize);
     if (keywrdFile.bad())
@@ -83,9 +103,6 @@
     }
     keywrdFile.close();
 
-    // length of keyword data should be same as keyword data size in dbus object
-    length = (uint32_t)keywrdSize;
-
     auto rc = readFile(keywrdFilePath, offset, keywrdSize, response);
     fs::remove(keywrdFilePath);
     if (rc)