pmt: start working on 1.3.0

- Add log cleaner function.
 - Introduce function flag structure.
 - Some improvements.
This commit is contained in:
2025-09-06 12:10:46 +03:00
parent 063d62fd85
commit 08e51c4a15
12 changed files with 105 additions and 20 deletions

View File

@@ -16,7 +16,7 @@
# Project info # Project info
cmake_minimum_required(VERSION 3.10) cmake_minimum_required(VERSION 3.10)
project(pmt VERSION 1.2.0) project(pmt VERSION 1.3.0)
# Set compiler flags # Set compiler flags
add_compile_options(-Wall -Werror -Wno-deprecated-declarations) add_compile_options(-Wall -Werror -Wno-deprecated-declarations)

View File

@@ -34,10 +34,17 @@
#define PART_MAP (*(*Variables).PartMap) #define PART_MAP (*(*Variables).PartMap)
namespace PartitionManager { namespace PartitionManager {
enum basic_function_flags {
NO_SU = 1,
NO_MAP_CHECK = 2,
ADB_SUFFICIENT = 3,
};
// All function classes must inherit from this class. // All function classes must inherit from this class.
class basic_function { class basic_function {
public: public:
CLI::App *cmd = nullptr; CLI::App *cmd = nullptr;
std::vector<int> flags = {};
virtual bool init(CLI::App &_app) = 0; virtual bool init(CLI::App &_app) = 0;
virtual bool run() = 0; virtual bool run() = 0;
@@ -56,6 +63,7 @@ private:
public: public:
void registerFunction(std::unique_ptr<basic_function> _func, CLI::App &_app); void registerFunction(std::unique_ptr<basic_function> _func, CLI::App &_app);
[[nodiscard]] bool hasFlagOnUsedFunction(int flag) const;
[[nodiscard]] bool isUsed(const std::string &name) const; [[nodiscard]] bool isUsed(const std::string &name) const;
[[nodiscard]] bool handleAll() const; [[nodiscard]] bool handleAll() const;
}; };
@@ -76,6 +84,7 @@ public:
using FunctionBase = basic_function; using FunctionBase = basic_function;
using FunctionManager = basic_function_manager; using FunctionManager = basic_function_manager;
using FunctionFlags = basic_function_flags;
using VariableTable = basic_variables; using VariableTable = basic_variables;
using Error = Helper::Error; using Error = Helper::Error;

View File

@@ -19,6 +19,7 @@ set(PMT_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/PartitionManager.cpp ${CMAKE_CURRENT_SOURCE_DIR}/PartitionManager.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Main.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Main.cpp
${CMAKE_CURRENT_SOURCE_DIR}/functions/BackupFunction.cpp ${CMAKE_CURRENT_SOURCE_DIR}/functions/BackupFunction.cpp
${CMAKE_CURRENT_SOURCE_DIR}/functions/CleanLogFunction.cpp
${CMAKE_CURRENT_SOURCE_DIR}/functions/EraseFunction.cpp ${CMAKE_CURRENT_SOURCE_DIR}/functions/EraseFunction.cpp
${CMAKE_CURRENT_SOURCE_DIR}/functions/FlashFunction.cpp ${CMAKE_CURRENT_SOURCE_DIR}/functions/FlashFunction.cpp
${CMAKE_CURRENT_SOURCE_DIR}/functions/InfoFunction.cpp ${CMAKE_CURRENT_SOURCE_DIR}/functions/InfoFunction.cpp
@@ -30,7 +31,8 @@ set(PMT_SOURCES
) )
# Add pmt # Add pmt
add_executable(pmt ${PMT_SOURCES}) add_executable(pmt ${PMT_SOURCES}
functions/CleanLogFunction.cpp)
add_executable(pmt_static ${PMT_SOURCES}) add_executable(pmt_static ${PMT_SOURCES})
# Set linker options # Set linker options

View File

@@ -20,6 +20,7 @@
#include <string> #include <string>
#include <unordered_set> #include <unordered_set>
#include <vector> #include <vector>
#include <algorithm>
namespace PartitionManager { namespace PartitionManager {
std::vector<std::string> splitIfHasDelim(const std::string &s, const char delim, std::vector<std::string> splitIfHasDelim(const std::string &s, const char delim,
@@ -91,6 +92,18 @@ bool basic_function_manager::isUsed(const std::string &name) const {
return false; return false;
} }
bool basic_function_manager::hasFlagOnUsedFunction(const int flag) const {
for (const auto &func : _functions) {
if (func->isUsed()) {
std::for_each(func->flags.begin(), func->flags.end(), [&](const int x) {
LOGN(PMTF, INFO) << "Used flag " << x << " on " << func->name() << std::endl;
});
return std::find(func->flags.begin(), func->flags.end(), flag) != func->flags.end();
}
}
return false;
}
bool basic_function_manager::handleAll() const { bool basic_function_manager::handleAll() const {
LOGN(PMTF, INFO) << "running caught function commands in command-line." LOGN(PMTF, INFO) << "running caught function commands in command-line."
<< std::endl; << std::endl;

View File

@@ -47,14 +47,13 @@ __attribute__((constructor)) void init() {
} }
static void sigHandler(const int sig) { static void sigHandler(const int sig) {
// Even if only SIGINT is to be captured for now, this is still a more if (sig == SIGINT) println("\n%sInterrupted.%s", YELLOW, STYLE_RESET);
// appropriate code if (sig == SIGABRT) println("\n%sAborted.%s", RED, STYLE_RESET);
if (sig == SIGINT) println("\n%sInterrupted.%s", YELLOW, STYLE_RESET);
exit(sig); exit(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 (!VARS.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);
@@ -87,6 +86,8 @@ int Main(int argc, char **argv) {
} }
signal(SIGINT, sigHandler); signal(SIGINT, sigHandler);
signal(SIGABRT, sigHandler);
signal(EXIT_FAILURE, sigHandler);
CLI::App AppMain{"Partition Manager Tool"}; CLI::App AppMain{"Partition Manager Tool"};
FunctionManager FuncManager; FunctionManager FuncManager;
@@ -121,6 +122,7 @@ int Main(int argc, char **argv) {
"Print version and exit"); "Print version and exit");
REGISTER_FUNCTION(backupFunction); REGISTER_FUNCTION(backupFunction);
REGISTER_FUNCTION(cleanLogFunction);
REGISTER_FUNCTION(flashFunction); REGISTER_FUNCTION(flashFunction);
REGISTER_FUNCTION(eraseFunction); REGISTER_FUNCTION(eraseFunction);
REGISTER_FUNCTION(partitionSizeFunction); REGISTER_FUNCTION(partitionSizeFunction);
@@ -137,25 +139,35 @@ int Main(int argc, char **argv) {
println("%s", getAppVersion().data()); println("%s", getAppVersion().data());
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
if (!VARS.searchPath.empty()) (PART_MAP)(VARS.searchPath);
if (!VARS.PartMap && VARS.searchPath.empty()) if (FuncManager.hasFlagOnUsedFunction(NO_MAP_CHECK)) {
throw Error("No default search entries were found. Specify a search " if (!VARS.searchPath.empty())
"directory with -S " WARNING("-S (--search-path) flag is ignored. Because, don't needed "
"(--search-path)"); "partition map by your used function.\n");
if (VARS.onLogical)
WARNING("-l (--logical) flag ignored. Because, partition type don't "
"needed by your used function.\n");
} else {
if (!VARS.searchPath.empty()) (PART_MAP)(VARS.searchPath);
if (!VARS.PartMap && VARS.searchPath.empty())
throw Error("No default search entries were found. Specify a search "
"directory with -S "
"(--search-path)");
if (VARS.onLogical) { if (VARS.onLogical) {
if (!PART_MAP.hasLogicalPartitions()) if (!PART_MAP.hasLogicalPartitions())
throw Error("This device doesn't contains logical partitions. But you " throw Error(
"used -l (--logical) flag."); "This device doesn't contains logical partitions. But you "
"used -l (--logical) flag.");
}
} }
if (!Helper::hasSuperUser()) { if (!Helper::hasSuperUser() && !FuncManager.hasFlagOnUsedFunction(NO_SU)) {
if (!((FuncManager.isUsed("rebootFunction") && if (!(FuncManager.hasFlagOnUsedFunction(ADB_SUFFICIENT) &&
Helper::hasAdbPermissions()) || Helper::hasAdbPermissions())) {
FuncManager.isUsed("memoryTestFunction")))
throw Error( throw Error(
"Partition Manager Tool is requires super-user privileges!"); "This function is requires super-user privileges!");
}
} }
return FuncManager.handleAll() == true ? EXIT_SUCCESS : EXIT_FAILURE; return FuncManager.handleAll() == true ? EXIT_SUCCESS : EXIT_FAILURE;

View File

@@ -0,0 +1,41 @@
/*
Copyright 2025 Yağız Zengin
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "functions.hpp"
#include <PartitionManager/PartitionManager.hpp>
#define CFUN "cleanLogFunction"
#define FUNCTION_CLASS cleanLogFunction
namespace PartitionManager {
INIT {
LOGN(CFUN, INFO) << "Initializing variables of clean log function." << std::endl;
flags = {FunctionFlags::NO_MAP_CHECK, FunctionFlags::NO_SU};
cmd = _app.add_subcommand("clean-logs", "Clean PMT logs.");
return true;
}
RUN {
LOGN(CFUN, INFO) << "Removing log file: " << VARS.logFile << std::endl;
Helper::LoggingProperties::setLoggingState(YES); // eraseEntry writes log!
return Helper::eraseEntry(VARS.logFile);
}
IS_USED_COMMON_BODY
NAME { return CFUN; }
} // namespace PartitionManager

View File

@@ -31,6 +31,7 @@ namespace PartitionManager {
INIT { INIT {
LOGN(MTFUN, INFO) << "Initializing variables of memory test function." LOGN(MTFUN, INFO) << "Initializing variables of memory test function."
<< std::endl; << std::endl;
flags = {FunctionFlags::NO_MAP_CHECK, FunctionFlags::ADB_SUFFICIENT};
cmd = _app.add_subcommand("memtest", "Test your write/read speed of device."); cmd = _app.add_subcommand("memtest", "Test your write/read speed of device.");
cmd->add_option("testDirectory", testPath, "Path to test directory") cmd->add_option("testDirectory", testPath, "Path to test directory")
->default_val("/data/local/tmp") ->default_val("/data/local/tmp")

View File

@@ -23,6 +23,7 @@ Copyright 2025 Yağız Zengin
namespace PartitionManager { namespace PartitionManager {
INIT { INIT {
LOGN(RFUN, INFO) << "Initializing variables of reboot function." << std::endl; LOGN(RFUN, INFO) << "Initializing variables of reboot function." << std::endl;
flags = {FunctionFlags::NO_MAP_CHECK, FunctionFlags::ADB_SUFFICIENT};
cmd = _app.add_subcommand("reboot", "Reboots device"); cmd = _app.add_subcommand("reboot", "Reboots device");
cmd->add_option("rebootTarget", rebootTarget, cmd->add_option("rebootTarget", rebootTarget,
"Reboot target (default: normal)"); "Reboot target (default: normal)");

View File

@@ -142,6 +142,11 @@ public:
COMMON_FUNCTION_BODY(); COMMON_FUNCTION_BODY();
}; };
class cleanLogFunction final : public FunctionBase {
public:
COMMON_FUNCTION_BODY();
};
} // namespace PartitionManager } // namespace PartitionManager
#endif // #ifndef FUNCTIONS_HPP #endif // #ifndef FUNCTIONS_HPP

View File

@@ -32,6 +32,7 @@
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <sys/_system_properties.h> #include <sys/_system_properties.h>
#include <cutils/android_reboot.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>