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