pmt: reformat code

This commit is contained in:
2025-08-10 18:44:58 +03:00
parent 787a2e4e46
commit f5e465b995
29 changed files with 12503 additions and 10715 deletions

103
src/FunctionManager.cpp Executable file → Normal file
View File

@@ -14,63 +14,76 @@
limitations under the License.
*/
#include <PartitionManager/PartitionManager.hpp>
#include <fcntl.h>
#include <vector>
#include <memory>
#include <string>
#include <unordered_set>
#include <PartitionManager/PartitionManager.hpp>
#include <vector>
namespace PartitionManager {
std::vector<std::string> splitIfHasDelim(const std::string &s, const char delim, const bool checkForBadUsage) {
if (s.find(delim) == std::string::npos) return {};
auto vec = CLI::detail::split(s, delim);
std::vector<std::string> splitIfHasDelim(const std::string &s, const char delim,
const bool checkForBadUsage) {
if (s.find(delim) == std::string::npos) return {};
auto vec = CLI::detail::split(s, delim);
if (checkForBadUsage) {
std::unordered_set<std::string> set;
for (const auto &str: vec) {
if (set.find(str) != set.end()) throw CLI::ValidationError("Duplicate element in your inputs!");
set.insert(str);
}
}
if (checkForBadUsage) {
std::unordered_set<std::string> set;
for (const auto &str : vec) {
if (set.find(str) != set.end())
throw CLI::ValidationError("Duplicate element in your inputs!");
set.insert(str);
}
}
return vec;
}
return vec;
}
void setupBufferSize(int &size, const std::string &partition) {
if (Variables->PartMap->sizeOf(partition) % size != 0) {
print("%sWARNING%s: Specified buffer size is invalid! Using 1 byte as buffer size.", YELLOW, STYLE_RESET);
size = 1;
}
}
void setupBufferSize(int &size, const std::string &partition) {
if (Variables->PartMap->sizeOf(partition) % size != 0) {
print("%sWARNING%s: Specified buffer size is invalid! Using 1 byte as "
"buffer size.",
YELLOW, STYLE_RESET);
size = 1;
}
}
void processCommandLine(std::vector<std::string> &vec1, std::vector<std::string> &vec2, const std::string &s1,
const std::string &s2, const char delim, const bool checkForBadUsage) {
vec1 = splitIfHasDelim(s1, delim, checkForBadUsage);
vec2 = splitIfHasDelim(s2, delim, checkForBadUsage);
void processCommandLine(std::vector<std::string> &vec1,
std::vector<std::string> &vec2, const std::string &s1,
const std::string &s2, const char delim,
const bool checkForBadUsage) {
vec1 = splitIfHasDelim(s1, delim, checkForBadUsage);
vec2 = splitIfHasDelim(s2, delim, checkForBadUsage);
if (vec1.empty() && !s1.empty()) vec1.push_back(s1);
if (vec2.empty() && !s2.empty()) vec2.push_back(s2);
}
if (vec1.empty() && !s1.empty()) vec1.push_back(s1);
if (vec2.empty() && !s2.empty()) vec2.push_back(s2);
}
void basic_function_manager::registerFunction(std::unique_ptr<basic_function> _func, CLI::App &_app) {
LOGN(PMTF, INFO) << "registering function: " << _func->name() << std::endl;
if (!_func->init(_app)) throw Error("Cannot init function: %s\n", _func->name());
_functions.push_back(std::move(_func));
LOGN(PMTF, INFO) << _functions.back()->name() << " successfully registered." << std::endl;
}
void basic_function_manager::registerFunction(
std::unique_ptr<basic_function> _func, CLI::App &_app) {
LOGN(PMTF, INFO) << "registering function: " << _func->name() << std::endl;
if (!_func->init(_app))
throw Error("Cannot init function: %s\n", _func->name());
_functions.push_back(std::move(_func));
LOGN(PMTF, INFO) << _functions.back()->name() << " successfully registered."
<< std::endl;
}
bool basic_function_manager::handleAll() const {
LOGN(PMTF, INFO) << "running caught function commands in command-line." << std::endl;
for (const auto &func: _functions) {
if (func->isUsed()) {
LOGN(PMTF, INFO) << func->name() << " is calling because used in command-line." << std::endl;
return func->run();
}
}
bool basic_function_manager::handleAll() const {
LOGN(PMTF, INFO) << "running caught function commands in command-line."
<< std::endl;
for (const auto &func : _functions) {
if (func->isUsed()) {
LOGN(PMTF, INFO) << func->name()
<< " is calling because used in command-line."
<< std::endl;
return func->run();
}
}
LOGN(PMTF, INFO) << "not found any used function from command-line." << std::endl;
println("Target progress is not specified. Specify a progress.");
return false;
}
LOGN(PMTF, INFO) << "not found any used function from command-line."
<< std::endl;
println("Target progress is not specified. Specify a progress.");
return false;
}
} // namespace PartitionManager

9
src/Main.cpp Executable file → Normal file
View File

