Coverage Report

Created: 2026-10-16 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/estoraged/include/pattern.hpp
Line
Count
Source
1
#pragma once
2
3
#include "erase.hpp"
4
#include "util.hpp"
5
6
#include <stdplus/fd/create.hpp>
7
#include <stdplus/fd/managed.hpp>
8
9
#include <chrono>
10
#include <span>
11
#include <string>
12
13
namespace estoraged
14
{
15
using stdplus::fd::Fd;
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
113
    Pattern(std::string_view inDevPath) : Erase(inDevPath) {}
25
26
    /** @brief writes an incompressible random pattern to the drive, using
27
     * default parameters. It also throws errors accordingly.
28
     */
29
    void writePattern()
30
0
    {
31
0
        stdplus::fd::ManagedFd fd =
32
0
            stdplus::fd::open(devPath, stdplus::fd::OpenAccess::WriteOnly);
33
0
        writePattern(util::findSizeOfBlockDevice(devPath), fd);
34
0
        /* Make sure that the data reached the medium and report errors. */
35
0
        util::syncBlockDevice(fd.get(), devPath);
36
0
        /* Drop the written data from the page cache. */
37
0
        util::dropBlockDeviceCache(fd.get(), devPath);
38
0
    }
39
40
    /** @brief writes an incompressible random pattern to the drive
41
     * and throws errors accordingly.
42
     *
43
     *  @param[in] bytes - Size of the block device
44
     *  @param[in] fd - the stdplus file descriptor
45
     */
46
    void writePattern(uint64_t driveSize, Fd& fd);
47
48
    /** @brief verifies the incompressible random pattern is on the drive, using
49
     * default parameters. It also throws errors accordingly.
50
     */
51
    void verifyPattern()
52
0
    {
53
0
        stdplus::fd::ManagedFd fd =
54
0
            stdplus::fd::open(devPath, stdplus::fd::OpenAccess::ReadOnly);
55
0
        verifyPattern(util::findSizeOfBlockDevice(devPath), fd);
56
0
    }
57
58
    /** @brief verifies the incompressible random pattern is on the drive
59
     * and throws errors accordingly.
60
     *
61
     *  @param[in] bytes - Size of the block device
62
     *  @param[in] fd - the stdplus file descriptor
63
     */
64
    void verifyPattern(uint64_t driveSize, Fd& fd);
65
66
  private:
67
    static constexpr uint32_t seed = 0x6a656272;
68
    static constexpr size_t blockSize = 4096;
69
    static constexpr size_t blockSizeUsing32 = blockSize / sizeof(uint32_t);
70
    static constexpr size_t maxRetry = 32;
71
    static constexpr std::chrono::duration delay = std::chrono::milliseconds(1);
72
};
73
74
} // namespace estoraged