pmt: Improve pmt & libpartition_map
- Change some operator returnn types. - Use std::map instead of std::unordered on magic number maps. - Some improvements.
This commit is contained in:
@@ -25,11 +25,11 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#define BFUN "backupFunction"
|
||||
#define FUNCTION_CLASS backupFunction
|
||||
|
||||
namespace PartitionManager {
|
||||
RUN_ASYNC(backupFunction)(const std::string &partitionName,
|
||||
const std::string &outputName,
|
||||
const uint64_t bufferSize) {
|
||||
RUN_ASYNC(const std::string &partitionName, const std::string &outputName,
|
||||
const uint64_t bufferSize) {
|
||||
if (!Variables->PartMap->hasPartition(partitionName))
|
||||
return {format("Couldn't find partition: %s", partitionName.data()), false};
|
||||
|
||||
@@ -105,7 +105,7 @@ RUN_ASYNC(backupFunction)(const std::string &partitionName,
|
||||
true};
|
||||
}
|
||||
|
||||
INIT(backupFunction) {
|
||||
INIT {
|
||||
LOGN(BFUN, INFO) << "Initializing variables of backup function." << std::endl;
|
||||
cmd = _app.add_subcommand("backup", "Backup partition(s) to file(s)");
|
||||
cmd->add_option("partition(s)", rawPartitions, "Partition name(s)")
|
||||
@@ -123,7 +123,7 @@ INIT(backupFunction) {
|
||||
return true;
|
||||
}
|
||||
|
||||
RUN(backupFunction) {
|
||||
RUN {
|
||||
processCommandLine(partitions, outputNames, rawPartitions, rawOutputNames,
|
||||
',', true);
|
||||
if (!outputNames.empty() && partitions.size() != outputNames.size())
|
||||
@@ -161,7 +161,7 @@ RUN(backupFunction) {
|
||||
return endResult;
|
||||
}
|
||||
|
||||
IS_USED_COMMON_BODY(backupFunction)
|
||||
IS_USED_COMMON_BODY
|
||||
|
||||
NAME(backupFunction) { return BFUN; }
|
||||
NAME { return BFUN; }
|
||||
} // namespace PartitionManager
|
||||
|
||||
@@ -23,10 +23,10 @@ Copyright 2025 Yağız Zengin
|
||||
#include <unistd.h>
|
||||
|
||||
#define EFUN "eraseFunction"
|
||||
#define FUNCTION_CLASS eraseFunction
|
||||
|
||||
namespace PartitionManager {
|
||||
RUN_ASYNC(eraseFunction)(const std::string &partitionName,
|
||||
const uint64_t bufferSize) {
|
||||
RUN_ASYNC(const std::string &partitionName, const uint64_t bufferSize) {
|
||||
if (!Variables->PartMap->hasPartition(partitionName))
|
||||
return {format("Couldn't find partition: %s", partitionName.data()), false};
|
||||
|
||||
@@ -87,7 +87,7 @@ RUN_ASYNC(eraseFunction)(const std::string &partitionName,
|
||||
true};
|
||||
}
|
||||
|
||||
INIT(eraseFunction) {
|
||||
INIT {
|
||||
LOGN(EFUN, INFO) << "Initializing variables of erase function." << std::endl;
|
||||
cmd = _app.add_subcommand("erase", "Writes zero bytes to partition(s)");
|
||||
cmd->add_option("partition(s)", partitions, "Partition name(s)")
|
||||
@@ -100,7 +100,7 @@ INIT(eraseFunction) {
|
||||
return true;
|
||||
}
|
||||
|
||||
RUN(eraseFunction) {
|
||||
RUN {
|
||||
std::vector<std::future<pair>> futures;
|
||||
for (const auto &partitionName : partitions) {
|
||||
uint64_t buf = bufferSize;
|
||||
@@ -127,7 +127,7 @@ RUN(eraseFunction) {
|
||||
return endResult;
|
||||
}
|
||||
|
||||
IS_USED_COMMON_BODY(eraseFunction)
|
||||
IS_USED_COMMON_BODY
|
||||
|
||||
NAME(eraseFunction) { return EFUN; }
|
||||
NAME { return EFUN; }
|
||||
} // namespace PartitionManager
|
||||
|
||||
@@ -23,11 +23,11 @@ Copyright 2025 Yağız Zengin
|
||||
#include <unistd.h>
|
||||
|
||||
#define FFUN "flashFunction"
|
||||
#define FUNCTION_CLASS flashFunction
|
||||
|
||||
namespace PartitionManager {
|
||||
RUN_ASYNC(flashFunction)(const std::string &partitionName,
|
||||
const std::string &imageName,
|
||||
const uint64_t bufferSize) {
|
||||
RUN_ASYNC(const std::string &partitionName, const std::string &imageName,
|
||||
const uint64_t bufferSize) {
|
||||
if (!Helper::fileIsExists(imageName))
|
||||
return {format("Couldn't find image file: %s", imageName.data()), false};
|
||||
if (!Variables->PartMap->hasPartition(partitionName))
|
||||
@@ -92,7 +92,7 @@ RUN_ASYNC(flashFunction)(const std::string &partitionName,
|
||||
true};
|
||||
}
|
||||
|
||||
INIT(flashFunction) {
|
||||
INIT {
|
||||
LOGN(FFUN, INFO) << "Initializing variables of flash function." << std::endl;
|
||||
cmd = _app.add_subcommand("flash", "Flash image(s) to partition(s)");
|
||||
cmd->add_option("partition(s)", rawPartitions, "Partition name(s)")
|
||||
@@ -110,7 +110,7 @@ INIT(flashFunction) {
|
||||
return true;
|
||||
}
|
||||
|
||||
RUN(flashFunction) {
|
||||
RUN {
|
||||
processCommandLine(partitions, imageNames, rawPartitions, rawImageNames, ',',
|
||||
true);
|
||||
if (partitions.size() != imageNames.size())
|
||||
@@ -148,7 +148,7 @@ RUN(flashFunction) {
|
||||
return endResult;
|
||||
}
|
||||
|
||||
IS_USED_COMMON_BODY(flashFunction)
|
||||
IS_USED_COMMON_BODY
|
||||
|
||||
NAME(flashFunction) { return FFUN; }
|
||||
NAME { return FFUN; }
|
||||
} // namespace PartitionManager
|
||||
|
||||
@@ -22,10 +22,11 @@ Copyright 2025 Yağız Zengin
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#define IFUN "infoFunction"
|
||||
#define FUNCTION_CLASS infoFunction
|
||||
|
||||
namespace PartitionManager {
|
||||
|
||||
INIT(infoFunction) {
|
||||
INIT {
|
||||
LOGN(IFUN, INFO) << "Initializing variables of info printer function."
|
||||
<< std::endl;
|
||||
cmd = _app.add_subcommand("info", "Tell info(s) of input partition list")
|
||||
@@ -56,7 +57,7 @@ INIT(infoFunction) {
|
||||
return true;
|
||||
}
|
||||
|
||||
RUN(infoFunction) {
|
||||
RUN {
|
||||
std::vector<PartitionMap::Partition_t> jParts;
|
||||
auto func = [this, &jParts] COMMON_LAMBDA_PARAMS -> bool {
|
||||
if (Variables->onLogical && !props.isLogical) {
|
||||
@@ -106,7 +107,7 @@ RUN(infoFunction) {
|
||||
return true;
|
||||
}
|
||||
|
||||
IS_USED_COMMON_BODY(infoFunction)
|
||||
IS_USED_COMMON_BODY
|
||||
|
||||
NAME(infoFunction) { return IFUN; };
|
||||
NAME { return IFUN; };
|
||||
} // namespace PartitionManager
|
||||
|
||||
@@ -24,10 +24,11 @@ Copyright 2025 Yağız Zengin
|
||||
#include <unistd.h>
|
||||
|
||||
#define MTFUN "memoryTestFunction"
|
||||
#define FUNCTION_CLASS memoryTestFunction
|
||||
|
||||
namespace PartitionManager {
|
||||
|
||||
INIT(memoryTestFunction) {
|
||||
INIT {
|
||||
LOGN(MTFUN, INFO) << "Initializing variables of memory test function."
|
||||
<< std::endl;
|
||||
cmd = _app.add_subcommand("memtest", "Test your write/read speed of device.");
|
||||
@@ -56,7 +57,7 @@ INIT(memoryTestFunction) {
|
||||
return true;
|
||||
}
|
||||
|
||||
RUN(memoryTestFunction) {
|
||||
RUN {
|
||||
LOGN(MTFUN, INFO) << "Starting memory test on " << testPath << std::endl;
|
||||
Helper::garbageCollector collector;
|
||||
const std::string test = Helper::pathJoin(testPath, "test.bin");
|
||||
@@ -121,7 +122,7 @@ RUN(memoryTestFunction) {
|
||||
return true;
|
||||
}
|
||||
|
||||
IS_USED_COMMON_BODY(memoryTestFunction)
|
||||
IS_USED_COMMON_BODY
|
||||
|
||||
NAME(memoryTestFunction) { return MTFUN; }
|
||||
NAME { return MTFUN; }
|
||||
} // namespace PartitionManager
|
||||
|
||||
@@ -18,6 +18,7 @@ Copyright 2025 Yağız Zengin
|
||||
#include <PartitionManager/PartitionManager.hpp>
|
||||
|
||||
#define SFUN "partitionSizeFunction"
|
||||
#define FUNCTION_CLASS partitionSizeFunction
|
||||
|
||||
static std::string convertTo(const uint64_t size, const std::string &multiple) {
|
||||
if (multiple == "KB") return std::to_string(TO_KB(size));
|
||||
@@ -27,7 +28,7 @@ static std::string convertTo(const uint64_t size, const std::string &multiple) {
|
||||
}
|
||||
|
||||
namespace PartitionManager {
|
||||
INIT(partitionSizeFunction) {
|
||||
INIT {
|
||||
LOGN(SFUN, INFO)
|
||||
<< "Initializing variables of partition size getter function."
|
||||
<< std::endl;
|
||||
@@ -59,7 +60,7 @@ INIT(partitionSizeFunction) {
|
||||
return true;
|
||||
}
|
||||
|
||||
RUN(partitionSizeFunction) {
|
||||
RUN {
|
||||
auto func = [this] COMMON_LAMBDA_PARAMS -> bool {
|
||||
if (Variables->onLogical && !props.isLogical) {
|
||||
if (Variables->forceProcess)
|
||||
@@ -97,7 +98,7 @@ RUN(partitionSizeFunction) {
|
||||
return true;
|
||||
}
|
||||
|
||||
IS_USED_COMMON_BODY(partitionSizeFunction)
|
||||
IS_USED_COMMON_BODY
|
||||
|
||||
NAME(partitionSizeFunction) { return SFUN; }
|
||||
NAME { return SFUN; }
|
||||
} // namespace PartitionManager
|
||||
|
||||
@@ -18,9 +18,10 @@
|
||||
#include <PartitionManager/PartitionManager.hpp>
|
||||
|
||||
#define RPFUN "realPathFunction"
|
||||
#define FUNCTION_CLASS realPathFunction
|
||||
|
||||
namespace PartitionManager {
|
||||
INIT(realPathFunction) {
|
||||
INIT {
|
||||
LOGN(RPFUN, INFO) << "Initializing variables of real path function."
|
||||
<< std::endl;
|
||||
cmd = _app.add_subcommand("real-path", "Tell real paths of partition(s)");
|
||||
@@ -32,7 +33,7 @@ INIT(realPathFunction) {
|
||||
return true;
|
||||
}
|
||||
|
||||
RUN(realPathFunction) {
|
||||
RUN {
|
||||
for (const auto &partition : partitions) {
|
||||
if (!Variables->PartMap->hasPartition(partition))
|
||||
throw Error("Couldn't find partition: %s", partition.data());
|
||||
@@ -56,7 +57,7 @@ RUN(realPathFunction) {
|
||||
return true;
|
||||
}
|
||||
|
||||
IS_USED_COMMON_BODY(realPathFunction)
|
||||
IS_USED_COMMON_BODY
|
||||
|
||||
NAME(realPathFunction) { return RPFUN; }
|
||||
NAME { return RPFUN; }
|
||||
} // namespace PartitionManager
|
||||
|
||||
@@ -18,9 +18,10 @@ Copyright 2025 Yağız Zengin
|
||||
#include <PartitionManager/PartitionManager.hpp>
|
||||
|
||||
#define RFUN "rebootFunction"
|
||||
#define FUNCTION_CLASS rebootFunction
|
||||
|
||||
namespace PartitionManager {
|
||||
INIT(rebootFunction) {
|
||||
INIT {
|
||||
LOGN(RFUN, INFO) << "Initializing variables of reboot function." << std::endl;
|
||||
cmd = _app.add_subcommand("reboot", "Reboots device");
|
||||
cmd->add_option("rebootTarget", rebootTarget,
|
||||
@@ -28,7 +29,7 @@ INIT(rebootFunction) {
|
||||
return true;
|
||||
}
|
||||
|
||||
RUN(rebootFunction) {
|
||||
RUN {
|
||||
LOGN(RFUN, INFO) << "Rebooting device!!! (custom reboot target: "
|
||||
<< (rebootTarget.empty() ? "none" : rebootTarget)
|
||||
<< std::endl;
|
||||
@@ -39,7 +40,7 @@ RUN(rebootFunction) {
|
||||
return true;
|
||||
}
|
||||
|
||||
IS_USED_COMMON_BODY(rebootFunction)
|
||||
IS_USED_COMMON_BODY
|
||||
|
||||
NAME(rebootFunction) { return RFUN; }
|
||||
NAME { return RFUN; }
|
||||
} // namespace PartitionManager
|
||||
|
||||
@@ -16,11 +16,13 @@
|
||||
|
||||
#include "functions.hpp"
|
||||
#include <PartitionManager/PartitionManager.hpp>
|
||||
#include <map>
|
||||
|
||||
#define TFUN "typeFunction"
|
||||
#define FUNCTION_CLASS typeFunction
|
||||
|
||||
namespace PartitionManager {
|
||||
INIT(typeFunction) {
|
||||
INIT {
|
||||
LOGN(TFUN, INFO) << "Initializing variables of type function." << std::endl;
|
||||
cmd = _app.add_subcommand("type", "Get type of the partition(s) or image(s)");
|
||||
cmd->add_option("content(s)", contents, "Content(s)")
|
||||
@@ -39,8 +41,8 @@ INIT(typeFunction) {
|
||||
return true;
|
||||
}
|
||||
|
||||
RUN(typeFunction) {
|
||||
std::unordered_map<uint64_t, std::string> magics;
|
||||
RUN {
|
||||
std::map<uint64_t, std::string> magics;
|
||||
if (onlyCheckAndroidMagics)
|
||||
magics.merge(PartitionMap::Extras::AndroidMagicMap);
|
||||
else if (onlyCheckFileSystemMagics)
|
||||
@@ -74,8 +76,8 @@ RUN(typeFunction) {
|
||||
return true;
|
||||
}
|
||||
|
||||
IS_USED_COMMON_BODY(typeFunction)
|
||||
IS_USED_COMMON_BODY
|
||||
|
||||
NAME(typeFunction) { return TFUN; }
|
||||
NAME { return TFUN; }
|
||||
|
||||
} // namespace PartitionManager
|
||||
|
||||
@@ -21,16 +21,18 @@
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#define INIT(cls) bool cls::init(CLI::App &_app)
|
||||
#define RUN(cls) bool cls::run()
|
||||
#define RUN_ASYNC(cls) pair cls::runAsync
|
||||
#define IS_USED(cls) bool cls::isUsed() const
|
||||
#define IS_USED_COMMON_BODY(cls) \
|
||||
bool cls::isUsed() const { return cmd->parsed(); }
|
||||
#define NAME(cls) const char *cls::name() const
|
||||
#define INIT bool FUNCTION_CLASS::init(CLI::App &_app)
|
||||
#define RUN bool FUNCTION_CLASS::run()
|
||||
#define RUN_ASYNC pair FUNCTION_CLASS::runAsync
|
||||
#define IS_USED bool FUNCTION_CLASS::isUsed() const
|
||||
#define IS_USED_COMMON_BODY \
|
||||
bool FUNCTION_CLASS::isUsed() const { return cmd->parsed(); }
|
||||
#define NAME const char *FUNCTION_CLASS::name() const
|
||||
|
||||
/**
|
||||
* Please define FUNCTION_CLASS before using these macros!!! (INIT etc.)
|
||||
*/
|
||||
|
||||
#define COMMON_LAMBDA_PARAMS \
|
||||
(std::string partition, const PartitionMap::Map_t::BasicInf props)
|
||||
#define COMMON_FUNCTION_BODY() \
|
||||
CLI::App *cmd = nullptr; \
|
||||
bool init(CLI::App &_app) override; \
|
||||
|
||||
Reference in New Issue
Block a user