@@ -16,9 +16,8 @@
#include <PartitionManager/PartitionManager.hpp>
int main(int argc, char** argv)
{
// Call integrated main function in library
Helper::LoggingProperties::setProgramName(PMTE);
return PartitionManager::Main(argc, argv);
int main(int argc, char **argv) {
// Call integrated main function in library
Helper::LoggingProperties::setProgramName(PMTE);
return PartitionManager::Main(argc, argv);
}

223
src/PartitionManager.cpp Executable file → Normal file
View File

@@ -14,125 +14,152 @@
limitations under the License.
*/
#include <string>
#include "functions/functions.hpp"
#include <PartitionManager/PartitionManager.hpp>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstdarg>
#include <unistd.h>
#include <PartitionManager/PartitionManager.hpp>
#include <generated/buildInfo.hpp>
#include "functions/functions.hpp"
#include <string>
#include <unistd.h>
namespace PartitionManager {
variableProtect protector;
auto Variables = new VariableTable();
variableProtect protector;
auto Variables = new VariableTable();
variableProtect::variableProtect() { Helper::LoggingProperties::setLogFile("/sdcard/Documents/last_pmt_logs.log"); }
variableProtect::~variableProtect() { delete _ptr; }
void variableProtect::setVariablePointer(basic_variables *&_ptr) { this->_ptr = _ptr; }
variableProtect::variableProtect() {
Helper::LoggingProperties::setLogFile("/sdcard/Documents/last_pmt_logs.log");
}
variableProtect::~variableProtect() { delete _ptr; }
void variableProtect::setVariablePointer(basic_variables *&_ptr) {
this->_ptr = _ptr;
}
basic_variables::~basic_variables() { delete PartMap; }
basic_variables::basic_variables() : PartMap(new PartitionMap::BuildMap()),
logFile("/sdcard/Documents/last_pmt_logs.log"),
onLogical(false),
quietProcess(false),
verboseMode(false),
viewVersion(false),
forceProcess(false)
{}
basic_variables::~basic_variables() { delete PartMap; }
basic_variables::basic_variables()
: PartMap(new PartitionMap::BuildMap()),
logFile("/sdcard/Documents/last_pmt_logs.log"), onLogical(false),
quietProcess(false), verboseMode(false), viewVersion(false),
forceProcess(false) {}
int Main(int argc, char **argv) {
try {
// try-catch start
CLI::App AppMain{"Partition Manager Tool"};
protector.setVariablePointer(Variables);
FunctionManager FuncManager;
int Main(int argc, char **argv) {
try {
// try-catch start
CLI::App AppMain{"Partition Manager Tool"};
protector.setVariablePointer(Variables);
FunctionManager FuncManager;
AppMain.fallthrough(true);
AppMain.set_help_all_flag("--help-all", "Print full help message and exit");
AppMain.footer("Partition Manager Tool is written by YZBruh\nThis project licensed under Apache 2.0 license\nReport bugs to https://github.com/ShawkTeam/pmt-renovated/issues");
AppMain.add_option("-S,--search-path", Variables->searchPath, "Set partition search path")->check(
[&](const std::string &val) {
if (val.find("/block") == std::string::npos) throw CLI::ValidationError(
"Partition search path is unexpected! Couldn't find 'block' in input path!");
return std::string();
});
AppMain.add_option("-L,--log-file", Variables->logFile, "Set log file");
AppMain.add_flag("-f,--force", Variables->forceProcess, "Force process to be processed");
AppMain.add_flag("-l,--logical", Variables->onLogical, "Specify that the target partition is dynamic");
AppMain.add_flag("-q,--quiet", Variables->quietProcess, "Quiet process");
AppMain.add_flag("-V,--verbose", Variables->verboseMode,
"Detailed information is written on the screen while the transaction is being carried out");
AppMain.add_flag("-v,--version", Variables->viewVersion, "Print version and exit");
AppMain.fallthrough(true);
AppMain.set_help_all_flag("--help-all", "Print full help message and exit");
AppMain.footer("Partition Manager Tool is written by YZBruh\nThis project "
"licensed under "
"Apache 2.0 license\nReport "
"bugs to https://github.com/ShawkTeam/pmt-renovated/issues");
AppMain
.add_option("-S,--search-path", Variables->searchPath,
"Set partition search path")
->check([&](const std::string &val) {
if (val.find("/block") == std::string::npos)
throw CLI::ValidationError(
"Partition search path is unexpected! Couldn't find "
"'block' in input path!");
return std::string();
});
AppMain.add_option("-L,--log-file", Variables->logFile, "Set log file");
AppMain.add_flag("-f,--force", Variables->forceProcess,
"Force process to be processed");
AppMain.add_flag("-l,--logical", Variables->onLogical,
"Specify that the target partition is dynamic");
AppMain.add_flag("-q,--quiet", Variables->quietProcess, "Quiet process");
AppMain.add_flag("-V,--verbose", Variables->verboseMode,
"Detailed information is written on the screen while the "
"transaction is "
"being carried out");
AppMain.add_flag("-v,--version", Variables->viewVersion,
"Print version and exit");
if (argc < 2) {
println("Usage: %s [OPTIONS] [SUBCOMMAND]\nUse --help for more information.", argv[0]);
return EXIT_FAILURE;
}
if (argc < 2) {
println(
"Usage: %s [OPTIONS] [SUBCOMMAND]\nUse --help for more information.",
argv[0]);
return EXIT_FAILURE;
}
FuncManager.registerFunction(std::make_unique<backupFunction>(), AppMain);
FuncManager.registerFunction(std::make_unique<flashFunction>(), AppMain);
FuncManager.registerFunction(std::make_unique<eraseFunction>(), AppMain);
FuncManager.registerFunction(std::make_unique<partitionSizeFunction>(), AppMain);
FuncManager.registerFunction(std::make_unique<infoFunction>(), AppMain);
FuncManager.registerFunction(std::make_unique<realPathFunction>(), AppMain);
FuncManager.registerFunction(std::make_unique<realLinkPathFunction>(), AppMain);
FuncManager.registerFunction(std::make_unique<typeFunction>(), AppMain);
FuncManager.registerFunction(std::make_unique<rebootFunction>(), AppMain);
FuncManager.registerFunction(std::make_unique<backupFunction>(), AppMain);
FuncManager.registerFunction(std::make_unique<flashFunction>(), AppMain);
FuncManager.registerFunction(std::make_unique<eraseFunction>(), AppMain);
FuncManager.registerFunction(std::make_unique<partitionSizeFunction>(),
AppMain);
FuncManager.registerFunction(std::make_unique<infoFunction>(), AppMain);
FuncManager.registerFunction(std::make_unique<realPathFunction>(), AppMain);
FuncManager.registerFunction(std::make_unique<realLinkPathFunction>(),
AppMain);
FuncManager.registerFunction(std::make_unique<typeFunction>(), AppMain);
FuncManager.registerFunction(std::make_unique<rebootFunction>(), AppMain);
CLI11_PARSE(AppMain, argc, argv);
CLI11_PARSE(AppMain, argc, argv);
if (Variables->verboseMode) Helper::LoggingProperties::setPrinting(YES);
if (Variables->viewVersion) { println("%s", getAppVersion().data()); return EXIT_SUCCESS; }
if (!Variables->searchPath.empty()) (*Variables->PartMap)(Variables->searchPath);
if (Variables->verboseMode) Helper::LoggingProperties::setPrinting(YES);
if (Variables->viewVersion) {
println("%s", getAppVersion().data());
return EXIT_SUCCESS;
}
if (!Variables->searchPath.empty())
(*Variables->PartMap)(Variables->searchPath);
if (!Variables->PartMap && Variables->searchPath.empty())
throw Error("No default search entries were found. Specify a search directory with -S (--search-path)");
if (!Variables->PartMap && Variables->searchPath.empty())
throw Error("No default search entries were found. Specify a search "
"directory with -S "
"(--search-path)");
if (!Helper::hasSuperUser()) throw Error("Partition Manager Tool is requires super-user privileges!\n");
if (!Helper::hasSuperUser())
throw Error(
"Partition Manager Tool is requires super-user privileges!\n");
return FuncManager.handleAll() == true ? EXIT_SUCCESS : EXIT_FAILURE;
} catch (Helper::Error &error) {
// catch Helper::Error
return FuncManager.handleAll() == true ? EXIT_SUCCESS : EXIT_FAILURE;
} catch (Helper::Error &error) {
// catch Helper::Error
if (!Variables->quietProcess) fprintf(stderr, "%s%sERROR(S) OCCURRED:%s\n%s", RED, BOLD, STYLE_RESET, error.what());
return EXIT_FAILURE;
} catch (CLI::Error &error) {
// catch CLI::Error
if (!Variables->quietProcess)
fprintf(stderr, "%s%sERROR(S) OCCURRED:%s\n%s", RED, BOLD, STYLE_RESET,
error.what());
return EXIT_FAILURE;
} catch (CLI::Error &error) {
// catch CLI::Error
fprintf(stderr, "%s: %s%sFLAG PARSE ERROR:%s %s\n", argv[0], RED, BOLD, STYLE_RESET, error.what());
return EXIT_FAILURE;
} // try-catch block end
}
fprintf(stderr, "%s: %s%sFLAG PARSE ERROR:%s %s\n", argv[0], RED, BOLD,
STYLE_RESET, error.what());
return EXIT_FAILURE;
} // try-catch block end
}
void print(const char *format, ...) {
va_list args;
va_start(args, format);
if (!Variables->quietProcess) vfprintf(stdout, format, args);
va_end(args);
}
void print(const char *format, ...) {
va_list args;
va_start(args, format);
if (!Variables->quietProcess) vfprintf(stdout, format, args);
va_end(args);
}
void println(const char *format, ...) {
va_list args;
va_start(args, format);
if (!Variables->quietProcess) { vfprintf(stdout, format, args); print("\n"); }
va_end(args);
}
void println(const char *format, ...) {
va_list args;
va_start(args, format);
if (!Variables->quietProcess) {
vfprintf(stdout, format, args);
print("\n");
}
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 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 getLibVersion() { MKVERSION(PMT); }
std::string getAppVersion() {
MKVERSION(PMTE);
}
std::string getAppVersion() { MKVERSION(PMTE); }
} // namespace PartitionManager

View File

@@ -14,109 +14,151 @@
limitations under the License.
*/
#include "functions.hpp"
#include <PartitionManager/PartitionManager.hpp>
#include <cerrno>
#include <chrono>
#include <cstdlib>
#include <fcntl.h>
#include <cerrno>
#include <unistd.h>
#include <future>
#include <chrono>
#include <PartitionManager/PartitionManager.hpp>
#include <private/android_filesystem_config.h>
#include "functions.hpp"
#include <unistd.h>
#define BFUN "backupFunction"
namespace PartitionManager {
pair backupFunction::runAsync(const std::string &partitionName, const std::string &outputName, int bufferSize) {
if (!Variables->PartMap->hasPartition(partitionName)) return {
format("Couldn't find partition: %s", partitionName.data()), false
};
pair backupFunction::runAsync(const std::string &partitionName,
const std::string &outputName, int bufferSize) {
if (!Variables->PartMap->hasPartition(partitionName))
return {format("Couldn't find partition: %s", partitionName.data()), false};
LOGN(BFUN, INFO) << "back upping " << partitionName << " as " << outputName << std::endl;
LOGN(BFUN, INFO) << "back upping " << partitionName << " as " << outputName
<< std::endl;
if (Variables->onLogical && !Variables->PartMap->isLogical(partitionName)) {
if (Variables->forceProcess)
LOGN(BFUN, WARNING) << "Partition " << partitionName <<
" is exists but not logical. Ignoring (from --force, -f)." << std::endl;
else return {format("Used --logical (-l) flag but is not logical partition: %s", partitionName.data()), false};
}
if (Variables->onLogical && !Variables->PartMap->isLogical(partitionName)) {
if (Variables->forceProcess)
LOGN(BFUN, WARNING)
<< "Partition " << partitionName
<< " is exists but not logical. Ignoring (from --force, -f)."
<< std::endl;
else
return {
format("Used --logical (-l) flag but is not logical partition: %s",
partitionName.data()),
false};
}
if (Helper::fileIsExists(outputName) && !Variables->forceProcess) return {
format("%s is exists. Remove it, or use --force (-f) flag.", outputName.data()), false
};
if (Helper::fileIsExists(outputName) && !Variables->forceProcess)
return {format("%s is exists. Remove it, or use --force (-f) flag.",
outputName.data()),
false};
setupBufferSize(bufferSize, partitionName);
LOGN(BFUN, INFO) << "Using buffer size (for back upping " << partitionName << "): " << bufferSize << std::endl;
setupBufferSize(bufferSize, partitionName);
LOGN(BFUN, INFO) << "Using buffer size (for back upping " << partitionName
<< "): " << bufferSize << std::endl;
// Automatically close file descriptors and delete allocated memories (arrays)
Helper::garbageCollector collector;
// Automatically close file descriptors and delete allocated memories (arrays)
Helper::garbageCollector collector;
const int pfd = Helper::openAndAddToCloseList(Variables->PartMap->getRealPathOf(partitionName), collector, O_RDONLY);
if (pfd < 0) return {format("Can't open partition: %s: %s", partitionName.data(), strerror(errno)), false};
const int pfd = Helper::openAndAddToCloseList(
Variables->PartMap->getRealPathOf(partitionName), collector, O_RDONLY);
if (pfd < 0)
return {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(), 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(),
strerror(errno)),
false};
LOGN(BFUN, INFO) << "Writing partition " << partitionName << " to file: " << outputName << std::endl;
auto *buffer = new char[bufferSize];
collector.delAfterProgress(buffer);
memset(buffer, 0x00, bufferSize);
LOGN(BFUN, INFO) << "Writing partition " << partitionName
<< " to file: " << outputName << std::endl;
auto *buffer = new char[bufferSize];
collector.delAfterProgress(buffer);
memset(buffer, 0x00, bufferSize);
ssize_t bytesRead;
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", outputName.data(), strerror(errno)), false};
}
ssize_t bytesRead;
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",
outputName.data(), strerror(errno)),
false};
}
if (!Helper::changeOwner(outputName, AID_EVERYBODY, AID_EVERYBODY))
LOGN(BFUN, WARNING) << "Failed to change owner of output file: " << outputName << ". Access problems maybe occur in non-root mode" << std::endl;
if (!Helper::changeMode(outputName, 0660))
LOGN(BFUN, WARNING) << "Failed to change mode of output file as 660: " << outputName << ". Access problems maybe occur in non-root mode" << std::endl;
if (!Helper::changeOwner(outputName, AID_EVERYBODY, AID_EVERYBODY))
LOGN(BFUN, WARNING) << "Failed to change owner of output file: "
<< outputName
<< ". Access problems maybe occur in non-root mode"
<< std::endl;
if (!Helper::changeMode(outputName, 0660))
LOGN(BFUN, WARNING) << "Failed to change mode of output file as 660: "
<< outputName
<< ". Access problems maybe occur in non-root mode"
<< std::endl;
return {format("%s partition successfully back upped to %s", partitionName.data(), outputName.data()), true};
}
return {format("%s partition successfully back upped to %s",
partitionName.data(), outputName.data()),
true};
}
bool backupFunction::init(CLI::App &_app) {
LOGN(BFUN, INFO) << "Initializing variables of backup function." << std::endl;
cmd = _app.add_subcommand("backup", "Backup partition(s) to file(s)");
cmd->add_option("partition(s)", rawPartitions, "Partition name(s)")->required();
cmd->add_option("output(s)", rawOutputNames, "File name(s) (or path(s)) to save the partition image(s)");
cmd->add_option("-O,--output-directory", outputDirectory, "Directory to save the partition image(s)")->check(CLI::ExistingDirectory);
cmd->add_option("-b,--buffer-size", bufferSize, "Buffer size for reading partition(s) and writing to file(s)");
bool backupFunction::init(CLI::App &_app) {
LOGN(BFUN, INFO) << "Initializing variables of backup function." << std::endl;
cmd = _app.add_subcommand("backup", "Backup partition(s) to file(s)");
cmd->add_option("partition(s)", rawPartitions, "Partition name(s)")
->required();
cmd->add_option("output(s)", rawOutputNames,
"File name(s) (or path(s)) to save the partition image(s)");
cmd->add_option("-O,--output-directory", outputDirectory,
"Directory to save the partition image(s)")
->check(CLI::ExistingDirectory);
cmd->add_option(
"-b,--buffer-size", bufferSize,
"Buffer size for reading partition(s) and writing to file(s)");
return true;
}
return true;
}
bool backupFunction::run() {
processCommandLine(partitions, outputNames, rawPartitions, rawOutputNames, ',', true);
if (!outputNames.empty() && partitions.size() != outputNames.size())
throw CLI::ValidationError("You must provide an output name(s) as long as the partition name(s)");
bool backupFunction::run() {
processCommandLine(partitions, outputNames, rawPartitions, rawOutputNames,
',', true);
if (!outputNames.empty() && partitions.size() != outputNames.size())
throw CLI::ValidationError(
"You must provide an output name(s) as long as the partition name(s)");
std::vector<std::future<pair>> futures;
for (size_t i = 0; i < partitions.size(); i++) {
std::string partitionName = partitions[i];
std::string outputName = outputNames.empty() ? partitionName + ".img" : outputNames[i];
if (!outputDirectory.empty()) outputName.insert(0, outputDirectory + '/');
std::vector<std::future<pair>> futures;
for (size_t i = 0; i < partitions.size(); i++) {
std::string partitionName = partitions[i];
std::string outputName =
outputNames.empty() ? partitionName + ".img" : outputNames[i];
if (!outputDirectory.empty()) outputName.insert(0, outputDirectory + '/');
futures.push_back(std::async(std::launch::async, runAsync, partitionName, outputName, bufferSize));
LOGN(BFUN, INFO) << "Created thread backup upping " << partitionName << std::endl;
}
futures.push_back(std::async(std::launch::async, runAsync, partitionName,
outputName, bufferSize));
LOGN(BFUN, INFO) << "Created thread backup upping " << partitionName
<< std::endl;
}
std::string end;
bool endResult = true;
for (auto &future: futures) {
auto [fst, snd] = future.get();
if (!snd) { end += fst + '\n'; endResult = false; }
else println("%s", fst.c_str());
}
std::string end;
bool endResult = true;
for (auto &future : futures) {
auto [fst, snd] = future.get();
if (!snd) {
end += fst + '\n';
endResult = false;
} else println("%s", fst.c_str());
}
if (!endResult) throw Error("%s", end.c_str());
if (!endResult) throw Error("%s", end.c_str());
LOGN(BFUN, INFO) << "Operation successfully completed." << std::endl;
return endResult;
}
LOGN(BFUN, INFO) << "Operation successfully completed." << std::endl;
return endResult;
}
bool backupFunction::isUsed() const { return cmd->parsed(); }
bool backupFunction::isUsed() const { return cmd->parsed(); }
const char *backupFunction::name() const { return BFUN; }
const char *backupFunction::name() const { return BFUN; }
} // namespace PartitionManager

