diff --git a/CMakeLists.txt b/CMakeLists.txt index ad6b485..aef8726 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/include/PartitionManager/PartitionManager.hpp b/include/PartitionManager/PartitionManager.hpp index f688d1f..2c8caeb 100644 --- a/include/PartitionManager/PartitionManager.hpp +++ b/include/PartitionManager/PartitionManager.hpp @@ -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 flags = {}; virtual bool init(CLI::App &_app) = 0; virtual bool run() = 0; @@ -56,6 +63,7 @@ private: public: void registerFunction(std::unique_ptr _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; diff --git a/include/LICENSE.nlohmann b/include/nlohmann/LICENSE similarity index 100% rename from include/LICENSE.nlohmann rename to include/nlohmann/LICENSE diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e747b0b..d166304 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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 diff --git a/src/FunctionManager.cpp b/src/FunctionManager.cpp index d80d2c9..3aeca39 100644 --- a/src/FunctionManager.cpp +++ b/src/FunctionManager.cpp @@ -20,6 +20,7 @@ #include #include #include +#include namespace PartitionManager { std::vector 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; diff --git a/src/PartitionManager.cpp b/src/PartitionManager.cpp index dc103e4..c9383ff 100644 --- a/src/PartitionManager.cpp +++ b/src/PartitionManager.cpp @@ -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 == 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(cookie); + auto *real = static_cast(cookie); if (!VARS.quietProcess) { const int ret = fwrite(buf, 1, static_cast(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,25 +139,35 @@ int Main(int argc, char **argv) { println("%s", getAppVersion().data()); return EXIT_SUCCESS; } - 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 (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 " + "(--search-path)"); - if (VARS.onLogical) { - if (!PART_MAP.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()) { - 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; diff --git a/src/functions/CleanLogFunction.cpp b/src/functions/CleanLogFunction.cpp new file mode 100644 index 0000000..03438d5 --- /dev/null +++ b/src/functions/CleanLogFunction.cpp @@ -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 + +#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 diff --git a/src/functions/MemoryTestFunction.cpp b/src/functions/MemoryTestFunction.cpp index b8c22a2..084cab5 100644 --- a/src/functions/MemoryTestFunction.cpp +++ b/src/functions/MemoryTestFunction.cpp @@ -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") diff --git a/src/functions/RebootFunction.cpp b/src/functions/RebootFunction.cpp index 12dce53..ece5dcc 100644 --- a/src/functions/RebootFunction.cpp +++ b/src/functions/RebootFunction.cpp @@ -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)"); diff --git a/src/functions/functions.hpp b/src/functions/functions.hpp index 7ae961b..020eb8c 100644 --- a/src/functions/functions.hpp +++ b/src/functions/functions.hpp @@ -142,6 +142,11 @@ public: COMMON_FUNCTION_BODY(); }; +class cleanLogFunction final : public FunctionBase { +public: + COMMON_FUNCTION_BODY(); +}; + } // namespace PartitionManager #endif // #ifndef FUNCTIONS_HPP diff --git a/include/cutils/android_reboot.h b/srclib/libhelper/include/cutils/android_reboot.h similarity index 100% rename from include/cutils/android_reboot.h rename to srclib/libhelper/include/cutils/android_reboot.h diff --git a/srclib/libhelper/src/Utilities.cpp b/srclib/libhelper/src/Utilities.cpp index ba12706..0494d1b 100644 --- a/srclib/libhelper/src/Utilities.cpp +++ b/srclib/libhelper/src/Utilities.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include