pmt: reformat code, etc.

This commit is contained in:
2025-11-24 18:50:57 +03:00
parent 579b2623a4
commit 11d75e401e
30 changed files with 755 additions and 647 deletions

View File

@@ -17,7 +17,7 @@
#ifndef LIBPARTITION_MAP_LIB_HPP
#define LIBPARTITION_MAP_LIB_HPP
#include <cstdint> // for uint64_t
#include <cstdint> // for uint64_t
#include <exception>
#include <functional>
#include <libhelper/lib.hpp>
@@ -27,7 +27,7 @@
#include <optional>
#include <string>
#include <string_view>
#include <utility> // for std::pair
#include <utility> // for std::pair
namespace PartitionMap {
struct _entry {
@@ -52,74 +52,73 @@ using Info = _entry;
* to be easily manipulated and modified only on this class.
*/
class basic_partition_map {
private:
private:
void _resize_map();
[[nodiscard]] int _index_of(std::string_view name) const;
public:
_entry *_data;
public:
_entry* _data;
size_t _count{}, _capacity{};
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(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();
bool insert(const std::string &name, uint64_t size, bool logical);
void merge(const basic_partition_map &map);
bool insert(const std::string& name, uint64_t size, bool logical);
void merge(const basic_partition_map& map);
void clear();
[[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]] std::string find_(const std::string& name) const;
[[nodiscard]] size_t size() const;
[[nodiscard]] bool empty() const;
basic_partition_map &operator=(const basic_partition_map &map);
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;
bool operator==(const basic_partition_map& other) const;
bool operator!=(const basic_partition_map& other) const;
bool operator!() const;
explicit operator bool() const;
Info operator[](int index) const;
BasicInf operator[](const std::string_view &name) const;
BasicInf operator[](const std::string_view& name) const;
explicit operator std::vector<Info>() const;
explicit operator int() const;
class iterator {
public:
_entry *ptr;
public:
_entry* ptr;
explicit iterator(_entry *p);
explicit iterator(_entry* p);
auto operator*() const
-> std::pair<std::string &, decltype(_entry::props) &>;
_entry *operator->() const;
iterator &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;
bool operator==(const iterator &other) const;
bool operator!=(const iterator& other) const;
bool operator==(const iterator& other) const;
};
class constant_iterator {
public:
const _entry *ptr;
public:
const _entry* ptr;
explicit 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;
constant_iterator &operator++();
-> 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;
bool operator!=(const constant_iterator& other) const;
bool operator==(const constant_iterator& other) const;
};
/* for-each support */
@@ -133,20 +132,20 @@ using Partition_t = _entry;
using Map_t = basic_partition_map;
class basic_partition_map_builder final {
private:
private:
Map_t _current_map;
std::string _workdir;
bool _any_generating_error, _map_builded;
Map_t _build_map(std::string_view path, bool logical = false);
void _insert_logicals(Map_t &&logicals);
void _insert_logicals(Map_t&& logicals);
void _map_build_check() const;
[[nodiscard]] static bool _is_real_block_dir(std::string_view path);
[[nodiscard]] uint64_t _get_size(const std::string &path);
[[nodiscard]] uint64_t _get_size(const std::string& path);
public:
public:
/**
* By default, it searches the directories in the
* defaultEntryList in PartitionMap.cpp in order and
@@ -162,7 +161,7 @@ public:
/**
* Move constructor
*/
basic_partition_map_builder(basic_partition_map_builder &&other) noexcept;
basic_partition_map_builder(basic_partition_map_builder&& other) noexcept;
/**
* Returns the current list content in Map_t type.
@@ -174,8 +173,8 @@ public:
* Map_temp_t type. If the partition is not in the
* currently created list, returns std::nullopt.
*/
[[nodiscard]] std::optional<std::pair<uint64_t, bool>>
get(std::string_view name) const;
[[nodiscard]] std::optional<std::pair<uint64_t, bool>> get(
std::string_view name) const;
/**
* If there is a logical partition(s) in the created
@@ -183,16 +182,16 @@ public:
* data of type std::string). If there is no logical
* partition in the created list, it returns std::nullopt.
*/
[[nodiscard]] std::optional<std::list<std::string>>
getLogicalPartitionList() const;
[[nodiscard]] std::optional<std::list<std::string>> getLogicalPartitionList()
const;
/**
* 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.
*/
[[nodiscard]] 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
@@ -244,17 +243,17 @@ public:
/**
* Copy partition list to vec, current vec contents are cleaned
*/
bool copyPartitionsToVector(std::vector<std::string> &vec) const;
bool copyPartitionsToVector(std::vector<std::string>& vec) const;
/**
* Copy logical partition list to vec, current vec contents are cleaned
*/
bool copyLogicalPartitionsToVector(std::vector<std::string> &vec) const;
bool copyLogicalPartitionsToVector(std::vector<std::string>& vec) const;
/**
* Copy physical partition list to vec, current vec contents are cleaned
*/
bool copyPhysicalPartitionsToVector(std::vector<std::string> &vec) const;
bool copyPhysicalPartitionsToVector(std::vector<std::string>& vec) const;
/**
* The created list and the current search index name are cleared.
@@ -265,26 +264,26 @@ public:
* Do input function (lambda) for all partitions.
*/
bool doForAllPartitions(
const std::function<bool(std::string, BasicInf)> &func) const;
const std::function<bool(std::string, BasicInf)>& func) const;
/**
* Do input function (lambda) for physical partitions.
*/
bool doForPhysicalPartitions(
const std::function<bool(std::string, BasicInf)> &func) const;
const std::function<bool(std::string, BasicInf)>& func) const;
/**
* Do input function (lambda) for logical partitions.
*/
bool doForLogicalPartitions(
const std::function<bool(std::string, BasicInf)> &func) const;
const std::function<bool(std::string, BasicInf)>& func) const;
/**
* Do input function (lambda) for input partition list.
*/
bool doForPartitionList(
const std::vector<std::string> &partitions,
const std::function<bool(std::string, BasicInf)> &func) const;
const std::vector<std::string>& partitions,
const std::function<bool(std::string, BasicInf)>& func) const;
/**
* The entered path is defined as the new search
@@ -317,14 +316,14 @@ public:
* the same (checked only according to the partition
* names), true is returned, otherwise false is returned
*/
friend bool operator==(const basic_partition_map_builder &lhs,
const basic_partition_map_builder &rhs);
friend bool operator==(const basic_partition_map_builder& lhs,
const basic_partition_map_builder& rhs);
/**
* The opposite logic of the == operator.
*/
friend bool operator!=(const basic_partition_map_builder &lhs,
const basic_partition_map_builder &rhs);
friend bool operator!=(const basic_partition_map_builder& lhs,
const basic_partition_map_builder& rhs);
/**
* You can check whether the object was created
@@ -347,12 +346,12 @@ public:
/**
* Get Map_t object reference
*/
Map_t &operator*();
Map_t& operator*();
/**
* Get constant Map_t object reference
*/
const Map_t &operator*() const;
const Map_t& operator*() const;
/**
* Get Info structure with given index
@@ -362,7 +361,7 @@ public:
/**
* Get BasicInfo structure with given index
*/
BasicInf operator[](const std::string_view &name) const;
BasicInf operator[](const std::string_view& name) const;
/**
* Get map contents as vector (PartitionManager::Info type).
@@ -402,7 +401,7 @@ constexpr uint64_t FAT16_FS = 0x3631544146;
constexpr uint64_t FAT32_FS = 0x3233544146;
constexpr uint64_t NTFS_FS = 0x5346544E;
constexpr uint64_t MSDOS_FS = 0x4d44;
} // namespace FileSystemMagic
} // namespace FileSystemMagic
namespace AndroidMagic {
constexpr uint64_t BOOT_IMAGE = 0x2144494F52444E41;
@@ -413,23 +412,23 @@ constexpr uint64_t VBMETA_IMAGE = 0x425641;
constexpr uint64_t SUPER_IMAGE = 0x7265797573;
constexpr uint64_t SPARSE_IMAGE = 0x3AFF26ED;
constexpr uint64_t ELF =
0x464C457F; // It makes more sense than between file systems
0x464C457F; // It makes more sense than between file systems
constexpr uint64_t RAW = 0x00000000;
} // namespace AndroidMagic
} // namespace AndroidMagic
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);
bool hasMagic(uint64_t magic, ssize_t buf, const std::string& path);
std::string formatMagic(uint64_t magic);
} // namespace Extras
} // namespace PartitionMap
} // namespace Extras
} // namespace PartitionMap
#define MAP "libpartition_map"
#define COMMON_LAMBDA_PARAMS \
(const std::string &partition, const PartitionMap::BasicInf props)
#define COMMON_LAMBDA_PARAMS \
(const std::string& partition, const PartitionMap::BasicInf props)
#endif // #ifndef LIBPARTITION_MAP_LIB_HPP
#endif // #ifndef LIBPARTITION_MAP_LIB_HPP

View File

@@ -25,8 +25,8 @@ Map_t basic_partition_map_builder::getAll() const {
return _current_map;
}
std::optional<std::pair<uint64_t, bool>>
basic_partition_map_builder::get(const std::string_view name) const {
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;
@@ -39,7 +39,7 @@ basic_partition_map_builder::getLogicalPartitionList() const {
_map_build_check();
std::list<std::string> logicals;
for (const auto &[name, props] : _current_map)
for (const auto& [name, props] : _current_map)
if (props.isLogical) logicals.push_back(name);
if (logicals.empty()) return std::nullopt;
@@ -51,7 +51,7 @@ basic_partition_map_builder::getPhysicalPartitionList() const {
_map_build_check();
std::list<std::string> physicals;
for (const auto &[name, props] : _current_map)
for (const auto& [name, props] : _current_map)
if (!props.isLogical) physicals.push_back(name);
if (physicals.empty()) return std::nullopt;
@@ -63,8 +63,7 @@ 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);
for (const auto& [name, props] : _current_map) partitions.push_back(name);
if (partitions.empty()) return std::nullopt;
return partitions;
@@ -78,8 +77,8 @@ std::string basic_partition_map_builder::getRealLinkPathOf(
return std::string(_workdir + "/" + name.data());
}
std::string
basic_partition_map_builder::getRealPathOf(const std::string_view name) const {
std::string basic_partition_map_builder::getRealPathOf(
const std::string_view name) const {
_map_build_check();
const std::string full = (isLogical(name))
@@ -93,4 +92,4 @@ basic_partition_map_builder::getRealPathOf(const std::string_view name) const {
std::string basic_partition_map_builder::getCurrentWorkDir() const {
return _workdir;
}
} // namespace PartitionMap
} // namespace PartitionMap

View File

@@ -15,12 +15,13 @@
*/
#include <fcntl.h>
#include <unistd.h>
#include <libhelper/lib.hpp>
#include <libpartition_map/lib.hpp>
#include <map>
#include <sstream>
#include <string>
#include <unistd.h>
#include "PartitionManager/PartitionManager.hpp"
@@ -76,7 +77,7 @@ size_t getMagicLength(const uint64_t magic) {
}
bool hasMagic(const uint64_t magic, const ssize_t buf,
const std::string &path) {
const std::string& path) {
LOGN(MAP, INFO) << "Checking magic of " << path << " with using " << buf
<< " byte buffer size (has magic 0x" << std::hex << magic
<< "?)" << std::endl;
@@ -89,7 +90,7 @@ bool hasMagic(const uint64_t magic, const ssize_t buf,
return false;
}
auto *buffer = new (std::nothrow) uint8_t[buf];
auto* buffer = new (std::nothrow) uint8_t[buf];
collector.delAfterProgress(buffer);
const ssize_t bytesRead = read(fd, buffer, buf);
@@ -120,4 +121,4 @@ std::string formatMagic(const uint64_t magic) {
<< magic;
return ss.str();
}
} // namespace PartitionMap::Extras
} // namespace PartitionMap::Extras

