pmt: reformat code

This commit is contained in:
2025-08-10 18:44:58 +03:00
parent 787a2e4e46
commit f5e465b995
29 changed files with 12503 additions and 10715 deletions

368
srclib/libpartition_map/src/PartitionMap.cpp Executable file → Normal file
View File

@@ -14,222 +14,242 @@
limitations under the License.
*/
#include <iostream>
#include <vector>
#include <filesystem>
#include <memory>
#include <algorithm>
#include <array>
#include <cerrno>
#include <cstring>
#include <fcntl.h>
#include <filesystem>
#include <generated/buildInfo.hpp>
#include <iostream>
#include <libpartition_map/lib.hpp>
#include <linux/fs.h>
#include <memory>
#include <string>
#include <string_view>
#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 <cstring>
#include <sys/stat.h>
#include <unistd.h>
#include <vector>
static constexpr std::array<std::string_view, 3> defaultEntryList = {
"/dev/block/by-name",
"/dev/block/bootdevice/by-name",
"/dev/block/platform/bootdevice/by-name"
};
"/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) {
if (path.find("/block/") == std::string::npos) {
LOGN(MAP, ERROR) << "Path " << path << " is not a real block directory.";
return false;
}
return true;
}
bool basic_partition_map_builder::_is_real_block_dir(
const std::string_view path) {
if (path.find("/block/") == std::string::npos) {
LOGN(MAP, ERROR) << "Path " << path << " is not a real block directory.";
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();
});
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();
});
LOGN_IF(MAP, WARNING, entries.empty()) << "" << path <<
"is exists but generated vector is empty (std::vector<std::filesystem::directory_entry>)." << std::endl;
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);
}
LOGN_IF(MAP, WARNING, entries.empty())
<< "" << path
<< "is exists but generated vector is empty "
"(std::vector<std::filesystem::directory_entry>)."
<< std::endl;
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);
}
LOGN(MAP, INFO) << std::boolalpha << "Map generated successfully. is_logical_map=" << logical << std::endl;
return map;
}
LOGN(MAP, INFO) << std::boolalpha
<< "Map generated successfully. is_logical_map=" << logical
<< std::endl;
return map;
}
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);
}
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);
}
void basic_partition_map_builder::_map_build_check() const {
if (!_map_builded)
throw Error("Please build partition map before!");
}
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) {
const std::string real = std::filesystem::read_symlink(path);
Helper::garbageCollector collector;
uint64_t basic_partition_map_builder::_get_size(const std::string &path) {
const std::string real = std::filesystem::read_symlink(path);
Helper::garbageCollector collector;
const int fd = Helper::openAndAddToCloseList(real, collector, O_RDONLY);
if (fd < 0) {
LOGN(MAP, ERROR) << "Cannot open " << real << ": " << strerror(errno) << std::endl;
return 0;
}
const int fd = Helper::openAndAddToCloseList(real, collector, O_RDONLY);
if (fd < 0) {
LOGN(MAP, ERROR) << "Cannot open " << real << ": " << strerror(errno)
<< std::endl;
return 0;
}
uint64_t size = 0;
if (ioctl(fd, BLKGETSIZE64, &size) != 0) {
LOGN(MAP, ERROR) << "ioctl() process failed for " << real << ": " << strerror(errno) << std::endl;
return 0;
}
uint64_t size = 0;
if (ioctl(fd, BLKGETSIZE64, &size) != 0) {
LOGN(MAP, ERROR) << "ioctl() process failed for " << real << ": "
<< strerror(errno) << std::endl;
return 0;
}
return size;
}
return size;
}
basic_partition_map_builder::basic_partition_map_builder() {
LOGN(MAP, INFO) << "default constructor called. Starting build." << std::endl;
basic_partition_map_builder::basic_partition_map_builder() {
LOGN(MAP, INFO) << "default constructor called. Starting build." << 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;
} else {
_workdir = path;
break;
}
}
}
for (const auto &path : defaultEntryList) {
if (std::filesystem::exists(path)) {
_current_map = _build_map(path);
if (_current_map.empty()) {
_any_generating_error = true;
} else {
_workdir = path;
break;
}
}
}
if (_current_map.empty())
LOGN(MAP, ERROR) << "Cannot build map by any default search entry." << std::endl;
if (_current_map.empty())
LOGN(MAP, ERROR) << "Cannot build map by any default search entry."
<< std::endl;
LOGN(MAP, INFO) << "default constructor successfully ended work." << std::endl;
_insert_logicals(_build_map("/dev/block/mapper", true));
_map_builded = true;
}
LOGN(MAP, INFO) << "default constructor successfully ended work."
<< std::endl;
_insert_logicals(_build_map("/dev/block/mapper", true));
_map_builded = true;
}
basic_partition_map_builder::basic_partition_map_builder(const std::string_view path) {
LOGN(MAP, INFO) << "argument-based constructor called. Starting build." << std::endl;
basic_partition_map_builder::basic_partition_map_builder(
const std::string_view path) {
LOGN(MAP, INFO) << "argument-based constructor called. Starting build."
<< std::endl;
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;
} else
throw Error("Cannot find directory: %s. Cannot build partition map!", path.data());
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;
} else
throw Error("Cannot find directory: %s. Cannot build partition map!",
path.data());
LOGN(MAP, INFO) << "argument-based constructor successfully ended work." << std::endl;
_insert_logicals(_build_map("/dev/block/mapper", true));
_map_builded = true;
}
LOGN(MAP, INFO) << "argument-based constructor successfully ended work."
<< std::endl;
_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::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);
}
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;
}
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;
LOGN(MAP, INFO) << "read " << path << " directory request." << std::endl;
bool basic_partition_map_builder::readDirectory(const std::string_view path) {
_map_builded = false;
LOGN(MAP, INFO) << "read " << path << " directory request." << std::endl;
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());
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());
LOGN(MAP, INFO) << "read " << path << " successfull." << std::endl;
_insert_logicals(_build_map("/dev/block/mapper", true));
_map_builded = true;
return true;
}
LOGN(MAP, INFO) << "read " << path << " successfull." << std::endl;
_insert_logicals(_build_map("/dev/block/mapper", true));
_map_builded = true;
return true;
}
bool basic_partition_map_builder::readDefaultDirectories() {
_map_builded = false;
LOGN(MAP, INFO) << "read default directories request." << std::endl;
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;
}
}
}
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;
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;
}
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();
return _current_map.empty();
}
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);
}
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) {
return lhs._current_map == rhs._current_map;
}
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) {
return !(lhs == rhs);
}
bool operator!=(const basic_partition_map_builder &lhs,
const basic_partition_map_builder &rhs) {
return !(lhs == rhs);
}
basic_partition_map_builder::operator bool() const {
return !this->_any_generating_error;
}
basic_partition_map_builder::operator bool() const {
return !this->_any_generating_error;
}
bool basic_partition_map_builder::operator!() const {
return this->_any_generating_error;
}
bool basic_partition_map_builder::operator!() const {
return this->_any_generating_error;
}
bool basic_partition_map_builder::operator()(const std::string_view path) {
LOGN(MAP, INFO) << "calling readDirectory() for building map with " << path << std::endl;
return readDirectory(path);
}
bool basic_partition_map_builder::operator()(const std::string_view path) {
LOGN(MAP, INFO) << "calling readDirectory() for building map with " << path
<< std::endl;
return readDirectory(path);
}
std::string getLibVersion() {
MKVERSION("libpartition_map");
}
std::string getLibVersion() { MKVERSION("libpartition_map"); }
} // namespace PartitionMap