From 2615ddd127bea7b57b33b984c04a8fb98b7e3892 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ya=C4=9F=C4=B1z=20Zengin?= Date: Fri, 5 Sep 2025 11:31:46 +0300 Subject: [PATCH] pmt: Last commit for 1.2.0 version - Major changes for developers. - Some changes to flash and sizeof functions. - Some minor changes. --- include/PartitionManager/PartitionManager.hpp | 5 +++ manager.sh | 2 +- src/FunctionManager.cpp | 5 ++- src/PartitionManager.cpp | 32 +++++++++---------- src/functions/BackupFunction.cpp | 10 +++--- src/functions/EraseFunction.cpp | 12 +++---- src/functions/FlashFunction.cpp | 26 ++++++++++----- src/functions/InfoFunction.cpp | 12 +++---- src/functions/MemoryTestFunction.cpp | 5 +++ src/functions/PartitionSizeFunction.cpp | 12 +++---- src/functions/RealPathFunction.cpp | 10 +++--- src/functions/TypeFunction.cpp | 5 ++- src/functions/functions.hpp | 4 ++- srclib/libhelper/include/libhelper/lib.hpp | 2 +- 14 files changed, 81 insertions(+), 61 deletions(-) diff --git a/include/PartitionManager/PartitionManager.hpp b/include/PartitionManager/PartitionManager.hpp index d9c3c3c..f688d1f 100644 --- a/include/PartitionManager/PartitionManager.hpp +++ b/include/PartitionManager/PartitionManager.hpp @@ -28,6 +28,11 @@ #define PMTE "pmt" #define PMTF "libpmt-function-manager" +// Quick access to variables. +#define VARS (*Variables) +// Quick access to partition map. +#define PART_MAP (*(*Variables).PartMap) + namespace PartitionManager { // All function classes must inherit from this class. class basic_function { diff --git a/manager.sh b/manager.sh index ae69447..827defb 100644 --- a/manager.sh +++ b/manager.sh @@ -15,7 +15,7 @@ # THIS="$(basename $0)" -RELEASE="20250821" +RELEASE="20250905" echo() { command echo "[$THIS]: $@"; } diff --git a/src/FunctionManager.cpp b/src/FunctionManager.cpp index ed43e31..d80d2c9 100644 --- a/src/FunctionManager.cpp +++ b/src/FunctionManager.cpp @@ -40,12 +40,11 @@ std::vector splitIfHasDelim(const std::string &s, const char delim, } void setupBufferSize(uint64_t &size, const std::string &entry) { - if (Variables->PartMap->hasPartition(entry) && - Variables->PartMap->sizeOf(entry) % size != 0) { + if (PART_MAP.hasPartition(entry) && PART_MAP.sizeOf(entry) % size != 0) { println("%sWARNING%s: Specified buffer size is invalid for %s! Using " "different buffer size for %s.", 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)) { if (Helper::fileSize(entry) % size != 0) { println("%sWARNING%s: Specified buffer size is invalid for %s! using " diff --git a/src/PartitionManager.cpp b/src/PartitionManager.cpp index cbaf8e7..dc103e4 100644 --- a/src/PartitionManager.cpp +++ b/src/PartitionManager.cpp @@ -55,7 +55,7 @@ static void sigHandler(const int sig) { static int write(void *cookie, const char *buf, const int size) { auto *real = static_cast(cookie); - if (!Variables->quietProcess) { + if (!VARS.quietProcess) { const int ret = fwrite(buf, 1, static_cast(size), real); fflush(real); return ret; @@ -98,7 +98,7 @@ int Main(int argc, char **argv) { "Apache 2.0 license\nReport " "bugs to https://github.com/ShawkTeam/pmt-renovated/issues"); AppMain - .add_option("-S,--search-path", Variables->searchPath, + .add_option("-S,--search-path", VARS.searchPath, "Set partition search path") ->check([&](const std::string &val) { if (val.find("/block") == std::string::npos) @@ -107,17 +107,17 @@ int Main(int argc, char **argv) { "'block' in input path!"); return std::string(); }); - AppMain.add_option("-L,--log-file", Variables->logFile, "Set log file"); - AppMain.add_flag("-f,--force", Variables->forceProcess, + AppMain.add_option("-L,--log-file", VARS.logFile, "Set log file"); + AppMain.add_flag("-f,--force", VARS.forceProcess, "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"); - AppMain.add_flag("-q,--quiet", Variables->quietProcess, "Quiet process"); - AppMain.add_flag("-V,--verbose", Variables->verboseMode, + AppMain.add_flag("-q,--quiet", VARS.quietProcess, "Quiet process"); + AppMain.add_flag("-V,--verbose", VARS.verboseMode, "Detailed information is written on the screen while the " "transaction is " "being carried out"); - AppMain.add_flag("-v,--version", Variables->viewVersion, + AppMain.add_flag("-v,--version", VARS.viewVersion, "Print version and exit"); REGISTER_FUNCTION(backupFunction); @@ -132,22 +132,22 @@ int Main(int argc, char **argv) { CLI11_PARSE(AppMain, argc, argv); - if (Variables->verboseMode) Helper::LoggingProperties::setPrinting(YES); - if (Variables->viewVersion) { + if (VARS.verboseMode) Helper::LoggingProperties::setPrinting(YES); + if (VARS.viewVersion) { println("%s", getAppVersion().data()); return EXIT_SUCCESS; } - if (!Variables->searchPath.empty()) - (*Variables->PartMap)(Variables->searchPath); + if (!VARS.searchPath.empty()) (PART_MAP)(VARS.searchPath); - if (!Variables->PartMap && Variables->searchPath.empty()) + if (!VARS.PartMap && VARS.searchPath.empty()) throw Error("No default search entries were found. Specify a search " "directory with -S " "(--search-path)"); - if (Variables->onLogical) { - if (!Variables->PartMap->hasLogicalPartitions()) - throw Error("This device doesn't contains logical partitions. But you used -l (--logical) flag."); + if (VARS.onLogical) { + if (!PART_MAP.hasLogicalPartitions()) + throw Error("This device doesn't contains logical partitions. But you " + "used -l (--logical) flag."); } if (!Helper::hasSuperUser()) { diff --git a/src/functions/BackupFunction.cpp b/src/functions/BackupFunction.cpp index 56e161d..7ffad38 100644 --- a/src/functions/BackupFunction.cpp +++ b/src/functions/BackupFunction.cpp @@ -30,14 +30,14 @@ namespace PartitionManager { RUN_ASYNC(const std::string &partitionName, const std::string &outputName, const uint64_t bufferSize) { - if (!Variables->PartMap->hasPartition(partitionName)) + if (!PART_MAP.hasPartition(partitionName)) return {format("Couldn't find partition: %s", partitionName.data()), false}; LOGN(BFUN, INFO) << "back upping " << partitionName << " as " << outputName << std::endl; - if (Variables->onLogical && !Variables->PartMap->isLogical(partitionName)) { - if (Variables->forceProcess) + if (VARS.onLogical && !PART_MAP.isLogical(partitionName)) { + if (VARS.forceProcess) LOGN(BFUN, WARNING) << "Partition " << partitionName << " is exists but not logical. Ignoring (from --force, -f)." @@ -49,7 +49,7 @@ RUN_ASYNC(const std::string &partitionName, const std::string &outputName, 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.", outputName.data()), false}; @@ -61,7 +61,7 @@ RUN_ASYNC(const std::string &partitionName, const std::string &outputName, Helper::garbageCollector collector; const int pfd = Helper::openAndAddToCloseList( - Variables->PartMap->getRealPathOf(partitionName), collector, O_RDONLY); + PART_MAP.getRealPathOf(partitionName), collector, O_RDONLY); if (pfd < 0) return {format("Can't open partition: %s: %s", partitionName.data(), strerror(errno)), diff --git a/src/functions/EraseFunction.cpp b/src/functions/EraseFunction.cpp index f4de40e..e08f870 100644 --- a/src/functions/EraseFunction.cpp +++ b/src/functions/EraseFunction.cpp @@ -27,11 +27,11 @@ Copyright 2025 Yağız Zengin namespace PartitionManager { 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}; - if (Variables->onLogical && !Variables->PartMap->isLogical(partitionName)) { - if (Variables->forceProcess) + if (VARS.onLogical && !PART_MAP.isLogical(partitionName)) { + if (VARS.forceProcess) LOGN(EFUN, WARNING) << "Partition " << partitionName << " 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; const int pfd = Helper::openAndAddToCloseList( - Variables->PartMap->getRealPathOf(partitionName), collector, O_WRONLY); + PART_MAP.getRealPathOf(partitionName), collector, O_WRONLY); if (pfd < 0) return {format("Can't open partition: %s: %s", partitionName.data(), strerror(errno)), false}; - if (!Variables->forceProcess) + if (!VARS.forceProcess) Helper::confirmPropt( "Are you sure you want to continue? This could render your device " "unusable! Do not continue if you " @@ -68,7 +68,7 @@ RUN_ASYNC(const std::string &partitionName, const uint64_t bufferSize) { memset(buffer, 0x00, bufferSize); ssize_t bytesWritten = 0; - const uint64_t partitionSize = Variables->PartMap->sizeOf(partitionName); + const uint64_t partitionSize = PART_MAP.sizeOf(partitionName); while (bytesWritten < partitionSize) { size_t toWrite = sizeof(buffer); diff --git a/src/functions/FlashFunction.cpp b/src/functions/FlashFunction.cpp index c5056fa..f00c753 100644 --- a/src/functions/FlashFunction.cpp +++ b/src/functions/FlashFunction.cpp @@ -27,12 +27,12 @@ Copyright 2025 Yağız Zengin namespace PartitionManager { 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)) 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}; - 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(), partitionName.data()), false}; @@ -40,8 +40,8 @@ RUN_ASYNC(const std::string &partitionName, const std::string &imageName, LOGN(FFUN, INFO) << "flashing " << imageName << " to " << partitionName << std::endl; - if (Variables->onLogical && !Variables->PartMap->isLogical(partitionName)) { - if (Variables->forceProcess) + if (VARS.onLogical && !PART_MAP.isLogical(partitionName)) { + if (VARS.forceProcess) LOGN(FFUN, WARNING) << "Partition " << partitionName << " is exists but not logical. Ignoring (from --force, -f)." @@ -65,8 +65,7 @@ RUN_ASYNC(const std::string &partitionName, const std::string &imageName, false}; const int pfd = Helper::openAndAddToCloseList( - Variables->PartMap->getRealPathOf(partitionName), collector, - O_RDWR | O_TRUNC); + PART_MAP.getRealPathOf(partitionName), collector, O_RDWR | O_TRUNC); if (pfd < 0) return {format("Can't open partition: %s: %s", partitionName.data(), strerror(errno)), @@ -87,6 +86,13 @@ RUN_ASYNC(const std::string &partitionName, const std::string &imageName, 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(), partitionName.data()), true}; @@ -106,6 +112,9 @@ INIT { ->default_val("4KB"); cmd->add_option("-I,--image-directory", imageDirectory, "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; } @@ -127,7 +136,8 @@ RUN { setupBufferSize(buf, imageNames[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] << std::endl; } diff --git a/src/functions/InfoFunction.cpp b/src/functions/InfoFunction.cpp index 611154e..2bb7d74 100644 --- a/src/functions/InfoFunction.cpp +++ b/src/functions/InfoFunction.cpp @@ -60,8 +60,8 @@ INIT { RUN { std::vector jParts; auto func = [this, &jParts] COMMON_LAMBDA_PARAMS -> bool { - if (Variables->onLogical && !props.isLogical) { - if (Variables->forceProcess) + if (VARS.onLogical && !props.isLogical) { + if (VARS.forceProcess) LOGN(IFUN, WARNING) << "Partition " << partition << " is exists but not logical. Ignoring (from --force, -f)." @@ -85,12 +85,12 @@ RUN { }; if (partitions.back() == "get-all" || partitions.back() == "getvar-all") - Variables->PartMap->doForAllPartitions(func); + PART_MAP.doForAllPartitions(func); else if (partitions.back() == "get-logicals") - Variables->PartMap->doForLogicalPartitions(func); + PART_MAP.doForLogicalPartitions(func); else if (partitions.back() == "get-physicals") - Variables->PartMap->doForPhysicalPartitions(func); - else Variables->PartMap->doForPartitionList(partitions, func); + PART_MAP.doForPhysicalPartitions(func); + else PART_MAP.doForPartitionList(partitions, func); if (jsonFormat) { nlohmann::json j; diff --git a/src/functions/MemoryTestFunction.cpp b/src/functions/MemoryTestFunction.cpp index 7fa260c..b8c22a2 100644 --- a/src/functions/MemoryTestFunction.cpp +++ b/src/functions/MemoryTestFunction.cpp @@ -58,6 +58,11 @@ INIT { } 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; Helper::garbageCollector collector; const std::string test = Helper::pathJoin(testPath, "test.bin"); diff --git a/src/functions/PartitionSizeFunction.cpp b/src/functions/PartitionSizeFunction.cpp index 04d9e02..371776b 100644 --- a/src/functions/PartitionSizeFunction.cpp +++ b/src/functions/PartitionSizeFunction.cpp @@ -62,8 +62,8 @@ INIT { RUN { auto func = [this] COMMON_LAMBDA_PARAMS -> bool { - if (Variables->onLogical && !props.isLogical) { - if (Variables->forceProcess) + if (VARS.onLogical && !props.isLogical) { + if (VARS.forceProcess) LOGN(SFUN, WARNING) << "Partition " << partition << " is exists but not logical. Ignoring (from --force, -f)." @@ -88,12 +88,12 @@ RUN { }; if (partitions.back() == "get-all" || partitions.back() == "getvar-all") - Variables->PartMap->doForAllPartitions(func); + PART_MAP.doForAllPartitions(func); else if (partitions.back() == "get-logicals") - Variables->PartMap->doForLogicalPartitions(func); + PART_MAP.doForLogicalPartitions(func); else if (partitions.back() == "get-physicals") - Variables->PartMap->doForPhysicalPartitions(func); - else Variables->PartMap->doForPartitionList(partitions, func); + PART_MAP.doForPhysicalPartitions(func); + else PART_MAP.doForPartitionList(partitions, func); return true; } diff --git a/src/functions/RealPathFunction.cpp b/src/functions/RealPathFunction.cpp index bc91c95..b37b9a4 100644 --- a/src/functions/RealPathFunction.cpp +++ b/src/functions/RealPathFunction.cpp @@ -35,11 +35,11 @@ INIT { RUN { for (const auto &partition : partitions) { - if (!Variables->PartMap->hasPartition(partition)) + if (!PART_MAP.hasPartition(partition)) throw Error("Couldn't find partition: %s", partition.data()); - if (Variables->onLogical && !Variables->PartMap->isLogical(partition)) { - if (Variables->forceProcess) + if (VARS.onLogical && !PART_MAP.isLogical(partition)) { + if (VARS.forceProcess) LOGN(RPFUN, WARNING) << "Partition " << partition << " is exists but not logical. Ignoring (from --force, -f)." @@ -50,8 +50,8 @@ RUN { } if (realLinkPath) - println("%s", Variables->PartMap->getRealLinkPathOf(partition).data()); - else println("%s", Variables->PartMap->getRealPathOf(partition).data()); + println("%s", PART_MAP.getRealLinkPathOf(partition).data()); + else println("%s", PART_MAP.getRealPathOf(partition).data()); } return true; diff --git a/src/functions/TypeFunction.cpp b/src/functions/TypeFunction.cpp index 3219589..324c1d0 100644 --- a/src/functions/TypeFunction.cpp +++ b/src/functions/TypeFunction.cpp @@ -50,8 +50,7 @@ RUN { else magics.merge(PartitionMap::Extras::MagicMap); for (const auto &content : contents) { - if (!Variables->PartMap->hasPartition(content) && - !Helper::fileIsExists(content)) + if (!PART_MAP.hasPartition(content) && !Helper::fileIsExists(content)) throw Error("Couldn't find partition or image file: %s", content.data()); bool found = false; @@ -60,7 +59,7 @@ RUN { magic, static_cast(bufferSize), Helper::fileIsExists(content) ? content - : Variables->PartMap->getRealPathOf(content))) { + : PART_MAP.getRealPathOf(content))) { println("%s contains %s magic (%s)", content.data(), name.data(), PartitionMap::Extras::formatMagic(magic).data()); found = true; diff --git a/src/functions/functions.hpp b/src/functions/functions.hpp index 5f163c9..7ae961b 100644 --- a/src/functions/functions.hpp +++ b/src/functions/functions.hpp @@ -62,11 +62,13 @@ private: std::vector partitions, imageNames; std::string rawPartitions, rawImageNames, imageDirectory; uint64_t bufferSize = 0; + bool deleteAfterProgress = false; public: COMMON_FUNCTION_BODY(); 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) diff --git a/srclib/libhelper/include/libhelper/lib.hpp b/srclib/libhelper/include/libhelper/lib.hpp index 2d46958..c7235f0 100644 --- a/srclib/libhelper/include/libhelper/lib.hpp +++ b/srclib/libhelper/include/libhelper/lib.hpp @@ -371,7 +371,7 @@ std::string getLibVersion(); #define HELPER "libhelper" -#define KB(x) (x * 1024) // KB(8) = 8192 (8 * 1024) +#define KB(x) (static_cast(x) * 1024) // KB(8) = 8192 (8 * 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)