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:
@@ -255,25 +255,25 @@ public:
|
|||||||
/**
|
/**
|
||||||
* Do input function (lambda) for all partitions.
|
* Do input function (lambda) for all partitions.
|
||||||
*/
|
*/
|
||||||
void doForAllPartitions(
|
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.
|
* Do input function (lambda) for physical partitions.
|
||||||
*/
|
*/
|
||||||
void doForPhysicalPartitions(
|
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.
|
* Do input function (lambda) for logical partitions.
|
||||||
*/
|
*/
|
||||||
void doForLogicalPartitions(
|
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.
|
* Do input function (lambda) for input partition list.
|
||||||
*/
|
*/
|
||||||
void doForPartitionList(
|
bool doForPartitionList(
|
||||||
const std::vector<std::string> &partitions,
|
const std::vector<std::string> &partitions,
|
||||||
const std::function<bool(std::string, BasicInf)> &func) const;
|
const std::function<bool(std::string, BasicInf)> &func) const;
|
||||||
|
|
||||||
|
|||||||
@@ -287,24 +287,30 @@ bool basic_partition_map_builder::empty() const {
|
|||||||
return _current_map.empty();
|
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 {
|
const std::function<bool(std::string, BasicInf)> &func) const {
|
||||||
_map_build_check();
|
_map_build_check();
|
||||||
|
bool err = false;
|
||||||
|
|
||||||
LOGN(MAP, INFO) << "Doing input function for all partitions." << std::endl;
|
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}))
|
if (func(name, {props.size, props.isLogical}))
|
||||||
LOGN(MAP, INFO) << "Done progress for " << name << " partition."
|
LOGN(MAP, INFO) << "Done progress for " << name << " partition."
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
else
|
else {
|
||||||
|
err = true;
|
||||||
LOGN(MAP, ERROR) << "Failed progress for " << name << " partition."
|
LOGN(MAP, ERROR) << "Failed progress for " << name << " partition."
|
||||||
<< std::endl;
|
<< 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 {
|
const std::function<bool(std::string, BasicInf)> &func) const {
|
||||||
_map_build_check();
|
_map_build_check();
|
||||||
|
bool err = false;
|
||||||
|
|
||||||
LOGN(MAP, INFO) << "Doing input function for physical partitions."
|
LOGN(MAP, INFO) << "Doing input function for physical partitions."
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
@@ -313,15 +319,20 @@ void basic_partition_map_builder::doForPhysicalPartitions(
|
|||||||
if (func(name, {props.size, props.isLogical}))
|
if (func(name, {props.size, props.isLogical}))
|
||||||
LOGN(MAP, INFO) << "Done progress for " << name << " partition."
|
LOGN(MAP, INFO) << "Done progress for " << name << " partition."
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
else
|
else {
|
||||||
|
err = true;
|
||||||
LOGN(MAP, ERROR) << "Failed progress for " << name << " partition."
|
LOGN(MAP, ERROR) << "Failed progress for " << name << " partition."
|
||||||
<< std::endl;
|
<< 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 {
|
const std::function<bool(std::string, BasicInf)> &func) const {
|
||||||
_map_build_check();
|
_map_build_check();
|
||||||
|
bool err = false;
|
||||||
|
|
||||||
LOGN(MAP, INFO) << "Doing input function for logical partitions."
|
LOGN(MAP, INFO) << "Doing input function for logical partitions."
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
@@ -330,26 +341,35 @@ void basic_partition_map_builder::doForLogicalPartitions(
|
|||||||
if (func(name, {props.size, props.isLogical}))
|
if (func(name, {props.size, props.isLogical}))
|
||||||
LOGN(MAP, INFO) << "Done progress for " << name << " partition."
|
LOGN(MAP, INFO) << "Done progress for " << name << " partition."
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
else
|
else {
|
||||||
|
err = true;
|
||||||
LOGN(MAP, ERROR) << "Failed progress for " << name << " partition."
|
LOGN(MAP, ERROR) << "Failed progress for " << name << " partition."
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
void basic_partition_map_builder::doForPartitionList(
|
bool basic_partition_map_builder::doForPartitionList(
|
||||||
const std::vector<std::string> &partitions,
|
const std::vector<std::string> &partitions,
|
||||||
const std::function<bool(std::string, BasicInf)> &func) const {
|
const std::function<bool(std::string, BasicInf)> &func) const {
|
||||||
_map_build_check();
|
_map_build_check();
|
||||||
|
bool err = false;
|
||||||
|
|
||||||
LOGN(MAP, INFO) << "Doing input function for input partition list."
|
LOGN(MAP, INFO) << "Doing input function for input partition list."
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
for (const auto &partition : partitions) {
|
for (const auto &partition : partitions) {
|
||||||
if (!hasPartition(partition))
|
if (!hasPartition(partition))
|
||||||
throw Error("Couldn't find partition: %s", partition.data());
|
throw Error("Couldn't find partition: %s", partition.data());
|
||||||
if (!func(partition, {sizeOf(partition), isLogical(partition)}))
|
if (!func(partition, {sizeOf(partition), isLogical(partition)})) {
|
||||||
LOGN(MAP, ERROR) << "Failed progress for partition: " << partition
|
err = true;
|
||||||
|
LOGN(MAP, ERROR) << "Failed progress for " << partition << " partition."
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t
|
uint64_t
|
||||||
|
|||||||
@@ -18,6 +18,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <libpartition_map/lib.hpp>
|
#include <libpartition_map/lib.hpp>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <pstl/glue_execution_defs.h>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
if (getuid() != 0) return 2;
|
if (getuid() != 0) return 2;
|
||||||
@@ -64,9 +65,11 @@ int main() {
|
|||||||
std::ofstream f("parts.txt");
|
std::ofstream f("parts.txt");
|
||||||
f << "Partition: " << partition << ", size: " << props.size
|
f << "Partition: " << partition << ", size: " << props.size
|
||||||
<< ", logical: " << props.isLogical;
|
<< ", logical: " << props.isLogical;
|
||||||
|
f.exceptions(std::ios_base::failbit | std::ios_base::badbit);
|
||||||
return !f.fail();
|
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 << "Total partitions count: " << (int)MyMap << std::endl;
|
||||||
std::cout << "Boot: " << MyMap.getRealLinkPathOf("boot") << std::endl;
|
std::cout << "Boot: " << MyMap.getRealLinkPathOf("boot") << std::endl;
|
||||||
@@ -95,6 +98,9 @@ int main() {
|
|||||||
} catch (PartitionMap::Error &error) {
|
} catch (PartitionMap::Error &error) {
|
||||||
std::cerr << error.what() << std::endl;
|
std::cerr << error.what() << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
|
} catch (std::ios_base::failure &error) {
|
||||||
|
std::cerr << "fstream error: " << error.what() << std::endl;
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user