Skip to content
API Documentation
BastionGuard™
DiskReader Class Reference

#include <disk_reader.hpp>

Public Member Functions

 DiskReader ()
 ~DiskReader ()
bool open_readonly (const std::string &path, std::string &error)
void close ()
bool is_open () const
bool is_block_device () const
uint64_t size () const
uint32_t logical_sector_size () const
const std::string & path () const
bool read_at (uint64_t offset, void *buffer, size_t len)
std::vector< std::byte > read_block (uint64_t offset, size_t len)

Private Attributes

int fd_
bool is_block_device_
uint64_t size_
uint32_t logical_sector_size_
std::string path_

Detailed Description

Definition at line 8 of file disk_reader.hpp.

Constructor & Destructor Documentation

◆ DiskReader()

DiskReader::DiskReader ( )

Definition at line 17 of file disk_reader.cpp.

18: fd_(-1),
19is_block_device_(false),
20size_(0),
bool is_block_device_
uint64_t size_
uint32_t logical_sector_size_

References fd_, is_block_device_, logical_sector_size_, and size_.

◆ ~DiskReader()

DiskReader::~DiskReader ( )

Definition at line 23 of file disk_reader.cpp.

23 {
24 close();
25}

References close().

Member Function Documentation

◆ close()

void DiskReader::close ( )

Definition at line 27 of file disk_reader.cpp.

27 {
28 if (fd_ >= 0) {
29 ::close(fd_);
30 fd_ = -1;
31 }
32 is_block_device_ = false;
33 size_ = 0;
35 path_.clear();
36}
std::string path_

References close(), fd_, is_block_device_, logical_sector_size_, path_, and size_.

Referenced by close(), FaultTolerantReader::close(), open_readonly(), and ~DiskReader().

◆ is_block_device()

bool DiskReader::is_block_device ( ) const

Definition at line 92 of file disk_reader.cpp.

92 {
93 return is_block_device_;
94}

References is_block_device_.

◆ is_open()

bool DiskReader::is_open ( ) const

Definition at line 88 of file disk_reader.cpp.

88 {
89 return fd_ >= 0;
90}

References fd_.

Referenced by FaultTolerantReader::is_open().

◆ logical_sector_size()

uint32_t DiskReader::logical_sector_size ( ) const

◆ open_readonly()

bool DiskReader::open_readonly ( const std::string & path,
std::string & error )

Definition at line 38 of file disk_reader.cpp.

38 {
39 close();
40
41 struct stat st {};
42 if (::stat(path.c_str(), &st) != 0) {
43 error = std::string("stat failed: ") + std::strerror(errno);
44 return false;
45 }
46
47 if (!S_ISREG(st.st_mode) && !S_ISBLK(st.st_mode)) {
48 error = trs("source is neither a regular file nor a block device");
49 return false;
50 }
51
52 fd_ = ::open(path.c_str(), O_RDONLY | O_CLOEXEC);
53 if (fd_ < 0) {
54 error = std::string("open failed: ") + std::strerror(errno);
55 return false;
56 }
57
58 path_ = path;
59 is_block_device_ = S_ISBLK(st.st_mode);
60
61 if (is_block_device_) {
62 #ifdef __linux__
63 unsigned long long bytes = 0;
64 if (::ioctl(fd_, BLKGETSIZE64, &bytes) != 0) {
65 error = std::string("BLKGETSIZE64 failed: ") + std::strerror(errno);
66 close();
67 return false;
68 }
69 size_ = static_cast<uint64_t>(bytes);
70
71 unsigned int sector = 512;
72 if (::ioctl(fd_, BLKSSZGET, &sector) == 0 && sector > 0) {
73 logical_sector_size_ = sector;
74 }
75 #else
76 error = trs("block-device support is Linux-only in this build");
77 close();
78 return false;
79 #endif
80 } else {
81 size_ = static_cast<uint64_t>(st.st_size);
83 }
84
85 return true;
86}
const std::string & path() const
std::string trs(const char *msgid)
Definition i18n.cpp:22

References close(), fd_, is_block_device_, logical_sector_size_, path(), path_, size_, and trs().

Referenced by RecoveryExporter::export_results_and_carve(), FaultTolerantReader::open(), DeviceHealthProbe::probe(), and ScannerEngine::run_scan().

◆ path()

const std::string & DiskReader::path ( ) const

Definition at line 104 of file disk_reader.cpp.

104 {
105 return path_;
106}

References path_.

Referenced by open_readonly(), and FaultTolerantReader::path().

◆ read_at()

bool DiskReader::read_at ( uint64_t offset,
void * buffer,
size_t len )

Definition at line 108 of file disk_reader.cpp.

108 {
109 if (fd_ < 0 || buffer == nullptr) return false;
110 if (len == 0) return true;
111 // Bug #1 fix: avoid uint64_t overflow on offset + len
112 if (offset > size_ || len > size_ - offset) return false;
113
114 auto* ptr = static_cast<unsigned char*>(buffer);
115 size_t done = 0;
116
117 while (done < len) {
118 const auto n = ::pread(fd_, ptr + done, len - done, static_cast<off_t>(offset + done));
119 if (n <= 0) {
120 return false;
121 }
122 done += static_cast<size_t>(n);
123 }
124
125 return true;
126}

References fd_, and size_.

