Updated Library Documentation (markdown)
@@ -772,45 +772,132 @@ Interface to build and query partition maps.
|
|||||||
#include <libpartition_map/lib.hpp>
|
#include <libpartition_map/lib.hpp>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <list>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
try {
|
try {
|
||||||
PartitionMap::Map builder;
|
// -------------------------------
|
||||||
|
// Builder usage
|
||||||
|
// -------------------------------
|
||||||
|
PartitionMap::BuildMap builder; // Read default dirs
|
||||||
|
|
||||||
// Read default directories
|
// Build map from default directories
|
||||||
if (!builder.readDefaultDirectories()) {
|
if (!builder.readDefaultDirectories()) {
|
||||||
std::cerr << "Failed to read default directories" << std::endl;
|
std::cerr << "Failed to read default directories.\n";
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Query partitions
|
// Build map from a specific directory
|
||||||
auto allPartitions = builder.getPartitionList();
|
if (!builder.readDirectory("/dev/block/by-name")) {
|
||||||
if (allPartitions) {
|
std::cerr << "Failed to read /dev/block/by-name\n";
|
||||||
for (const auto &p : *allPartitions) {
|
|
||||||
std::cout << p << std::endl;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use doForAllPartitions lambda
|
// -------------------------------
|
||||||
builder.doForAllPartitions([](const std::string &name, PartitionMap::BasicInf props) {
|
// Accessing the map
|
||||||
std::cout << name << " -> size: " << props.size << ", logical: " << props.isLogical << std::endl;
|
// -------------------------------
|
||||||
return true; // Continue iteration
|
PartitionMap::Map_t map = *builder;
|
||||||
|
|
||||||
|
// Check if a partition exists
|
||||||
|
if (builder.hasPartition("boot")) {
|
||||||
|
std::cout << "Partition boot exists\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if a partition is logical
|
||||||
|
if (builder.isLogical("system")) {
|
||||||
|
std::cout << "Partition system is logical\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get partition size
|
||||||
|
uint64_t size = builder.sizeOf("boot");
|
||||||
|
std::cout << "Size of boot: " << size << " bytes\n";
|
||||||
|
|
||||||
|
// Get partition info (size + logical)
|
||||||
|
auto partInfo = builder.get("boot");
|
||||||
|
if (partInfo) {
|
||||||
|
std::cout << "boot - size: " << partInfo->first
|
||||||
|
<< ", logical: " << (partInfo->second ? "yes" : "no") << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get lists
|
||||||
|
auto allParts = builder.getPartitionList();
|
||||||
|
auto logicalParts = builder.getLogicalPartitionList();
|
||||||
|
auto physicalParts = builder.getPhysicalPartitionList();
|
||||||
|
|
||||||
|
if (allParts) {
|
||||||
|
std::cout << "All partitions:\n";
|
||||||
|
for (const auto &p : *allParts)
|
||||||
|
std::cout << " " << p << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (logicalParts) {
|
||||||
|
std::cout << "Logical partitions:\n";
|
||||||
|
for (const auto &p : *logicalParts)
|
||||||
|
std::cout << " " << p << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (physicalParts) {
|
||||||
|
std::cout << "Physical partitions:\n";
|
||||||
|
for (const auto &p : *physicalParts)
|
||||||
|
std::cout << " " << p << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------
|
||||||
|
// Lambda examples
|
||||||
|
// -------------------------------
|
||||||
|
builder.doForAllPartitions([](const std::string &name, PartitionMap::BasicInf info) {
|
||||||
|
std::cout << "Partition: " << name
|
||||||
|
<< " size: " << info.size
|
||||||
|
<< " logical: " << (info.isLogical ? "yes" : "no") << "\n";
|
||||||
|
return true;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Check specific partition
|
builder.doForPhysicalPartitions([](const std::string &name, PartitionMap::BasicInf info) {
|
||||||
if (builder.hasPartition("boot")) {
|
std::cout << "[Physical] " << name << "\n";
|
||||||
std::cout << "sda1 size: " << builder.sizeOf("boot") << std::endl;
|
return true;
|
||||||
}
|
});
|
||||||
|
|
||||||
// Other library functions...
|
|
||||||
|
|
||||||
} catch (PartitionMap::Error &e) {
|
builder.doForLogicalPartitions([](const std::string &name, PartitionMap::BasicInf info) {
|
||||||
std::cerr << "PartitionMap Error: " << e.what() << std::endl;
|
std::cout << "[Logical] " << name << "\n";
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
|
||||||
|
std::vector<std::string> partitionVec;
|
||||||
|
builder.copyPartitionsToVector(partitionVec);
|
||||||
|
|
||||||
|
std::cout << "Copied partitions to vector:\n";
|
||||||
|
for (auto &p : partitionVec)
|
||||||
|
std::cout << " " << p << "\n";
|
||||||
|
|
||||||
|
// -------------------------------
|
||||||
|
// Map operators
|
||||||
|
// -------------------------------
|
||||||
|
int count = map;
|
||||||
|
std::cout << "Total partitions: " << count << "\n";
|
||||||
|
|
||||||
|
std::vector<PartitionMap::Info> infoVec = map;
|
||||||
|
for (auto &info : infoVec) {
|
||||||
|
std::cout << "Partition: " << info.name
|
||||||
|
<< " size: " << info.props.size
|
||||||
|
<< " logical: " << (info.props.isLogical ? "yes" : "no") << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------
|
||||||
|
// For-each support (iterator)
|
||||||
|
// -------------------------------
|
||||||
|
for (auto &[name, props] : map) {
|
||||||
|
std::cout << "[Iterator] " << name
|
||||||
|
<< " size: " << props.size
|
||||||
|
<< " logical: " << (props.isLogical ? "yes" : "no") << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (const PartitionMap::Error &e) {
|
||||||
|
std::cerr << "PartitionMap error: " << e.message() << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
> Note: `PartitionMap::Error` can be thrown, so always wrap operations in try-catch blocks.
|
> Note: `PartitionMap::Error` can be thrown, so always wrap operations in try-catch blocks.
|
||||||
|
|||||||
Reference in New Issue
Block a user