pmt: ready for release

- Fix memory leaks
 - Writed functions and apply
 - Test pmt and verify stability
This commit is contained in:
2025-08-06 21:58:05 +03:00
parent 670f2bfad5
commit 6294482b39
29 changed files with 1033 additions and 502 deletions

View File

@@ -24,14 +24,18 @@ set(LIBPARTITION_MAP_SOURCES
# Add targets
add_library(partition_map_shared SHARED ${LIBPARTITION_MAP_SOURCES})
add_library(partition_map_static STATIC ${LIBPARTITION_MAP_SOURCES})
add_executable(libpartition_map_test tests/test.cpp)
# Set appropriate output names
set_target_properties(partition_map_shared PROPERTIES OUTPUT_NAME "partition_map")
set_target_properties(partition_map_static PROPERTIES OUTPUT_NAME "partition_map")
# Set linker flags
target_link_options(libpartition_map_test PRIVATE "LINKER:-rpath,/data/data/com.termux/files/usr/lib" "LINKER:-rpath,/data/local")
target_link_options(partition_map_shared PRIVATE "LINKER:-rpath,/data/data/com.termux/files/usr/lib")
target_link_libraries(libpartition_map_test PRIVATE partition_map_shared PRIVATE helper_shared)
target_link_libraries(partition_map_shared PRIVATE helper_shared)
# Build libpartition_map_test if CMAKE_BUILD_TYPE is not release
if(NOT "${CMAKE_BUILD_TYPE}" STREQUAL "Release")
add_executable(libpartition_map_test ${CMAKE_CURRENT_SOURCE_DIR}/tests/test.cpp)
target_link_libraries(libpartition_map_test PRIVATE partition_map_shared PRIVATE helper_shared)
target_link_options(libpartition_map_test PRIVATE "LINKER:-rpath,/data/data/com.termux/files/usr/lib")
endif()

View File

@@ -39,19 +39,17 @@ struct _entry {
};
/**
* basic_partition_map
* -------------------
* The main type of the library. The Builder class is designed
* to be easily manipulated and modified only on this class.
*/
class basic_partition_map {
private:
void _resize_map();
int _index_of(const std::string_view name) const;
[[nodiscard]] int _index_of(std::string_view name) const;
public:
_entry* _data;
size_t _count, _capacity;
size_t _count{}, _capacity{};
struct _returnable_entry {
uint64_t size;
@@ -60,20 +58,20 @@ public:
using BasicInf = _returnable_entry;
basic_partition_map(const std::string name, uint64_t size, bool logical);
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();
bool insert(const std::string name, uint64_t size, bool logical);
bool insert(const std::string& name, uint64_t size, bool logical);
void merge(const basic_partition_map& map);
uint64_t get_size(const std::string_view name) const;
bool is_logical(const std::string_view name) const;
_returnable_entry get_all(const std::string_view name) const;
bool find(const std::string_view name) const;
std::string find_(const std::string name) const;
size_t size() const;
bool empty() const;
[[nodiscard]] uint64_t get_size(std::string_view name) const;
[[nodiscard]] bool is_logical(std::string_view name) const;
[[nodiscard]] _returnable_entry get_all(std::string_view name) const;
[[nodiscard]] bool find(std::string_view name) const;
[[nodiscard]] std::string find_(const std::string& name) const;
[[nodiscard]] size_t size() const;
[[nodiscard]] bool empty() const;
void clear();
basic_partition_map& operator=(const basic_partition_map& map);
@@ -84,10 +82,10 @@ public:
public:
_entry* ptr;
iterator(_entry* p);
explicit iterator(_entry* p);
auto operator*() -> std::pair<std::string&, decltype(_entry::props)&>;
_entry* operator->();
auto operator*() const -> std::pair<std::string&, decltype(_entry::props)&>;
_entry* operator->() const;
iterator& operator++();
iterator operator++(int);
bool operator!=(const iterator& other) const;
@@ -98,7 +96,7 @@ public:
public:
const _entry* ptr;
constant_iterator(const _entry* p);
explicit constant_iterator(const _entry* p);
auto operator*() const -> std::pair<const std::string&, const decltype(_entry::props)&>;
const _entry* operator->() const;
@@ -109,13 +107,11 @@ public:
};
/* for-each support */
iterator begin();
iterator end();
[[nodiscard]] iterator begin() const;
[[nodiscard]] iterator end() const;
constant_iterator begin() const;
constant_iterator cbegin() const;
constant_iterator end() const;
constant_iterator cend() const;
[[nodiscard]] constant_iterator cbegin() const;
[[nodiscard]] constant_iterator cend() const;
};
using Map_t = basic_partition_map;
@@ -126,16 +122,14 @@ private:
std::string _workdir;
bool _any_generating_error, _map_builded;
bool _is_real_block_dir(const std::string_view path) const;
[[nodiscard]] static bool _is_real_block_dir(std::string_view path);
Map_t _build_map(std::string_view path, bool logical = false);
void _insert_logicals(Map_t&& logicals);
void _map_build_check() const;
uint64_t _get_size(const std::string path);
[[nodiscard]] uint64_t _get_size(const std::string& path);
public:
/**
* Default constructor
* -------------------
* By default, it searches the directories in the
* defaultEntryList in PartitionMap.cpp in order and
* uses the directory it finds.
@@ -143,189 +137,145 @@ public:
basic_partition_map_builder();
/**
* Secondary constructor
* ---------------------
* It has one arguments:
* - Directory path to search
* A constructor with input. Need search path
*/
basic_partition_map_builder(const std::string_view path);
explicit basic_partition_map_builder(std::string_view path);
/**
* getAll()
* --------
* Returns the current list content in Map_t type.
* If no list is created, returns std::nullopt.
*/
Map_t getAll() const;
[[nodiscard]] Map_t getAll() const;
/**
* get(name)
* ---------
* WARNING: Learn about std::optional before using this function.
*
* Returns information of a specific partition in
* Map_temp_t type. If the partition is not in the
* currently created list, returns std::nullopt.
*/
std::optional<std::pair<uint64_t, bool>> get(const std::string_view name) const;
[[nodiscard]] std::optional<std::pair<uint64_t, bool>> get(std::string_view name) const;
/**
* getLogicalPartitionList()
* -------------------------
* WARNING: Learn about std::optional before using this function.
*
* If there is a logical partition(s) in the created
* list, it returns a list of type std::list (containing
* data of type std::string). If there is no logical
* partition in the created list, it returns std::nullopt.
*/
std::optional<std::list<std::string>> getLogicalPartitionList() const;
[[nodiscard]] std::optional<std::list<std::string>> getLogicalPartitionList() const;
/**
* getPhysicalPartitionList()
* --------------------------
* WARNING: Learn about std::optional before using this function.
*
* The physical partitions in the created list are
* returned as std::list type. If there is no content
* due to any problem, returns std::nullopt.
*/
std::optional<std::list<std::string>> getPhysicalPartitionList() const;
[[nodiscard]] std::optional<std::list<std::string>> getPhysicalPartitionList() const;
/**
* The partitions in the created list are returned as std::list
* If there is no content due to any problem, returns std::nullopt
*/
[[nodiscard]] std::optional<std::list<std::string>> getPartitionList() const;
/**
* getRealLinkPathOf(name)
* -----------------------
* WARNING: Learn about std::optional before using this function.
*
* Returns the full link path of the entered partition
* name in the current search directory as std::string.
* If the partition is not in the list, an empty
* std::string is returned.
*/
std::string getRealLinkPathOf(const std::string_view name) const;
[[nodiscard]] std::string getRealLinkPathOf(std::string_view name) const;
/**
* getRealPathOf(name)
* -------------------
* WARNING: Learn about std::optional before using this function.
*
* Returns the actual path of the partition as
* std::string. Like /dev/block/sda5
*/
std::string getRealPathOf(const std::string_view name) const;
[[nodiscard]] std::string getRealPathOf(std::string_view name) const;
/**
* getCurrentWorkDir()
* -------------------
* WARNING: Learn about std::optional before using this function.
*
* If it exists, the path to the search string is
* returned as std::string. If it does not exist,
* an empty std::string is returned.
*/
std::string getCurrentWorkDir() const;
[[nodiscard]] std::string getCurrentWorkDir() const;
/**
* hasPartition(name)
* ------------------
* Returns whether the entered partition name is in the
* created partition list as a bool.
*/
bool hasPartition(const std::string_view name) const;
[[nodiscard]] bool hasPartition(std::string_view name) const;
/**
* isLogical(name)
* ---------------
* Returns the bool type status of whether the
* entered section name is marked as logical in the
* created list. Alternatively, the current section
* information can be retrieved with the Get() function
* entered partition name is marked as logical in the
* created list. Alternatively, the current partition
* information can be retrieved with the get() function
* and checked for logicality.
*/
bool isLogical(const std::string_view name) const;
[[nodiscard]] bool isLogical(std::string_view name) const;
/**
* clear()
* -------
* The created list and the current search index name are cleared.
*/
void clear();
/**
* readDirectory(path)
* -------------------
* The entered path is defined as the new search
* directory and the search is performed in the entered
* directory. If everything goes well, true is returned.
*/
bool readDirectory(const std::string_view path);
bool readDirectory(std::string_view path);
/**
* Reads default /dev entries and builds map.
*/
bool readDefaultDirectories();
/**
* empty()
* -------
* Whether the current list is empty or not is returned
* as bool type. If there is content in the list, true
* is returned, otherwise false is returned.
*/
bool empty() const;
[[nodiscard]] bool empty() const;
/**
* sizeOf(name)
* ------------
* WARNING: Learn about std::optional before using this function.
*
* If it exists, the size of the partition with the
* entered name is returned as uint64_t type.
* If it does not exist, 0 is returned.
*/
uint64_t sizeOf(const std::string_view name) const;
[[nodiscard]] uint64_t sizeOf(std::string_view name) const;
/**
* == operator
* -----------
* If the content lists of the two created objects are
* the same (checked only according to the partition
* names), true is returned, otherwise false is returned
*/
friend bool operator==(basic_partition_map_builder& lhs, basic_partition_map_builder& rhs);
friend bool operator==(const basic_partition_map_builder& lhs, const basic_partition_map_builder& rhs);
/**
* != operator
* -----------
* The opposite logic of the == operator.
*/
friend bool operator!=(basic_partition_map_builder& lhs, basic_partition_map_builder& rhs);
friend bool operator!=(const basic_partition_map_builder& lhs, const basic_partition_map_builder& rhs);
/**
* Boolean operator
* ----------------
* You can check whether the object was created
* successfully. If the problem did not occur, true is
* returned, if it did, false is returned.
*/
operator bool() const;
explicit operator bool() const;
/**
* ! operator
* ----------
* Returns true if the object creation failed (i.e., there's a problem),
* and false if the object is correctly created.
*/
bool operator!() const;
/**
* () operator
* -----------
* Build map with input path. Implementation of readDirectory().
*/
bool operator()(const std::string_view path);
bool operator()(std::string_view path);
};
using Error = Helper::Error;
/**
* getLibVersion()
* ---------------
* To get the version information of libpartition_map
* library. It is returned as std::string type.
*/

