pmt: initial 1.9.0 update
This commit is contained in:
288
src/pmt.c
288
src/pmt.c
@@ -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,42 +173,12 @@ 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);
|
||||||
if (strncmp(outdir, opt_symbol, 1) == 0)
|
|
||||||
{
|
|
||||||
if (!pmt_force_mode)
|
|
||||||
{
|
|
||||||
error(common_symbol_rule, 19);
|
|
||||||
} else {
|
|
||||||
exit(19);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
check_root();
|
|
||||||
check_psf();
|
|
||||||
struct stat out_info;
|
|
||||||
if (stat(outdir, &out_info) != 0)
|
|
||||||
{
|
|
||||||
if (!pmt_force_mode)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "%s: %s: %s\n", argv[0], outdir, strerror(errno));
|
|
||||||
exit(18);
|
|
||||||
} else {
|
|
||||||
exit(18);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (!S_ISDIR(out_info.st_mode))
|
|
||||||
{
|
|
||||||
if (!pmt_force_mode)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "%s: %s: is a not directory.\n", argv[0], outdir);
|
|
||||||
exit(20);
|
|
||||||
} else {
|
|
||||||
exit(20);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
/* context selector option */
|
||||||
case 'c':
|
case 'c':
|
||||||
pmt_use_cust_cxt = true;
|
pmt_use_cust_cxt = true;
|
||||||
cust_cxt = strdup(optarg);
|
cust_cxt = strdup(optarg);
|
||||||
@@ -236,35 +192,47 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
/* partition lister function */
|
||||||
case 'D':
|
case 'D':
|
||||||
check_root();
|
list_partitions = true;
|
||||||
listpart();
|
/* check combo wiewer options and progress */
|
||||||
exit(EXIT_SUCCESS);
|
if (wiew_version || wiew_help || wiew_licenses)
|
||||||
|
{
|
||||||
|
combo_wiewers = true;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
/* force mode option */
|
||||||
case 'f':
|
case 'f':
|
||||||
pmt_force_mode = true;
|
pmt_force_mode = true;
|
||||||
break;
|
break;
|
||||||
|
/* version info option */
|
||||||
case 'v':
|
case 'v':
|
||||||
printf("Version: %s (code %s)\n", PMT_VERSION, PMT_VERSION_CODE);
|
wiew_version = true;
|
||||||
#ifdef __clang__
|
/* check combo wiewer options and progress */
|
||||||
printf("Compiler: clang %s", __clang_version__);
|
if (list_partitions || wiew_help || wiew_licenses)
|
||||||
#endif
|
{
|
||||||
#ifdef __GNUC__
|
combo_wiewers = true;
|
||||||
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;
|
break;
|
||||||
|
/* help message opption */
|
||||||
case 0:
|
case 0:
|
||||||
help();
|
wiew_help = true;
|
||||||
exit(EXIT_SUCCESS);
|
/* check combo wiewer options and progress */
|
||||||
|
if (wiew_version || list_partitions || wiew_licenses)
|
||||||
|
{
|
||||||
|
combo_wiewers = true;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
/* license wiewer option */
|
||||||
case 'L':
|
case 'L':
|
||||||
licenses();
|
wiew_licenses = true;
|
||||||
exit(EXIT_SUCCESS);
|
/* check combo wiewer options and progress */
|
||||||
|
if (wiew_version || wiew_help || list_partitions)
|
||||||
|
{
|
||||||
|
combo_wiewers = true;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
/* for invalid options */
|
||||||
case '?':
|
case '?':
|
||||||
printf("Try `%s --help' for more information.\n", argv[0]);
|
printf("Try `%s --help' for more information.\n", argv[0]);
|
||||||
exit(43);
|
exit(43);
|
||||||
@@ -275,18 +243,46 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* minor checks */
|
/* stop the program if multiple viewer is used */
|
||||||
if (!pmt_backup && !pmt_flash)
|
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]);
|
fprintf(stderr, "%s: missing operand.\nTry `%s --help` for more information.\n", argv[0], argv[0]);
|
||||||
exit(3);
|
exit(3);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* prevent multiple mode use */
|
||||||
if (pmt_backup && pmt_flash)
|
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);
|
error("Backup and flash functions cannot be used in the same command.\n", 9);
|
||||||
} else {
|
} else {
|
||||||
exit(9);
|
exit(9);
|
||||||
}
|
}
|
||||||
@@ -296,6 +292,81 @@ int main(int argc, char *argv[])
|
|||||||
check_root();
|
check_root();
|
||||||
check_psf();
|
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 (!pmt_force_mode)
|
||||||
|
{
|
||||||
|
error(common_symbol_rule, 19);
|
||||||
|
} else {
|
||||||
|
exit(19);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
struct stat out_info;
|
||||||
|
if (stat(outdir, &out_info) != 0)
|
||||||
|
{
|
||||||
|
if (!pmt_force_mode)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "%s: cannot stat '%s': %s\n", argv[0], outdir, strerror(errno));
|
||||||
|
exit(18);
|
||||||
|
} else {
|
||||||
|
exit(18);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!S_ISDIR(out_info.st_mode))
|
||||||
|
{
|
||||||
|
if (!pmt_force_mode)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "%s: %s: is a not directory.\n", argv[0], outdir);
|
||||||
|
exit(20);
|
||||||
|
} else {
|
||||||
|
exit(20);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pmt_flash)
|
||||||
|
{
|
||||||
|
struct stat flashf_info;
|
||||||
|
if (stat(target_flash_file, &flashf_info) != 0)
|
||||||
|
{
|
||||||
|
if (!pmt_force_mode)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "%s: cannot stat '%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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* 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);
|
||||||
{
|
exit(EXIT_SUCCESS);
|
||||||
backup(target_partition, "logical");
|
|
||||||
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);
|
||||||
{
|
exit(EXIT_SUCCESS);
|
||||||
flash(target_partition, target_flash_file, "logical");
|
|
||||||
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);
|
||||||
|
|||||||
Reference in New Issue
Block a user