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:
@@ -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 {
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
#
|
||||
|
||||
THIS="$(basename $0)"
|
||||
RELEASE="20250821"
|
||||
RELEASE="20250905"
|
||||
|
||||
echo() { command echo "[$THIS]: $@"; }
|
||||
|
||||
|
||||
@@ -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) {
|
||||
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 "
|
||||
|
||||
@@ -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<FILE *>(cookie);
|
||||
if (!Variables->quietProcess) {
|
||||
if (!VARS.quietProcess) {
|
||||
const int ret = fwrite(buf, 1, static_cast<size_t>(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()) {
|
||||
|
||||
@@ -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)),
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -60,8 +60,8 @@ INIT {
|
||||
RUN {
|
||||
std::vector<PartitionMap::Partition_t> 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;
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<ssize_t>(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;
|
||||
|
||||
@@ -62,11 +62,13 @@ private:
|
||||
std::vector<std::string> 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)
|
||||
|
||||
@@ -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<uint64_t>(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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user