static handler: transition to generic file handler
The static layout image handler simply writes the bytes into an expected
location and the verification mechanism is responsible for doing things
with the file there. Therefore, use a file handler.
Change-Id: I97128fb04df640fe682c77ef7ee278c61293748f
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/Makefile.am b/Makefile.am
index bd7e932..7d94589 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -5,7 +5,6 @@
libfirmwareblob_la_SOURCES = \
main.cpp \
firmware_handler.cpp \
- static_handler.cpp \
lpc_handler.cpp \
pci_handler.cpp \
file_handler.cpp
diff --git a/main.cpp b/main.cpp
index 9540890..7daceb9 100644
--- a/main.cpp
+++ b/main.cpp
@@ -5,7 +5,6 @@
#include "image_handler.hpp"
#include "lpc_handler.hpp"
#include "pci_handler.hpp"
-#include "static_handler.hpp"
#include <blobs-ipmid/manager.hpp>
#include <cstdint>
@@ -17,7 +16,7 @@
namespace
{
FileHandler hashHandler(HASH_FILENAME);
-StaticLayoutHandler staticLayoutHandler(STATIC_HANDLER_STAGED_NAME);
+FileHandler staticLayoutHandler(STATIC_HANDLER_STAGED_NAME);
#ifdef ENABLE_LPC_BRIDGE
#if defined(ASPEED_LPC)
diff --git a/static_handler.cpp b/static_handler.cpp
deleted file mode 100644
index 616a882..0000000
--- a/static_handler.cpp
+++ /dev/null
@@ -1,75 +0,0 @@
-#include "static_handler.hpp"
-
-#include <cstdint>
-#include <memory>
-#include <string>
-#include <vector>
-
-namespace blobs
-{
-
-bool StaticLayoutHandler::open(const std::string& path)
-{
- this->path = path;
-
- if (stagedOutput.is_open())
- {
- /* This wasn't properly closed somehow.
- * TODO: Throw an error or just reset the state?
- */
- return false;
- }
-
- /* using ofstream no need to set out */
- stagedOutput.open(stagedFilename, std::ios::binary);
- if (stagedOutput.bad())
- {
- /* TODO: Oh no! Care about this. */
- return false;
- }
-
- /* We were able to open the file for staging.
- * TODO: We'll need to do other stuff to eventually.
- */
- return true;
-}
-
-void StaticLayoutHandler::close()
-{
- if (stagedOutput.is_open())
- {
- stagedOutput.close();
- }
- return;
-}
-
-bool StaticLayoutHandler::write(std::uint32_t offset,
- const std::vector<std::uint8_t>& data)
-{
- if (!stagedOutput.is_open())
- {
- return false;
- }
-
- /* We could track this, but if they write in a scattered method, this is
- * easier.
- */
- stagedOutput.seekp(offset, std::ios_base::beg);
- if (!stagedOutput.good())
- {
- /* the documentation wasn't super clear on fail vs bad in these cases,
- * so let's only be happy with goodness.
- */
- return false;
- }
-
- stagedOutput.write(reinterpret_cast<const char*>(data.data()), data.size());
- if (!stagedOutput.good())
- {
- return false;
- }
-
- return true;
-}
-
-} // namespace blobs
diff --git a/static_handler.hpp b/static_handler.hpp
deleted file mode 100644
index 35d40fb..0000000
--- a/static_handler.hpp
+++ /dev/null
@@ -1,46 +0,0 @@
-#pragma once
-
-#include "image_handler.hpp"
-
-#include <cstdint>
-#include <fstream>
-#include <memory>
-#include <string>
-#include <vector>
-
-namespace blobs
-{
-
-class StaticLayoutHandler : public ImageHandlerInterface
-{
- public:
- /**
- * Create a StaticLayoutHandler. This appears to be a basic file writer, if
- * it doesn't end up doing more, then it will be converted into such.
- *
- * @param[in] temporaryName - where to write the firmware image while it's
- * being uploaded.
- */
- explicit StaticLayoutHandler(const std::string& temporaryName) :
- stagedFilename(temporaryName){};
-
- /* Destroying the ofstream closes it first. */
- ~StaticLayoutHandler() = default;
-
- /* Opens the stagedFilename. */
- bool open(const std::string& path) override;
- void close() override;
- bool write(std::uint32_t offset,
- const std::vector<std::uint8_t>& data) override;
-
- private:
- std::string path;
-
- /** The staged output file stream object. */
- std::ofstream stagedOutput;
-
- /** The file to use for staging the bytes. */
- std::string stagedFilename;
-};
-
-} // namespace blobs
diff --git a/test/Makefile.am b/test/Makefile.am
index f5bf4de..d7ca5a0 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -18,7 +18,7 @@
firmware_writemeta_unittest \
firmware_close_unittest \
firmware_delete_unittest \
- static_handler_unittest
+ file_handler_unittest
TESTS = $(check_PROGRAMS)
@@ -46,5 +46,5 @@
firmware_delete_unittest_SOURCES = firmware_delete_unittest.cpp
firmware_delete_unittest_LDADD = $(top_builddir)/firmware_handler.o
-static_handler_unittest_SOURCES = static_handler_unittest.cpp
-static_handler_unittest_LDADD = $(top_builddir)/static_handler.o
+file_handler_unittest_SOURCES = file_handler_unittest.cpp
+file_handler_unittest_LDADD = $(top_builddir)/file_handler.o
diff --git a/test/static_handler_unittest.cpp b/test/file_handler_unittest.cpp
similarity index 80%
rename from test/static_handler_unittest.cpp
rename to test/file_handler_unittest.cpp
index 4fda33d..26a490d 100644
--- a/test/static_handler_unittest.cpp
+++ b/test/file_handler_unittest.cpp
@@ -1,4 +1,4 @@
-#include "static_handler.hpp"
+#include "file_handler.hpp"
#include <cstdint>
#include <cstdio>
@@ -12,7 +12,7 @@
static constexpr auto TESTPATH = "test.output";
-class StaticHandlerOpenTest : public ::testing::Test
+class FileHandlerOpenTest : public ::testing::Test
{
protected:
void TearDown() override
@@ -21,23 +21,23 @@
}
};
-TEST_F(StaticHandlerOpenTest, VerifyItIsHappy)
+TEST_F(FileHandlerOpenTest, VerifyItIsHappy)
{
/* Opening a fail may create it? */
- StaticLayoutHandler handler(TESTPATH);
+ FileHandler handler(TESTPATH);
EXPECT_TRUE(handler.open(""));
/* Calling open twice fails the second time. */
EXPECT_FALSE(handler.open(""));
}
-TEST_F(StaticHandlerOpenTest, VerifyWriteDataWrites)
+TEST_F(FileHandlerOpenTest, VerifyWriteDataWrites)
{
/* Verify writing bytes writes them... flushing data can be an issue here,
* so we close first.
*/
- StaticLayoutHandler handler(TESTPATH);
+ FileHandler handler(TESTPATH);
EXPECT_TRUE(handler.open(""));
std::vector<std::uint8_t> bytes = {0x01, 0x02};