pmt: Last commit for 1.2.0 version

- Major changes for developers.
 - Some changes to flash and sizeof functions.
 - Some minor changes.
This commit is contained in:
2025-09-05 11:31:46 +03:00
parent 51ae72aba1
commit 2615ddd127
14 changed files with 81 additions and 61 deletions

View File

@@ -28,6 +28,11 @@
#define PMTE "pmt" #define PMTE "pmt"
#define PMTF "libpmt-function-manager" #define PMTF "libpmt-function-manager"
// Quick access to variables.
#define VARS (*Variables)
// Quick access to partition map.
#define PART_MAP (*(*Variables).PartMap)
namespace PartitionManager { namespace PartitionManager {
// All function classes must inherit from this class. // All function classes must inherit from this class.
class basic_function { class basic_function {

View File

@@ -15,7 +15,7 @@
# #
THIS="$(basename $0)" THIS="$(basename $0)"
RELEASE="20250821" RELEASE="20250905"
echo() { command echo "[$THIS]: $@"; } echo() { command echo "[$THIS]: $@"; }

View File

@@ -40,12 +40,11 @@ std::vector<std::string> splitIfHasDelim(const std::string &s, const char delim,
} }
void setupBufferSize(uint64_t &size, const std::string &entry) { void setupBufferSize(uint64_t &size, const std::string &entry) {
if (Variables->PartMap->hasPartition(entry) && if (PART_MAP.hasPartition(entry) && PART_MAP.sizeOf(entry) % size != 0) {
Variables->PartMap->sizeOf(entry) % size != 0) {
println("%sWARNING%s: Specified buffer size is invalid for %s! Using " println("%sWARNING%s: Specified buffer size is invalid for %s! Using "
"different buffer size for %s.", "different buffer size for %s.",
YELLOW, STYLE_RESET, entry.data(), entry.data()); YELLOW, STYLE_RESET, entry.data(), entry.data());
size = Variables->PartMap->sizeOf(entry) % 4096 == 0 ? 4096 : 1; size = PART_MAP.sizeOf(entry) % 4096 == 0 ? 4096 : 1;
} else if (Helper::fileIsExists(entry)) { } else if (Helper::fileIsExists(entry)) {
if (Helper::fileSize(entry) % size != 0) { if (Helper::fileSize(entry) % size != 0) {
println("%sWARNING%s: Specified buffer size is invalid for %s! using " println("%sWARNING%s: Specified buffer size is invalid for %s! using "

View File

@@ -55,7 +55,7 @@ static void sigHandler(const int sig) {
static int write(void *cookie, const char *buf, const int size) { static int write(void *cookie, const char *buf, const int size) {
auto *real = static_cast<FILE *>(cookie); auto *real = static_cast<FILE *>(cookie);
if (!Variables->quietProcess) { if (!VARS.quietProcess) {
const int ret = fwrite(buf, 1, static_cast<size_t>(size), real); const int ret = fwrite(buf, 1, static_cast<size_t>(size), real);
fflush(real); fflush(real);
return ret; return ret;
@@ -98,7 +98,7 @@ int Main(int argc, char **argv) {
"Apache 2.0 license\nReport " "Apache 2.0 license\nReport "
"bugs to https://github.com/ShawkTeam/pmt-renovated/issues"); "bugs to https://github.com/ShawkTeam/pmt-renovated/issues");
AppMain AppMain
.add_option("-S,--search-path", Variables->searchPath, .add_option("-S,--search-path", VARS.searchPath,
"Set partition search path") "Set partition search path")
->check([&](const std::string &val) { ->check([&](const std::string &val) {
if (val.find("/block") == std::string::npos) if (val.find("/block") == std::string::npos)
@@ -107,17 +107,17 @@ int Main(int argc, char **argv) {
"'block' in input path!"); "'block' in input path!");
return std::string(); return std::string();
}); });
AppMain.add_option("-L,--log-file", Variables->logFile, "Set log file"); AppMain.add_option("-L,--log-file", VARS.logFile, "Set log file");
AppMain.add_flag("-f,--force", Variables->forceProcess, AppMain.add_flag("-f,--force", VARS.forceProcess,
"Force process to be processed"); "Force process to be processed");
AppMain.add_flag("-l,--logical", Variables->onLogical, AppMain.add_flag("-l,--logical", VARS.onLogical,
"Specify that the target partition is dynamic"); "Specify that the target partition is dynamic");
AppMain.add_flag("-q,--quiet", Variables->quietProcess, "Quiet process"); AppMain.add_flag("-q,--quiet", VARS.quietProcess, "Quiet process");
AppMain.add_flag("-V,--verbose", Variables->verboseMode, AppMain.add_flag("-V,--verbose", VARS.verboseMode,
"Detailed information is written on the screen while the " "Detailed information is written on the screen while the "
"transaction is " "transaction is "
"being carried out"); "being carried out");
AppMain.add_flag("-v,--version", Variables->viewVersion, AppMain.add_flag("-v,--version", VARS.viewVersion,
"Print version and exit"); "Print version and exit");
REGISTER_FUNCTION(backupFunction); REGISTER_FUNCTION(backupFunction);
@@ -132,22 +132,22 @@ int Main(int argc, char **argv) {
CLI11_PARSE(AppMain, argc, argv); CLI11_PARSE(AppMain, argc, argv);
if (Variables->verboseMode) Helper::LoggingProperties::setPrinting(YES); if (VARS.verboseMode) Helper::LoggingProperties::setPrinting(YES);
if (Variables->viewVersion) { if (VARS.viewVersion) {
println("%s", getAppVersion().data()); println("%s", getAppVersion().data());
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
if (!Variables->searchPath.empty()) if (!VARS.searchPath.empty()) (PART_MAP)(VARS.searchPath);
(*Variables->PartMap)(Variables->searchPath);
if (!Variables->PartMap && Variables->searchPath.empty()) if (!VARS.PartMap && VARS.searchPath.empty())
throw Error("No default search entries were found. Specify a search " throw Error("No default search entries were found. Specify a search "
"directory with -S " "directory with -S "
"(--search-path)"); "(--search-path)");
if (Variables->onLogical) { if (VARS.onLogical) {
if (!Variables->PartMap->hasLogicalPartitions()) if (!PART_MAP.hasLogicalPartitions())
throw Error("This device doesn't contains logical partitions. But you used -l (--logical) flag."); throw Error("This device doesn't contains logical partitions. But you "
"used -l (--logical) flag.");
} }
if (!Helper::hasSuperUser()) { if (!Helper::hasSuperUser()) {

View File

@@ -30,14 +30,14 @@
namespace PartitionManager { namespace PartitionManager {
RUN_ASYNC(const std::string &partitionName, const std::string &outputName, RUN_ASYNC(const std::string &partitionName, const std::string &outputName,
const uint64_t bufferSize) { const uint64_t bufferSize) {
if (!Variables->PartMap->hasPartition(partitionName)) if (!PART_MAP.hasPartition(partitionName))
return {format("Couldn't find partition: %s", partitionName.data()), false}; return {format("Couldn't find partition: %s", partitionName.data()), false};
LOGN(BFUN, INFO) << "back upping " << partitionName << " as " << outputName LOGN(BFUN, INFO) << "back upping " << partitionName << " as " << outputName
<< std::endl; << std::endl;
if (Variables->onLogical && !Variables->PartMap->isLogical(partitionName)) { if (VARS.onLogical && !PART_MAP.isLogical(partitionName)) {
if (Variables->forceProcess) if (VARS.forceProcess)
LOGN(BFUN, WARNING) LOGN(BFUN, WARNING)
<< "Partition " << partitionName << "Partition " << partitionName
<< " is exists but not logical. Ignoring (from --force, -f)." << " is exists but not logical. Ignoring (from --force, -f)."
@@ -49,7 +49,7 @@ RUN_ASYNC(const std::string &partitionName, const std::string &outputName,
false}; false};
} }
if (Helper::fileIsExists(outputName) && !Variables->forceProcess) if (Helper::fileIsExists(outputName) && !VARS.forceProcess)
return {format("%s is exists. Remove it, or use --force (-f) flag.", return {format("%s is exists. Remove it, or use --force (-f) flag.",
outputName.data()), outputName.data()),
false}; false};
@@ -61,7 +61,7 @@ RUN_ASYNC(const std::string &partitionName, const std::string &outputName,
Helper::garbageCollector collector; Helper::garbageCollector collector;
const int pfd = Helper::openAndAddToCloseList( const int pfd = Helper::openAndAddToCloseList(
Variables->PartMap->getRealPathOf(partitionName), collector, O_RDONLY); PART_MAP.getRealPathOf(partitionName), collector, O_RDONLY);
if (pfd < 0) if (pfd < 0)
return {format("Can't open partition: %s: %s", partitionName.data(), return {format("Can't open partition: %s: %s", partitionName.data(),
strerror(errno)), strerror(errno)),

View File

@@ -27,11 +27,11 @@ Copyright 2025 Yağız Zengin
namespace PartitionManager { namespace PartitionManager {
RUN_ASYNC(const std::string &partitionName, const uint64_t bufferSize) { RUN_ASYNC(const std::string &partitionName, const uint64_t bufferSize) {
if (!Variables->PartMap->hasPartition(partitionName)) if (!PART_MAP.hasPartition(partitionName))
return {format("Couldn't find partition: %s", partitionName.data()), false}; return {format("Couldn't find partition: %s", partitionName.data()), false};
if (Variables->onLogical && !Variables->PartMap->isLogical(partitionName)) { if (VARS.onLogical && !PART_MAP.isLogical(partitionName)) {
if (Variables->forceProcess) if (VARS.forceProcess)
LOGN(EFUN, WARNING) LOGN(EFUN, WARNING)
<< "Partition " << partitionName << "Partition " << partitionName
<< " is exists but not logical. Ignoring (from --force, -f)." << " is exists but not logical. Ignoring (from --force, -f)."
@@ -49,13 +49,13 @@ RUN_ASYNC(const std::string &partitionName, const uint64_t bufferSize) {
Helper::garbageCollector collector; Helper::garbageCollector collector;
const int pfd = Helper::openAndAddToCloseList( const int pfd = Helper::openAndAddToCloseList(
Variables->PartMap->getRealPathOf(partitionName), collector, O_WRONLY); PART_MAP.getRealPathOf(partitionName), collector, O_WRONLY);
if (pfd < 0) if (pfd < 0)
return {format("Can't open partition: %s: %s", partitionName.data(), return {format("Can't open partition: %s: %s", partitionName.data(),
strerror(errno)), strerror(errno)),
false}; false};
if (!Variables->forceProcess) if (!VARS.forceProcess)
Helper::confirmPropt( Helper::confirmPropt(
"Are you sure you want to continue? This could render your device " "Are you sure you want to continue? This could render your device "
"unusable! Do not continue if you " "unusable! Do not continue if you "
@@ -68,7 +68,7 @@ RUN_ASYNC(const std::string &partitionName, const uint64_t bufferSize) {
memset(buffer, 0x00, bufferSize); memset(buffer, 0x00, bufferSize);
ssize_t bytesWritten = 0; ssize_t bytesWritten = 0;
const uint64_t partitionSize = Variables->PartMap->sizeOf(partitionName); const uint64_t partitionSize = PART_MAP.sizeOf(partitionName);
while (bytesWritten < partitionSize) { while (bytesWritten < partitionSize) {
size_t toWrite = sizeof(buffer); size_t toWrite = sizeof(buffer);

View File

@@ -27,12 +27,12 @@ Copyright 2025 Yağız Zengin
namespace PartitionManager { namespace PartitionManager {
RUN_ASYNC(const std::string &partitionName, const std::string &imageName, RUN_ASYNC(const std::string &partitionName, const std::string &imageName,
const uint64_t bufferSize) { const uint64_t bufferSize, const bool deleteAfterProgress) {
if (!Helper::fileIsExists(imageName)) if (!Helper::fileIsExists(imageName))
return {format("Couldn't find image file: %s", imageName.data()), false}; return {format("Couldn't find image file: %s", imageName.data()), false};
if (!Variables->PartMap->hasPartition(partitionName)) if (!PART_MAP.hasPartition(partitionName))
return {format("Couldn't find partition: %s", partitionName.data()), false}; return {format("Couldn't find partition: %s", partitionName.data()), false};
if (Helper::fileSize(imageName) > Variables->PartMap->sizeOf(partitionName)) if (Helper::fileSize(imageName) > PART_MAP.sizeOf(partitionName))
return {format("%s is larger than %s partition size!", imageName.data(), return {format("%s is larger than %s partition size!", imageName.data(),
partitionName.data()), partitionName.data()),
false}; false};
@@ -40,8 +40,8 @@ RUN_ASYNC(const std::string &partitionName, const std::string &imageName,
LOGN(FFUN, INFO) << "flashing " << imageName << " to " << partitionName LOGN(FFUN, INFO) << "flashing " << imageName << " to " << partitionName
<< std::endl; << std::endl;
if (Variables->onLogical && !Variables->PartMap->isLogical(partitionName)) { if (VARS.onLogical && !PART_MAP.isLogical(partitionName)) {
if (Variables->forceProcess) if (VARS.forceProcess)
LOGN(FFUN, WARNING) LOGN(FFUN, WARNING)
<< "Partition " << partitionName << "Partition " << partitionName
<< " is exists but not logical. Ignoring (from --force, -f)." << " is exists but not logical. Ignoring (from --force, -f)."
@@ -65,8 +65,7 @@ RUN_ASYNC(const std::string &partitionName, const std::string &imageName,
false}; false};
const int pfd = Helper::openAndAddToCloseList( const int pfd = Helper::openAndAddToCloseList(
Variables->PartMap->getRealPathOf(partitionName), collector, PART_MAP.getRealPathOf(partitionName), collector, O_RDWR | O_TRUNC);
O_RDWR | O_TRUNC);
if (pfd < 0) if (pfd < 0)
return {format("Can't open partition: %s: %s", partitionName.data(), return {format("Can't open partition: %s: %s", partitionName.data(),
strerror(errno)), strerror(errno)),
@@ -87,6 +86,13 @@ RUN_ASYNC(const std::string &partitionName, const std::string &imageName,
false}; false};
} }
if (deleteAfterProgress) {
LOGN(FFUN, INFO) << "Deleting flash file: " << imageName << std::endl;
if (!Helper::eraseEntry(imageName) && !VARS.quietProcess)
WARNING(
std::string("Cannot erase flash file: " + imageName + "\n").data());
}
return {format("%s is successfully wrote to %s partition", imageName.data(), return {format("%s is successfully wrote to %s partition", imageName.data(),
partitionName.data()), partitionName.data()),
true}; true};
@@ -106,6 +112,9 @@ INIT {
->default_val("4KB"); ->default_val("4KB");
cmd->add_option("-I,--image-directory", imageDirectory, cmd->add_option("-I,--image-directory", imageDirectory,
"Directory to find image(s) and flash to partition(s)"); "Directory to find image(s) and flash to partition(s)");
cmd->add_flag("-d,--delete", deleteAfterProgress,
"Delete flash file(s) after progress.")
->default_val(false);
return true; return true;
} }
@@ -127,7 +136,8 @@ RUN {
setupBufferSize(buf, imageNames[i]); setupBufferSize(buf, imageNames[i]);
futures.push_back(std::async(std::launch::async, runAsync, partitions[i], futures.push_back(std::async(std::launch::async, runAsync, partitions[i],
imageNames[i], bufferSize)); imageNames[i], bufferSize,
deleteAfterProgress));
LOGN(FFUN, INFO) << "Created thread for flashing image to " << partitions[i] LOGN(FFUN, INFO) << "Created thread for flashing image to " << partitions[i]
<< std::endl; << std::endl;
} }

View File

@@ -60,8 +60,8 @@ INIT {
RUN { RUN {
std::vector<PartitionMap::Partition_t> jParts; std::vector<PartitionMap::Partition_t> jParts;
auto func = [this, &jParts] COMMON_LAMBDA_PARAMS -> bool { auto func = [this, &jParts] COMMON_LAMBDA_PARAMS -> bool {
if (Variables->onLogical && !props.isLogical) { if (VARS.onLogical && !props.isLogical) {
if (Variables->forceProcess) if (VARS.forceProcess)
LOGN(IFUN, WARNING) LOGN(IFUN, WARNING)
<< "Partition " << partition << "Partition " << partition
<< " is exists but not logical. Ignoring (from --force, -f)." << " is exists but not logical. Ignoring (from --force, -f)."
@@ -85,12 +85,12 @@ RUN {
}; };
if (partitions.back() == "get-all" || partitions.back() == "getvar-all") if (partitions.back() == "get-all" || partitions.back() == "getvar-all")
Variables->PartMap->doForAllPartitions(func); PART_MAP.doForAllPartitions(func);
else if (partitions.back() == "get-logicals") else if (partitions.back() == "get-logicals")
Variables->PartMap->doForLogicalPartitions(func); PART_MAP.doForLogicalPartitions(func);
else if (partitions.back() == "get-physicals") else if (partitions.back() == "get-physicals")
Variables->PartMap->doForPhysicalPartitions(func); PART_MAP.doForPhysicalPartitions(func);
else Variables->PartMap->doForPartitionList(partitions, func); else PART_MAP.doForPartitionList(partitions, func);
if (jsonFormat) { if (jsonFormat) {
nlohmann::json j; nlohmann::json j;

View File

@@ -58,6 +58,11 @@ INIT {
} }
RUN { RUN {
if (testFileSize > GB(2) && !VARS.forceProcess)
throw Error(
"File size is more than 2GB! Sizes over 2GB may not give accurate "
"results in the write test. Use -f (--force) for skip this error.");
LOGN(MTFUN, INFO) << "Starting memory test on " << testPath << std::endl; LOGN(MTFUN, INFO) << "Starting memory test on " << testPath << std::endl;
Helper::garbageCollector collector; Helper::garbageCollector collector;
const std::string test = Helper::pathJoin(testPath, "test.bin"); const std::string test = Helper::pathJoin(testPath, "test.bin");

View File

@@ -62,8 +62,8 @@ INIT {
RUN { RUN {
auto func = [this] COMMON_LAMBDA_PARAMS -> bool { auto func = [this] COMMON_LAMBDA_PARAMS -> bool {
if (Variables->onLogical && !props.isLogical) { if (VARS.onLogical && !props.isLogical) {
if (Variables->forceProcess) if (VARS.forceProcess)
LOGN(SFUN, WARNING) LOGN(SFUN, WARNING)
<< "Partition " << partition << "Partition " << partition
<< " is exists but not logical. Ignoring (from --force, -f)." << " is exists but not logical. Ignoring (from --force, -f)."
@@ -88,12 +88,12 @@ RUN {
}; };
if (partitions.back() == "get-all" || partitions.back() == "getvar-all") if (partitions.back() == "get-all" || partitions.back() == "getvar-all")
Variables->PartMap->doForAllPartitions(func); PART_MAP.doForAllPartitions(func);
else if (partitions.back() == "get-logicals") else if (partitions.back() == "get-logicals")
Variables->PartMap->doForLogicalPartitions(func); PART_MAP.doForLogicalPartitions(func);
else if (partitions.back() == "get-physicals") else if (partitions.back() == "get-physicals")
Variables->PartMap->doForPhysicalPartitions(func); PART_MAP.doForPhysicalPartitions(func);
else Variables->PartMap->doForPartitionList(partitions, func); else PART_MAP.doForPartitionList(partitions, func);
return true; return true;
} }

View File

@@ -35,11 +35,11 @@ INIT {
RUN { RUN {
for (const auto &partition : partitions) { for (const auto &partition : partitions) {
if (!Variables->PartMap->hasPartition(partition)) if (!PART_MAP.hasPartition(partition))
throw Error("Couldn't find partition: %s", partition.data()); throw Error("Couldn't find partition: %s", partition.data());
if (Variables->onLogical && !Variables->PartMap->isLogical(partition)) { if (VARS.onLogical && !PART_MAP.isLogical(partition)) {
if (Variables->forceProcess) if (VARS.forceProcess)
LOGN(RPFUN, WARNING) LOGN(RPFUN, WARNING)
<< "Partition " << partition << "Partition " << partition
<< " is exists but not logical. Ignoring (from --force, -f)." << " is exists but not logical. Ignoring (from --force, -f)."
@@ -50,8 +50,8 @@ RUN {
} }
if (realLinkPath) if (realLinkPath)
println("%s", Variables->PartMap->getRealLinkPathOf(partition).data()); println("%s", PART_MAP.getRealLinkPathOf(partition).data());
else println("%s", Variables->PartMap->getRealPathOf(partition).data()); else println("%s", PART_MAP.getRealPathOf(partition).data());
} }
return true; return true;

View File

@@ -50,8 +50,7 @@ RUN {
else magics.merge(PartitionMap::Extras::MagicMap); else magics.merge(PartitionMap::Extras::MagicMap);
for (const auto &content : contents) { for (const auto &content : contents) {
if (!Variables->PartMap->hasPartition(content) && if (!PART_MAP.hasPartition(content) && !Helper::fileIsExists(content))
!Helper::fileIsExists(content))
throw Error("Couldn't find partition or image file: %s", content.data()); throw Error("Couldn't find partition or image file: %s", content.data());
bool found = false; bool found = false;
@@ -60,7 +59,7 @@ RUN {
magic, static_cast<ssize_t>(bufferSize), magic, static_cast<ssize_t>(bufferSize),
Helper::fileIsExists(content) Helper::fileIsExists(content)
? content ? content
: Variables->PartMap->getRealPathOf(content))) { : PART_MAP.getRealPathOf(content))) {
println("%s contains %s magic (%s)", content.data(), name.data(), println("%s contains %s magic (%s)", content.data(), name.data(),
PartitionMap::Extras::formatMagic(magic).data()); PartitionMap::Extras::formatMagic(magic).data());
found = true; found = true;

View File

@@ -62,11 +62,13 @@ private:
std::vector<std::string> partitions, imageNames; std::vector<std::string> partitions, imageNames;
std::string rawPartitions, rawImageNames, imageDirectory; std::string rawPartitions, rawImageNames, imageDirectory;
uint64_t bufferSize = 0; uint64_t bufferSize = 0;
bool deleteAfterProgress = false;
public: public:
COMMON_FUNCTION_BODY(); COMMON_FUNCTION_BODY();
static pair runAsync(const std::string &partitionName, static pair runAsync(const std::string &partitionName,
const std::string &imageName, uint64_t bufferSize); const std::string &imageName, uint64_t bufferSize,
bool deleteAfterProgress);
}; };
// Eraser function (writes zero bytes to partition) // Eraser function (writes zero bytes to partition)

View File

@@ -371,7 +371,7 @@ std::string getLibVersion();
#define HELPER "libhelper" #define HELPER "libhelper"
#define KB(x) (x * 1024) // KB(8) = 8192 (8 * 1024) #define KB(x) (static_cast<uint64_t>(x) * 1024) // KB(8) = 8192 (8 * 1024)
#define MB(x) (KB(x) * 1024) // MB(4) = 4194304 (KB(4) * 1024) #define MB(x) (KB(x) * 1024) // MB(4) = 4194304 (KB(4) * 1024)
#define GB(x) (MB(x) * 1024) // GB(1) = 1073741824 (MB(1) * 1024) #define GB(x) (MB(x) * 1024) // GB(1) = 1073741824 (MB(1) * 1024)