John Edward Broadbent | 4bc8a10 | 2021-12-30 16:11:49 -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 | 4bc8a10 | 2021-12-30 16:11:49 -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> |
| 10 | |
John Edward Broadbent | 4bc8a10 | 2021-12-30 16:11:49 -0800 | [diff] [blame] | 11 | namespace estoraged |
| 12 | { |
| 13 | |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 14 | using stdplus::fd::Fd; |
John Edward Broadbent | 4bc8a10 | 2021-12-30 16:11:49 -0800 | [diff] [blame] | 15 | |
| 16 | class Zero : public Erase |
| 17 | { |
| 18 | public: |
| 19 | /** @brief Creates a zero erase object. |
| 20 | * |
| 21 | * @param[in] inDevPath - the linux device path for the block device. |
| 22 | */ |
Patrick Williams | 04c28fa | 2023-05-10 07:51:24 -0500 | [diff] [blame] | 23 | Zero(std::string_view inDevPath) : Erase(inDevPath) {} |
John Edward Broadbent | 4bc8a10 | 2021-12-30 16:11:49 -0800 | [diff] [blame] | 24 | /** @brief writes zero to the drive |
| 25 | * and throws errors accordingly. |
John Edward Broadbent | a6e3b99 | 2022-03-17 14:33:15 -0700 | [diff] [blame] | 26 | * @param[in] driveSize - the size of the block device in bytes |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 27 | * @param[in] fd - the stdplus file descriptor |
John Edward Broadbent | 4bc8a10 | 2021-12-30 16:11:49 -0800 | [diff] [blame] | 28 | */ |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 29 | void writeZero(uint64_t driveSize, Fd& fd); |
John Edward Broadbent | 4bc8a10 | 2021-12-30 16:11:49 -0800 | [diff] [blame] | 30 | |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 31 | /** @brief writes zero to the drive using default parameters, |
John Edward Broadbent | a6e3b99 | 2022-03-17 14:33:15 -0700 | [diff] [blame] | 32 | * and throws errors accordingly. |
| 33 | */ |
| 34 | void writeZero() |
| 35 | { |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 36 | stdplus::fd::Fd&& fd = |
| 37 | stdplus::fd::open(devPath, stdplus::fd::OpenAccess::WriteOnly); |
| 38 | writeZero(util::findSizeOfBlockDevice(devPath), fd); |
John Edward Broadbent | a6e3b99 | 2022-03-17 14:33:15 -0700 | [diff] [blame] | 39 | } |
| 40 | |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 41 | /** @brief verifies the drive has only zeros on it, |
John Edward Broadbent | 4bc8a10 | 2021-12-30 16:11:49 -0800 | [diff] [blame] | 42 | * and throws errors accordingly. |
John Edward Broadbent | a6e3b99 | 2022-03-17 14:33:15 -0700 | [diff] [blame] | 43 | * @param[in] driveSize - the size of the block device in bytes |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 44 | * @param[in] fd - the stdplus file descriptor |
John Edward Broadbent | 4bc8a10 | 2021-12-30 16:11:49 -0800 | [diff] [blame] | 45 | */ |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 46 | void verifyZero(uint64_t driveSize, Fd& fd); |
John Edward Broadbent | 4bc8a10 | 2021-12-30 16:11:49 -0800 | [diff] [blame] | 47 | |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 48 | /** @brief verifies the drive has only zeros on it, |
| 49 | * using the default parameters. It also throws errors accordingly. |
John Edward Broadbent | a6e3b99 | 2022-03-17 14:33:15 -0700 | [diff] [blame] | 50 | */ |
| 51 | void verifyZero() |
| 52 | { |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 53 | stdplus::fd::Fd&& fd = |
| 54 | stdplus::fd::open(devPath, stdplus::fd::OpenAccess::ReadOnly); |
| 55 | verifyZero(util::findSizeOfBlockDevice(devPath), fd); |
John Edward Broadbent | a6e3b99 | 2022-03-17 14:33:15 -0700 | [diff] [blame] | 56 | } |
| 57 | |
John Edward Broadbent | 4bc8a10 | 2021-12-30 16:11:49 -0800 | [diff] [blame] | 58 | private: |
| 59 | /* @brief the size of the blocks in bytes used for write and verify. |
| 60 | * 32768 was also tested. It had almost identical performance. |
| 61 | */ |
| 62 | static constexpr size_t blockSize = 4096; |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 63 | static constexpr size_t maxRetry = 32; |
| 64 | static constexpr std::chrono::duration delay = std::chrono::milliseconds(1); |
John Edward Broadbent | 4bc8a10 | 2021-12-30 16:11:49 -0800 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | } // namespace estoraged |