View File

@@ -14,91 +14,116 @@ Copyright 2025 Yağız Zengin
limitations under the License.
*/
#include "functions.hpp"
#include <PartitionManager/PartitionManager.hpp>
#include <cerrno>
#include <cstdlib>
#include <fcntl.h>
#include <cerrno>
#include <future>
#include <unistd.h>
#include <PartitionManager/PartitionManager.hpp>
#include "functions.hpp"
#define EFUN "eraseFunction"
namespace PartitionManager {
pair eraseFunction::runAsync(const std::string &partitionName, int bufferSize) {
if (!Variables->PartMap->hasPartition(partitionName)) return {
format("Couldn't find partition: %s", partitionName.data()), false
};
pair eraseFunction::runAsync(const std::string &partitionName, int bufferSize) {
if (!Variables->PartMap->hasPartition(partitionName))
return {format("Couldn't find partition: %s", partitionName.data()), false};
if (Variables->onLogical && !Variables->PartMap->isLogical(partitionName)) {
if (Variables->forceProcess)
LOGN(EFUN, WARNING) << "Partition " << partitionName <<
" is exists but not logical. Ignoring (from --force, -f)." << std::endl;
else return {format("Used --logical (-l) flag but is not logical partition: %s", partitionName.data()), false};
}
if (Variables->onLogical && !Variables->PartMap->isLogical(partitionName)) {
if (Variables->forceProcess)
LOGN(EFUN, WARNING)
<< "Partition " << partitionName
<< " is exists but not logical. Ignoring (from --force, -f)."
<< std::endl;
else
return {
format("Used --logical (-l) flag but is not logical partition: %s",
partitionName.data()),
false};
}
setupBufferSize(bufferSize, partitionName);
LOGN(EFUN, INFO) << "Using buffer size: " << bufferSize;
setupBufferSize(bufferSize, partitionName);
LOGN(EFUN, INFO) << "Using buffer size: " << bufferSize;
// Automatically close file descriptors and delete allocated memories (arrays)
Helper::garbageCollector collector;
// Automatically close file descriptors and delete allocated memories (arrays)
Helper::garbageCollector collector;
const int pfd = Helper::openAndAddToCloseList(Variables->PartMap->getRealPathOf(partitionName), collector, O_WRONLY);
if (pfd < 0) return {format("Can't open partition: %s: %s", partitionName.data(), strerror(errno)), false};
const int pfd = Helper::openAndAddToCloseList(
Variables->PartMap->getRealPathOf(partitionName), collector, O_WRONLY);
if (pfd < 0)
return {format("Can't open partition: %s: %s", partitionName.data(),
strerror(errno)),
false};
if (!Variables->forceProcess) 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!");
if (!Variables->forceProcess)
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!");
LOGN(EFUN, INFO) << "Writing zero bytes to partition: " << partitionName << std::endl;
auto *buffer = new char[bufferSize];
collector.delAfterProgress(buffer);
memset(buffer, 0x00, bufferSize);
LOGN(EFUN, INFO) << "Writing zero bytes to partition: " << partitionName
<< std::endl;
auto *buffer = new char[bufferSize];
collector.delAfterProgress(buffer);
memset(buffer, 0x00, bufferSize);
ssize_t bytesWritten = 0;
const uint64_t partitionSize = Variables->PartMap->sizeOf(partitionName);
ssize_t bytesWritten = 0;
const uint64_t partitionSize = Variables->PartMap->sizeOf(partitionName);
while (bytesWritten < partitionSize) {
size_t toWrite = sizeof(buffer);
if (partitionSize - bytesWritten < sizeof(buffer)) toWrite = partitionSize - bytesWritten;
while (bytesWritten < partitionSize) {
size_t toWrite = sizeof(buffer);
if (partitionSize - bytesWritten < sizeof(buffer))
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", partitionName.data(), strerror(errno)), false};
else bytesWritten += result;
}
if (const ssize_t result = write(pfd, buffer, toWrite); result == -1)
return {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", partitionName.data()), true};
}
return {format("Successfully wrote zero bytes to the %s partition\n",
partitionName.data()),
true};
}
bool eraseFunction::init(CLI::App &_app) {
LOGN(EFUN, INFO) << "Initializing variables of erase function." << std::endl;
cmd = _app.add_subcommand("erase", "Writes zero bytes to partition(s)");
cmd->add_option("partition(s)", partitions, "Partition name(s)")->required()->delimiter(',');
cmd->add_option("-b,--buffer-size", bufferSize, "Buffer size for writing zero bytes to partition(s)");
return true;
}
bool eraseFunction::init(CLI::App &_app) {
LOGN(EFUN, INFO) << "Initializing variables of erase function." << std::endl;
cmd = _app.add_subcommand("erase", "Writes zero bytes to partition(s)");
cmd->add_option("partition(s)", partitions, "Partition name(s)")
->required()
->delimiter(',');
cmd->add_option("-b,--buffer-size", bufferSize,
"Buffer size for writing zero bytes to partition(s)");
return true;
}
bool eraseFunction::run() {
std::vector<std::future<pair>> futures;
for (const auto &partitionName: partitions) {
futures.push_back(std::async(std::launch::async, runAsync, partitionName, bufferSize));
LOGN(EFUN, INFO) << "Created thread for writing zero bytes to " << partitionName << std::endl;
}
bool eraseFunction::run() {
std::vector<std::future<pair>> futures;
for (const auto &partitionName : partitions) {
futures.push_back(
std::async(std::launch::async, runAsync, partitionName, bufferSize));
LOGN(EFUN, INFO) << "Created thread for writing zero bytes to "
<< partitionName << std::endl;
}
std::string end;
bool endResult = true;
for (auto &future: futures) {
auto [fst, snd] = future.get();
if (!snd) { end += fst + '\n'; endResult = false; }
else println("%s", fst.c_str());
}
std::string end;
bool endResult = true;
for (auto &future : futures) {
auto [fst, snd] = future.get();
if (!snd) {
end += fst + '\n';
endResult = false;
} else println("%s", fst.c_str());
}
if (!endResult) throw Error("%s", end.c_str());
if (!endResult) throw Error("%s", end.c_str());
LOGN(EFUN, INFO) << "Operation successfully completed." << std::endl;
return endResult;
}
LOGN(EFUN, INFO) << "Operation successfully completed." << std::endl;
return endResult;
}
bool eraseFunction::isUsed() const { return cmd->parsed(); }
bool eraseFunction::isUsed() const { return cmd->parsed(); }
const char *eraseFunction::name() const { return EFUN; }
const char *eraseFunction::name() const { return EFUN; }
} // namespace PartitionManager

View File

@@ -14,102 +14,135 @@ Copyright 2025 Yağız Zengin
limitations under the License.
*/
#include "functions.hpp"
#include <PartitionManager/PartitionManager.hpp>
#include <cerrno>
#include <cstdlib>
#include <fcntl.h>
#include <cerrno>
#include <future>
#include <unistd.h>
#include <PartitionManager/PartitionManager.hpp>
#include "functions.hpp"
#define FFUN "flashFunction"
namespace PartitionManager {
pair flashFunction::runAsync(const std::string &partitionName, const std::string &imageName, int bufferSize) {
if (!Helper::fileIsExists(imageName)) return {format("Couldn't find image file: %s", imageName.data()), false};
if (!Variables->PartMap->hasPartition(partitionName)) return {format("Couldn't find partition: %s", partitionName.data()), false};
if (Helper::fileSize(imageName) > Variables->PartMap->sizeOf(partitionName))
return {format("%s is larger than %s partition size!", imageName.data(), partitionName.data()), false};
pair flashFunction::runAsync(const std::string &partitionName,
const std::string &imageName, int bufferSize) {
if (!Helper::fileIsExists(imageName))
return {format("Couldn't find image file: %s", imageName.data()), false};
if (!Variables->PartMap->hasPartition(partitionName))
return {format("Couldn't find partition: %s", partitionName.data()), false};
if (Helper::fileSize(imageName) > Variables->PartMap->sizeOf(partitionName))
return {format("%s is larger than %s partition size!", imageName.data(),
partitionName.data()),
false};
LOGN(FFUN, INFO) << "flashing " << imageName << " to " << partitionName << std::endl;
LOGN(FFUN, INFO) << "flashing " << imageName << " to " << partitionName
<< std::endl;
if (Variables->onLogical && !Variables->PartMap->isLogical(partitionName)) {
if (Variables->forceProcess)
LOGN(FFUN, WARNING) << "Partition " << partitionName <<
" is exists but not logical. Ignoring (from --force, -f)." << std::endl;
else return {
format("Used --logical (-l) flag but is not logical partition: %s", partitionName.data()), false
};
}
if (Variables->onLogical && !Variables->PartMap->isLogical(partitionName)) {
if (Variables->forceProcess)
LOGN(FFUN, WARNING)
<< "Partition " << partitionName
<< " is exists but not logical. Ignoring (from --force, -f)."
<< std::endl;
else
return {
format("Used --logical (-l) flag but is not logical partition: %s",
partitionName.data()),
false};
}
setupBufferSize(bufferSize, imageName);
LOGN(FFUN, INFO) << "Using buffer size: " << bufferSize;
setupBufferSize(bufferSize, imageName);
LOGN(FFUN, INFO) << "Using buffer size: " << bufferSize;
// Automatically close file descriptors and delete allocated memories (arrays)
Helper::garbageCollector collector;
// Automatically close file descriptors and delete allocated memories (arrays)
Helper::garbageCollector collector;
const int ffd = Helper::openAndAddToCloseList(imageName, collector, O_RDONLY);
if (ffd < 0) return {format("Can't open image file %s: %s", imageName.data(), strerror(errno)), false};
const int ffd = Helper::openAndAddToCloseList(imageName, collector, O_RDONLY);
if (ffd < 0)
return {format("Can't open image file %s: %s", imageName.data(),
strerror(errno)),
false};
const int pfd = Helper::openAndAddToCloseList(Variables->PartMap->getRealPathOf(partitionName), collector, O_RDWR | O_TRUNC);
if (pfd < 0) return {format("Can't open partition: %s: %s", partitionName.data(), strerror(errno)), false};
const int pfd = Helper::openAndAddToCloseList(
Variables->PartMap->getRealPathOf(partitionName), collector,
O_RDWR | O_TRUNC);
if (pfd < 0)
return {format("Can't open partition: %s: %s", partitionName.data(),
strerror(errno)),
false};
LOGN(FFUN, INFO) << "Writing image " << imageName << " to partition: " << partitionName << std::endl;
auto *buffer = new char[bufferSize];
collector.delAfterProgress(buffer);
memset(buffer, 0x00, bufferSize);
LOGN(FFUN, INFO) << "Writing image " << imageName
<< " to partition: " << partitionName << std::endl;
auto *buffer = new char[bufferSize];
collector.delAfterProgress(buffer);
memset(buffer, 0x00, bufferSize);
ssize_t bytesRead;
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", imageName.data(), strerror(errno)), false};
}
ssize_t bytesRead;
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",
imageName.data(), strerror(errno)),
false};
}
return {format("%s is successfully wrote to %s partition", imageName.data(), partitionName.data()), true};
}
return {format("%s is successfully wrote to %s partition", imageName.data(),
partitionName.data()),
true};
}
bool flashFunction::init(CLI::App &_app) {
LOGN(FFUN, INFO) << "Initializing variables of flash function." << std::endl;
cmd = _app.add_subcommand("flash", "Flash image(s) to partition(s)");
cmd->add_option("partition(s)", rawPartitions, "Partition name(s)")->required();
cmd->add_option("imageFile(s)", rawImageNames, "Name(s) of image file(s)")->required();
cmd->add_option("-b,--buffer-size", bufferSize, "Buffer size for reading image(s) and writing to partition(s)");
cmd->add_option("-I,--image-directory", imageDirectory, "Directory to find image(s) and flash to partition(s)");
bool flashFunction::init(CLI::App &_app) {
LOGN(FFUN, INFO) << "Initializing variables of flash function." << std::endl;
cmd = _app.add_subcommand("flash", "Flash image(s) to partition(s)");
cmd->add_option("partition(s)", rawPartitions, "Partition name(s)")
->required();
cmd->add_option("imageFile(s)", rawImageNames, "Name(s) of image file(s)")
->required();
cmd->add_option(
"-b,--buffer-size", bufferSize,
"Buffer size for reading image(s) and writing to partition(s)");
cmd->add_option("-I,--image-directory", imageDirectory,
"Directory to find image(s) and flash to partition(s)");
return true;
}
return true;
}
bool flashFunction::run() {
processCommandLine(partitions, imageNames, rawPartitions, rawImageNames, ',', true);
if (partitions.size() != imageNames.size())
throw CLI::ValidationError("You must provide an image file(s) as long as the partition name(s)");
bool flashFunction::run() {
processCommandLine(partitions, imageNames, rawPartitions, rawImageNames, ',',
true);
if (partitions.size() != imageNames.size())
throw CLI::ValidationError(
"You must provide an image file(s) as long as the partition name(s)");
std::vector<std::future<pair>> futures;
for (size_t i = 0; i < partitions.size(); i++) {
std::string imageName = imageNames[i];
if (!imageDirectory.empty()) imageName.insert(0, imageDirectory + '/');
std::vector<std::future<pair>> futures;
for (size_t i = 0; i < partitions.size(); i++) {
std::string imageName = imageNames[i];
if (!imageDirectory.empty()) imageName.insert(0, imageDirectory + '/');
futures.push_back(std::async(std::launch::async, runAsync, partitions[i], imageName, bufferSize));
LOGN(FFUN, INFO) << "Created thread for flashing image to " << partitions[i] << std::endl;
}
futures.push_back(std::async(std::launch::async, runAsync, partitions[i],
imageName, bufferSize));
LOGN(FFUN, INFO) << "Created thread for flashing image to " << partitions[i]
<< std::endl;
}
std::string end;
bool endResult = true;
for (auto &future: futures) {
auto [fst, snd] = future.get();
if (!snd) {
end += fst + '\n';
endResult = false;
} else println("%s", fst.c_str());
}
std::string end;
bool endResult = true;
for (auto &future : futures) {
auto [fst, snd] = future.get();
if (!snd) {
end += fst + '\n';
endResult = false;
} else println("%s", fst.c_str());
}
if (!endResult) throw Error("%s", end.c_str());
if (!endResult) throw Error("%s", end.c_str());
LOGN(FFUN, INFO) << "Operation successfully completed." << std::endl;
return endResult;
}
LOGN(FFUN, INFO) << "Operation successfully completed." << std::endl;
return endResult;
}
bool flashFunction::isUsed() const { return cmd->parsed(); }
bool flashFunction::isUsed() const { return cmd->parsed(); }
const char *flashFunction::name() const { return FFUN; }
const char *flashFunction::name() const { return FFUN; }
} // namespace PartitionManager

