pmt: first codes.

This commit is contained in:
2025-07-21 12:08:45 +03:00
commit d65867c249
22 changed files with 2542 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
#
# Copyright 2025 Yağız Zengin
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Sources
set(LIBPARTITION_MAP_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/src/Getters.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/PartitionMap.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/Type.cpp
)
set(LIBPARTITION_MAP_FLAGS -Wall -Werror -DLOG_FILE="/storage/emulated/0/.last_pmt.log" -DPROGRAM_NAME="libpartition_map")
# 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 compiler flags
target_compile_options(partition_map_shared PRIVATE ${LIBPARTITION_MAP_FLAGS})
target_compile_options(partition_map_static PRIVATE ${LIBPARTITION_MAP_FLAGS})
target_compile_options(libpartition_map_test PRIVATE ${LIBPARTITION_MAP_FLAGS})
target_link_options(libpartition_map_test PRIVATE "LINKER:-rpath,/data/data/com.termux/files/usr/lib")
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)

View File

@@ -0,0 +1,332 @@
/*
Copyright 2025 Yağız Zengin
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef LIBPARTITION_MAP_LIB_HPP
#define LIBPARTITION_MAP_LIB_HPP
#define LIBPARTITION_MAP_MAJOR 1
#define LIBPARTITION_MAP_MINOR 0
#define LIBPARTITION_MAP_PATCH 0
#include <string>
#include <string_view>
#include <optional>
#include <exception>
#include <list>
#include <memory>
#include <utility> // for std::pair
#include <cstdint> // for uint64_t
#include <libhelper/lib.hpp>
namespace PartitionMap {
struct _entry {
std::string name;
struct {
uint64_t size;
bool isLogical;
} props;
};
class basic_partition_map {
private:
void _resize_map();
int _index_of(const std::string_view name) const;
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();
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;
void clear();
basic_partition_map& operator=(const basic_partition_map& map);
bool operator==(const basic_partition_map& other) const;
bool operator!=(const basic_partition_map& other) const;
class iterator {
public:
_entry* ptr;
iterator(_entry* p);
auto operator*() -> std::pair<std::string&, decltype(_entry::props)&>;
_entry* operator->();
iterator& operator++();
iterator operator++(int);
bool operator!=(const iterator& other) const;
bool operator==(const iterator& other) const;
};
class constant_iterator {
public:
const _entry* ptr;
constant_iterator(const _entry* p);
auto operator*() const -> std::pair<const std::string&, const decltype(_entry::props)&>;
const _entry* operator->() const;
constant_iterator& operator++();
constant_iterator operator++(int);
bool operator!=(const constant_iterator& other) const;
bool operator==(const constant_iterator& other) const;
};
/* for-each support */
iterator begin();
iterator end();
constant_iterator begin() const;
constant_iterator cbegin() const;
constant_iterator end() const;
constant_iterator cend() const;
};
using Map_t = basic_partition_map;
class basic_partition_map_builder {
private:
Map_t _current_map;
std::string _workdir;
bool _any_generating_error, _map_builded;
bool _is_real_block_dir(const std::string_view path) const;
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);
public:
/**
* Default constructor
* -------------------
* By default, it searches the directories in the
* defaultEntryList in PartitionMap.cpp in order and
* uses the directory it finds.
*/
basic_partition_map_builder();
/**
* Secondary constructor
* ---------------------
* It has two arguments:
* - Directory path to search
*/
basic_partition_map_builder(const std::string_view path);
/**
* getAll()
* ------
* WARNING: Learn about std::optional before using this function.
*
* Returns the current list content in Map_t type.
* If no list is created, returns std::nullopt.
*/
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;
/**
* 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;
/**
* 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;
/**
* 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;
/**
* 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;
/**
* 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;
/**
* 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;
/**
* 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
* and checked for logicality.
*/
bool isLogical(const 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);
/**
* 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;
/**
* 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;
/**
* == 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);
/**
* != operator
* -----------
* The opposite logic of the == operator.
*/
friend bool operator!=(basic_partition_map_builder& lhs, 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;
/**
* ! 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;
};
using Error = Helper::Error;
/**
* getLibVersion()
* ---------------
* To get the version information of libpartition_map
* library. It is returned as std::string type.
*/
std::string getLibVersion();
using BuildMap = basic_partition_map_builder;
using Map = basic_partition_map_builder;
} // namespace PartitionMap
#endif // #ifndef LIBPARTITION_MAP_LIB_HPP

View File