View File

@@ -14,24 +14,26 @@
limitations under the License.
*/
#include <fcntl.h>
#include <algorithm>
#include <array>
#include <cerrno>
#include <cstring>
#include <fcntl.h>
#include <filesystem>
#ifndef ANDROID_BUILD
#include <generated/buildInfo.hpp>
#endif
#include <iostream>
#include <libpartition_map/lib.hpp>
#include <linux/fs.h>
#include <memory>
#include <string>
#include <string_view>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <iostream>
#include <libpartition_map/lib.hpp>
#include <memory>
#include <string>
#include <string_view>
#include <vector>
static constexpr std::array<std::string_view, 3> defaultEntryList = {
@@ -60,7 +62,7 @@ Map_t basic_partition_map_builder::_build_map(std::string_view path,
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) {
std::sort(entries.begin(), entries.end(), [](const auto& a, const auto& b) {
return a.path().filename() < b.path().filename();
});
@@ -69,7 +71,7 @@ Map_t basic_partition_map_builder::_build_map(std::string_view path,
<< " is exists but generated vector is empty "
"(std::vector<std::filesystem::directory_entry>)."
<< std::endl;
for (const auto &entry : entries) {
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()),
@@ -82,7 +84,7 @@ Map_t basic_partition_map_builder::_build_map(std::string_view path,
return map;
}
void basic_partition_map_builder::_insert_logicals(Map_t &&logicals) {
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;
@@ -96,7 +98,7 @@ 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) {
uint64_t basic_partition_map_builder::_get_size(const std::string& path) {
const std::string real = std::filesystem::read_symlink(path);
Helper::garbageCollector collector;
@@ -120,7 +122,7 @@ uint64_t basic_partition_map_builder::_get_size(const std::string &path) {
basic_partition_map_builder::basic_partition_map_builder() {
LOGN(MAP, INFO) << "default constructor called. Starting build." << std::endl;
for (const auto &path : defaultEntryList) {
for (const auto& path : defaultEntryList) {
if (std::filesystem::exists(path)) {
_current_map = _build_map(path);
if (_current_map.empty()) {
@@ -149,8 +151,10 @@ basic_partition_map_builder::basic_partition_map_builder(
if (std::filesystem::exists(path)) {
if (!_is_real_block_dir(path)) return;
_current_map = _build_map(path);
if (_current_map.empty()) _any_generating_error = true;
else _workdir = 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());
@@ -162,7 +166,7 @@ basic_partition_map_builder::basic_partition_map_builder(
}
basic_partition_map_builder::basic_partition_map_builder(
basic_partition_map_builder &&other) noexcept {
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;
@@ -178,7 +182,7 @@ bool basic_partition_map_builder::hasPartition(
bool basic_partition_map_builder::hasLogicalPartitions() const {
_map_build_check();
for (const auto &[name, props] : _current_map)
for (const auto& [name, props] : _current_map)
if (props.isLogical) return true;
return false;
@@ -205,7 +209,8 @@ bool basic_partition_map_builder::readDirectory(const std::string_view path) {
if (_current_map.empty()) {
_any_generating_error = true;
return false;
} else _workdir = path;
} else
_workdir = path;
} else
throw Error("Cannot find directory: %s. Cannot build partition map!",
path.data());
@@ -220,7 +225,7 @@ bool basic_partition_map_builder::readDefaultDirectories() {
_map_builded = false;
LOGN(MAP, INFO) << "read default directories request." << std::endl;
for (const auto &path : defaultEntryList) {
for (const auto& path : defaultEntryList) {
if (std::filesystem::exists(path)) {
_current_map = _build_map(path);
if (_current_map.empty()) {
@@ -244,48 +249,49 @@ bool basic_partition_map_builder::readDefaultDirectories() {
}
bool basic_partition_map_builder::copyPartitionsToVector(
std::vector<std::string> &vec) const {
std::vector<std::string>& vec) const {
if (_current_map.empty()) {
LOGN(MAP, ERROR) << "Current map is empty.";
return false;
}
vec.clear();
for (const auto &[name, props] : _current_map)
vec.push_back(name);
for (const auto& [name, props] : _current_map) vec.push_back(name);
return true;
}
bool basic_partition_map_builder::copyLogicalPartitionsToVector(
std::vector<std::string> &vec) const {
std::vector<std::string>& vec) const {
if (_current_map.empty()) {
LOGN(MAP, ERROR) << "Current map is empty.";
return false;
}
std::vector<std::string> vec2;
for (const auto &[name, props] : _current_map)
for (const auto& [name, props] : _current_map)
if (props.isLogical) vec2.push_back(name);
if (vec2.empty()) {
LOGN(MAP, ERROR) << "Cannot find logical partitions in current map.";
return false;
} else vec = vec2;
} else
vec = vec2;
return true;
}
bool basic_partition_map_builder::copyPhysicalPartitionsToVector(
std::vector<std::string> &vec) const {
std::vector<std::string>& vec) const {
if (_current_map.empty()) {
LOGN(MAP, ERROR) << "Current map is empty.";
return false;
}
std::vector<std::string> vec2;
for (const auto &[name, props] : _current_map)
for (const auto& [name, props] : _current_map)
if (!props.isLogical) vec2.push_back(name);
if (vec2.empty()) {
LOGN(MAP, ERROR) << "Cannot find physical partitions in current map.";
return false;
} else vec = vec2;
} else
vec = vec2;
return true;
}
@@ -295,12 +301,12 @@ bool basic_partition_map_builder::empty() const {
}
bool basic_partition_map_builder::doForAllPartitions(
const std::function<bool(std::string, BasicInf)> &func) const {
const std::function<bool(std::string, BasicInf)>& func) const {
_map_build_check();
bool err = false;
LOGN(MAP, INFO) << "Doing input function for all partitions." << std::endl;
for (const auto &[name, props] : _current_map) {
for (const auto& [name, props] : _current_map) {
if (func(name, {props.size, props.isLogical}))
LOGN(MAP, INFO) << "Done progress for " << name << " partition."
<< std::endl;
@@ -315,13 +321,13 @@ bool basic_partition_map_builder::doForAllPartitions(
}
bool basic_partition_map_builder::doForPhysicalPartitions(
const std::function<bool(std::string, BasicInf)> &func) const {
const std::function<bool(std::string, BasicInf)>& func) const {
_map_build_check();
bool err = false;
LOGN(MAP, INFO) << "Doing input function for physical partitions."
<< std::endl;
for (const auto &[name, props] : _current_map) {
for (const auto& [name, props] : _current_map) {
if (props.isLogical) continue;
if (func(name, {props.size, props.isLogical}))
LOGN(MAP, INFO) << "Done progress for " << name << " partition."
@@ -337,13 +343,13 @@ bool basic_partition_map_builder::doForPhysicalPartitions(
}
bool basic_partition_map_builder::doForLogicalPartitions(
const std::function<bool(std::string, BasicInf)> &func) const {
const std::function<bool(std::string, BasicInf)>& func) const {
_map_build_check();
bool err = false;
LOGN(MAP, INFO) << "Doing input function for logical partitions."
<< std::endl;
for (const auto &[name, props] : _current_map) {
for (const auto& [name, props] : _current_map) {
if (!props.isLogical) continue;
if (func(name, {props.size, props.isLogical}))
LOGN(MAP, INFO) << "Done progress for " << name << " partition."
@@ -359,14 +365,14 @@ bool basic_partition_map_builder::doForLogicalPartitions(
}
bool basic_partition_map_builder::doForPartitionList(
const std::vector<std::string> &partitions,
const std::function<bool(std::string, BasicInf)> &func) const {
const std::vector<std::string>& partitions,
const std::function<bool(std::string, BasicInf)>& func) const {
_map_build_check();
bool err = false;
LOGN(MAP, INFO) << "Doing input function for input partition list."
<< std::endl;
for (const auto &partition : partitions) {
for (const auto& partition : partitions) {
if (!hasPartition(partition))
throw Error("Couldn't find partition: %s", partition.data());
if (!func(partition, _current_map[partition])) {
@@ -379,19 +385,19 @@ bool basic_partition_map_builder::doForPartitionList(
return err;
}
uint64_t
basic_partition_map_builder::sizeOf(const std::string_view name) const {
uint64_t basic_partition_map_builder::sizeOf(
const std::string_view name) const {
_map_build_check();
return _current_map.get_size(name);
}
bool operator==(const basic_partition_map_builder &lhs,
const 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!=(const basic_partition_map_builder &lhs,
const basic_partition_map_builder &rhs) {
bool operator!=(const basic_partition_map_builder& lhs,
const basic_partition_map_builder& rhs) {
return !(lhs == rhs);
}
@@ -409,9 +415,9 @@ bool basic_partition_map_builder::operator()(const std::string_view path) {
return readDirectory(path);
}
Map_t &basic_partition_map_builder::operator*() { return _current_map; }
Map_t& basic_partition_map_builder::operator*() { return _current_map; }
const Map_t &basic_partition_map_builder::operator*() const {
const Map_t& basic_partition_map_builder::operator*() const {
return _current_map;
}
@@ -419,8 +425,8 @@ Info basic_partition_map_builder::operator[](const int index) const {
return _current_map[index];
}
BasicInf
basic_partition_map_builder::operator[](const std::string_view &name) const {
BasicInf basic_partition_map_builder::operator[](
const std::string_view& name) const {
return _current_map[name];
}
@@ -435,4 +441,4 @@ basic_partition_map_builder::operator int() const {
basic_partition_map_builder::operator std::string() const { return _workdir; }
std::string getLibVersion() { MKVERSION("libpartition_map"); }
} // namespace PartitionMap
} // namespace PartitionMap

View File

@@ -20,16 +20,16 @@
#include <utility>
namespace PartitionMap {
basic_partition_map::iterator::iterator(_entry *p) : ptr(p) {}
basic_partition_map::iterator::iterator(_entry* p) : ptr(p) {}
auto basic_partition_map::iterator::operator*() const
-> std::pair<std::string &, decltype(_entry::props) &> {
-> std::pair<std::string&, decltype(_entry::props)&> {
return {ptr->name, ptr->props};
}
_entry *basic_partition_map::iterator::operator->() const { return ptr; }
_entry* basic_partition_map::iterator::operator->() const { return ptr; }
basic_partition_map::iterator &basic_partition_map::iterator::operator++() {
basic_partition_map::iterator& basic_partition_map::iterator::operator++() {
++ptr;
return *this;
}
@@ -40,27 +40,27 @@ basic_partition_map::iterator basic_partition_map::iterator::operator++(int) {
return tmp;
}
bool basic_partition_map::iterator::operator==(const iterator &other) const {
bool basic_partition_map::iterator::operator==(const iterator& other) const {
return ptr == other.ptr;
}
bool basic_partition_map::iterator::operator!=(const iterator &other) const {
bool basic_partition_map::iterator::operator!=(const iterator& other) const {
return ptr != other.ptr;
}
basic_partition_map::constant_iterator::constant_iterator(const _entry *p)
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) &> {
-> std::pair<const std::string&, const decltype(_entry::props)&> {
return {ptr->name, ptr->props};
}
const _entry *basic_partition_map::constant_iterator::operator->() const {
const _entry* basic_partition_map::constant_iterator::operator->() const {
return ptr;
}
basic_partition_map::constant_iterator &
basic_partition_map::constant_iterator&
basic_partition_map::constant_iterator::operator++() {
++ptr;
return *this;
@@ -74,21 +74,20 @@ basic_partition_map::constant_iterator::operator++(int) {
}
bool basic_partition_map::constant_iterator::operator==(
const constant_iterator &other) const {
const constant_iterator& other) const {
return ptr == other.ptr;
}
bool basic_partition_map::constant_iterator::operator!=(
const constant_iterator &other) const {
const constant_iterator& other) const {
return ptr != other.ptr;
}
void basic_partition_map::_resize_map() {
const size_t new_capacity = _capacity * 2;
auto *new_data = new _entry[new_capacity];
auto* new_data = new _entry[new_capacity];
for (size_t i = 0; i < _count; i++)
new_data[i] = _data[i];
for (size_t i = 0; i < _count; i++) new_data[i] = _data[i];
delete[] _data;
_data = new_data;
@@ -103,21 +102,23 @@ int basic_partition_map::_index_of(const std::string_view name) const {
return 0;
}
basic_partition_map::basic_partition_map(const std::string &name,
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);
}
basic_partition_map::basic_partition_map(const basic_partition_map &other)
: _data(new _entry[other._capacity]), _count(other._count),
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(basic_partition_map &&other) noexcept
: _data(new _entry[other._capacity]), _count(other._count),
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();
@@ -129,7 +130,7 @@ basic_partition_map::basic_partition_map() : _capacity(6) {
basic_partition_map::~basic_partition_map() { delete[] _data; }
bool basic_partition_map::insert(const std::string &name, const uint64_t size,
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();
@@ -141,9 +142,9 @@ bool basic_partition_map::insert(const std::string &name, const uint64_t size,
return true;
}
void basic_partition_map::merge(const basic_partition_map &map) {
void basic_partition_map::merge(const basic_partition_map& map) {
LOGN(MAP, INFO) << "map merge request." << std::endl;
for (const auto &[name, props] : map)
for (const auto& [name, props] : map)
insert(name, props.size, props.isLogical);
LOGN(MAP, INFO) << "map merged successfully." << std::endl;
}
@@ -162,8 +163,8 @@ bool basic_partition_map::is_logical(const std::string_view name) const {
return false;
}
_returnable_entry
basic_partition_map::get_all(const std::string_view name) const {
_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};
@@ -176,7 +177,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;
return {};
@@ -194,8 +195,8 @@ void basic_partition_map::clear() {
_data = new _entry[_capacity];
}
basic_partition_map &
basic_partition_map::operator=(const basic_partition_map &map) {
basic_partition_map& basic_partition_map::operator=(
const basic_partition_map& map) {
if (this != &map) {
delete[] _data;
@@ -208,7 +209,7 @@ basic_partition_map::operator=(const basic_partition_map &map) {
return *this;
}
bool basic_partition_map::operator==(const basic_partition_map &other) const {
bool basic_partition_map::operator==(const basic_partition_map& other) const {
if (this->_capacity != other._capacity || this->_count != other._count)
return false;
@@ -217,12 +218,13 @@ bool basic_partition_map::operator==(const basic_partition_map &other) const {
_data[i].props.size == other._data[i].props.size &&
_data[i].props.isLogical == other._data[i].props.isLogical)
continue;
else return false;
else
return false;
return true;
}
bool basic_partition_map::operator!=(const basic_partition_map &other) const {
bool basic_partition_map::operator!=(const basic_partition_map& other) const {
return !(*this == other);
}
@@ -235,7 +237,7 @@ Info basic_partition_map::operator[](const int index) const {
return _data[index];
}
BasicInf basic_partition_map::operator[](const std::string_view &name) const {
BasicInf basic_partition_map::operator[](const std::string_view& name) const {
if (_count == 0) return {};
if (const int i = _index_of(name); name == _data[i].name)
@@ -270,4 +272,4 @@ basic_partition_map::constant_iterator basic_partition_map::cbegin() const {
basic_partition_map::constant_iterator basic_partition_map::cend() const {
return constant_iterator(_data + _count);
}
} // namespace PartitionMap
} // namespace PartitionMap

View File

@@ -14,10 +14,11 @@
limitations under the License.
*/
#include <unistd.h>
#include <fstream>
#include <iostream>
#include <libpartition_map/lib.hpp>
#include <unistd.h>
int main() {
if (getuid() != 0) return 2;
@@ -31,7 +32,7 @@ int main() {
const auto map = MyMap.getAll();
if (map.empty()) throw PartitionMap::Error("getAll() empty");
for (const auto &[name, props] : map) {
for (const auto& [name, props] : map) {
std::cout << "Partition: " << name << ", size: " << props.size
<< ", logical: " << props.isLogical << std::endl;
}
@@ -45,14 +46,14 @@ int main() {
if (!logicals)
throw PartitionMap::Error("getLogicalPartitionList() returned nullopt");
std::cout << "Logical partitions: " << std::endl;
for (const auto &name : *logicals)
for (const auto& name : *logicals)
std::cout << " - " << name << std::endl;
const auto physicals = MyMap.getPhysicalPartitionList();
if (!physicals)
throw PartitionMap::Error("getPhysicalPartitionList() returned nullopt");
std::cout << "Physical partitions: " << std::endl;
for (const auto &name : *physicals)
for (const auto& name : *physicals)
std::cout << " - " << name << std::endl;
if (const std::vector<PartitionMap::Info> parts =
@@ -61,7 +62,7 @@ int main() {
throw PartitionMap::Error(
"operator std::vector<PartitionMap::Info>() returned empty vector");
auto func = [](const std::string &partition,
auto func = [](const std::string& partition,
const PartitionMap::BasicInf props) -> bool {
std::ofstream f("parts.txt");
f << "Partition: " << partition << ", size: " << props.size
@@ -96,10 +97,10 @@ int main() {
if (MyMap != MyMap2) std::cout << "map1 != map2" << std::endl;
std::cout << PartitionMap::getLibVersion() << std::endl;
} catch (PartitionMap::Error &error) {
} catch (PartitionMap::Error& error) {
std::cerr << error.what() << std::endl;
return 1;
} catch (std::ios_base::failure &error) {
} catch (std::ios_base::failure& error) {
std::cerr << "fstream error: " << error.what() << std::endl;
return 1;
}