pmt: Improve pmt & libpartition_map
- Change some operator returnn types. - Use std::map instead of std::unordered on magic number maps. - Some improvements.
This commit is contained in:
@@ -22,12 +22,11 @@
|
||||
#include <functional>
|
||||
#include <libhelper/lib.hpp>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <tuple>
|
||||
#include <unordered_map>
|
||||
#include <utility> // for std::pair
|
||||
|
||||
namespace PartitionMap {
|
||||
@@ -40,6 +39,14 @@ struct _entry {
|
||||
} props;
|
||||
};
|
||||
|
||||
struct _returnable_entry {
|
||||
uint64_t size;
|
||||
bool isLogical;
|
||||
};
|
||||
|
||||
using BasicInf = _returnable_entry;
|
||||
using Info = _entry;
|
||||
|
||||
/**
|
||||
* The main type of the library. The Builder class is designed
|
||||
* to be easily manipulated and modified only on this class.
|
||||
@@ -54,13 +61,6 @@ public:
|
||||
_entry *_data;
|
||||
size_t _count{}, _capacity{};
|
||||
|
||||
struct _returnable_entry {
|
||||
uint64_t size;
|
||||
bool isLogical;
|
||||
};
|
||||
|
||||
using BasicInf = _returnable_entry;
|
||||
|
||||
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;
|
||||
@@ -84,7 +84,7 @@ 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 std::vector<Info>() const;
|
||||
operator int() const;
|
||||
|
||||
class iterator {
|
||||
@@ -256,26 +256,26 @@ public:
|
||||
* Do input function (lambda) for all partitions.
|
||||
*/
|
||||
void doForAllPartitions(
|
||||
const std::function<bool(std::string, Map_t::BasicInf)> &func) const;
|
||||
const std::function<bool(std::string, BasicInf)> &func) const;
|
||||
|
||||
/**
|
||||
* Do input function (lambda) for physical partitions.
|
||||
*/
|
||||
void doForPhysicalPartitions(
|
||||
const std::function<bool(std::string, Map_t::BasicInf)> &func) const;
|
||||
const std::function<bool(std::string, BasicInf)> &func) const;
|
||||
|
||||
/**
|
||||
* Do input function (lambda) for logical partitions.
|
||||
*/
|
||||
void doForLogicalPartitions(
|
||||
const std::function<bool(std::string, Map_t::BasicInf)> &func) const;
|
||||
const std::function<bool(std::string, BasicInf)> &func) const;
|
||||
|
||||
/**
|
||||
* Do input function (lambda) for input partition list.
|
||||
*/
|
||||
void doForPartitionList(
|
||||
const std::vector<std::string> &partitions,
|
||||
const std::function<bool(std::string, Map_t::BasicInf)> &func) const;
|
||||
const std::function<bool(std::string, BasicInf)> &func) const;
|
||||
|
||||
/**
|
||||
* The entered path is defined as the new search
|
||||
@@ -346,10 +346,9 @@ public:
|
||||
const Map_t &operator*() const;
|
||||
|
||||
/**
|
||||
* Get map contents as vector (std::tuple type).
|
||||
* Get map contents as vector (PartitionManager::Info type).
|
||||
*/
|
||||
[[nodiscard]]
|
||||
operator std::vector<std::tuple<std::string, uint64_t, bool>>() const;
|
||||
[[nodiscard]] operator std::vector<Info>() const;
|
||||
|
||||
/**
|
||||
* Get total partition count in map (int type).
|
||||
@@ -399,9 +398,9 @@ constexpr uint64_t ELF =
|
||||
constexpr uint64_t RAW = 0x00000000;
|
||||
} // namespace AndroidMagic
|
||||
|
||||
extern std::unordered_map<uint64_t, std::string> FileSystemMagicMap;
|
||||
extern std::unordered_map<uint64_t, std::string> AndroidMagicMap;
|
||||
extern std::unordered_map<uint64_t, std::string> MagicMap;
|
||||
extern std::map<uint64_t, std::string> FileSystemMagicMap;
|
||||
extern std::map<uint64_t, std::string> AndroidMagicMap;
|
||||
extern std::map<uint64_t, std::string> MagicMap;
|
||||
|
||||
size_t getMagicLength(uint64_t magic);
|
||||
bool hasMagic(uint64_t magic, ssize_t buf, const std::string &path);
|
||||
@@ -411,9 +410,7 @@ 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>>
|
||||
#define COMMON_LAMBDA_PARAMS \
|
||||
(const std::string &partition, const PartitionMap::BasicInf props)
|
||||
|
||||
#endif // #ifndef LIBPARTITION_MAP_LIB_HPP
|
||||
|
||||
@@ -17,15 +17,15 @@
|
||||
#include <fcntl.h>
|
||||
#include <libhelper/lib.hpp>
|
||||
#include <libpartition_map/lib.hpp>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <unistd.h>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "PartitionManager/PartitionManager.hpp"
|
||||
|
||||
namespace PartitionMap::Extras {
|
||||
std::unordered_map<uint64_t, std::string> FileSystemMagicMap = {
|
||||
std::map<uint64_t, std::string> FileSystemMagicMap = {
|
||||
{FileSystemMagic::EXTFS_FS, "EXT2/3/4"},
|
||||
{FileSystemMagic::F2FS_FS, "F2FS"},
|
||||
{FileSystemMagic::EROFS_FS, "EROFS"},
|
||||
@@ -36,7 +36,7 @@ std::unordered_map<uint64_t, std::string> FileSystemMagicMap = {
|
||||
{FileSystemMagic::NTFS_FS, "NTFS"},
|
||||
{FileSystemMagic::MSDOS_FS, "MSDOS"}};
|
||||
|
||||
std::unordered_map<uint64_t, std::string> AndroidMagicMap = {
|
||||
std::map<uint64_t, std::string> AndroidMagicMap = {
|
||||
{AndroidMagic::BOOT_IMAGE, "Android Boot Image"},
|
||||
{AndroidMagic::VBOOT_IMAGE, "Android Vendor Boot Image"},
|
||||
{AndroidMagic::LK_IMAGE, "Android LK (Bootloader)"},
|
||||
@@ -47,7 +47,7 @@ std::unordered_map<uint64_t, std::string> AndroidMagicMap = {
|
||||
{AndroidMagic::ELF, "ELF"},
|
||||
{AndroidMagic::RAW, "Raw Data"}};
|
||||
|
||||
std::unordered_map<uint64_t, std::string> MagicMap = {
|
||||
std::map<uint64_t, std::string> MagicMap = {
|
||||
{AndroidMagic::BOOT_IMAGE, "Android Boot Image"},
|
||||
{AndroidMagic::VBOOT_IMAGE, "Android Vendor Boot Image"},
|
||||
{AndroidMagic::LK_IMAGE, "Android LK (Bootloader)"},
|
||||
|
||||
@@ -288,7 +288,7 @@ bool basic_partition_map_builder::empty() const {
|
||||
}
|
||||
|
||||
void basic_partition_map_builder::doForAllPartitions(
|
||||
const std::function<bool(std::string, Map_t::BasicInf)> &func) const {
|
||||
const std::function<bool(std::string, BasicInf)> &func) const {
|
||||
_map_build_check();
|
||||
|
||||
LOGN(MAP, INFO) << "Doing input function for all partitions." << std::endl;
|
||||
@@ -303,7 +303,7 @@ void basic_partition_map_builder::doForAllPartitions(
|
||||
}
|
||||
|
||||
void basic_partition_map_builder::doForPhysicalPartitions(
|
||||
const std::function<bool(std::string, Map_t::BasicInf)> &func) const {
|
||||
const std::function<bool(std::string, BasicInf)> &func) const {
|
||||
_map_build_check();
|
||||
|
||||
LOGN(MAP, INFO) << "Doing input function for physical partitions."
|
||||
@@ -320,7 +320,7 @@ void basic_partition_map_builder::doForPhysicalPartitions(
|
||||
}
|
||||
|
||||
void basic_partition_map_builder::doForLogicalPartitions(
|
||||
const std::function<bool(std::string, Map_t::BasicInf)> &func) const {
|
||||
const std::function<bool(std::string, BasicInf)> &func) const {
|
||||
_map_build_check();
|
||||
|
||||
LOGN(MAP, INFO) << "Doing input function for logical partitions."
|
||||
@@ -338,7 +338,7 @@ void basic_partition_map_builder::doForLogicalPartitions(
|
||||
|
||||
void basic_partition_map_builder::doForPartitionList(
|
||||
const std::vector<std::string> &partitions,
|
||||
const std::function<bool(std::string, Map_t::BasicInf)> &func) const {
|
||||
const std::function<bool(std::string, BasicInf)> &func) const {
|
||||
_map_build_check();
|
||||
|
||||
LOGN(MAP, INFO) << "Doing input function for input partition list."
|
||||
@@ -388,8 +388,7 @@ 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 {
|
||||
basic_partition_map_builder::operator std::vector<Info>() const {
|
||||
return _current_map;
|
||||
}
|
||||
|
||||
|
||||
@@ -162,7 +162,7 @@ bool basic_partition_map::is_logical(const std::string_view name) const {
|
||||
return false;
|
||||
}
|
||||
|
||||
basic_partition_map::_returnable_entry
|
||||
_returnable_entry
|
||||
basic_partition_map::get_all(const std::string_view name) const {
|
||||
if (const int pos = _index_of(name); name == _data[pos].name)
|
||||
return _returnable_entry{_data[pos].props.size, _data[pos].props.isLogical};
|
||||
@@ -229,13 +229,12 @@ 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;
|
||||
basic_partition_map::operator std::vector<Info>() const {
|
||||
std::vector<Info> 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);
|
||||
v.push_back(
|
||||
{_data[i].name, {_data[i].props.size, _data[i].props.isLogical}});
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <libpartition_map/lib.hpp>
|
||||
#include <unistd.h>
|
||||
@@ -54,10 +55,26 @@ int main() {
|
||||
for (const auto &name : *physicals)
|
||||
std::cout << " - " << name << std::endl;
|
||||
|
||||
if (const std::vector<PartitionMap::Info> parts = MyMap; parts.empty())
|
||||
throw PartitionMap::Error(
|
||||
"operator std::vector>PartitionMap::Info>() returned empty vector");
|
||||
|
||||
auto func = [](const std::string &partition,
|
||||
const PartitionMap::BasicInf props) -> bool {
|
||||
std::ofstream f("parts.txt");
|
||||
f << "Partition: " << partition << ", size: " << props.size
|
||||
<< ", logical: " << props.isLogical;
|
||||
return !f.fail();
|
||||
};
|
||||
MyMap.doForAllPartitions(func);
|
||||
|
||||
std::cout << "Total partitions count: " << (int)MyMap << std::endl;
|
||||
std::cout << "Boot: " << MyMap.getRealLinkPathOf("boot") << std::endl;
|
||||
std::cout << "Boot (realpath): " << MyMap.getRealPathOf("boot")
|
||||
<< std::endl;
|
||||
std::cout << "Search dir: " << MyMap.getCurrentWorkDir() << std::endl;
|
||||
std::cout << "Search dir test 2: " << static_cast<std::string>(MyMap)
|
||||
<< std::endl;
|
||||
std::cout << "Has partition cache? = " << MyMap.hasPartition("cache")
|
||||
<< std::endl;
|
||||
std::cout << "system partition is logical? = " << MyMap.isLogical("system")
|
||||
|
||||
Reference in New Issue
Block a user