View File

@@ -14,77 +14,89 @@ Copyright 2025 Yağız Zengin
limitations under the License.
*/
#include "functions.hpp"
#include <PartitionManager/PartitionManager.hpp>
#include <cerrno>
#include <cstdlib>
#include <fcntl.h>
#include <cerrno>
#include <unistd.h>
#include <PartitionManager/PartitionManager.hpp>
#include "functions.hpp"
#define IFUN "infoFunction"
namespace PartitionManager {
bool infoFunction::init(CLI::App &_app) {
LOGN(IFUN, INFO) << "Initializing variables of info printer function." << std::endl;
cmd = _app.add_subcommand("info", "Tell info(s) of input partition list")->footer(
"Use get-all or getvar-all as partition name for getting info's of all partitions.");
cmd->add_option("partition(s)", partitions, "Partition name(s).")->required()->delimiter(',');
cmd->add_flag("-J,--json", jsonFormat,
"Print info(s) as JSON body. The body of each partition will be written separately");
cmd->add_option("--json-partition-name", jNamePartition, "Speficy partition name element for JSON body");
cmd->add_option("--json-size-name", jNameSize, "Speficy size element name for JSON body");
cmd->add_option("--json-logical-name", jNameLogical, "Speficy logical element name for JSON body");
return true;
}
bool infoFunction::init(CLI::App &_app) {
LOGN(IFUN, INFO) << "Initializing variables of info printer function."
<< std::endl;
cmd = _app.add_subcommand("info", "Tell info(s) of input partition list")
->footer("Use get-all or getvar-all as partition name for getting "
"info's of all "
"partitions.");
cmd->add_option("partition(s)", partitions, "Partition name(s).")
->required()
->delimiter(',');
cmd->add_flag("-J,--json", jsonFormat,
"Print info(s) as JSON body. The body of each partition will "
"be written separately");
cmd->add_option("--json-partition-name", jNamePartition,
"Speficy partition name element for JSON body");
cmd->add_option("--json-size-name", jNameSize,
"Speficy size element name for JSON body");
cmd->add_option("--json-logical-name", jNameLogical,
"Speficy logical element name for JSON body");
return true;
}
bool infoFunction::run() {
if (partitions.back() == "get-all" || partitions.back() == "getvar-all") {
partitions.clear();
const auto parts = Variables->PartMap->getPartitionList();
if (!parts) throw Error("Cannot get list of all partitions! See logs for more information (%s)",
Helper::LoggingProperties::FILE.data());
bool infoFunction::run() {
if (partitions.back() == "get-all" || partitions.back() == "getvar-all") {
partitions.clear();
const auto parts = Variables->PartMap->getPartitionList();
if (!parts)
throw Error("Cannot get list of all partitions! See logs for more "
"information (%s)",
Helper::LoggingProperties::FILE.data());
for (const auto &name: *parts) partitions.push_back(name);
}
for (const auto &name : *parts)
partitions.push_back(name);
}
for (const auto &partition: partitions) {
if (!Variables->PartMap->hasPartition(partition))
throw Error("Couldn't find partition: %s", partition.data());
for (const auto &partition : partitions) {
if (!Variables->PartMap->hasPartition(partition))
throw Error("Couldn't find partition: %s", partition.data());
if (Variables->onLogical && !Variables->PartMap->isLogical(partition)) {
if (Variables->forceProcess)
LOGN(IFUN, WARNING) << "Partition " << partition <<
" is exists but not logical. Ignoring (from --force, -f)." << std::endl;
else throw Error("Used --logical (-l) flag but is not logical partition: %s", partition.data());
}
if (Variables->onLogical && !Variables->PartMap->isLogical(partition)) {
if (Variables->forceProcess)
LOGN(IFUN, WARNING)
<< "Partition " << partition
<< " is exists but not logical. Ignoring (from --force, -f)."
<< std::endl;
else
throw Error("Used --logical (-l) flag but is not logical partition: %s",
partition.data());
}
if (jsonFormat)
if (jsonFormat)
#ifdef __LP64__
println("{\"%s\": \"%s\", \"%s\": %lu, \"%s\": %s}",
println("{\"%s\": \"%s\", \"%s\": %lu, \"%s\": %s}",
#else
println("{\"%s\": \"%s\", \"%s\": %llu, \"%s\": %s}",
println("{\"%s\": \"%s\", \"%s\": %llu, \"%s\": %s}",
#endif
jNamePartition.data(),
partition.data(),
jNameSize.data(),
Variables->PartMap->sizeOf(partition),
jNameLogical.data(),
Variables->PartMap->isLogical(partition) ? "true" : "false");
else
jNamePartition.data(), partition.data(), jNameSize.data(),
Variables->PartMap->sizeOf(partition), jNameLogical.data(),
Variables->PartMap->isLogical(partition) ? "true" : "false");
else
#ifdef __LP64__
println("partition=%s size=%lu isLogical=%s",
println("partition=%s size=%lu isLogical=%s",
#else
println("partition=%s size=%llu isLogical=%s",
println("partition=%s size=%llu isLogical=%s",
#endif
partition.data(),
Variables->PartMap->sizeOf(partition),
Variables->PartMap->isLogical(partition) ? "true" : "false");
}
partition.data(), Variables->PartMap->sizeOf(partition),
Variables->PartMap->isLogical(partition) ? "true" : "false");
}
return true;
}
return true;
}
bool infoFunction::isUsed() const { return cmd->parsed(); }
bool infoFunction::isUsed() const { return cmd->parsed(); }
const char *infoFunction::name() const { return IFUN; };
const char *infoFunction::name() const { return IFUN; };
} // namespace PartitionManager

View File

@@ -14,59 +14,77 @@ Copyright 2025 Yağız Zengin
limitations under the License.
*/
#include <PartitionManager/PartitionManager.hpp>
#include "functions.hpp"
#include <PartitionManager/PartitionManager.hpp>
#define SFUN "partitionSizeFunction"
std::string convertTo(const uint64_t size, const std::string &multiple) {
if (multiple == "KB") return std::to_string(TO_KB(size));
if (multiple == "MB") return std::to_string(TO_MB(size));
if (multiple == "GB") return std::to_string(TO_GB(size));
return std::to_string(size);
if (multiple == "KB") return std::to_string(TO_KB(size));
if (multiple == "MB") return std::to_string(TO_MB(size));
if (multiple == "GB") return std::to_string(TO_GB(size));
return std::to_string(size);
}
namespace PartitionManager {
bool partitionSizeFunction::init(CLI::App &_app) {
LOGN(SFUN, INFO) << "Initializing variables of partition size getter function." << std::endl;
cmd = _app.add_subcommand("sizeof", "Tell size(s) of input partition list");
cmd->add_option("partition(s)", partitions, "Partition name(s).")->required()->delimiter(',');
cmd->add_flag("--as-byte", asByte, "Tell input size of partition list as byte.");
cmd->add_flag("--as-kilobyte", asKiloBytes, "Tell input size of partition list as kilobyte.");
cmd->add_flag("--as-megabyte", asMega, "Tell input size of partition list as megabyte.");
cmd->add_flag("--as-gigabyte", asGiga, "Tell input size of partition list as gigabyte.");
cmd->add_flag("--only-size", onlySize, "Tell input size of partition list as not printing multiple and partition name.");
return true;
bool partitionSizeFunction::init(CLI::App &_app) {
LOGN(SFUN, INFO)
<< "Initializing variables of partition size getter function."
<< std::endl;
cmd = _app.add_subcommand("sizeof", "Tell size(s) of input partition list");
cmd->add_option("partition(s)", partitions, "Partition name(s).")
->required()
->delimiter(',');
cmd->add_flag("--as-byte", asByte,
"Tell input size of partition list as byte.");
cmd->add_flag("--as-kilobyte", asKiloBytes,
"Tell input size of partition list as kilobyte.");
cmd->add_flag("--as-megabyte", asMega,
"Tell input size of partition list as megabyte.");
cmd->add_flag("--as-gigabyte", asGiga,
"Tell input size of partition list as gigabyte.");
cmd->add_flag("--only-size", onlySize,
"Tell input size of partition list as not printing multiple "
"and partition name.");
return true;
}
bool partitionSizeFunction::run() {
for (const auto &partition : partitions) {
if (!Variables->PartMap->hasPartition(partition))
throw Error("Couldn't find partition: %s", partition.data());
if (Variables->onLogical && !Variables->PartMap->isLogical(partition)) {
if (Variables->forceProcess)
LOGN(SFUN, WARNING)
<< "Partition " << partition
<< " is exists but not logical. Ignoring (from --force, -f)."
<< std::endl;
else
throw Error("Used --logical (-l) flag but is not logical partition: %s",
partition.data());
}
bool partitionSizeFunction::run() {
for (const auto &partition: partitions) {
if (!Variables->PartMap->hasPartition(partition))
throw Error("Couldn't find partition: %s", partition.data());
std::string multiple = "MB";
if (asByte) multiple = "B";
if (asKiloBytes) multiple = "KB";
if (asMega) multiple = "MB";
if (asGiga) multiple = "GB";
if (Variables->onLogical && !Variables->PartMap->isLogical(partition)) {
if (Variables->forceProcess)
LOGN(SFUN, WARNING) << "Partition " << partition <<
" is exists but not logical. Ignoring (from --force, -f)." << std::endl;
else throw Error("Used --logical (-l) flag but is not logical partition: %s", partition.data());
}
if (onlySize)
println(
"%s",
convertTo(Variables->PartMap->sizeOf(partition), multiple).data());
else
println("%s: %s%s", partition.data(),
convertTo(Variables->PartMap->sizeOf(partition), multiple).data(),
multiple.data());
}
std::string multiple = "MB";
if (asByte) multiple = "B";
if (asKiloBytes) multiple = "KB";
if (asMega) multiple = "MB";
if (asGiga) multiple = "GB";
return true;
}
if (onlySize)
println("%s", convertTo(Variables->PartMap->sizeOf(partition), multiple).data());
else
println("%s: %s%s", partition.data(), convertTo(Variables->PartMap->sizeOf(partition), multiple).data(), multiple.data());
}
bool partitionSizeFunction::isUsed() const { return cmd->parsed(); }
return true;
}
bool partitionSizeFunction::isUsed() const { return cmd->parsed(); }
const char *partitionSizeFunction::name() const { return SFUN; }
const char *partitionSizeFunction::name() const { return SFUN; }
} // namespace PartitionManager

View File

@@ -1,11 +1,11 @@
/*
Copyright 2025 Yağız Zengin
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
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,
@@ -14,38 +14,46 @@
limitations under the License.
*/
#include <PartitionManager/PartitionManager.hpp>
#include "functions.hpp"
#include <PartitionManager/PartitionManager.hpp>
#define RLPFUN "realPathFunction"
namespace PartitionManager {
bool realLinkPathFunction::init(CLI::App &_app) {
LOGN(RLPFUN, INFO) << "Initializing variables of real link path function." << std::endl;
cmd = _app.add_subcommand("real-linkpath", "Tell real link paths of partition(s)");
cmd->add_option("partition(s)", partitions, "Partition name(s)")->required()->delimiter(',');
return true;
}
bool realLinkPathFunction::init(CLI::App &_app) {
LOGN(RLPFUN, INFO) << "Initializing variables of real link path function."
<< std::endl;
cmd = _app.add_subcommand("real-linkpath",
"Tell real link paths of partition(s)");
cmd->add_option("partition(s)", partitions, "Partition name(s)")
->required()
->delimiter(',');
return true;
}
bool realLinkPathFunction::run() {
for (const auto &partition: partitions) {
if (!Variables->PartMap->hasPartition(partition))
throw Error("Couldn't find partition: %s", partition.data());
bool realLinkPathFunction::run() {
for (const auto &partition : partitions) {
if (!Variables->PartMap->hasPartition(partition))
throw Error("Couldn't find partition: %s", partition.data());
if (Variables->onLogical && !Variables->PartMap->isLogical(partition)) {
if (Variables->forceProcess)
LOGN(RLPFUN, WARNING) << "Partition " << partition <<
" is exists but not logical. Ignoring (from --force, -f)." << std::endl;
else throw Error("Used --logical (-l) flag but is not logical partition: %s", partition.data());
}
if (Variables->onLogical && !Variables->PartMap->isLogical(partition)) {
if (Variables->forceProcess)
LOGN(RLPFUN, WARNING)
<< "Partition " << partition
<< " is exists but not logical. Ignoring (from --force, -f)."
<< std::endl;
else
throw Error("Used --logical (-l) flag but is not logical partition: %s",
partition.data());
}
println("%s", Variables->PartMap->getRealPathOf(partition).data());
}
println("%s", Variables->PartMap->getRealPathOf(partition).data());
}
return true;
}
return true;
}
bool realLinkPathFunction::isUsed() const { return cmd->parsed(); }
bool realLinkPathFunction::isUsed() const { return cmd->parsed(); }
const char *realLinkPathFunction::name() const { return RLPFUN; }
const char *realLinkPathFunction::name() const { return RLPFUN; }
} // namespace PartitionManager

View File

@@ -1,11 +1,11 @@
/*
Copyright 2025 Yağız Zengin
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
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,
@@ -14,38 +14,45 @@
limitations under the License.
*/
#include <PartitionManager/PartitionManager.hpp>
#include "functions.hpp"
#include <PartitionManager/PartitionManager.hpp>
#define RPFUN "realPathFunction"
namespace PartitionManager {
bool realPathFunction::init(CLI::App &_app) {
LOGN(RPFUN, INFO) << "Initializing variables of real path function." << std::endl;
cmd = _app.add_subcommand("real-path", "Tell real paths of partition(s)");
cmd->add_option("partition(s)", partitions, "Partition name(s)")->required()->delimiter(',');
return true;
}
bool realPathFunction::init(CLI::App &_app) {
LOGN(RPFUN, INFO) << "Initializing variables of real path function."
<< std::endl;
cmd = _app.add_subcommand("real-path", "Tell real paths of partition(s)");
cmd->add_option("partition(s)", partitions, "Partition name(s)")
->required()
->delimiter(',');
return true;
}
bool realPathFunction::run() {
for (const auto &partition: partitions) {
if (!Variables->PartMap->hasPartition(partition))
throw Error("Couldn't find partition: %s", partition.data());
bool realPathFunction::run() {
for (const auto &partition : partitions) {
if (!Variables->PartMap->hasPartition(partition))
throw Error("Couldn't find partition: %s", partition.data());
if (Variables->onLogical && !Variables->PartMap->isLogical(partition)) {
if (Variables->forceProcess)
LOGN(RPFUN, WARNING) << "Partition " << partition <<
" is exists but not logical. Ignoring (from --force, -f)." << std::endl;
else throw Error("Used --logical (-l) flag but is not logical partition: %s", partition.data());
}
if (Variables->onLogical && !Variables->PartMap->isLogical(partition)) {
if (Variables->forceProcess)
LOGN(RPFUN, WARNING)
<< "Partition " << partition
<< " is exists but not logical. Ignoring (from --force, -f)."
<< std::endl;
else
throw Error("Used --logical (-l) flag but is not logical partition: %s",
partition.data());
}
println("%s", Variables->PartMap->getRealPathOf(partition).data());
}
println("%s", Variables->PartMap->getRealPathOf(partition).data());
}
return true;
}
return true;
}
bool realPathFunction::isUsed() const { return cmd->parsed(); }
bool realPathFunction::isUsed() const { return cmd->parsed(); }
const char *realPathFunction::name() const { return RPFUN; }
const char *realPathFunction::name() const { return RPFUN; }
} // namespace PartitionManager

View File

@@ -5,7 +5,7 @@ Copyright 2025 Yağız Zengin
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
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,
@@ -14,29 +14,32 @@ Copyright 2025 Yağız Zengin
limitations under the License.
*/
#include <PartitionManager/PartitionManager.hpp>
#include "functions.hpp"
#include <PartitionManager/PartitionManager.hpp>
#define RFUN "rebootFunction"
namespace PartitionManager {
bool rebootFunction::init(CLI::App &_app) {
LOGN(RFUN, INFO) << "Initializing variables of reboot function." << std::endl;
cmd = _app.add_subcommand("reboot", "Reboots device");
cmd->add_option("rebootTarget", rebootTarget, "Reboot target (default: normal)");
return true;
}
bool rebootFunction::init(CLI::App &_app) {
LOGN(RFUN, INFO) << "Initializing variables of reboot function." << std::endl;
cmd = _app.add_subcommand("reboot", "Reboots device");
cmd->add_option("rebootTarget", rebootTarget,
"Reboot target (default: normal)");
return true;
}
bool rebootFunction::run() {
LOGN(RFUN, INFO) << "Rebooting device!!! (custom reboot target: " << (rebootTarget.empty() ? "none" : rebootTarget) << std::endl;
bool rebootFunction::run() {
LOGN(RFUN, INFO) << "Rebooting device!!! (custom reboot target: "
<< (rebootTarget.empty() ? "none" : rebootTarget)
<< std::endl;
if (Helper::reboot(rebootTarget)) println("Reboot command was sent");
else throw Error("Cannot reboot device");
if (Helper::reboot(rebootTarget)) println("Reboot command was sent");
else throw Error("Cannot reboot device");
return true;
}
return true;
}
bool rebootFunction::isUsed() const { return cmd->parsed(); }
bool rebootFunction::isUsed() const { return cmd->parsed(); }
const char* rebootFunction::name() const { return RFUN; }
const char *rebootFunction::name() const { return RFUN; }
} // namespace PartitionManager

View File

@@ -5,7 +5,7 @@
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
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,
@@ -14,49 +14,65 @@
limitations under the License.
*/
#include <PartitionManager/PartitionManager.hpp>
#include "functions.hpp"
#include <PartitionManager/PartitionManager.hpp>
#define TFUN "typeFunction"
namespace PartitionManager {
bool typeFunction::init(CLI::App &_app) {
LOGN(TFUN, INFO) << "Initializing variables of type function." << std::endl;
cmd = _app.add_subcommand("type", "Get type of the partition(s) or image(s)");
cmd->add_option("content(s)", contents, "Content(s)")->required()->delimiter(',');
cmd->add_option("-b,--buffer-size", bufferSize, "Buffer size for max seek depth");
cmd->add_flag("--only-check-android-magics", onlyCheckAndroidMagics, "Only check Android magic values.");
cmd->add_flag("--only-check-filesystem-magics", onlyCheckFileSystemMagics, "Only check filesystem magic values.");
return true;
}
bool typeFunction::init(CLI::App &_app) {
LOGN(TFUN, INFO) << "Initializing variables of type function." << std::endl;
cmd = _app.add_subcommand("type", "Get type of the partition(s) or image(s)");
cmd->add_option("content(s)", contents, "Content(s)")
->required()
->delimiter(',');
cmd->add_option("-b,--buffer-size", bufferSize,
"Buffer size for max seek depth");
cmd->add_flag("--only-check-android-magics", onlyCheckAndroidMagics,
"Only check Android magic values.");
cmd->add_flag("--only-check-filesystem-magics", onlyCheckFileSystemMagics,
"Only check filesystem magic values.");
return true;
}
bool typeFunction::run() {
std::unordered_map<uint64_t, std::string> magics;
if (onlyCheckAndroidMagics) magics.merge(PartitionMap::Extras::AndroidMagicMap);
else if (onlyCheckFileSystemMagics) magics.merge(PartitionMap::Extras::FileSystemMagicMap);
else magics.merge(PartitionMap::Extras::MagicMap);
bool typeFunction::run() {
std::unordered_map<uint64_t, std::string> magics;
if (onlyCheckAndroidMagics)
magics.merge(PartitionMap::Extras::AndroidMagicMap);
else if (onlyCheckFileSystemMagics)
magics.merge(PartitionMap::Extras::FileSystemMagicMap);
else magics.merge(PartitionMap::Extras::MagicMap);
for (const auto& content : contents) {
if (!Variables->PartMap->hasPartition(content) && !Helper::fileIsExists(content))
throw Error("Couldn't find partition or image file: %s\n", content.data());
for (const auto &content : contents) {
if (!Variables->PartMap->hasPartition(content) &&
!Helper::fileIsExists(content))
throw Error("Couldn't find partition or image file: %s\n",
content.data());
bool found = false;
for (const auto& [magic, name] : magics) {
if (PartitionMap::Extras::hasMagic(magic, bufferSize, Helper::fileIsExists(content) ? content : Variables->PartMap->getRealPathOf(content))) {
println("%s contains %s magic (%s)", content.data(), name.data(), PartitionMap::Extras::formatMagic(magic).data());
found = true;
break;
}
}
bool found = false;
for (const auto &[magic, name] : magics) {
if (PartitionMap::Extras::hasMagic(
magic, bufferSize,
Helper::fileIsExists(content)
? content
: Variables->PartMap->getRealPathOf(content))) {
println("%s contains %s magic (%s)", content.data(), name.data(),
PartitionMap::Extras::formatMagic(magic).data());
found = true;
break;
}
}
if (!found) throw Error("Couldn't determine type of %s%s\n", content.data(), content == "userdata" ? " (encrypted file system?)" : "");
}
if (!found)
throw Error("Couldn't determine type of %s%s\n", content.data(),
content == "userdata" ? " (encrypted file system?)" : "");
}
return true;
}
return true;
}
bool typeFunction::isUsed() const { return cmd->parsed(); }
bool typeFunction::isUsed() const { return cmd->parsed(); }
const char* typeFunction::name() const { return TFUN; }
const char *typeFunction::name() const { return TFUN; }
} // namespace PartitionManager

222
src/functions/functions.hpp Executable file → Normal file
View File

@@ -22,151 +22,155 @@
#include <vector>
namespace PartitionManager {
using pair = std::pair<std::string, bool>;
using pair = std::pair<std::string, bool>;
// Back-up function
class backupFunction final : public FunctionBase {
private:
std::vector<std::string> partitions, outputNames;
std::string rawPartitions, rawOutputNames, outputDirectory;
int bufferSize = 4096;
// Back-up function
class backupFunction final : public FunctionBase {
private:
std::vector<std::string> partitions, outputNames;
std::string rawPartitions, rawOutputNames, outputDirectory;
int bufferSize = 4096;
public:
CLI::App *cmd = nullptr;
public:
CLI::App *cmd = nullptr;
bool init(CLI::App &_app) override;
bool run() override;
static pair runAsync(const std::string &partitionName, const std::string &outputName, int bufferSize);
bool init(CLI::App &_app) override;
bool run() override;
static pair runAsync(const std::string &partitionName,
const std::string &outputName, int bufferSize);
[[nodiscard]] bool isUsed() const override;
[[nodiscard]] const char *name() const override;
};
[[nodiscard]] bool isUsed() const override;
[[nodiscard]] const char *name() const override;
};
// Image flasher function
class flashFunction final : public FunctionBase {
private:
std::vector<std::string> partitions, imageNames;
std::string rawPartitions, rawImageNames, imageDirectory;
int bufferSize = 4096;
// Image flasher function
class flashFunction final : public FunctionBase {
private:
std::vector<std::string> partitions, imageNames;
std::string rawPartitions, rawImageNames, imageDirectory;
int bufferSize = 4096;
public:
CLI::App *cmd = nullptr;
public:
CLI::App *cmd = nullptr;
bool init(CLI::App &_app) override;
bool run() override;
static pair runAsync(const std::string &partitionName, const std::string &imageName, int bufferSize);
bool init(CLI::App &_app) override;
bool run() override;
static pair runAsync(const std::string &partitionName,
const std::string &imageName, int bufferSize);
[[nodiscard]] bool isUsed() const override;
[[nodiscard]] const char *name() const override;
};
[[nodiscard]] bool isUsed() const override;
[[nodiscard]] const char *name() const override;
};
// Eraser function (writes zero bytes to partition)
class eraseFunction final : public FunctionBase {
private:
std::vector<std::string> partitions;
int bufferSize = 4096;
// Eraser function (writes zero bytes to partition)
class eraseFunction final : public FunctionBase {
private:
std::vector<std::string> partitions;
int bufferSize = 4096;
public:
CLI::App *cmd = nullptr;
public:
CLI::App *cmd = nullptr;
bool init(CLI::App &_app) override;
bool run() override;
static pair runAsync(const std::string &partitionName, int bufferSize);
bool init(CLI::App &_app) override;
bool run() override;
static pair runAsync(const std::string &partitionName, int bufferSize);
[[nodiscard]] bool isUsed() const override;
[[nodiscard]] const char *name() const override;
};
[[nodiscard]] bool isUsed() const override;
[[nodiscard]] const char *name() const override;
};
// Partition size getter function
class partitionSizeFunction final : public FunctionBase {
private:
std::vector<std::string> partitions;
bool onlySize = false, asByte = false, asKiloBytes = false, asMega = false, asGiga = false;
// Partition size getter function
class partitionSizeFunction final : public FunctionBase {
private:
std::vector<std::string> partitions;
bool onlySize = false, asByte = false, asKiloBytes = false, asMega = false,
asGiga = false;
public:
CLI::App *cmd = nullptr;
public:
CLI::App *cmd = nullptr;
bool init(CLI::App &_app) override;
bool run() override;
bool init(CLI::App &_app) override;
bool run() override;
[[nodiscard]] bool isUsed() const override;
[[nodiscard]] const char *name() const override;
};
[[nodiscard]] bool isUsed() const override;
[[nodiscard]] const char *name() const override;
};
// Partition info getter function
class infoFunction final : public FunctionBase {
private:
std::vector<std::string> partitions;
std::string jNamePartition = "name", jNameSize = "size", jNameLogical = "isLogical";
bool jsonFormat = false;
// Partition info getter function
class infoFunction final : public FunctionBase {
private:
std::vector<std::string> partitions;
std::string jNamePartition = "name", jNameSize = "size",
jNameLogical = "isLogical";
bool jsonFormat = false;
public:
CLI::App *cmd = nullptr;
public:
CLI::App *cmd = nullptr;
bool init(CLI::App &_app) override;
bool run() override;
bool init(CLI::App &_app) override;
bool run() override;
[[nodiscard]] bool isUsed() const override;
[[nodiscard]] const char *name() const override;
};
[[nodiscard]] bool isUsed() const override;
[[nodiscard]] const char *name() const override;
};
class realPathFunction final : public FunctionBase {
private:
std::vector<std::string> partitions;
class realPathFunction final : public FunctionBase {
private:
std::vector<std::string> partitions;
public:
CLI::App *cmd = nullptr;
public:
CLI::App *cmd = nullptr;
bool init(CLI::App &_app) override;
bool run() override;
bool init(CLI::App &_app) override;
bool run() override;
[[nodiscard]] bool isUsed() const override;
[[nodiscard]] const char *name() const override;
};
[[nodiscard]] bool isUsed() const override;
[[nodiscard]] const char *name() const override;
};
class realLinkPathFunction final : public FunctionBase {
private:
std::vector<std::string> partitions;
class realLinkPathFunction final : public FunctionBase {
private:
std::vector<std::string> partitions;
public:
CLI::App *cmd = nullptr;
public:
CLI::App *cmd = nullptr;
bool init(CLI::App &_app) override;
bool run() override;
bool init(CLI::App &_app) override;
bool run() override;
[[nodiscard]] bool isUsed() const override;
[[nodiscard]] const char *name() const override;
};
[[nodiscard]] bool isUsed() const override;
[[nodiscard]] const char *name() const override;
};
class typeFunction final : public FunctionBase {
private:
std::vector<std::string> contents;
bool onlyCheckAndroidMagics = false, onlyCheckFileSystemMagics = false;
int bufferSize = 4096;
class typeFunction final : public FunctionBase {
private:
std::vector<std::string> contents;
bool onlyCheckAndroidMagics = false, onlyCheckFileSystemMagics = false;
int bufferSize = 4096;
public:
CLI::App *cmd = nullptr;
public:
CLI::App *cmd = nullptr;
bool init(CLI::App &_app) override;
bool run() override;
bool init(CLI::App &_app) override;
bool run() override;
[[nodiscard]] bool isUsed() const override;
[[nodiscard]] const char *name() const override;
};
[[nodiscard]] bool isUsed() const override;
[[nodiscard]] const char *name() const override;
};
class rebootFunction final : public FunctionBase {
private:
std::string rebootTarget;
class rebootFunction final : public FunctionBase {
private:
std::string rebootTarget;
public:
CLI::App *cmd = nullptr;
public:
CLI::App *cmd = nullptr;
bool init(CLI::App &_app) override;
bool run() override;
bool init(CLI::App &_app) override;
bool run() override;
[[nodiscard]] bool isUsed() const override;
[[nodiscard]] const char *name() const override;
};
[[nodiscard]] bool isUsed() const override;
[[nodiscard]] const char *name() const override;
};
} // namespace PartitionManager
#endif // #ifndef FUNCTIONS_HPP