pmt: improve libhelper, etc.

This commit is contained in:
2025-09-13 11:36:29 +03:00
parent b99f20c6a1
commit 398b119cb4
9 changed files with 96 additions and 72 deletions

View File

@@ -199,15 +199,6 @@ void println(const char *format, ...) {
va_end(args);
}
std::string format(const char *format, ...) {
va_list args;
va_start(args, format);
char str[1024];
vsnprintf(str, sizeof(str), format, args);
va_end(args);
return str;
}
std::string getLibVersion() { MKVERSION(PMT); }
std::string getAppVersion() { MKVERSION(PMTE); }

View File

@@ -31,7 +31,7 @@ namespace PartitionManager {
RUN_ASYNC(const std::string &partitionName, const std::string &outputName,
const uint64_t bufferSize) {
if (!PART_MAP.hasPartition(partitionName))
return {format("Couldn't find partition: %s", partitionName.data()), false};
return {Helper::format("Couldn't find partition: %s", partitionName.data()), false};
LOGN(BFUN, INFO) << "Back upping " << partitionName << " as " << outputName
<< std::endl;
@@ -44,13 +44,13 @@ RUN_ASYNC(const std::string &partitionName, const std::string &outputName,
<< std::endl;
else
return {
format("Used --logical (-l) flag but is not logical partition: %s",
Helper::format("Used --logical (-l) flag but is not logical partition: %s",
partitionName.data()),
false};
}
if (Helper::fileIsExists(outputName) && !VARS.forceProcess)
return {format("%s is exists. Remove it, or use --force (-f) flag.",
return {Helper::format("%s is exists. Remove it, or use --force (-f) flag.",
outputName.data()),
false};
@@ -63,14 +63,14 @@ RUN_ASYNC(const std::string &partitionName, const std::string &outputName,
const int pfd = Helper::openAndAddToCloseList(
PART_MAP.getRealPathOf(partitionName), collector, O_RDONLY);
if (pfd < 0)
return {format("Can't open partition: %s: %s", partitionName.data(),
return {Helper::format("Can't open partition: %s: %s", partitionName.data(),
strerror(errno)),
false};
const int ffd = Helper::openAndAddToCloseList(
outputName, collector, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (ffd < 0)
return {format("Can't create/open output file %s: %s", outputName.data(),
return {Helper::format("Can't create/open output file %s: %s", outputName.data(),
strerror(errno)),
false};
@@ -84,7 +84,7 @@ RUN_ASYNC(const std::string &partitionName, const std::string &outputName,
while ((bytesRead = read(pfd, buffer, bufferSize)) > 0) {
if (const ssize_t bytesWritten = write(ffd, buffer, bytesRead);
bytesWritten != bytesRead)
return {format("Can't write partition to output file %s: %s",
return {Helper::format("Can't write partition to output file %s: %s",
outputName.data(), strerror(errno)),
false};
}
@@ -100,7 +100,7 @@ RUN_ASYNC(const std::string &partitionName, const std::string &outputName,
<< ". Access problems maybe occur in non-root mode"
<< std::endl;
return {format("%s partition successfully back upped to %s",
return {Helper::format("%s partition successfully back upped to %s",
partitionName.data(), outputName.data()),
true};
}

View File

@@ -17,7 +17,6 @@ Copyright 2025 Yağız Zengin
#include "functions.hpp"
#include <PartitionManager/PartitionManager.hpp>
#include <cerrno>
#include <cstdlib>
#include <fcntl.h>
#include <future>
#include <unistd.h>
@@ -28,7 +27,7 @@ Copyright 2025 Yağız Zengin
namespace PartitionManager {
RUN_ASYNC(const std::string &partitionName, const uint64_t bufferSize) {
if (!PART_MAP.hasPartition(partitionName))
return {format("Couldn't find partition: %s", partitionName.data()), false};
return {Helper::format("Couldn't find partition: %s", partitionName.data()), false};
if (VARS.onLogical && !PART_MAP.isLogical(partitionName)) {
if (VARS.forceProcess)
@@ -38,7 +37,7 @@ RUN_ASYNC(const std::string &partitionName, const uint64_t bufferSize) {
<< std::endl;
else
return {
format("Used --logical (-l) flag but is not logical partition: %s",
Helper::format("Used --logical (-l) flag but is not logical partition: %s",
partitionName.data()),
false};
}
@@ -51,15 +50,17 @@ RUN_ASYNC(const std::string &partitionName, const uint64_t bufferSize) {
const int pfd = Helper::openAndAddToCloseList(
PART_MAP.getRealPathOf(partitionName), collector, O_WRONLY);
if (pfd < 0)
return {format("Can't open partition: %s: %s", partitionName.data(),
return {Helper::format("Can't open partition: %s: %s", partitionName.data(),
strerror(errno)),
false};
if (!VARS.forceProcess)
Helper::confirmPropt(
if (!VARS.forceProcess) {
if (!Helper::confirmPropt(
"Are you sure you want to continue? This could render your device "
"unusable! Do not continue if you "
"do not know what you are doing!");
"do not know what you are doing!"))
throw Error("Operation canceled.");
}
LOGN(EFUN, INFO) << "Writing zero bytes to partition: " << partitionName
<< std::endl;
@@ -76,13 +77,13 @@ RUN_ASYNC(const std::string &partitionName, const uint64_t bufferSize) {
toWrite = partitionSize - bytesWritten;
if (const ssize_t result = write(pfd, buffer, toWrite); result == -1)
return {format("Can't write zero bytes to partition: %s: %s",
return {Helper::format("Can't write zero bytes to partition: %s: %s",
partitionName.data(), strerror(errno)),
false};
else bytesWritten += result;
}
return {format("Successfully wrote zero bytes to the %s partition\n",
return {Helper::format("Successfully wrote zero bytes to the %s partition",
partitionName.data()),
true};
}

View File

@@ -29,11 +29,11 @@ namespace PartitionManager {
RUN_ASYNC(const std::string &partitionName, const std::string &imageName,
const uint64_t bufferSize, const bool deleteAfterProgress) {
if (!Helper::fileIsExists(imageName))
return {format("Couldn't find image file: %s", imageName.data()), false};
return {Helper::format("Couldn't find image file: %s", imageName.data()), false};
if (!PART_MAP.hasPartition(partitionName))
return {format("Couldn't find partition: %s", partitionName.data()), false};
return {Helper::format("Couldn't find partition: %s", partitionName.data()), false};
if (Helper::fileSize(imageName) > PART_MAP.sizeOf(partitionName))
return {format("%s is larger than %s partition size!", imageName.data(),
return {Helper::format("%s is larger than %s partition size!", imageName.data(),
partitionName.data()),
false};
@@ -48,7 +48,7 @@ RUN_ASYNC(const std::string &partitionName, const std::string &imageName,
<< std::endl;
else
return {
format("Used --logical (-l) flag but is not logical partition: %s",
Helper::format("Used --logical (-l) flag but is not logical partition: %s",
partitionName.data()),
false};
}
@@ -60,14 +60,14 @@ RUN_ASYNC(const std::string &partitionName, const std::string &imageName,
const int ffd = Helper::openAndAddToCloseList(imageName, collector, O_RDONLY);
if (ffd < 0)
return {format("Can't open image file %s: %s", imageName.data(),
return {Helper::format("Can't open image file %s: %s", imageName.data(),
strerror(errno)),
false};
const int pfd = Helper::openAndAddToCloseList(
PART_MAP.getRealPathOf(partitionName), collector, O_RDWR | O_TRUNC);
if (pfd < 0)
return {format("Can't open partition: %s: %s", partitionName.data(),
return {Helper::format("Can't open partition: %s: %s", partitionName.data(),
strerror(errno)),
false};
@@ -81,7 +81,7 @@ RUN_ASYNC(const std::string &partitionName, const std::string &imageName,
while ((bytesRead = read(ffd, buffer, bufferSize)) > 0) {
if (const ssize_t bytesWritten = write(pfd, buffer, bytesRead);
bytesWritten != bytesRead)
return {format("Can't write partition to output file %s: %s",
return {Helper::format("Can't write partition to output file %s: %s",
imageName.data(), strerror(errno)),
false};
}
@@ -93,7 +93,7 @@ RUN_ASYNC(const std::string &partitionName, const std::string &imageName,
std::string("Cannot erase flash file: " + imageName + "\n").data());
}
return {format("%s is successfully wrote to %s partition", imageName.data(),
return {Helper::format("%s is successfully wrote to %s partition", imageName.data(),
partitionName.data()),
true};
}

View File

@@ -66,11 +66,11 @@ INIT {
RUN {
std::vector<PartitionMap::Partition_t> jParts;
std::string multiple;
if (asByte) multiple = "B";
if (asKiloBytes) multiple = "KB";
if (asMega) multiple = "MB";
if (asGiga) multiple = "GB";
sizeCastTypes multiple;
if (asByte) multiple = B;
if (asKiloBytes) multiple = KB;
if (asMega) multiple = MB;
if (asGiga) multiple = GB;
auto func = [this, &jParts, &multiple] COMMON_LAMBDA_PARAMS -> bool {
if (VARS.onLogical && !props.isLogical) {
@@ -86,11 +86,11 @@ RUN {
if (jsonFormat)
jParts.push_back({partition,
{std::stoull(Helper::convertTo(props.size, multiple)),
{static_cast<uint64_t>(Helper::convertTo(props.size, multiple)),
props.isLogical}});
else
println("partition=%s size=%s isLogical=%s", partition.data(),
Helper::convertTo(props.size, multiple).data(),
println("partition=%s size=%d isLogical=%s", partition.data(),
Helper::convertTo(props.size, multiple),
props.isLogical ? "true" : "false");
return true;
@@ -106,7 +106,7 @@ RUN {
if (jsonFormat) {
nlohmann::json j;
j["multipleType"] = multiple;
j["multipleType"] = Helper::multipleToString(multiple);
j["partitions"] = nlohmann::json::array();
for (const auto &[name, props] : jParts) {
j["partitions"].push_back({{jNamePartition, name},

View File

@@ -42,7 +42,7 @@ INIT {
->default_val(false);
cmd->add_flag("--as-megabyte", asMega,
"Tell input size of partition list as megabyte.")
->default_val(false);
->default_val(true);
cmd->add_flag("--as-gigabyte", asGiga,
"Tell input size of partition list as gigabyte.")
->default_val(false);
@@ -54,7 +54,13 @@ INIT {
}
RUN {
auto func = [this] COMMON_LAMBDA_PARAMS -> bool {
sizeCastTypes multiple = {};
if (asByte) multiple = B;
if (asKiloBytes) multiple = KB;
if (asMega) multiple = MB;
if (asGiga) multiple = GB;
auto func = [this, &multiple] COMMON_LAMBDA_PARAMS -> bool {
if (VARS.onLogical && !props.isLogical) {
if (VARS.forceProcess)
LOGN(SFUN, WARNING)
@@ -66,16 +72,10 @@ RUN {
partition.data());
}
std::string multiple = "MB";
if (asByte) multiple = "B";
if (asKiloBytes) multiple = "KB";
if (asMega) multiple = "MB";
if (asGiga) multiple = "GB";
if (onlySize) println("%s", Helper::convertTo(props.size, multiple).data());
if (onlySize) println("%d", Helper::convertTo(props.size, multiple));
else
println("%s: %s%s", partition.data(),
Helper::convertTo(props.size, multiple).data(), multiple.data());
println("%s: %d%s", partition.data(),
Helper::convertTo(props.size, multiple), Helper::multipleToString(multiple).data());
return true;
};