pmt: initial 1.9.0 update

This commit is contained in:
2024-05-05 21:04:48 +03:00
committed by GitHub
parent e2f8cf4ca3
commit b9e58df98f

306
src/pmt.c
View File

@@ -1,6 +1,6 @@
/* By YZBruh */ /* By YZBruh */
/* /**
* Copyright 2024 Partition Manager * Copyright 2024 Partition Manager
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,10 +16,12 @@
* limitations under the License. * limitations under the License.
*/ */
/* force use C std (if default is C++) */
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
/* include needed libs (headers) */
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
@@ -29,13 +31,16 @@ extern "C" {
#include <getopt.h> #include <getopt.h>
#include <errno.h> #include <errno.h>
/* include custom pmt header */
#include "include/pmt.h" #include "include/pmt.h"
/* add value to variables that are added globally and are not worth */
char *out = NULL; char *out = NULL;
char *outdir = NULL; char *outdir = NULL;
char *cust_cxt = NULL; char *cust_cxt = NULL;
char *target_partition = NULL; char *target_partition = NULL;
char *target_flash_file = NULL; char *target_flash_file = NULL;
char *partition_type = NULL;
char *format_fs = NULL; char *format_fs = NULL;
bool pmt_use_logical = NULL; bool pmt_use_logical = NULL;
bool pmt_use_cust_cxt = NULL; bool pmt_use_cust_cxt = NULL;
@@ -74,19 +79,30 @@ int main(int argc, char *argv[])
{0, 0, 0, 0} {0, 0, 0, 0}
}; };
/* boolean statements (only valid in this file) to distinguish. and pointer from a shortcut for the symbol rule */
static bool wiew_help = false;
static bool wiew_licenses = false;
static bool wiew_version = false;
static bool list_partitions = false;
static bool combo_wiewers = false;
static bool use_cust_outdir = false;
static char *opt_symbol = "-"; static char *opt_symbol = "-";
static char *common_symbol_rule; static char *common_symbol_rule;
common_symbol_rule = "When entering the attached argument of an option, an argument of another option type cannot be used. In short, the rule is: there can be no '-' at the beginning of the attached argument.\n"; common_symbol_rule = "When entering the attached argument of an option, an argument of another option type cannot be used. In short, the rule is: there can be no '-' at the beginning of the attached argument.\n";
int opt; int opt;
/* control for each argument */ /* control for each argument */
while ((opt = getopt_long(argc, argv, "bF:rp:lo:d:c:DfvL", long_options, NULL)) != -1) while ((opt = getopt_long(argc, argv, "bF:rp:lo:d:c:DfvL", long_options, NULL)) != -1)
{ {
/* process arguments */ /* process arguments */
switch (opt) switch (opt)
{ {
/* backup mode */
case 'b': case 'b':
pmt_backup = true; pmt_backup = true;
break; break;
/* flash mode */
case 'F': case 'F':
target_flash_file = strdup(optarg); target_flash_file = strdup(optarg);
if (strncmp(target_flash_file, opt_symbol, 1) == 0) if (strncmp(target_flash_file, opt_symbol, 1) == 0)
@@ -99,31 +115,8 @@ int main(int argc, char *argv[])
} }
} }
pmt_flash = true; pmt_flash = true;
check_root();
check_psf();
struct stat flashf_info;
if (stat(target_flash_file, &flashf_info) != 0)
{
if (!pmt_force_mode)
{
fprintf(stderr, "%s: %s: %s\n", argv[0], target_flash_file, strerror(errno));
exit(15);
} else {
exit(15);
}
} else {
if (!S_ISREG(flashf_info.st_mode))
{
if (!pmt_force_mode)
{
fprintf(stderr, "%s: %s: is a not file.\n", argv[0], target_flash_file);
exit(16);
} else {
exit(16);
}
}
}
break; break;
/* format mode */
case 'r': case 'r':
format_fs = strdup(optarg); format_fs = strdup(optarg);
if (strncmp(format_fs, opt_symbol, 1) == 0) if (strncmp(format_fs, opt_symbol, 1) == 0)
@@ -137,17 +130,8 @@ int main(int argc, char *argv[])
} }
} }
pmt_format = true; pmt_format = true;
if (strcmp(format_fs, "ext4") != 0 || strcmp(format_fs, "ext3") != 0 || strcmp(format_fs, "ext2") != 0)
{
if (!pmt_force_mode)
{
fprintf(stderr, "%s: formatter: unsupported filesystem: %s", argv[0], format_fs);
exit(41);
} else {
exit(41);
}
}
break; break;
/* partition selector option */
case 'p': case 'p':
target_partition = strdup(optarg); target_partition = strdup(optarg);
if (strncmp(target_partition, opt_symbol, 1) == 0) if (strncmp(target_partition, opt_symbol, 1) == 0)
@@ -160,6 +144,7 @@ int main(int argc, char *argv[])
} }
} }
break; break;
/* logical partitions option */
case 'l': case 'l':
check_root(); check_root();
check_psf(); check_psf();
@@ -175,6 +160,7 @@ int main(int argc, char *argv[])
} }
} }
break; break;
/* output file option */
case 'o': case 'o':
out = strdup(optarg); out = strdup(optarg);
if (strncmp(out, opt_symbol, 1) == 0) if (strncmp(out, opt_symbol, 1) == 0)
@@ -187,8 +173,141 @@ int main(int argc, char *argv[])
} }
} }
break; break;
/* output dir option */
case 'd': case 'd':
use_cust_outdir = true;
outdir = strdup(optarg); outdir = strdup(optarg);
break;
/* context selector option */
case 'c':
pmt_use_cust_cxt = true;
cust_cxt = strdup(optarg);
if (strncmp(cust_cxt, opt_symbol, 1) == 0)
{
if (!pmt_force_mode)
{
error(common_symbol_rule, 19);
} else {
exit(19);
}
}
break;
/* partition lister function */
case 'D':
list_partitions = true;
/* check combo wiewer options and progress */
if (wiew_version || wiew_help || wiew_licenses)
{
combo_wiewers = true;
}
break;
/* force mode option */
case 'f':
pmt_force_mode = true;
break;
/* version info option */
case 'v':
wiew_version = true;
/* check combo wiewer options and progress */
if (list_partitions || wiew_help || wiew_licenses)
{
combo_wiewers = true;
}
break;
/* help message opption */
case 0:
wiew_help = true;
/* check combo wiewer options and progress */
if (wiew_version || list_partitions || wiew_licenses)
{
combo_wiewers = true;
}
break;
/* license wiewer option */
case 'L':
wiew_licenses = true;
/* check combo wiewer options and progress */
if (wiew_version || wiew_help || list_partitions)
{
combo_wiewers = true;
}
break;
/* for invalid options */
case '?':
printf("Try `%s --help' for more information.\n", argv[0]);
exit(43);
break;
default:
printf("Usage: %s [-b | --backup] [-f | --flash FILE] [-r | --format FS_TYPE] [-p | --partition PARTITION] [-l | --logical] [-o | --out OUTNAME] [-d | --outdir OUTDIR] [-c | --context] [-D | --list] [-v | --version] [--help] [-L | --license]\n", argv[0]);
exit(44);
}
}
/* stop the program if multiple viewer is used */
if (combo_wiewers)
{
fprintf(stderr, "%s: Multiple wiewers cannot be used at the same line.\n", argv[0]);
exit(81);
}
/* controller to handle viewer */
if (wiew_help)
{
help();
exit(EXIT_SUCCESS);
} else if (wiew_version)
{
version();
exit(EXIT_SUCCESS);
} else if (wiew_licenses)
{
licenses();
exit(EXIT_SUCCESS);
} else if (list_partitions)
{
check_root();
listpart();
exit(EXIT_SUCCESS);
}
/* target control is done */
if (!pmt_backup && !pmt_flash && !pmt_format)
{
fprintf(stderr, "%s: missing operand.\nTry `%s --help` for more information.\n", argv[0], argv[0]);
exit(3);
}
/* prevent multiple mode use */
if (pmt_backup && pmt_flash)
{
if (!pmt_force_mode)
{
error("Backup and flash functions cannot be used in the same command.\n", 9);
} else {
exit(9);
}
}
/* checks */
check_root();
check_psf();
if (pmt_format)
{
if (strcmp(format_fs, "ext4") != 0 || strcmp(format_fs, "ext3") != 0 || strcmp(format_fs, "ext2") != 0)
{
if (!pmt_force_mode)
{
fprintf(stderr, "%s: formatter: unsupported filesystem: %s", argv[0], format_fs);
exit(41);
} else {
exit(41);
}
}
}
if (use_cust_outdir)
{
if (strncmp(outdir, opt_symbol, 1) == 0) if (strncmp(outdir, opt_symbol, 1) == 0)
{ {
if (!pmt_force_mode) if (!pmt_force_mode)
@@ -198,14 +317,12 @@ int main(int argc, char *argv[])
exit(19); exit(19);
} }
} }
check_root();
check_psf();
struct stat out_info; struct stat out_info;
if (stat(outdir, &out_info) != 0) if (stat(outdir, &out_info) != 0)
{ {
if (!pmt_force_mode) if (!pmt_force_mode)
{ {
fprintf(stderr, "%s: %s: %s\n", argv[0], outdir, strerror(errno)); fprintf(stderr, "%s: cannot stat '%s': %s\n", argv[0], outdir, strerror(errno));
exit(18); exit(18);
} else { } else {
exit(18); exit(18);
@@ -222,79 +339,33 @@ int main(int argc, char *argv[])
} }
} }
} }
break; }
case 'c':
pmt_use_cust_cxt = true; if (pmt_flash)
cust_cxt = strdup(optarg); {
if (strncmp(cust_cxt, opt_symbol, 1) == 0) struct stat flashf_info;
if (stat(target_flash_file, &flashf_info) != 0)
{ {
if (!pmt_force_mode) if (!pmt_force_mode)
{ {
error(common_symbol_rule, 19); fprintf(stderr, "%s: cannot stat '%s': %s\n", argv[0], target_flash_file, strerror(errno));
exit(15);
} else { } else {
exit(19); exit(15);
} }
} } else {
break; if (!S_ISREG(flashf_info.st_mode))
case 'D':
check_root();
listpart();
exit(EXIT_SUCCESS);
break;
case 'f':
pmt_force_mode = true;
break;
case 'v':
printf("Version: %s (code %s)\n", PMT_VERSION, PMT_VERSION_CODE);
#ifdef __clang__
printf("Compiler: clang %s", __clang_version__);
#endif
#ifdef __GNUC__
printf("(GNUC %d.%d.%d)\n", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__);
#else
printf("\n");
#endif
printf("See licenses with -L argument.\n");
exit(EXIT_SUCCESS);
break;
case 0:
help();
exit(EXIT_SUCCESS);
break;
case 'L':
licenses();
exit(EXIT_SUCCESS);
break;
case '?':
printf("Try `%s --help' for more information.\n", argv[0]);
exit(43);
break;
default:
printf("Usage: %s [-b | --backup] [-f | --flash FILE] [-r | --format FS_TYPE] [-p | --partition PARTITION] [-l | --logical] [-o | --out OUTNAME] [-d | --outdir OUTDIR] [-c | --context] [-D | --list] [-v | --version] [--help] [-L | --license]\n", argv[0]);
exit(44);
}
}
/* minor checks */
if (!pmt_backup && !pmt_flash)
{
fprintf(stderr, "%s: missing operand.\nTry `%s --help` for more information.\n", argv[0], argv[0]);
exit(3);
}
if (pmt_backup && pmt_flash)
{ {
if (!pmt_force_mode) if (!pmt_force_mode)
{ {
error("Backup and flash functions cannot be used in the same command\n", 9); fprintf(stderr, "%s: %s: is a not file.\n", argv[0], target_flash_file);
exit(16);
} else { } else {
exit(9); exit(16);
}
}
} }
} }
/* checks */
check_root();
check_psf();
/* custom context checker */ /* custom context checker */
if (pmt_use_cust_cxt) if (pmt_use_cust_cxt)
@@ -323,7 +394,8 @@ int main(int argc, char *argv[])
} }
if (strstr(cust_cxt, "/dev") == NULL && !pmt_force_mode) if (strstr(cust_cxt, "/dev") == NULL && !pmt_force_mode)
{ {
printf("%sThis custom context is strange...%s\n", ANSI_YELLOW, ANSI_RESET); fprintf(stderr, "%sYou're going through my wave? There's nothing about this /dev. Use force mode if you don't want this error%s\n", ANSI_YELLOW, ANSI_RESET);
exit(81);
} }
} }
@@ -337,35 +409,25 @@ int main(int argc, char *argv[])
exit(5); exit(5);
} }
} else { } else {
/**
*
* 1 = backup mode
*
* 2 = flash mode
*
* 3 = format
*/
if (pmt_backup) if (pmt_backup)
{ {
if (pmt_use_logical) pmt(1);
{
backup(target_partition, "logical");
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} else {
backup(target_partition, "classic");
exit(EXIT_SUCCESS);
}
} else if (pmt_flash) } else if (pmt_flash)
{ {
if (pmt_use_logical) pmt(2);
{
flash(target_partition, target_flash_file, "logical");
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} else {
flash(target_partition, target_flash_file, "classic");
exit(EXIT_SUCCESS);
}
} else if (pmt_format) } else if (pmt_format)
{ {
if (pmt_use_logical) { pmt(3);
format(target_partition, "logical");
exit(EXIT_SUCCESS);
} else {
format(target_partition, "classic");
exit(EXIT_SUCCESS);
}
} else { } else {
fprintf(stderr, "%s: no target (backup or flash).\nTry `%s --help` for more information.\n", argv[0], argv[0]); fprintf(stderr, "%s: no target (backup or flash).\nTry `%s --help` for more information.\n", argv[0], argv[0]);
exit(3); exit(3);