pmt: apply version 1.7.0 changes

This commit is contained in:
2024-04-09 23:32:06 +03:00
committed by GitHub
parent 97edfbc133
commit d3d6bfd244

View File

@@ -1,16 +1,3 @@
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
#include <stdbool.h>
#include <getopt.h>
#include <stdint.h>
#include <stddef.h>
#include "include/common.h"
#include "include/documentation.h"
/* By YZBruh */ /* By YZBruh */
/* /*
@@ -29,39 +16,56 @@
* limitations under the License. * limitations under the License.
*/ */
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
#include <stdbool.h>
#include <getopt.h>
#include <errno.h>
#include "include/pmt.h"
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;
bool use_logical = NULL; bool pmt_use_logical = NULL;
bool use_cust_cxt = NULL; bool pmt_use_cust_cxt = NULL;
bool pmt_ab = false; bool pmt_ab = false;
bool pmt_logical = false; bool pmt_logical = false;
bool pmt_flash = false; bool pmt_flash = false;
bool pmt_backup = false; bool pmt_backup = false;
bool pmt_force_mode = false;
/* classic main function (C binary here xd) */ /* classic main function (C binary here xd) */
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
/* check argument total */ /* check argument total */
if (argc < 2) { if (argc < 2) {
printf("Usage: %s [-b, --backup] [-f, --flash] [-p, --partition] [-l, --logical] [-f, --flash] [-o, --out] [-d, --outdir] [-D, --list] [-v, --version] [-h, --help] [-L, --license]\n", argv[0]); fprintf(stderr, "%s: missing operand\nTry `%s --help` for more information.\n", argv[0], argv[0]);
exit(EXIT_FAILURE); exit(44);
} }
/* a structure for long arguments... */ /* a structure for long arguments... */
struct option long_options[] = { struct option long_options[] = {
{"backup", no_argument, 0, 'b'}, {"backup", no_argument, 0, 'b'},
{"flash", required_argument, 0, 'f'}, {"flash", required_argument, 0, 'F'},
{"partition", required_argument, 0, 'p'}, {"partition", required_argument, 0, 'p'},
{"logical", no_argument, 0, 'l'}, {"logical", no_argument, 0, 'l'},
{"out", required_argument, 0, 'o'}, {"out", required_argument, 0, 'o'},
{"outdir", required_argument, 0, 'd'}, {"outdir", required_argument, 0, 'd'},
{"context", required_argument, 0, 'c'}, {"context", required_argument, 0, 'c'},
{"list", no_argument, 0, 'D'}, {"list", no_argument, 0, 'D'},
{"force", no_argument, 0, 'f'},
{"version", no_argument, 0, 'v'}, {"version", no_argument, 0, 'v'},
{"help", no_argument, 0, 'h'}, {"help", no_argument, 0, 0},
{"license", no_argument, 0, 'L'}, {"license", no_argument, 0, 'L'},
{0, 0, 0, 0} {0, 0, 0, 0}
}; };
@@ -71,75 +75,115 @@ int main(int argc, char *argv[])
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:p:lo:d:c:DvhL", long_options, NULL)) != -1) { while ((opt = getopt_long(argc, argv, "bF:p:lo:d:c:DfvL", long_options, NULL)) != -1) {
/* process arguments */ /* process arguments */
switch (opt) { switch (opt) {
case 'b': case 'b':
pmt_backup = true; pmt_backup = true;
break; break;
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) {
error(common_symbol_rule); if (!pmt_force_mode) {
error(common_symbol_rule, 19);
} else {
exit(19);
}
} }
pmt_flash = true; pmt_flash = true;
check_root(); check_root();
check_psf(); check_psf();
struct stat flashf_info; struct stat flashf_info;
if (stat(target_flash_file, &flashf_info) != 0) { if (stat(target_flash_file, &flashf_info) != 0) {
fprintf(stderr, "%s: %s: no such file or directory.\n", argv[0], target_flash_file); if (!pmt_force_mode) {
exit(EXIT_FAILURE); fprintf(stderr, "%s: %s: %s\n", argv[0], target_flash_file, strerror(errno));
exit(15);
} else {
exit(15);
}
} else { } else {
if (!S_ISREG(flashf_info.st_mode)) { if (!S_ISREG(flashf_info.st_mode)) {
fprintf(stderr, "%s: %s: is a not file.\n", argv[0], target_flash_file); if (!pmt_force_mode) {
exit(EXIT_FAILURE); fprintf(stderr, "%s: %s: is a not file.\n", argv[0], target_flash_file);
exit(16);
} else {
exit(16);
}
} }
} }
break; break;
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) {
error(common_symbol_rule); if (!pmt_force_mode) {
error(common_symbol_rule, 19);
} else {
exit(19);
}
} }
break; break;
case 'l': case 'l':
check_root(); check_root();
check_psf(); check_psf();
if (pmt_logical) { if (pmt_logical) {
use_logical = true; pmt_use_logical = true;
} else { } else {
error("This device does not have logical partitions!\n"); if (!pmt_force_mode) {
error("This device does not have logical partitions!\n", 17);
} else {
exit(17);
}
} }
break; break;
case 'o': case 'o':
out = strdup(optarg); out = strdup(optarg);
if (strncmp(out, opt_symbol, 1) == 0) { if (strncmp(out, opt_symbol, 1) == 0) {
error(common_symbol_rule); if (!pmt_force_mode) {
error(common_symbol_rule, 19);
} else {
exit(19);
}
} }
break; break;
case 'd': case 'd':
outdir = strdup(optarg); outdir = strdup(optarg);
if (strncmp(outdir, opt_symbol, 1) == 0) { if (strncmp(outdir, opt_symbol, 1) == 0) {
error(common_symbol_rule); if (!pmt_force_mode) {
error(common_symbol_rule, 19);
} else {
exit(19);
}
} }
check_root(); check_root();
check_psf(); check_psf();
struct stat out_info; struct stat out_info;
if (stat(outdir, &out_info) != 0) { if (stat(outdir, &out_info) != 0) {
fprintf(stderr, "%s: %s: no such file or directory.\n", argv[0], outdir); if (!pmt_force_mode) {
exit(EXIT_FAILURE); fprintf(stderr, "%s: %s: %s\n", argv[0], outdir, strerror(errno));
exit(18);
} else {
exit(18);
}
} else { } else {
if (!S_ISDIR(out_info.st_mode)) { if (!S_ISDIR(out_info.st_mode)) {
fprintf(stderr, "%s: %s: is a not directory.\n", argv[0], outdir); if (!pmt_force_mode) {
exit(EXIT_FAILURE); fprintf(stderr, "%s: %s: is a not directory.\n", argv[0], outdir);
exit(20);
} else {
exit(20);
}
} }
} }
break; break;
case 'c': case 'c':
use_cust_cxt = true; pmt_use_cust_cxt = true;
cust_cxt = strdup(optarg); cust_cxt = strdup(optarg);
if (strncmp(cust_cxt, opt_symbol, 1) == 0) { if (strncmp(cust_cxt, opt_symbol, 1) == 0) {
error(common_symbol_rule); if (!pmt_force_mode) {
error(common_symbol_rule, 19);
} else {
exit(19);
}
} }
break; break;
case 'D': case 'D':
@@ -147,20 +191,23 @@ int main(int argc, char *argv[])
listpart(); listpart();
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
break; break;
case 'f':
pmt_force_mode = true;
break;
case 'v': case 'v':
printf("Version: %s (code %s)\n", PACK_VER, PACK_VER_CODE); printf("Version: %s (code %s)\n", PMT_VERSION, PMT_VERSION_CODE);
# ifdef __clang__ #ifdef __clang__
printf("Compiler: clang %s", __clang_version__); printf("Compiler: clang %s", __clang_version__);
# endif #endif
# ifdef __GNUC__ #ifdef __GNUC__
printf("(GNUC %d.%d.%d)\n", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__); printf("(GNUC %d.%d.%d)\n", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__);
# else #else
printf("\n"); printf("\n");
# endif #endif
printf("See licenses with -L argument.\n"); printf("See licenses with -L argument.\n");
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
break; break;
case 'h': case 0:
help(); help();
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
break; break;
@@ -170,21 +217,26 @@ int main(int argc, char *argv[])
break; break;
case '?': case '?':
printf("Try `%s --help' for more information.\n", argv[0]); printf("Try `%s --help' for more information.\n", argv[0]);
exit(EXIT_FAILURE); exit(43);
break; break;
default: default:
printf("Usage: %s [-b, --backup] [-f, --flash] [-p, --partition] [-l, --logical] [-f, --flash] [-o, --out] [-d, --outdir] [-D, --list] [-v, --version] [-h, --help] [-L, --license]\n", argv[0]); printf("Usage: %s [-b | --backup] [-f | --flash FILE] [-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 */ /* minor checks */
if (!pmt_backup && !pmt_flash) { if (!pmt_backup && !pmt_flash) {
fprintf(stderr, "%s: no target (backup or flash).\nTry `%s --help` for more information.\n", argv[0], argv[0]); fprintf(stderr, "%s: missing operand.\nTry `%s --help` for more information.\n", argv[0], argv[0]);
exit(EXIT_FAILURE); exit(3);
} }
if (pmt_backup && pmt_flash) { if (pmt_backup && pmt_flash) {
error("Backup and flash functions cannot be used in the same command\n"); if (!pmt_force_mode) {
error("Backup and flash functions cannot be used in the same command\n", 9);
} else {
exit(9);
}
} }
/* checks */ /* checks */
@@ -192,29 +244,40 @@ int main(int argc, char *argv[])
check_psf(); check_psf();
/* custom context checker */ /* custom context checker */
if (use_cust_cxt) { if (pmt_use_cust_cxt) {
struct stat cxtinfo; struct stat cxtinfo;
if (stat(cust_cxt, &cxtinfo) == 0) { if (stat(cust_cxt, &cxtinfo) == 0) {
if (S_ISDIR(cxtinfo.st_mode)) { if (!S_ISREG(cxtinfo.st_mode)) {
/* empty */ if (!pmt_force_mode) {
} else { fprintf(stderr, "%s: %s: is a not directory.\n", argv[0], cust_cxt);
fprintf(stderr, "%s: custom context: %s: is a not directory.\n", argv[0], cust_cxt); exit(8);
exit(EXIT_FAILURE); } else {
exit(8);
}
} }
} else { } else {
error("The specified context was not found!\n"); if (!pmt_force_mode) {
fprintf(stderr, "%s: %s: %s\n", argv[0], cust_cxt, strerror(errno));
exit(6);
} else {
exit(6);
}
} }
if (strstr(cust_cxt, "/dev") == NULL) { if (strstr(cust_cxt, "/dev") == NULL && !pmt_force_mode) {
printf("%sThis custom context is strange...%s\n", ANSI_YELLOW, ANSI_RESET); printf("%sThis custom context is strange...%s\n", ANSI_YELLOW, ANSI_RESET);
} }
} }
if (target_partition == NULL) { if (target_partition == NULL) {
fprintf(stderr, "%s: required partition name.\nTry `%s --help' for more information.\n", argv[0], argv[0]); if (!pmt_force_mode) {
exit(EXIT_FAILURE); fprintf(stderr, "%s: required partition name.\nTry `%s --help' for more information.\n", argv[0], argv[0]);
exit(5);
} else {
exit(5);
}
} else { } else {
if (pmt_backup) { if (pmt_backup) {
if (use_logical) { if (pmt_use_logical) {
backup(target_partition, "logical"); backup(target_partition, "logical");
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} else { } else {
@@ -222,7 +285,7 @@ int main(int argc, char *argv[])
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
} else if (pmt_flash) { } else if (pmt_flash) {
if (use_logical) { if (pmt_use_logical) {
flash(target_partition, target_flash_file, "logical"); flash(target_partition, target_flash_file, "logical");
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} else { } else {
@@ -231,9 +294,13 @@ int main(int argc, char *argv[])
} }
} 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(EXIT_FAILURE); exit(3);
} }
} }
} }
#ifdef __cplusplus
}
#endif
/* end of code */ /* end of code */