Referenced by NtfsProbe::analyze(), anonymous_namespace{filesystem_probes.cpp}::btrfs_has_magic(), anonymous_namespace{filesystem_probes.cpp}::btrfs_probe(), anonymous_namespace{lost_partition_finder.cpp}::btrfs_size_bytes(), anonymous_namespace{ext4_recovery.cpp}::Ext4RecoveryImpl::build_journal_replay_map(), anonymous_namespace{file_carver.cpp}::carve_one_range(), anonymous_namespace{xfs_recovery.cpp}::collect_dir_entries_block_or_leaf(), anonymous_namespace{file_carver.cpp}::copy_range(), anonymous_namespace{ext4_recovery.cpp}::Ext4RecoveryImpl::export_journal(), anonymous_namespace{recovery_exporter.cpp}::export_range(), anonymous_namespace{filesystem_probes.cpp}::ext4_has_magic(), anonymous_namespace{filesystem_probes.cpp}::ext4_probe(), anonymous_namespace{lost_partition_finder.cpp}::ext4_size_bytes(), LostPartitionFinder::find(), anonymous_namespace{filesystem_probes.cpp}::hfs_probe(), anonymous_namespace{lost_partition_finder.cpp}::hfs_size_bytes(), anonymous_namespace{filesystem_recovery.cpp}::Fat32Recovery::load_bpb(), anonymous_namespace{ext4_recovery.cpp}::Ext4RecoveryImpl::load_superblock(), anonymous_namespace{filesystem_recovery.cpp}::ExfatRecovery::load_vbr(), anonymous_namespace{lost_partition_finder.cpp}::looks_btrfs(), anonymous_namespace{signature_scanner.cpp}::looks_btrfs(), anonymous_namespace{lost_partition_finder.cpp}::looks_ext4(), anonymous_namespace{signature_scanner.cpp}::looks_ext4(), anonymous_namespace{lost_partition_finder.cpp}::looks_hfs(), anonymous_namespace{lost_partition_finder.cpp}::looks_xfs(), anonymous_namespace{filesystem_recovery.cpp}::ExfatRecovery::next_cluster(), anonymous_namespace{filesystem_recovery.cpp}::Fat32Recovery::next_cluster(), anonymous_namespace{partition_parser.cpp}::parse_gpt_table(), PartitionParser::parse_mbr(), anonymous_namespace{encryption_support.cpp}::probe_bitlocker(), anonymous_namespace{encryption_support.cpp}::probe_luks(), FaultTolerantReader::read_at(), read_block(), NtfsRecoveryImpl::read_boot_sector(), anonymous_namespace{encrypted_volume_support.cpp}::read_bytes(), anonymous_namespace{filesystem_recovery.cpp}::ExfatRecovery::read_cluster(), anonymous_namespace{filesystem_recovery.cpp}::Fat32Recovery::read_cluster(), anonymous_namespace{xfs_recovery.cpp}::read_dinode(), anonymous_namespace{hfs_recovery.cpp}::read_exact(), anonymous_namespace{xfs_recovery.cpp}::read_exact(), anonymous_namespace{xfs_recovery.cpp}::read_extents_data(), hfs::read_fork_block_inline(), hfs::read_fork_inline(), anonymous_namespace{ext4_recovery.cpp}::Ext4RecoveryImpl::read_fs_block(), NtfsRecoveryImpl::read_indx_block(), BtrfsRecoveryImpl::read_logical(), NtfsRecoveryImpl::read_mft_record_from_mirror(), hfs::anonymous_namespace{hfs_fork_io.cpp}::read_one_extent(), NtfsRecoveryImpl::read_raw_record(), FaultTolerantReader::read_rescue_recursive(), NtfsRecoveryImpl::read_runs_to_buffer(), anonymous_namespace{filesystem_probes.cpp}::read_sector(), BtrfsRecoveryImpl::reconstruct_raid56_read(), BtrfsRecoveryImpl::recover(), XfsRecovery::recover(), carving::run_carving(), SignatureScanner::scan(), anonymous_namespace{device_health_probe.cpp}::take_sample(), xfs::anonymous_namespace{xfs_btree.cpp}::walk_bmbt_block(), xfs::anonymous_namespace{xfs_btree.cpp}::walk_ibt_block(), anonymous_namespace{encryption_support.cpp}::write_bytes(), anonymous_namespace{filesystem_recovery.cpp}::write_data_to_file(), anonymous_namespace{xfs_recovery.cpp}::write_extents_to_path(), anonymous_namespace{filesystem_probes.cpp}::xfs_probe(), and anonymous_namespace{lost_partition_finder.cpp}::xfs_size_bytes().

◆ read_block()

std::vector< std::byte > DiskReader::read_block ( uint64_t offset,
size_t len )

Definition at line 128 of file disk_reader.cpp.

128 {
129 std::vector<std::byte> out(len);
130 if (!read_at(offset, out.data(), len)) {
131 out.clear();
132 }
133 return out;
134}
bool read_at(uint64_t offset, void *buffer, size_t len)

References read_at().

◆ size()

Member Data Documentation

◆ fd_

int DiskReader::fd_
private

Definition at line 26 of file disk_reader.hpp.

Referenced by close(), DiskReader(), is_open(), open_readonly(), and read_at().

◆ is_block_device_

bool DiskReader::is_block_device_
private

Definition at line 27 of file disk_reader.hpp.

Referenced by close(), DiskReader(), is_block_device(), and open_readonly().

◆ logical_sector_size_

uint32_t DiskReader::logical_sector_size_
private

Definition at line 29 of file disk_reader.hpp.

Referenced by close(), DiskReader(), logical_sector_size(), and open_readonly().

◆ path_

std::string DiskReader::path_
private

Definition at line 30 of file disk_reader.hpp.

Referenced by close(), open_readonly(), and path().

◆ size_

uint64_t DiskReader::size_
private

Definition at line 28 of file disk_reader.hpp.

Referenced by close(), DiskReader(), open_readonly(), read_at(), and size().


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