pmt: start working on 1.3.0
- Add log cleaner function. - Introduce function flag structure. - Some improvements.
This commit is contained in:
@@ -16,7 +16,7 @@
|
||||
|
||||
# Project info
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
project(pmt VERSION 1.2.0)
|
||||
project(pmt VERSION 1.3.0)
|
||||
|
||||
# Set compiler flags
|
||||
add_compile_options(-Wall -Werror -Wno-deprecated-declarations)
|
||||
|
||||
@@ -34,10 +34,17 @@
|
||||
#define PART_MAP (*(*Variables).PartMap)
|
||||
|
||||
namespace PartitionManager {
|
||||
enum basic_function_flags {
|
||||
NO_SU = 1,
|
||||
NO_MAP_CHECK = 2,
|
||||
ADB_SUFFICIENT = 3,
|
||||
};
|
||||
|
||||
// All function classes must inherit from this class.
|
||||
class basic_function {
|
||||
public:
|
||||
CLI::App *cmd = nullptr;
|
||||
std::vector<int> flags = {};
|
||||
|
||||
virtual bool init(CLI::App &_app) = 0;
|
||||
virtual bool run() = 0;
|
||||
@@ -56,6 +63,7 @@ private:
|
||||
public:
|
||||
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 handleAll() const;
|
||||
};
|
||||
@@ -76,6 +84,7 @@ public:
|
||||
|
||||
using FunctionBase = basic_function;
|
||||
using FunctionManager = basic_function_manager;
|
||||
using FunctionFlags = basic_function_flags;
|
||||
using VariableTable = basic_variables;
|
||||
using Error = Helper::Error;
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ set(PMT_SOURCES
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/PartitionManager.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/Main.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/FlashFunction.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/functions/InfoFunction.cpp
|
||||
@@ -30,7 +31,8 @@ set(PMT_SOURCES
|
||||
)
|
||||
|
||||
# Add pmt
|
||||
add_executable(pmt ${PMT_SOURCES})
|
||||
add_executable(pmt ${PMT_SOURCES}
|
||||
functions/CleanLogFunction.cpp)
|
||||
add_executable(pmt_static ${PMT_SOURCES})
|
||||
|
||||
# Set linker options
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
#include <string>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
namespace PartitionManager {
|
||||
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;
|
||||
}
|
||||
|
||||
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 {
|
||||
LOGN(PMTF, INFO) << "running caught function commands in command-line."
|
||||
<< std::endl;
|
||||
|
||||
@@ -47,14 +47,13 @@ __attribute__((constructor)) void init() {
|
||||
}
|
||||
|
||||
static void sigHandler(const int sig) {
|
||||
// Even if only SIGINT is to be captured for now, this is still a more
|
||||
// appropriate code
|
||||
if (sig == SIGINT) println("\n%sInterrupted.%s", YELLOW, STYLE_RESET);
|
||||
if (sig == SIGABRT) println("\n%sAborted.%s", RED, STYLE_RESET);
|
||||
exit(sig);
|
||||
}
|
||||
|
||||
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) {
|
||||
const int ret = fwrite(buf, 1, static_cast<size_t>(size), real);
|
||||
fflush(real);
|
||||
@@ -87,6 +86,8 @@ int Main(int argc, char **argv) {
|
||||
}
|
||||
|
||||
signal(SIGINT, sigHandler);
|
||||
signal(SIGABRT, sigHandler);
|
||||
signal(EXIT_FAILURE, sigHandler);
|
||||
|
||||
CLI::App AppMain{"Partition Manager Tool"};
|
||||
FunctionManager FuncManager;
|
||||
@@ -121,6 +122,7 @@ int Main(int argc, char **argv) {
|
||||
"Print version and exit");
|
||||
|
||||
REGISTER_FUNCTION(backupFunction);
|
||||
REGISTER_FUNCTION(cleanLogFunction);
|
||||
REGISTER_FUNCTION(flashFunction);
|
||||
REGISTER_FUNCTION(eraseFunction);
|
||||
REGISTER_FUNCTION(partitionSizeFunction);
|
||||
@@ -137,8 +139,16 @@ int Main(int argc, char **argv) {
|
||||
println("%s", getAppVersion().data());
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
if (!VARS.searchPath.empty()) (PART_MAP)(VARS.searchPath);
|
||||
|
||||
if (FuncManager.hasFlagOnUsedFunction(NO_MAP_CHECK)) {
|
||||
if (!VARS.searchPath.empty())
|
||||
WARNING("-S (--search-path) flag is ignored. Because, don't needed "
|
||||
"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 "
|
||||
@@ -146,16 +156,18 @@ int Main(int argc, char **argv) {
|
||||
|
||||
if (VARS.onLogical) {
|
||||
if (!PART_MAP.hasLogicalPartitions())
|
||||
throw Error("This device doesn't contains logical partitions. But you "
|
||||
throw Error(
|
||||
"This device doesn't contains logical partitions. But you "
|
||||
"used -l (--logical) flag.");
|
||||
}
|
||||
}
|
||||
|
||||
if (!Helper::hasSuperUser()) {
|
||||
if (!((FuncManager.isUsed("rebootFunction") &&
|
||||
Helper::hasAdbPermissions()) ||
|
||||
FuncManager.isUsed("memoryTestFunction")))
|
||||
if (!Helper::hasSuperUser() && !FuncManager.hasFlagOnUsedFunction(NO_SU)) {
|
||||
if (!(FuncManager.hasFlagOnUsedFunction(ADB_SUFFICIENT) &&
|
||||
Helper::hasAdbPermissions())) {
|
||||
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;
|
||||
|
||||
41
src/functions/CleanLogFunction.cpp
Normal file
41
src/functions/CleanLogFunction.cpp
Normal 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
|
||||
@@ -31,6 +31,7 @@ namespace PartitionManager {
|
||||
INIT {
|
||||
LOGN(MTFUN, INFO) << "Initializing variables of memory test function."
|
||||
<< std::endl;
|
||||
flags = {FunctionFlags::NO_MAP_CHECK, FunctionFlags::ADB_SUFFICIENT};
|
||||
cmd = _app.add_subcommand("memtest", "Test your write/read speed of device.");
|
||||
cmd->add_option("testDirectory", testPath, "Path to test directory")
|
||||
->default_val("/data/local/tmp")
|
||||
|
||||
@@ -23,6 +23,7 @@ Copyright 2025 Yağız Zengin
|
||||
namespace PartitionManager {
|
||||
INIT {
|
||||
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->add_option("rebootTarget", rebootTarget,
|
||||
"Reboot target (default: normal)");
|
||||
|
||||
@@ -142,6 +142,11 @@ public:
|
||||
COMMON_FUNCTION_BODY();
|
||||
};
|
||||
|
||||
class cleanLogFunction final : public FunctionBase {
|
||||
public:
|
||||
COMMON_FUNCTION_BODY();
|
||||
};
|
||||
|
||||
} // namespace PartitionManager
|
||||
|
||||
#endif // #ifndef FUNCTIONS_HPP
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <sys/_system_properties.h>
|
||||
#include <cutils/android_reboot.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user