View File

@@ -59,6 +59,16 @@ std::optional<std::list<std::string>> basic_partition_map_builder::getPhysicalPa
return physicals;
}
std::optional<std::list<std::string>> basic_partition_map_builder::getPartitionList() const {
_map_build_check();
std::list<std::string> partitions;
for (const auto& [name, props] : _current_map) partitions.push_back(name);
if (partitions.empty()) return std::nullopt;
return partitions;
}
std::string basic_partition_map_builder::getRealLinkPathOf(const std::string_view name) const
{
_map_build_check();

View File

@@ -18,19 +18,18 @@
#include <vector>
#include <filesystem>
#include <memory>
#include <vector>
#include <algorithm>
#include <array>
#include <string>
#include <string_view>
#include <errno.h>
#include <cerrno>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
#include <libpartition_map/lib.hpp>
#include <generated/buildInfo.hpp>
#include <string.h>
#include <cstring>
#include <unistd.h>
static constexpr std::array<std::string_view, 3> defaultEntryList = {
@@ -41,9 +40,12 @@ static constexpr std::array<std::string_view, 3> defaultEntryList = {
namespace PartitionMap {
bool basic_partition_map_builder::_is_real_block_dir(const std::string_view path) const
bool basic_partition_map_builder::_is_real_block_dir(const std::string_view path)
{
if (path.find("/block/") == std::string::npos) return false;
if (path.find("/block/") == std::string::npos) {
LOGN(MAP, ERROR) << "Path " << path << " is not a real block directory.";
return false;
}
return true;
}
@@ -68,6 +70,7 @@ Map_t basic_partition_map_builder::_build_map(std::string_view path, bool logica
void basic_partition_map_builder::_insert_logicals(Map_t&& logicals)
{
LOGN(MAP, INFO) << "merging created logical partition list to this object's variable." << std::endl;
_current_map.merge(logicals);
}
@@ -77,10 +80,10 @@ void basic_partition_map_builder::_map_build_check() const
throw Error("Please build partition map before!");
}
uint64_t basic_partition_map_builder::_get_size(const std::string path)
uint64_t basic_partition_map_builder::_get_size(const std::string& path)
{
std::string real = std::filesystem::read_symlink(path);
int fd = open(real.data(), O_RDONLY);
const std::string real = std::filesystem::read_symlink(path);
const int fd = open(real.data(), O_RDONLY);
if (fd < 0) {
LOGN(MAP, ERROR) << "Cannot open " << real << ": " << strerror(errno) << std::endl;
return 0;
@@ -106,7 +109,6 @@ basic_partition_map_builder::basic_partition_map_builder()
_current_map = _build_map(path);
if (_current_map.empty()) {
_any_generating_error = true;
continue;
} else {
_workdir = path;
break;
@@ -127,7 +129,7 @@ basic_partition_map_builder::basic_partition_map_builder(const std::string_view
LOGN(MAP, INFO) << "argument-based constructor called. Starting build." << std::endl;
if (std::filesystem::exists(path)) {
_is_real_block_dir(path);
if (!_is_real_block_dir(path)) return;
_current_map = _build_map(path);
if (_current_map.empty()) _any_generating_error = true;
else _workdir = path;
@@ -179,6 +181,33 @@ bool basic_partition_map_builder::readDirectory(const std::string_view path)
return true;
}
bool basic_partition_map_builder::readDefaultDirectories()
{
_map_builded = false;
LOGN(MAP, INFO) << "read default directories request." << std::endl;
for (const auto& path : defaultEntryList) {
if (std::filesystem::exists(path)) {
_current_map = _build_map(path);
if (_current_map.empty()) {
_any_generating_error = true;
return false;
} else {
_workdir = path;
break;
}
}
}
if (_current_map.empty())
LOGN(MAP, ERROR) << "Cannot build map by any default search entry." << std::endl;
LOGN(MAP, INFO) << "read default directories successfull." << std::endl;
_insert_logicals(_build_map("/dev/block/mapper", true));
_map_builded = true;
return true;
}
bool basic_partition_map_builder::empty() const
{
_map_build_check();
@@ -191,12 +220,12 @@ uint64_t basic_partition_map_builder::sizeOf(const std::string_view name) const
return _current_map.get_size(name);
}
bool operator==(basic_partition_map_builder& lhs, basic_partition_map_builder& rhs)
bool operator==(const basic_partition_map_builder& lhs, const basic_partition_map_builder& rhs)
{
return lhs._current_map == rhs._current_map;
}
bool operator!=(basic_partition_map_builder& lhs, basic_partition_map_builder& rhs)
bool operator!=(const basic_partition_map_builder& lhs, const basic_partition_map_builder& rhs)
{
return !(lhs == rhs);
}
@@ -219,9 +248,7 @@ bool basic_partition_map_builder::operator()(const std::string_view path)
std::string getLibVersion()
{
char vinfo[512];
sprintf(vinfo, MKVERSION("libpartition_map"));
return std::string(vinfo);
MKVERSION("libpartition_map");
}
} // namespace PartitionMap

View File

@@ -23,12 +23,12 @@ namespace PartitionMap {
basic_partition_map::iterator::iterator(_entry* p) : ptr(p) {}
auto basic_partition_map::iterator::operator*() -> std::pair<std::string&, decltype(_entry::props)&>
auto basic_partition_map::iterator::operator*() const -> std::pair<std::string&, decltype(_entry::props)&>
{
return {ptr->name, ptr->props};
}
_entry* basic_partition_map::iterator::operator->()
_entry* basic_partition_map::iterator::operator->() const
{
return ptr;
}
@@ -41,17 +41,17 @@ basic_partition_map::iterator& basic_partition_map::iterator::operator++()
basic_partition_map::iterator basic_partition_map::iterator::operator++(int)
{
basic_partition_map::iterator tmp = *this;
iterator tmp = *this;
++ptr;
return tmp;
}
bool basic_partition_map::iterator::operator==(const basic_partition_map::iterator& other) const
bool basic_partition_map::iterator::operator==(const iterator& other) const
{
return ptr == other.ptr;
}
bool basic_partition_map::iterator::operator!=(const basic_partition_map::iterator& other) const
bool basic_partition_map::iterator::operator!=(const iterator& other) const
{
return ptr != other.ptr;
}
@@ -76,25 +76,25 @@ basic_partition_map::constant_iterator& basic_partition_map::constant_iterator::
basic_partition_map::constant_iterator basic_partition_map::constant_iterator::operator++(int)
{
basic_partition_map::constant_iterator tmp = *this;
constant_iterator tmp = *this;
++ptr;
return tmp;
}
bool basic_partition_map::constant_iterator::operator==(const basic_partition_map::constant_iterator& other) const
bool basic_partition_map::constant_iterator::operator==(const constant_iterator& other) const
{
return ptr == other.ptr;
}
bool basic_partition_map::constant_iterator::operator!=(const basic_partition_map::constant_iterator& other) const
bool basic_partition_map::constant_iterator::operator!=(const constant_iterator& other) const
{
return ptr != other.ptr;
}
void basic_partition_map::_resize_map()
{
size_t new_capacity = _capacity * 2;
_entry* new_data = new _entry[new_capacity];
const size_t new_capacity = _capacity * 2;
auto* new_data = new _entry[new_capacity];
for (size_t i = 0; i < _count; i++) new_data[i] = _data[i];
@@ -106,13 +106,13 @@ void basic_partition_map::_resize_map()
int basic_partition_map::_index_of(const std::string_view name) const
{
for (size_t i = 0; i < _count; i++) {
if (name == _data[i].name) return (int)i;
if (name == _data[i].name) return static_cast<int>(i);
}
return 0;
}
basic_partition_map::basic_partition_map(const std::string name, uint64_t size, bool logical)
basic_partition_map::basic_partition_map(const std::string& name, const uint64_t size, const bool logical)
{
_data = new _entry[_capacity];
insert(name, size, logical);
@@ -126,7 +126,7 @@ 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() : _count(0), _capacity(6)
basic_partition_map::basic_partition_map() : _capacity(6)
{
_data = new _entry[_capacity];
}
@@ -136,7 +136,7 @@ basic_partition_map::~basic_partition_map()
delete[] _data;
}
bool basic_partition_map::insert(const std::string name, uint64_t size, bool logical)
bool basic_partition_map::insert(const std::string& name, const uint64_t size, const bool logical)
{
if (name == _data[_index_of(name)].name) return false;
if (_count == _capacity) _resize_map();
@@ -186,7 +186,7 @@ bool basic_partition_map::find(const std::string_view name) const
return false;
}
std::string basic_partition_map::find_(const std::string name) const
std::string basic_partition_map::find_(const std::string& name) const
{
if (name == _data[_index_of(name)].name) return name;
@@ -249,34 +249,24 @@ bool basic_partition_map::operator!=(const basic_partition_map& other) const
return !(*this == other);
}
basic_partition_map::iterator basic_partition_map::begin()
basic_partition_map::iterator basic_partition_map::begin() const
{
return basic_partition_map::iterator(_data);
return iterator(_data);
}
basic_partition_map::iterator basic_partition_map::end()
basic_partition_map::iterator basic_partition_map::end() const
{
return basic_partition_map::iterator(_data + _count);
}
basic_partition_map::constant_iterator basic_partition_map::begin() const
{
return basic_partition_map::constant_iterator(_data);
return iterator(_data + _count);
}
basic_partition_map::constant_iterator basic_partition_map::cbegin() const
{
return basic_partition_map::constant_iterator(_data);
}
basic_partition_map::constant_iterator basic_partition_map::end() const
{
return basic_partition_map::constant_iterator(_data + _count);
return constant_iterator(_data);
}
basic_partition_map::constant_iterator basic_partition_map::cend() const
{
return basic_partition_map::constant_iterator(_data + _count);
return constant_iterator(_data + _count);
}
} // namespace PartitionMap

View File

@@ -18,7 +18,7 @@
#include <unistd.h>
#include <libpartition_map/lib.hpp>
int main(void) {
int main() {
if (getuid() != 0) return 2;
try {
@@ -28,7 +28,7 @@ int main(void) {
if (!MyMap) throw PartitionMap::Error("Cannot generate object!");
}
auto map = MyMap.getAll();
const auto map = MyMap.getAll();
if (map.empty()) throw PartitionMap::Error("getAll() empty");
for (const auto& [name, props] : map) {
std::cout << "Partition: " << name << ", size: "
@@ -36,19 +36,19 @@ int main(void) {
<< props.isLogical << std::endl;
}
auto boot = MyMap.get("boot");
const auto boot = MyMap.get("boot");
if (!boot) throw PartitionMap::Error("get(\"boot\") returned nullopt");
std::cout << "Name: boot" << ", size: "
<< boot->first << ", logical: "
<< boot->second << std::endl;
auto logicals = MyMap.getLogicalPartitionList();
const auto logicals = MyMap.getLogicalPartitionList();
if (!logicals) throw PartitionMap::Error("getLogicalPartitionList() returned nullopt");
std::cout << "Logical partitions: " << std::endl;
for (const auto& name : *logicals)
std::cout << " - " << name << std::endl;
auto physicals = MyMap.getPhysicalPartitionList();
const auto physicals = MyMap.getPhysicalPartitionList();
if (!physicals) throw PartitionMap::Error("getPhysicalPartitionList() returned nullopt");
std::cout << "Physical partitions: " << std::endl;
for (const auto& name : *physicals)