Refactor findsize into util away from erase
At first the size was only needed for erase, now it is needed for the
drives interface. The code needed to be refactored to make drive
interface implementation possible.
Signed-off-by: John Edward Broadbent <jebr@google.com>
Change-Id: I0a23ee23a0de3fd89a9e776b4854e8da9a8ff2e4
diff --git a/include/erase.hpp b/include/erase.hpp
index 33489ea..8acd3d8 100644
--- a/include/erase.hpp
+++ b/include/erase.hpp
@@ -15,11 +15,6 @@
Erase(std::string_view inDevPath) : devPath(inDevPath)
{}
- /** @brief finds the size of the linux block device in bytes
- * @return size of a block device using the devPath
- */
- uint64_t findSizeOfBlockDevice();
-
protected:
/* The linux path for the block device */
std::string devPath;
diff --git a/include/pattern.hpp b/include/pattern.hpp
index 69301e7..4def784 100644
--- a/include/pattern.hpp
+++ b/include/pattern.hpp
@@ -1,6 +1,7 @@
#pragma once
#include "erase.hpp"
+#include "util.hpp"
#include <stdplus/fd/create.hpp>
#include <stdplus/fd/managed.hpp>
@@ -27,6 +28,11 @@
*
* @param[in] bytes - Size of the block device
*/
+ void writePattern()
+ {
+ writePattern(util::Util::findSizeOfBlockDevice(devPath));
+ }
+
void writePattern(uint64_t driveSize);
/** @brief verifies the uncompressible random pattern is on the drive
@@ -34,6 +40,11 @@
*
* @param[in] bytes - Size of the block device
*/
+
+ void verifyPattern()
+ {
+ verifyPattern(util::Util::findSizeOfBlockDevice(devPath));
+ }
void verifyPattern(uint64_t driveSize);
};
diff --git a/include/util.hpp b/include/util.hpp
new file mode 100644
index 0000000..1d706ce
--- /dev/null
+++ b/include/util.hpp
@@ -0,0 +1,21 @@
+#pragma once
+#include <string_view>
+
+namespace estoraged
+{
+namespace util
+{
+
+class Util
+{
+ public:
+ /** @brief finds the size of the linux block device in bytes
+ * @param[in] devpath - the name of the linux block device
+ * @return size of a block device using the devPath
+ */
+ static uint64_t findSizeOfBlockDevice(const std::string& devPath);
+};
+
+} // namespace util
+
+} // namespace estoraged
diff --git a/include/verifyDriveGeometry.hpp b/include/verifyDriveGeometry.hpp
index 457297b..ea54998 100644
--- a/include/verifyDriveGeometry.hpp
+++ b/include/verifyDriveGeometry.hpp
@@ -1,6 +1,7 @@
#pragma once
#include "erase.hpp"
+#include "util.hpp"
#include <string_view>
@@ -22,6 +23,10 @@
*
* @param[in] bytes - Size of the block device
*/
+ void geometryOkay()
+ {
+ geometryOkay(util::Util::findSizeOfBlockDevice(devPath));
+ }
void geometryOkay(uint64_t bytes);
};
diff --git a/include/zero.hpp b/include/zero.hpp
index e8675e8..00c2b38 100644
--- a/include/zero.hpp
+++ b/include/zero.hpp
@@ -1,6 +1,7 @@
#pragma once
#include "erase.hpp"
+#include "util.hpp"
#include <stdplus/fd/create.hpp>
#include <stdplus/fd/managed.hpp>
@@ -19,21 +20,34 @@
*/
Zero(std::string_view inDevPath) : Erase(inDevPath)
{}
-
/** @brief writes zero to the drive
* and throws errors accordingly.
- *
- * @param[in] bytes - Size of the block device
+ * @param[in] driveSize - the size of the block device in bytes
*/
void writeZero(uint64_t driveSize);
+ /** @brief writes zero to the drive
+ * and throws errors accordingly.
+ */
+ void writeZero()
+ {
+ writeZero(util::Util::findSizeOfBlockDevice(devPath));
+ }
+
/** @brief verifies the uncompressible random pattern is on the drive
* and throws errors accordingly.
- *
- * @param[in] bytes - Size of the block device
+ * @param[in] driveSize - the size of the block device in bytes
*/
void verifyZero(uint64_t driveSize);
+ /** @brief verifies the uncompressible random pattern is on the drive
+ * and throws errors accordingly.
+ */
+ void verifyZero()
+ {
+ verifyZero(util::Util::findSizeOfBlockDevice(devPath));
+ }
+
private:
/* @brief the size of the blocks in bytes used for write and verify.
* 32768 was also tested. It had almost identical performance.
diff --git a/src/erase/meson.build b/src/erase/meson.build
index d5be948..233fc59 100644
--- a/src/erase/meson.build
+++ b/src/erase/meson.build
@@ -3,7 +3,6 @@
'verifyDriveGeometry.cpp',
'pattern.cpp',
'cryptoErase.cpp',
- 'erase.cpp',
'zero.cpp',
include_directories : eStoraged_headers,
implicit_include_directories: false,
diff --git a/src/estoraged.cpp b/src/estoraged.cpp
index b52a5f4..6285079 100644
--- a/src/estoraged.cpp
+++ b/src/estoraged.cpp
@@ -69,21 +69,19 @@
case EraseMethod::VerifyGeometry:
{
VerifyDriveGeometry myVerifyGeometry(devPath);
- uint64_t size = myVerifyGeometry.findSizeOfBlockDevice();
- myVerifyGeometry.geometryOkay(size);
+ myVerifyGeometry.geometryOkay();
break;
}
case EraseMethod::LogicalOverWrite:
{
Pattern myErasePattern(devPath);
- myErasePattern.writePattern(myErasePattern.findSizeOfBlockDevice());
+ myErasePattern.writePattern();
break;
}
case EraseMethod::LogicalVerify:
{
Pattern myErasePattern(devPath);
- myErasePattern.verifyPattern(
- myErasePattern.findSizeOfBlockDevice());
+ myErasePattern.verifyPattern();
break;
}
case EraseMethod::VendorSanitize:
@@ -93,13 +91,13 @@
case EraseMethod::ZeroOverWrite:
{
Zero myZero(devPath);
- myZero.writeZero(myZero.findSizeOfBlockDevice());
+ myZero.writeZero();
break;
}
case EraseMethod::ZeroVerify:
{
Zero myZero(devPath);
- myZero.verifyZero(myZero.findSizeOfBlockDevice());
+ myZero.verifyZero();
break;
}
case EraseMethod::SecuredLocked:
diff --git a/src/meson.build b/src/meson.build
index 4bb75a2..ed28e88 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -17,6 +17,7 @@
libeStoraged_lib = static_library(
'eStoraged-lib',
'estoraged.cpp',
+ 'util.cpp',
include_directories : eStoraged_headers,
implicit_include_directories: false,
dependencies: [libeStoraged_deps, libeStoragedErase_dep],
diff --git a/src/erase/erase.cpp b/src/util.cpp
similarity index 75%
rename from src/erase/erase.cpp
rename to src/util.cpp
index 40a954e..7200d0a 100644
--- a/src/erase/erase.cpp
+++ b/src/util.cpp
@@ -1,4 +1,4 @@
-#include "erase.hpp"
+#include "util.hpp"
#include <linux/fs.h>
@@ -8,13 +8,16 @@
#include <stdplus/handle/managed.hpp>
#include <xyz/openbmc_project/Common/error.hpp>
+#include <string_view>
+
namespace estoraged
{
+namespace util
+{
+using ::sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
+using ::stdplus::fd::ManagedFd;
-using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
-using stdplus::fd::ManagedFd;
-
-uint64_t Erase::findSizeOfBlockDevice()
+uint64_t Util::findSizeOfBlockDevice(const std::string& devPath)
{
ManagedFd fd;
uint64_t bytes = 0;
@@ -35,4 +38,6 @@
return bytes;
}
+} // namespace util
+
} // namespace estoraged