John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "erase.hpp" |
John Edward Broadbent | a6e3b99 | 2022-03-17 14:33:15 -0700 | [diff] [blame] | 4 | #include "util.hpp" |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 5 | |
| 6 | #include <stdplus/fd/create.hpp> |
| 7 | #include <stdplus/fd/managed.hpp> |
| 8 | |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 9 | #include <chrono> |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 10 | #include <span> |
| 11 | #include <string> |
| 12 | |
John Edward Broadbent | d3bfa7b | 2022-01-13 17:41:32 -0800 | [diff] [blame] | 13 | namespace estoraged |
| 14 | { |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 15 | using stdplus::fd::Fd; |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 16 | |
| 17 | class Pattern : public Erase |
| 18 | { |
| 19 | public: |
| 20 | /** @brief Creates a pattern erase object. |
| 21 | * |
| 22 | * @param[in] inDevPath - the linux device path for the block device. |
| 23 | */ |
| 24 | Pattern(std::string_view inDevPath) : Erase(inDevPath) |
| 25 | {} |
| 26 | |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 27 | /** @brief writes an uncompressible random pattern to the drive, using |
| 28 | * default parameters. It also throws errors accordingly. |
| 29 | */ |
| 30 | void writePattern() |
| 31 | { |
| 32 | stdplus::fd::Fd&& fd = |
| 33 | stdplus::fd::open(devPath, stdplus::fd::OpenAccess::WriteOnly); |
| 34 | writePattern(util::findSizeOfBlockDevice(devPath), fd); |
| 35 | } |
| 36 | |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 37 | /** @brief writes an uncompressible random pattern to the drive |
| 38 | * and throws errors accordingly. |
| 39 | * |
| 40 | * @param[in] bytes - Size of the block device |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 41 | * @param[in] fd - the stdplus file descriptor |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 42 | */ |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 43 | void writePattern(uint64_t driveSize, Fd& fd); |
John Edward Broadbent | a6e3b99 | 2022-03-17 14:33:15 -0700 | [diff] [blame] | 44 | |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 45 | /** @brief verifies the uncompressible random pattern is on the drive, using |
| 46 | * default parameters. It also throws errors accordingly. |
| 47 | */ |
| 48 | void verifyPattern() |
| 49 | { |
| 50 | stdplus::fd::Fd&& fd = |
| 51 | stdplus::fd::open(devPath, stdplus::fd::OpenAccess::ReadOnly); |
| 52 | verifyPattern(util::findSizeOfBlockDevice(devPath), fd); |
| 53 | } |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 54 | |
| 55 | /** @brief verifies the uncompressible random pattern is on the drive |
| 56 | * and throws errors accordingly. |
| 57 | * |
| 58 | * @param[in] bytes - Size of the block device |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 59 | * @param[in] fd - the stdplus file descriptor |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 60 | */ |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 61 | void verifyPattern(uint64_t driveSize, Fd& fd); |
John Edward Broadbent | a6e3b99 | 2022-03-17 14:33:15 -0700 | [diff] [blame] | 62 | |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 63 | private: |
| 64 | static constexpr uint32_t seed = 0x6a656272; |
| 65 | static constexpr size_t blockSize = 4096; |
| 66 | static constexpr size_t blockSizeUsing32 = blockSize / sizeof(uint32_t); |
| 67 | static constexpr size_t maxRetry = 32; |
| 68 | static constexpr std::chrono::duration delay = std::chrono::milliseconds(1); |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 69 | }; |
John Edward Broadbent | d3bfa7b | 2022-01-13 17:41:32 -0800 | [diff] [blame] | 70 | |
| 71 | } // namespace estoraged |