pmt: add stdin parsing support, etc.

This commit is contained in:
2025-09-15 00:01:52 +03:00
parent 398b119cb4
commit 318739adc8
7 changed files with 67 additions and 44 deletions

View File

@@ -28,7 +28,11 @@
namespace PartitionManager {
// Usage: REGISTER_FUNCTION(FUNCTION_CLASS);
/**
* Register functions. Uses ready 'FuncManager' variable.
*
* Usage: REGISTER_FUNCTION(FUNCTION_CLASS);
*/
#define REGISTER_FUNCTION(cls) \
FuncManager.registerFunction(std::make_unique<cls>(), AppMain)
@@ -78,6 +82,22 @@ int Main(int argc, char **argv) {
collector.closeAfterProgress(pstdout);
collector.closeAfterProgress(pstderr);
signal(SIGINT, sigHandler);
signal(SIGABRT, sigHandler);
if (!isatty(fileno(stdin))) {
char buf[128];
while (fgets(buf, sizeof(buf), stdin) != nullptr) {
buf[strcspn(buf, "\n")] = 0;
const char *token = strtok(buf, " \t");
while (token != nullptr) {
argv[argc] = strdup(token);
argc++;
token = strtok(nullptr, " \t");
}
}
}
if (argc < 2) {
println(
"Usage: %s [OPTIONS] [SUBCOMMAND]\nUse --help for more information.",
@@ -85,9 +105,6 @@ int Main(int argc, char **argv) {
return EXIT_FAILURE;
}
signal(SIGINT, sigHandler);
signal(SIGABRT, sigHandler);
CLI::App AppMain{"Partition Manager Tool"};
FunctionManager FuncManager;

View File

@@ -31,7 +31,8 @@ namespace PartitionManager {
RUN_ASYNC(const std::string &partitionName, const std::string &outputName,
const uint64_t bufferSize) {
if (!PART_MAP.hasPartition(partitionName))
return {Helper::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;
@@ -43,8 +44,8 @@ RUN_ASYNC(const std::string &partitionName, const std::string &outputName,
<< " is exists but not logical. Ignoring (from --force, -f)."
<< std::endl;
else
return {
Helper::format("Used --logical (-l) flag but is not logical partition: %s",
return {Helper::format(
"Used --logical (-l) flag but is not logical partition: %s",
partitionName.data()),
false};
}
@@ -70,8 +71,8 @@ RUN_ASYNC(const std::string &partitionName, const std::string &outputName,
const int ffd = Helper::openAndAddToCloseList(
outputName, collector, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (ffd < 0)
return {Helper::format("Can't create/open output file %s: %s", outputName.data(),
strerror(errno)),
return {Helper::format("Can't create/open output file %s: %s",
outputName.data(), strerror(errno)),
false};
LOGN(BFUN, INFO) << "Writing partition " << partitionName

View File

@@ -27,7 +27,8 @@ Copyright 2025 Yağız Zengin
namespace PartitionManager {
RUN_ASYNC(const std::string &partitionName, const uint64_t bufferSize) {
if (!PART_MAP.hasPartition(partitionName))
return {Helper::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)
@@ -36,8 +37,8 @@ RUN_ASYNC(const std::string &partitionName, const uint64_t bufferSize) {
<< " is exists but not logical. Ignoring (from --force, -f)."
<< std::endl;
else
return {
Helper::format("Used --logical (-l) flag but is not logical partition: %s",
return {Helper::format(
"Used --logical (-l) flag but is not logical partition: %s",
partitionName.data()),
false};
}

View File

@@ -29,12 +29,14 @@ namespace PartitionManager {
RUN_ASYNC(const std::string &partitionName, const std::string &imageName,
const uint64_t bufferSize, const bool deleteAfterProgress) {
if (!Helper::fileIsExists(imageName))
return {Helper::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 {Helper::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 {Helper::format("%s is larger than %s partition size!", imageName.data(),
partitionName.data()),
return {Helper::format("%s is larger than %s partition size!",
imageName.data(), partitionName.data()),
false};
LOGN(FFUN, INFO) << "flashing " << imageName << " to " << partitionName
@@ -47,8 +49,8 @@ RUN_ASYNC(const std::string &partitionName, const std::string &imageName,
<< " is exists but not logical. Ignoring (from --force, -f)."
<< std::endl;
else
return {
Helper::format("Used --logical (-l) flag but is not logical partition: %s",
return {Helper::format(
"Used --logical (-l) flag but is not logical partition: %s",
partitionName.data()),
false};
}
@@ -93,8 +95,8 @@ RUN_ASYNC(const std::string &partitionName, const std::string &imageName,
std::string("Cannot erase flash file: " + imageName + "\n").data());
}
return {Helper::format("%s is successfully wrote to %s partition", imageName.data(),
partitionName.data()),
return {Helper::format("%s is successfully wrote to %s partition",
imageName.data(), partitionName.data()),
true};
}

View File

@@ -85,7 +85,8 @@ RUN {
}
if (jsonFormat)
jParts.push_back({partition,
jParts.push_back(
{partition,
{static_cast<uint64_t>(Helper::convertTo(props.size, multiple)),
props.isLogical}});
else

View File

@@ -75,7 +75,8 @@ RUN {
if (onlySize) println("%d", Helper::convertTo(props.size, multiple));
else
println("%s: %d%s", partition.data(),
Helper::convertTo(props.size, multiple), Helper::multipleToString(multiple).data());
Helper::convertTo(props.size, multiple),
Helper::multipleToString(multiple).data());
return true;
};

View File

@@ -25,6 +25,7 @@
#else
#include <sys/system_properties.h>
#endif
#include <cstdarg>
#include <cutils/android_reboot.h>
#include <iostream>
#include <libgen.h>
@@ -35,7 +36,6 @@
#include <sys/_system_properties.h>
#include <sys/stat.h>
#include <unistd.h>
#include <cstdarg>
#ifdef __ANDROID__
// From system/core/libcutils/android_reboot.cpp android16-s2-release