pmt: Improve libpartition_map

- Add new operators to Map_t and builder.
 - Some improvements.
This commit is contained in:
2025-08-31 15:36:26 +03:00
parent d19343d644
commit 025ccf3acb
3 changed files with 75 additions and 3 deletions

View File

@@ -28,6 +28,7 @@
#include <string_view>
#include <unordered_map>
#include <utility> // for std::pair
#include <tuple>
namespace PartitionMap {
struct _entry {
@@ -62,6 +63,7 @@ public:
basic_partition_map(const std::string &name, uint64_t size, bool logical);
basic_partition_map(const basic_partition_map &other);
basic_partition_map(basic_partition_map&& other) noexcept;
basic_partition_map();
~basic_partition_map();
@@ -82,6 +84,9 @@ public:
bool operator==(const basic_partition_map &other) const;
bool operator!=(const basic_partition_map &other) const;
operator std::vector<std::tuple<std::string, uint64_t, bool>>() const;
operator int() const;
class iterator {
public:
_entry *ptr;
@@ -149,6 +154,11 @@ public:
*/
explicit basic_partition_map_builder(std::string_view path);
/**
* Move constructor
*/
basic_partition_map_builder(basic_partition_map_builder &&other) noexcept;
/**
* Returns the current list content in Map_t type.
* If no list is created, returns std::nullopt.
@@ -334,6 +344,21 @@ public:
* Get constant Map_t object reference
*/
const Map_t &operator*() const;
/**
* Get map contents as vector (std::tuple type).
*/
[[nodiscard]] operator std::vector<std::tuple<std::string, uint64_t, bool>>() const;
/**
* Get total partition count in map (int type).
*/
[[nodiscard]] operator int() const;
/**
* Get current working directory.
*/
[[nodiscard]] operator std::string() const;
};
using Error = Helper::Error;
@@ -385,4 +410,10 @@ std::string formatMagic(uint64_t magic);
#define MAP "libpartition_map"
#define T_NAME 0
#define T_TYPE 1
#define T_SIZE 2
#define T_VEC_DECL_TYPE \
std::vector<std::tuple<std::string, uint64_t, bool>>
#endif // #ifndef LIBPARTITION_MAP_LIB_HPP

View File

@@ -87,6 +87,8 @@ void basic_partition_map_builder::_insert_logicals(Map_t &&logicals) {
<< "merging created logical partition list to this object's variable."
<< std::endl;
_current_map.merge(logicals);
LOGN(MAP, INFO) << "Cleaning created logical partition because not need more." << std::endl;
logicals.clear();
}
void basic_partition_map_builder::_map_build_check() const {
@@ -159,6 +161,14 @@ basic_partition_map_builder::basic_partition_map_builder(
_map_builded = true;
}
basic_partition_map_builder::basic_partition_map_builder(basic_partition_map_builder&& other) noexcept {
_current_map = Map_t(std::move(other._current_map));
_workdir = std::move(other._workdir);
_any_generating_error = other._any_generating_error;
_map_builded = other._map_builded;
other.clear();
}
bool basic_partition_map_builder::hasPartition(
const std::string_view name) const {
_map_build_check();
@@ -191,7 +201,7 @@ bool basic_partition_map_builder::readDirectory(const std::string_view path) {
throw Error("Cannot find directory: %s. Cannot build partition map!",
path.data());
LOGN(MAP, INFO) << "read " << path << " successfull." << std::endl;
LOGN(MAP, INFO) << "read " << path << " successfully." << std::endl;
_insert_logicals(_build_map("/dev/block/mapper", true));
_map_builded = true;
return true;
@@ -218,7 +228,7 @@ bool basic_partition_map_builder::readDefaultDirectories() {
LOGN(MAP, ERROR) << "Cannot build map by any default search entry."
<< std::endl;
LOGN(MAP, INFO) << "read default directories successfull." << std::endl;
LOGN(MAP, INFO) << "read default directories successfully." << std::endl;
_insert_logicals(_build_map("/dev/block/mapper", true));
_map_builded = true;
return true;
@@ -376,5 +386,17 @@ const Map_t &basic_partition_map_builder::operator*() const {
return _current_map;
}
basic_partition_map_builder::operator std::vector<std::tuple<std::string, uint64_t, bool>>() const {
return _current_map;
}
basic_partition_map_builder::operator int() const {
return _current_map;
}
basic_partition_map_builder::operator std::string() const {
return _workdir;
}
std::string getLibVersion() { MKVERSION("libpartition_map"); }
} // namespace PartitionMap

View File

@@ -116,6 +116,13 @@ basic_partition_map::basic_partition_map(const basic_partition_map &other)
std::copy(other._data, other._data + _count, _data);
}
basic_partition_map::basic_partition_map(basic_partition_map&& other) noexcept
: _data(new _entry[other._capacity]), _count(other._count),
_capacity(other._capacity) {
std::copy(other._data, other._data + _count, _data);
other.clear();
}
basic_partition_map::basic_partition_map() : _capacity(6) {
_data = new _entry[_capacity];
}
@@ -183,7 +190,7 @@ bool basic_partition_map::empty() const {
}
void basic_partition_map::clear() {
LOGN(MAP, INFO) << "map clean requested. Map is empty now." << std::endl;
LOGN(MAP, INFO) << "map clean requested. Cleaning..." << std::endl;
delete[] _data;
_count = 0;
_capacity = 6;
@@ -222,6 +229,18 @@ bool basic_partition_map::operator!=(const basic_partition_map &other) const {
return !(*this == other);
}
basic_partition_map::operator std::vector<std::tuple<std::string, uint64_t, bool>>() const {
std::vector<std::tuple<std::string, uint64_t, bool>> v;
if (_count == 0) return {};
for (size_t i = 0; i < _count; i++)
v.emplace_back(_data[i].name, _data[i].props.size, _data[i].props.isLogical);
return v;
}
basic_partition_map::operator int() const{
return static_cast<int>(_count);
}
basic_partition_map::iterator basic_partition_map::begin() const {
return iterator(_data);
}