@@ -0,0 +1,87 @@
/*
Copyright 2025 Yağız Zengin
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <string>
#include <optional>
#include <filesystem>
#include <libpartition_map/lib.hpp>
namespace PartitionMap {
Map_t basic_partition_map_builder::getAll() const
{
_map_build_check();
return _current_map;
}
std::optional<std::pair<uint64_t, bool>> basic_partition_map_builder::get(const std::string_view name) const
{
_map_build_check();
if (!_current_map.find(name)) return std::nullopt;
return std::make_pair(_current_map.get_size(name), _current_map.is_logical(name));
}
std::optional<std::list<std::string>> basic_partition_map_builder::getLogicalPartitionList() const
{
_map_build_check();
std::list<std::string> logicals;
for (const auto& [name, props] : _current_map)
if (props.isLogical) logicals.push_back(name);
if (logicals.empty()) return std::nullopt;
return logicals;
}
std::optional<std::list<std::string>> basic_partition_map_builder::getPhysicalPartitionList() const
{
_map_build_check();
std::list<std::string> physicals;
for (const auto& [name, props] : _current_map)
if (!props.isLogical) physicals.push_back(name);
if (physicals.empty()) return std::nullopt;
return physicals;
}
std::string basic_partition_map_builder::getRealLinkPathOf(const std::string_view name) const
{
_map_build_check();
if (!_current_map.find(name)) return {};
return std::string(_workdir + "/" + name.data());
}
std::string basic_partition_map_builder::getRealPathOf(const std::string_view name) const
{
_map_build_check();
std::string full = _workdir + "/" + name.data();
if (!_current_map.find(name)
|| !std::filesystem::is_symlink(full))
return {};
return std::filesystem::read_symlink(full);
}
std::string basic_partition_map_builder::getCurrentWorkDir() const
{
return _workdir;
}
} // namespace PartitionMap

View File

@@ -0,0 +1,209 @@
/*
Copyright 2025 Yağız Zengin
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <iostream>
#include <vector>
#include <filesystem>
#include <memory>
#include <vector>
#include <algorithm>
#include <array>
#include <string>
#include <string_view>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
#include <libpartition_map/lib.hpp>
#include <string.h>
#include <unistd.h>
static constexpr std::array<std::string_view, 3> defaultEntryList = {
"/dev/block/by-name",
"/dev/block/bootdevice/by-name",
"/dev/block/platform/bootdevice/by-name"
};
namespace PartitionMap {
bool basic_partition_map_builder::_is_real_block_dir(const std::string_view path) const
{
if (path.find("/block/") == std::string::npos) return false;
return true;
}
Map_t basic_partition_map_builder::_build_map(std::string_view path, bool logical)
{
Map_t map;
std::vector<std::filesystem::directory_entry> entries{std::filesystem::directory_iterator(path), std::filesystem::directory_iterator()};
std::sort(entries.begin(), entries.end(), [](const auto& a, const auto& b) {
return a.path().filename() < b.path().filename();
});
for (const auto& entry : entries) {
if (entry.path().filename() != "by-uuid"
&& std::string(entry.path()).find("com.") == std::string::npos)
map.insert(entry.path().filename().string(), _get_size(entry.path()), logical);
}
return map;
}
void basic_partition_map_builder::_insert_logicals(Map_t&& logicals)
{
_current_map.merge(logicals);
}
void basic_partition_map_builder::_map_build_check() const
{
if (!_map_builded)
throw Error("Please build partition map before!");
}
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);
if (fd < 0)
throw Error("Cannot open %s: %s", real.data(), strerror(errno));
uint64_t size = 0;
if (ioctl(fd, BLKGETSIZE64, &size) != 0) {
close(fd);
throw Error("ioctl() process failed for %s: %s", real.data(), strerror(errno));
}
close(fd);
return size;
}
basic_partition_map_builder::basic_partition_map_builder()
{
for (const auto& path : defaultEntryList) {
if (std::filesystem::exists(path)) {
_current_map = _build_map(path);
if (_current_map.empty()) {
_any_generating_error = true;
continue;
} else {
_workdir = path;
break;
}
}
}
if (_current_map.empty())
throw Error("Cannot build map by any default search entry.");
_insert_logicals(_build_map("/dev/block/mapper", true));
_map_builded = true;
}
basic_partition_map_builder::basic_partition_map_builder(const std::string_view path)
{
if (std::filesystem::exists(path)) {
_is_real_block_dir(path);
_current_map = _build_map(path);
if (_current_map.empty()) _any_generating_error = true;
else _workdir = path;
} else
throw Error("Cannot find directory: %s. Cannot build partition map!", path.data());
_insert_logicals(_build_map("/dev/block/mapper", true));
_map_builded = true;
}
bool basic_partition_map_builder::hasPartition(const std::string_view name) const
{
_map_build_check();
return _current_map.find(name);
}
bool basic_partition_map_builder::isLogical(const std::string_view name) const
{
_map_build_check();
return _current_map.is_logical(name);
}
void basic_partition_map_builder::clear()
{
_current_map.clear();
_workdir.clear();
_any_generating_error = false;
}
bool basic_partition_map_builder::readDirectory(const std::string_view path)
{
_map_builded = false;
if (std::filesystem::exists(path)) {
if (!_is_real_block_dir(path)) return false;
_current_map = _build_map(path);
if (_current_map.empty()) {
_any_generating_error = true;
return false;
} else _workdir = path;
} else
throw Error("Cannot find directory: %s. Cannot build partition map!", path.data());
_insert_logicals(_build_map("/dev/block/mapper", true));
_map_builded = true;
return true;
}
bool basic_partition_map_builder::empty() const
{
_map_build_check();
return _current_map.empty();
}
uint64_t basic_partition_map_builder::sizeOf(const std::string_view name) const
{
_map_build_check();
return _current_map.get_size(name);
}
bool operator==(basic_partition_map_builder& lhs, basic_partition_map_builder& rhs)
{
return lhs._current_map == rhs._current_map;
}
bool operator!=(basic_partition_map_builder& lhs, basic_partition_map_builder& rhs)
{
return !(lhs == rhs);
}
basic_partition_map_builder::operator bool() const
{
return !this->_any_generating_error;
}
bool basic_partition_map_builder::operator!() const
{
return this->_any_generating_error;
}
std::string getLibVersion()
{
return std::string(
std::to_string(LIBPARTITION_MAP_MAJOR) + "."
+ std::to_string(LIBPARTITION_MAP_MINOR) + "."
+ std::to_string(LIBPARTITION_MAP_PATCH)
);
}
} // namespace PartitionMap

View File

@@ -0,0 +1,278 @@
/*
Copyright 2025 Yağız Zengin
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <string>
#include <cstdint>
#include <utility>
#include <libpartition_map/lib.hpp>
namespace PartitionMap {
basic_partition_map::iterator::iterator(_entry* p) : ptr(p) {}
auto basic_partition_map::iterator::operator*() -> std::pair<std::string&, decltype(_entry::props)&>
{
return {ptr->name, ptr->props};
}
_entry* basic_partition_map::iterator::operator->()
{
return ptr;
}
basic_partition_map::iterator& basic_partition_map::iterator::operator++()
{
++ptr;
return *this;
}
basic_partition_map::iterator basic_partition_map::iterator::operator++(int)
{
basic_partition_map::iterator tmp = *this;
++ptr;
return tmp;
}
bool basic_partition_map::iterator::operator==(const basic_partition_map::iterator& other) const
{
return ptr == other.ptr;
}
bool basic_partition_map::iterator::operator!=(const basic_partition_map::iterator& other) const
{
return ptr != other.ptr;
}
basic_partition_map::constant_iterator::constant_iterator(const _entry* p) : ptr(p) {}
auto basic_partition_map::constant_iterator::operator*() const -> std::pair<const std::string&, const decltype(_entry::props)&> {
return {ptr->name, ptr->props};
}
const _entry* basic_partition_map::constant_iterator::operator->() const
{
return ptr;
}
basic_partition_map::constant_iterator& basic_partition_map::constant_iterator::operator++()
{
++ptr;
return *this;
}
basic_partition_map::constant_iterator basic_partition_map::constant_iterator::operator++(int)
{
basic_partition_map::constant_iterator tmp = *this;
++ptr;
return tmp;
}
bool basic_partition_map::constant_iterator::operator==(const basic_partition_map::constant_iterator& other) const
{
return ptr == other.ptr;
}
bool basic_partition_map::constant_iterator::operator!=(const basic_partition_map::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];
for (size_t i = 0; i < _count; i++) new_data[i] = _data[i];
delete[] _data;
_data = new_data;
_capacity = new_capacity;
}
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;
}
return 0;
}
basic_partition_map::basic_partition_map(const std::string name, uint64_t size, bool logical)
{
_data = new _entry[_capacity];
insert(name, size, logical);
}
basic_partition_map::basic_partition_map(const basic_partition_map& other) :
_data(new _entry[other._capacity]),
_count(other._count),
_capacity(other._capacity)
{
std::copy(other._data, other._data + _count, _data);
}
basic_partition_map::basic_partition_map() : _count(0), _capacity(6)
{
_data = new _entry[_capacity];
}
basic_partition_map::~basic_partition_map()
{
delete[] _data;
}
bool basic_partition_map::insert(const std::string name, uint64_t size, bool logical)
{
if (name == _data[_index_of(name)].name) return false;
if (_count == _capacity) _resize_map();
_data[_count++] = {name, {size, logical}};
return true;
}
void basic_partition_map::merge(const basic_partition_map& map)
{
for (const auto& [name, props] : map)
insert(name, props.size, props.isLogical);
}
uint64_t basic_partition_map::get_size(const std::string_view name) const
{
int pos = _index_of(name);
if (name == _data[pos].name) return _data[pos].props.size;
return 0;
}
bool basic_partition_map::is_logical(const std::string_view name) const
{
int pos = _index_of(name);
if (name == _data[pos].name) return _data[pos].props.isLogical;
return false;
}
basic_partition_map::_returnable_entry basic_partition_map::get_all(const std::string_view name) const
{
int pos = _index_of(name);
if (name == _data[pos].name)
return _returnable_entry{_data[pos].props.size, _data[pos].props.isLogical};
return _returnable_entry{};
}
bool basic_partition_map::find(const std::string_view name) const
{
if (name == _data[_index_of(name)].name) return true;
return false;
}
std::string basic_partition_map::find_(const std::string name) const
{
if (name == _data[_index_of(name)].name) return name;
return {};
}
size_t basic_partition_map::size() const
{
return _count;
}
bool basic_partition_map::empty() const
{
if (_count > 0) return false;
return true;
}
void basic_partition_map::clear()
{
delete[] _data;
_count = 0;
_capacity = 6;
_data = new _entry[_capacity];
}
basic_partition_map& basic_partition_map::operator=(const basic_partition_map& map)
{
if (this != &map) {
delete[] _data;
_capacity = map._capacity;
_count = map._count;
_data = new _entry[_capacity];
std::copy(map._data, map._data + _count, _data);
}
return *this;
}
bool basic_partition_map::operator==(const basic_partition_map& other) const
{
if (this->_capacity != other._capacity
|| this->_count != other._count)
return false;
for (size_t i = 0; i < _count; i++)
if (_data[i].name == other._data[i].name
&& _data[i].props.size == other._data[i].props.size
&& _data[i].props.isLogical == other._data[i].props.isLogical)
continue;
else
return false;
return true;
}
bool basic_partition_map::operator!=(const basic_partition_map& other) const
{
return !(*this == other);
}
basic_partition_map::iterator basic_partition_map::begin()
{
return basic_partition_map::iterator(_data);
}
basic_partition_map::iterator basic_partition_map::end()
{
return basic_partition_map::iterator(_data + _count);
}
basic_partition_map::constant_iterator basic_partition_map::begin() const
{
return basic_partition_map::constant_iterator(_data);
}
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);
}
basic_partition_map::constant_iterator basic_partition_map::cend() const
{
return basic_partition_map::constant_iterator(_data + _count);
}
} // namespace PartitionMap

View File

@@ -0,0 +1,78 @@
/*
Copyright 2025 Yağız Zengin
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <iostream>
#include <unistd.h>
#include <libpartition_map/lib.hpp>
int main(void) {
if (getuid() != 0) return 2;
try {
PartitionMap::BuildMap MyMap;
if (!MyMap) {
MyMap.readDirectory("/dev/block/by-name");
if (!MyMap) throw PartitionMap::Error("Cannot generate object!");
}
auto map = MyMap.getAll();
if (map.empty()) throw PartitionMap::Error("getAll() empty");
for (const auto& [name, props] : map) {
std::cout << "Partition: " << name << ", size: "
<< props.size << ", logical: "
<< props.isLogical << std::endl;
}
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();
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();
if (!physicals) throw PartitionMap::Error("getPhysicalPartitionList() returned nullopt");
std::cout << "Physical partitions: " << std::endl;
for (const auto& name : *physicals)
std::cout << " - " << name << 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 << "Has partition cache? = " << MyMap.hasPartition("cache") << std::endl;
std::cout << "system partition is logical? = " << MyMap.isLogical("system") << std::endl;
std::cout << "Size of system partition: " << MyMap.sizeOf("system") << std::endl;
MyMap.clear();
if (!MyMap.empty()) throw PartitionMap::Error("map cleaned but check fail");
MyMap.readDirectory("/dev/block/by-name");
PartitionMap::BuildMap MyMap2;
if (MyMap == MyMap2) std::cout << "map1 = map2" << std::endl;
if (MyMap != MyMap2) std::cout << "map1 != map2" << std::endl;
} catch (PartitionMap::Error& error) {
std::cerr << error.what() << std::endl;
return 1;
}
return 0;
}