Skip to content
API Documentation
BastionGuard™
RescueImager Class Reference

#include <rescue_imager.hpp>

Public Types

using ProgressCallback = std::function<void(uint64_t, uint64_t)>
using CancelCallback = std::function<bool()>

Static Public Member Functions

static bool create_image (const std::string &device_path, const std::string &image_path, RescueImageReport &report, std::string &error, ProgressCallback progress_cb={}, CancelCallback cancel_cb={})

Detailed Description

Definition at line 20 of file rescue_imager.hpp.

Member Typedef Documentation

◆ CancelCallback

using RescueImager::CancelCallback = std::function<bool()>

Definition at line 23 of file rescue_imager.hpp.

◆ ProgressCallback

using RescueImager::ProgressCallback = std::function<void(uint64_t, uint64_t)>

Definition at line 22 of file rescue_imager.hpp.

Member Function Documentation

◆ create_image()

bool RescueImager::create_image ( const std::string & device_path,
const std::string & image_path,
RescueImageReport & report,
std::string & error,
ProgressCallback progress_cb = {},
CancelCallback cancel_cb = {} )
static

Definition at line 9 of file rescue_imager.cpp.

14 {
15 report = {};
16 report.image_path = image_path;
17 report.map_path = image_path + ".badblocks.txt";
18
19 FaultTolerantReader reader;
20 reader.set_cancel_callback(cancel_cb);
21 std::string open_error;
22 if (!reader.open(device_path, open_error)) {
23 error = open_error;
24 return false;
25 }
26
27 const uint64_t total = reader.size();
28 report.bytes_requested = total;
29
30 fs::create_directories(fs::path(image_path).parent_path());
31 std::ofstream out(image_path, std::ios::binary);
32 if (!out) {
33 error = "unable to create rescue image";
34 return false;
35 }
36
37 std::vector<unsigned char> buffer(1024 * 1024);
38 for (uint64_t offset = 0; offset < total; offset += buffer.size()) {
39 if (cancel_cb && cancel_cb()) {
40 error = "cancelled";
41 return false;
42 }
43
44 const size_t chunk = static_cast<size_t>(std::min<uint64_t>(buffer.size(), total - offset));
45 std::fill(buffer.begin(), buffer.begin() + static_cast<std::ptrdiff_t>(chunk), 0);
46 auto rr = reader.read_rescue(offset, buffer.data(), chunk);
47 out.write(reinterpret_cast<const char*>(buffer.data()), static_cast<std::streamsize>(chunk));
48 if (!out) {
49 error = "write error during rescue imaging";
50 return false;
51 }
52 report.bytes_written += rr.bytes_read;
53 if (progress_cb) progress_cb(offset + chunk, total);
54 }
55
56 std::ofstream map_out(report.map_path, std::ios::binary);
57 if (!map_out) {
58 error = "unable to create bad block map";
59 return false;
60 }
61
63 const auto bad = reader.bad_block_map().bad_ranges();
64 report.bad_ranges = bad.size();
65 for (const auto& range : bad) {
66 map_out << range.offset << '\t' << range.length << '\t' << "bad" << '\n';
67 }
68 for (const auto& range : reader.bad_block_map().slow_ranges()) {
69 map_out << range.offset << '\t' << range.length << '\t' << "slow" << '\n';
70 }
71
72 report.logs.push_back("Rescue image completed");
73 report.logs.push_back("Bytes written: " + std::to_string(report.bytes_written));
74 report.logs.push_back("Bad ranges: " + std::to_string(report.bad_ranges));
75 return true;
76}
std::vector< BlockRange > bad_ranges() const
void merge_adjacent()
std::vector< BlockRange > slow_ranges() const
bool open(const std::string &path, std::string &error)
void set_cancel_callback(CancelCallback cb)
ReadResult read_rescue(uint64_t offset, void *buffer, size_t size)
const BadBlockMap & bad_block_map() const
std::string image_path
std::vector< std::string > logs

References FaultTolerantReader::bad_block_map(), BadBlockMap::bad_ranges(), RescueImageReport::bad_ranges, RescueImageReport::bytes_requested, RescueImageReport::bytes_written, RescueImageReport::image_path, RescueImageReport::logs, RescueImageReport::map_path, BadBlockMap::merge_adjacent(), FaultTolerantReader::open(), FaultTolerantReader::read_rescue(), FaultTolerantReader::set_cancel_callback(), FaultTolerantReader::size(), and BadBlockMap::slow_ranges().

Referenced by RecoveryExporter::create_rescue_image().


The documentation for this class was generated from the following files: