pmt: Improve libpartition_map and tests

- Change doFor*** function return types as bool.
 - Add catching of doFor*** function test results.
 - Use std::ios_base_failure for catch std::fstream errors on tests
This commit is contained in:
2025-09-01 01:35:45 +03:00
parent e7baf4f5bc
commit 0832b57828
3 changed files with 40 additions and 14 deletions

View File

@@ -255,25 +255,25 @@ public:
/**
* Do input function (lambda) for all partitions.
*/
void doForAllPartitions(
bool doForAllPartitions(
const std::function<bool(std::string, BasicInf)> &func) const;
/**
* Do input function (lambda) for physical partitions.
*/
void doForPhysicalPartitions(
bool doForPhysicalPartitions(
const std::function<bool(std::string, BasicInf)> &func) const;
/**
* Do input function (lambda) for logical partitions.
*/
void doForLogicalPartitions(
bool doForLogicalPartitions(
const std::function<bool(std::string, BasicInf)> &func) const;
/**
* Do input function (lambda) for input partition list.
*/
void doForPartitionList(
bool doForPartitionList(
const std::vector<std::string> &partitions,
const std::function<bool(std::string, BasicInf)> &func) const;

View File

@@ -287,24 +287,30 @@ bool basic_partition_map_builder::empty() const {
return _current_map.empty();
}
void basic_partition_map_builder::doForAllPartitions(
bool basic_partition_map_builder::doForAllPartitions(
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) {
if (func(name, {props.size, props.isLogical}))
LOGN(MAP, INFO) << "Done progress for " << name << " partition."
<< std::endl;
else
else {
err = true;
LOGN(MAP, ERROR) << "Failed progress for " << name << " partition."
<< std::endl;
}
}
return err;
}
void basic_partition_map_builder::doForPhysicalPartitions(
bool basic_partition_map_builder::doForPhysicalPartitions(
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;
@@ -313,15 +319,20 @@ void basic_partition_map_builder::doForPhysicalPartitions(
if (func(name, {props.size, props.isLogical}))
LOGN(MAP, INFO) << "Done progress for " << name << " partition."
<< std::endl;
else
else {
err = true;
LOGN(MAP, ERROR) << "Failed progress for " << name << " partition."
<< std::endl;
}
}
return err;
}
void basic_partition_map_builder::doForLogicalPartitions(
bool basic_partition_map_builder::doForLogicalPartitions(
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;
@@ -330,26 +341,35 @@ void basic_partition_map_builder::doForLogicalPartitions(
if (func(name, {props.size, props.isLogical}))
LOGN(MAP, INFO) << "Done progress for " << name << " partition."
<< std::endl;
else
else {
err = true;
LOGN(MAP, ERROR) << "Failed progress for " << name << " partition."
<< std::endl;
}
}
return err;
}
void basic_partition_map_builder::doForPartitionList(
bool basic_partition_map_builder::doForPartitionList(
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) {
if (!hasPartition(partition))
throw Error("Couldn't find partition: %s", partition.data());
if (!func(partition, {sizeOf(partition), isLogical(partition)}))
LOGN(MAP, ERROR) << "Failed progress for partition: " << partition
if (!func(partition, {sizeOf(partition), isLogical(partition)})) {
err = true;
LOGN(MAP, ERROR) << "Failed progress for " << partition << " partition."
<< std::endl;
}
}
return err;
}
uint64_t

View File

@@ -18,6 +18,7 @@
#include <iostream>
#include <libpartition_map/lib.hpp>
#include <unistd.h>
#include <pstl/glue_execution_defs.h>
int main() {
if (getuid() != 0) return 2;
@@ -64,9 +65,11 @@ int main() {
std::ofstream f("parts.txt");
f << "Partition: " << partition << ", size: " << props.size
<< ", logical: " << props.isLogical;
f.exceptions(std::ios_base::failbit | std::ios_base::badbit);
return !f.fail();
};
MyMap.doForAllPartitions(func);
if (!MyMap.doForAllPartitions(func))
throw PartitionMap::Error("doForAllPartitions() progress failed");
std::cout << "Total partitions count: " << (int)MyMap << std::endl;
std::cout << "Boot: " << MyMap.getRealLinkPathOf("boot") << std::endl;
@@ -95,6 +98,9 @@ int main() {
} catch (PartitionMap::Error &error) {
std::cerr << error.what() << std::endl;
return 1;
} catch (std::ios_base::failure &error) {
std::cerr << "fstream error: " << error.what() << std::endl;
return 1;
}
return 0;