From 545376afb287427c3b649ccf631f83a53f98dc48 Mon Sep 17 00:00:00 2001 From: YZ-Bruh Date: Tue, 4 Jun 2024 17:55:15 +0300 Subject: [PATCH] Initial 2.1.0 update --- CHANGELOG.md | 0 README.md | 36 +-- debutils/DEBIAN/control_32 | 2 +- debutils/DEBIAN/control_64 | 2 +- debutils/mandoc/pmt.1 | 24 -- debutils/mandoc/pmt.1.gz | Bin 0 -> 889 bytes jni/Android.mk | 3 +- jni/docs.c | 26 +- jni/include/pmt-versioning.h | 4 +- jni/include/pmt.h | 21 +- jni/listpart.c | 51 ++-- jni/{checkers.c => partition_tool.c} | 46 ++- jni/pmt.c | 427 +++++++++++++-------------- jni/root.c | 46 +++ jni/tools.c | 285 ++++++++++-------- jni/versioner.c | 11 +- make-deb.sh | 92 ++++-- 17 files changed, 573 insertions(+), 503 deletions(-) mode change 100644 => 100755 CHANGELOG.md mode change 100644 => 100755 README.md delete mode 100755 debutils/mandoc/pmt.1 create mode 100755 debutils/mandoc/pmt.1.gz rename jni/{checkers.c => partition_tool.c} (62%) create mode 100755 jni/root.c diff --git a/CHANGELOG.md b/CHANGELOG.md old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 index 3167bfc..f151c85 --- a/README.md +++ b/README.md @@ -3,34 +3,29 @@ This binary C is for manage partitions of android devices. It offers a lot of options. I will place these below. But first let me talk about the operation... -``` -1. The partition name is obtained (with the -p or --partition argument) -2. Other arguments (if used) are processed. -3. The backup and flashing processes were written according to DD's code. So there is a piece of toybox code -``` - #### Presented arguments (options) ``` Usage: - -b | --backup backup mode - -F | --flash flash mode - -r | --format format mode (only ext2/3/4 file systems) - -p | --partition name of the partition to be backed up - -l | --logical know that the partition that will be backed up is logical - -o | --out (only backups) the output name of the backed-up partition (default: partition name) - -d | --outdir (only backups) directory where the backup partition will be saved (default: /storage/emulated/0) + pmt backup PARTITION [OUTPUT] [OPTIONS]... + pmt flash FILE PARTITION [OPTIONS]... + pmt format FILE_SYSTEM[ext/2/3/4] PARTITION [OPTIONS]... + +Options: + -l | --logical it is meant to determine whether the target partition is logical -c | --context it is meant to specify a custom /dev context. Only classic partitions (default: /dev/block/by-name) - -D | --list list partitions - -f | --force force mode. Output is not produced. Even if it's a mistake. But if the target is not a mode, the error is given. If you want to work stable, it is important to specify this option first. + -p | --list list partitions + -s | --silent information and warning messages are silenced in normal work. + -f | --force force mode. Error messages are silenced and some things are ignored. -v | --version see version --help see help message -L | --license see license Examples: - -b --partition boot_a -o boot_slot_a_image -d /sdcard/backup -c /dev/block/platform/bootdevice/by-name - --flash /sdcard/twrp/boot.img -p boot_a -c /dev/block/platform/bootdevice/by-name - -c /dev/block/platform/bootdevice/by-name --list + pmt backup boot_a -c /dev/block/platform/bootdevice/by-name + pmt flash /sdcard/twrp/boot.img boot_a -c /dev/block/platform/bootdevice/by-name + pmt format ext4 system_a --logical + pmt -c /dev/block/platform/bootdevice/by-name --list Report bugs to ``` @@ -39,9 +34,8 @@ Report bugs to - Feel free to ask any questions you want. - Packages are available in publications. -- İt is mandatory to use the `-b` | `--backup` or `-f` | `--flash` and `-p` | `--partition` argument. After all, a partition name and progress type is required to be progress. - If the logical partition flag is not used, a classic partition is tried to be processing by default. -- [Click to see special version changes](https://github.com/YZBruh/pbt/blob/2.0.0-en/CHANGELOG.md) +- [Click to see special version changes](https://github.com/YZBruh/pbt/blob/2.1.0-en/CHANGELOG.md) - Let me know your suggestions! ### How is it built? @@ -49,7 +43,7 @@ Android NDK is required to build. - [Download](https://developer.android.com/ndk/downloads) and extract the NDK package. - Clone this repository. And get access to it. ``` -git clone https://github.com/YZBruh/pbt -b 2.0.0 ./pmt +git clone https://github.com/YZBruh/pbt -b 2.1.0 ./pmt cd pmt ``` - Set the NDK working directory variable. diff --git a/debutils/DEBIAN/control_32 b/debutils/DEBIAN/control_32 index 6a3eb75..3507018 100755 --- a/debutils/DEBIAN/control_32 +++ b/debutils/DEBIAN/control_32 @@ -1,6 +1,6 @@ Source: pmt Package: pmt -Version: 1.2.0 +Version: 2.1.0 Architecture: arm Description: pmt is for reading, writing and formatting partitions of android devices Section: misc diff --git a/debutils/DEBIAN/control_64 b/debutils/DEBIAN/control_64 index ac6f7a1..a1c3da3 100755 --- a/debutils/DEBIAN/control_64 +++ b/debutils/DEBIAN/control_64 @@ -1,6 +1,6 @@ Source: pmt Package: pmt -Version: 2.0.0 +Version: 2.1.0 Architecture: aarch64 Description: pmt is for reading, writing and formatting partitions of android devices Section: misc diff --git a/debutils/mandoc/pmt.1 b/debutils/mandoc/pmt.1 deleted file mode 100755 index 6a3f042..0000000 --- a/debutils/mandoc/pmt.1 +++ /dev/null @@ -1,24 +0,0 @@ -PMT Android Partition Manager 1.8.0 - -OPTIONS: - -b | --backup backup mode - -F | --flash flash mode - -r | --format format mode (only ext2/3/4 file systems) - -p | --partition name of the partition to be backed up - -l | --logical know that the partition that will be backed up is logical - -o | --out (only backups) the output name of the backed-up partition (default: partition name) - -d | --outdir (only backups) directory where the backup partition will be saved (default: /storage/emulated/0) - -c | --context it is meant to specify a custom /dev context. Only classic partitions (default: /dev/block/by-name) - -D | --list list partitions - -f | --force force mode. Output is not produced. Even if it's a mistake. But if the target is not a mode, the error is given. If you want to work stable, it is important to specify this option first. - -v | --version see version - --help see help message - -L | --license see license - -EXAMPLES: - -b --partition boot_a -o boot_slot_a_image -d /sdcard/backup -c /dev/block/platform/bootdevice/by-name - --flash /sdcard/twrp/boot.img -p boot_a -c /dev/block/platform/bootdevice/by-name - -c /dev/block/platform/bootdevice/by-name --list - -BUGS: - Report bugs to diff --git a/debutils/mandoc/pmt.1.gz b/debutils/mandoc/pmt.1.gz new file mode 100755 index 0000000000000000000000000000000000000000..fc8f01ad7c3cd8fdeaae1571b7b111ad47a07cae GIT binary patch literal 889 zcmV-<1BUz`iwFqB@LFaB18{A0E-?V5Ro`#ZFc5yvUvb(KNNWOvCqPIk1c>M_H7PLI zgdE37ttGaxon`&+IXi9X+9Dybhc1r4`|j?$@7nO@0>&G-{yx^-ItbkHom3mm>$Qce zXIC#_LA2$TO94|NNX@hZdEkUoCJZZ~6D8AN*9>AsDE`~tT*4QojSIaBpTQFcxY%A@ zJbmpsHnM}3Y(Pj0onc7A4OF~gz#b@57PtT^Th(wPD1)BcwNJFA2|da6mP8hoAf8}^ z!c%pK1Y-AvyT7%y>Qxl=eIF8EbSQNlH3Qg0lk{CO%aRcmz7Jc8h8Y8z{pz^Jwu=oA zg_TiKV7(3LL24kiW_V|%tGw5!oTx9s$jbIaGtX0UV>K^2`$)Q7o!I{&0L3E$F5(Q* z3`Z4bX?E#ZzNY!7`6`F6@p2i@@-)d{zJz!4+50q4=QBKgfOvKdpVQgyO8jT^7Q5=+n1@RnQR z++t2!^QnR`{{)L^j+S`tw9P)jES@HTYAxK1AQq*TygYRsD?(TnKi?cc(&!r_=-daU zzl1(^`#hL6`4&$XlLX!RXrl@#?N0(DdN^R!5bK1G+%3LFp54$-cPug`CAy4kuayr7 zd0YP<@34<(%$Hy`#HwOR!EO4%N literal 0 HcmV?d00001 diff --git a/jni/Android.mk b/jni/Android.mk index 84bc3f3..5a2aeda 100755 --- a/jni/Android.mk +++ b/jni/Android.mk @@ -26,7 +26,8 @@ LOCAL_SRC_FILES = \ pmt.c \ versioner.c \ tools.c \ - checkers.c \ + partition_tool.c \ + root.c \ listpart.c \ docs.c diff --git a/jni/docs.c b/jni/docs.c index a2c6926..dee9ac0 100755 --- a/jni/docs.c +++ b/jni/docs.c @@ -23,6 +23,8 @@ extern "C" { #include #include +extern char* bin_name; + void licenses() { printf("Copyright 2024 Partition Manager\n"); @@ -39,23 +41,23 @@ void licenses() void help() { printf("Usage: \n"); - printf(" -b | --backup backup mode\n"); - printf(" -F | --flash flash mode\n"); - printf(" -r | --format format mode (only ext2/3/4 file systems)\n"); - printf(" -p | --partition name of the partition to be backed up\n"); - printf(" -l | --logical know that the partition that will be backed up is logical\n"); - printf(" -o | --out (only backups) the output name of the backed-up partition (default: partition name)\n"); - printf(" -d | --outdir (only backups) directory where the backup partition will be saved (default: /storage/emulated/0)\n"); + printf(" %s backup PARTITION [OUTPUT] [OPTIONS]...\n", bin_name); + printf(" %s flash FILE PARTITION [OPTIONS]...\n", bin_name); + printf(" %s format FILE_SYSTEM[ext/2/3/4] PARTITION [OPTIONS]...\n\n", bin_name); + printf("Options: \n"); + printf(" -l | --logical it is meant to determine whether the target partition is logical\n"); printf(" -c | --context it is meant to specify a custom /dev context. Only classic partitions (default: /dev/block/by-name)\n"); - printf(" -D | --list list partitions\n"); - printf(" -f | --force force mode. Output is not produced. Even if it's a mistake. But if the target is not a mode, the error is given. If you want to work stable, it is important to specify this option first.\n"); + printf(" -p | --list list partitions\n"); + printf(" -s | --silent information and warning messages are silenced in normal work.\n"); + printf(" -f | --force force mode. Error messages are silenced and some things are ignored.\n"); printf(" -v | --version see version\n"); printf(" --help see help message\n"); printf(" -L | --license see license\n\n"); printf("Examples:\n"); - printf(" -b --partition boot_a -o boot_slot_a_image -d /sdcard/backup -c /dev/block/platform/bootdevice/by-name\n"); - printf(" --flash /sdcard/twrp/boot.img -p boot_a -c /dev/block/platform/bootdevice/by-name\n"); - printf(" -c /dev/block/platform/bootdevice/by-name --list\n\n"); + printf(" %s backup boot_a -c /dev/block/platform/bootdevice/by-name\n", bin_name); + printf(" %s flash /sdcard/twrp/boot.img boot_a -c /dev/block/platform/bootdevice/by-name\n", bin_name); + printf(" %s format ext4 system_a --logical\n", bin_name); + printf(" %s -c /dev/block/platform/bootdevice/by-name --list\n\n", bin_name); printf("Report bugs to \n"); } diff --git a/jni/include/pmt-versioning.h b/jni/include/pmt-versioning.h index a813b36..43055be 100755 --- a/jni/include/pmt-versioning.h +++ b/jni/include/pmt-versioning.h @@ -22,8 +22,8 @@ extern "C" { /* versioning */ #define PMT_MAJOR 2 -#define PMT_LEVEL 0 -#define PMT_PATCH 0 +#define PMT_MINOR 1 +#define PMT_PATCHLEVEL 0 #if defined(__cplusplus) } diff --git a/jni/include/pmt.h b/jni/include/pmt.h index 21d2f68..c8c0ea6 100755 --- a/jni/include/pmt.h +++ b/jni/include/pmt.h @@ -32,27 +32,28 @@ extern "C" { #define PMT_PACKAGE_NAME "Partition Manager" /* variable definations */ -extern char *out; -extern char *outdir; -extern char *cust_cxt; -extern char *target_partition; -extern char *target_flash_file; -extern char *format_fs; -extern char *partition_type; +extern char* out; +extern char* cust_cxt; +extern char* target_partition; +extern char* target_flash_file; +extern char* format_fs; +extern char* partition_type; +extern char* bin_name; extern bool pmt_use_logical; extern bool pmt_use_cust_cxt; extern bool pmt_ab; extern bool pmt_logical; +extern bool pmt_silent; extern bool pmt_flash; extern bool pmt_backup; extern bool pmt_format; extern bool pmt_force_mode; /* function definations */ -void listpart(); -void check_psf(); +int listpart(); +void check_dev_point(); void check_root(); -void pmt(unsigned short progress_code); +int pmt(unsigned short progress_code); void version(); #endif /* __PMT_H_ */ diff --git a/jni/listpart.c b/jni/listpart.c index 099e664..82605c9 100755 --- a/jni/listpart.c +++ b/jni/listpart.c @@ -25,6 +25,8 @@ extern "C" { #include #include #include +#include +#include #include #include #include @@ -32,11 +34,13 @@ extern "C" { extern bool pmt_use_cust_cxt; extern bool pmt_ab; extern bool pmt_logical; +extern bool pmt_silent; extern bool pmt_force_mode; -extern char *cust_cxt; +extern char* cust_cxt; +extern char* bin_name; /* list existing partitions */ -void listpart() { +int listpart() { DIR *dir; struct dirent *entry; @@ -45,46 +49,49 @@ void listpart() { dir = opendir(cust_cxt); if (dir == NULL) { - if (!pmt_force_mode) { - fprintf(stderr, "Could not open: `%s`. Error reason: %s\n", cust_cxt, strerror(errno)); - exit(62); - } else exit(62); + if (!pmt_force_mode) errx(EX_OSFILE, "could not open: `%s': %s", cust_cxt, strerror(errno)); + else return EX_OSFILE; } - } else { + } + else + { dir = opendir("/dev/block/by-name"); if (dir == NULL) { - if (!pmt_force_mode) - { - fprintf(stderr, "Could not open: `/dev/block/by-name`. Error reason: %s\n", strerror(errno)); - exit(63); - } else exit(63); + if (!pmt_force_mode) errx(EX_OSFILE, "could not open: `/dev/block/by-name': %s", strerror(errno)); + else return EX_OSFILE; } } - while ((entry = readdir(dir)) != NULL) { - printf("%s\n", entry->d_name); - } + while ((entry = readdir(dir)) != NULL) printf("%s\n", entry->d_name); closedir(dir); if (pmt_logical) { - printf("List of logical partitions (/dev/block/mapper): \n"); - if (system("ls /dev/block/mapper") != 0 && !pmt_force_mode) + dir = opendir("/dev/block/mapper"); + + if (dir == NULL) { - fprintf(stderr, "%sAn error occurred when the logical partition list appears!%s\n", ANSI_RED, ANSI_RESET); - exit(64); + if (!pmt_silent) errx(EX_OSFILE, "could not open: `/dev/block/mapper': %s", strerror(errno)); + else return EX_OSFILE; } + else printf("List of logical partitions (`/dev/block/mapper'):\n"); } - if (pmt_ab && !pmt_force_mode) printf("%sWarning: device using A/B partition style.%s\n", ANSI_YELLOW, ANSI_RESET); + while ((entry = readdir(dir)) != NULL) printf("%s\n", entry->d_name); - if (pmt_logical && !pmt_force_mode) printf("%sWarning: device using logical partition type.%s\n", ANSI_YELLOW, ANSI_RESET); + closedir(dir); + + if (pmt_ab && !pmt_silent) warnx(ANSI_YELLOW "warning: device using A/B partition style." ANSI_RESET); + + if (pmt_logical && !pmt_silent) warnx(ANSI_YELLOW "warning: device using logical partition type." ANSI_RESET); + + return EX_OK; } #if defined(__cplusplus) } #endif /* __cplusplus */ -/* end of code */ +/* end of code */ \ No newline at end of file diff --git a/jni/checkers.c b/jni/partition_tool.c similarity index 62% rename from jni/checkers.c rename to jni/partition_tool.c index 41b28c1..b9e8fa3 100755 --- a/jni/checkers.c +++ b/jni/partition_tool.c @@ -20,32 +20,34 @@ extern "C" { #endif +#include +#include #include #include -#include -#include -#include -#include #include extern bool pmt_use_cust_cxt; extern bool pmt_ab; extern bool pmt_logical; -extern bool pmt_force_mode; -extern char *cust_cxt; +extern char* cust_cxt; + +static int +search(const char* _Nonnull target) { return access(target, F_OK); } /* check parts */ -void check_psf() +void check_dev_point() { /* true = ab | false = a */ if (pmt_use_cust_cxt) { static char cust_cxt_ck_path[150]; sprintf(cust_cxt_ck_path, "%s/boot_a", cust_cxt); - if (access(cust_cxt_ck_path, F_OK) != 0) pmt_ab = false; + if (search(cust_cxt_ck_path) != 0) pmt_ab = false; else pmt_ab = true; - } else { - if (access("/dev/block/by-name/boot_a", F_OK) != 0) pmt_ab = false; + } + else + { + if (search("/dev/block/by-name/boot_a") != 0) pmt_ab = false; else pmt_ab = true; } @@ -54,30 +56,16 @@ void check_psf() { static char cust_cxt_ckl_path[150]; sprintf(cust_cxt_ckl_path, "%s/super", cust_cxt); - if (access(cust_cxt_ckl_path, F_OK) != 0) pmt_logical = false; - else pmt_logical = true; - } else { - if (access("/dev/block/by-name/super", F_OK) != 0) pmt_logical = false; + if (search(cust_cxt_ckl_path) != 0) pmt_logical = false; else pmt_logical = true; } -} - -/* root checker function */ -void check_root() -{ - /* a quick, easy method to verify root :D */ - if (getuid() != 0) + else { - if (!pmt_force_mode) - { - fprintf(stderr, ANSI_RED "Root privileges could not be detected! Please run this binary with root. Error reason: %s\n" ANSI_RESET, strerror(errno)); - exit(27); - } else exit(27); + if (search("/dev/block/by-name/super") != 0) pmt_logical = false; + else pmt_logical = true; } } #if defined(__cplusplus) } -#endif /* __cplusplus */ - -/* end of code */ \ No newline at end of file +#endif /* __cplusplus */ \ No newline at end of file diff --git a/jni/pmt.c b/jni/pmt.c index faaaa47..1ef7686 100755 --- a/jni/pmt.c +++ b/jni/pmt.c @@ -30,47 +30,99 @@ extern "C" { #include #include #include +#include +#include #include #include /* add value to variables that are added globally and are not worth */ -char *out = NULL; -char *outdir = NULL; -char *cust_cxt = NULL; -char *target_partition = NULL; -char *target_flash_file = NULL; -char *partition_type = NULL; -char *format_fs = NULL; +char* out = NULL; +char* cust_cxt = NULL; +char* target_partition = NULL; +char* target_flash_file = NULL; +char* partition_type = NULL; +char* format_fs = NULL; +char* bin_name = NULL; bool pmt_use_logical = NULL; bool pmt_use_cust_cxt = NULL; bool pmt_ab = false; bool pmt_logical = false; +bool pmt_silent = false; bool pmt_flash = false; bool pmt_backup = false; bool pmt_format = false; bool pmt_force_mode = false; -/* classic main function (C binary here xd) */ -int main(int argc, char *argv[]) +/* variables for use in control of '-' expression */ +static const char* opt_symbol = "-"; +static const char* 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."; + +/** + * He controls whether the '-' sign at + * the beginning of the given word + */ +static void +check_optsym(const char* _Nonnull mystring) { - /* check argument total */ - if (argc < 2) + if (strncmp(mystring, opt_symbol, 1) == 0) { - fprintf(stderr, "%s: missing operand\nTry `%s --help' for more information.\n", argv[0], argv[0]); - exit(44); + if (!pmt_force_mode) errx(EX_USAGE, "%s", common_symbol_rule); + else exit(EX_USAGE); + } +} + +/** + * The target file is controlled by the stat function. + * Files, directories, links and blocks (disks) are for. + * If it is never found, it returns 1 value. + * If he finds 0 value is returned. + * If the desired type is not in -1 value is returned. + */ +static int +search_stat(const char* _Nonnull filepath, const char* _Nonnull stype) +{ + struct stat search_stat; + + if (stat(filepath, &search_stat) != 0) return 1; + + if (strcmp(stype, "dir") == 0) + { + if (S_ISDIR(search_stat.st_mode)) return 0; + else return -1; + } + else if (strcmp(stype, "file") == 0) + { + if (S_ISREG(search_stat.st_mode)) return 0; + else return -1; + } + else if (strcmp(stype, "blk") == 0) + { + if (S_ISBLK(search_stat.st_mode)) return 0; + else return -1; + } + else if (strcmp(stype, "link") == 0) + { + if (S_ISLNK(search_stat.st_mode)) return 0; + else return -1; } - /* a structure for long arguments... */ + return 0; +} + +/* classic main function (C binary here xd) */ +int main(int argc, char* argv[]) +{ + /* check argument total */ + if (argc < 2) errx(EX_USAGE, "missing operand\nTry `%s --help' for more information.", argv[0]); + + bin_name = argv[0]; + + /* a structure for long arguments */ struct option long_options[] = { - {"backup", no_argument, 0, 'b'}, - {"flash", required_argument, 0, 'F'}, - {"format", required_argument, 0, 'r'}, - {"partition", required_argument, 0, 'p'}, {"logical", no_argument, 0, 'l'}, - {"out", required_argument, 0, 'o'}, - {"outdir", required_argument, 0, 'd'}, {"context", required_argument, 0, 'c'}, - {"list", no_argument, 0, 'D'}, + {"list", no_argument, 0, 'p'}, + {"silent", no_argument, 0, 's'}, {"force", no_argument, 0, 'f'}, {"version", no_argument, 0, 'v'}, {"help", no_argument, 0, 0}, @@ -84,108 +136,38 @@ int main(int argc, char *argv[]) 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 *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."; - - int opt; + static int search_result = 3; + static int opt; /* 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, "lc:psfvL", long_options, NULL)) != -1) { /* process arguments */ switch (opt) { - /* backup mode */ - case 'b': - pmt_backup = true; - break; - /* flash mode */ - case 'F': - target_flash_file = strdup(optarg); - if (strncmp(target_flash_file, opt_symbol, 1) == 0) - { - if (!pmt_force_mode) - { - fprintf(stderr, "%s%s%s\n", ANSI_RED, common_symbol_rule, ANSI_RESET); - exit(19); - } else exit(19); - } - pmt_flash = true; - break; - /* format mode */ - case 'r': - format_fs = strdup(optarg); - if (strncmp(format_fs, opt_symbol, 1) == 0) - { - if (!pmt_force_mode) - { - fprintf(stderr, "%s%s%s\n", ANSI_RED, common_symbol_rule, ANSI_RESET); - exit(19); - } else exit(19); - } - pmt_format = true; - break; - /* partition selector option */ - case 'p': - target_partition = strdup(optarg); - if (strncmp(target_partition, opt_symbol, 1) == 0) - { - if (!pmt_force_mode) - { - fprintf(stderr, "%s%s%s\n", ANSI_RED, common_symbol_rule, ANSI_RESET); - exit(19); - } else exit(19); - } - break; /* logical partitions option */ case 'l': check_root(); - check_psf(); - if (pmt_logical) + check_dev_point(); + if (pmt_logical) pmt_use_logical = true; + else { - pmt_use_logical = true; - } else { - if (!pmt_force_mode) - { - fprintf(stderr, "This device does not have logical partitions!\n"); - exit(17); - } else exit(17); + if (!pmt_force_mode) errx(EX_USAGE, "this device does not have logical partitions!"); + else return EX_USAGE; } break; - /* output file option */ - case 'o': - out = strdup(optarg); - if (strncmp(out, opt_symbol, 1) == 0) - { - if (!pmt_force_mode) - { - fprintf(stderr, "%s%s%s\n", ANSI_RED, common_symbol_rule, ANSI_RESET); - exit(19); - } else exit(19); - } - break; - /* output dir option */ - case 'd': - use_cust_outdir = true; - 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) - { - fprintf(stderr, "%s%s%s\n", ANSI_RED, common_symbol_rule, ANSI_RESET); - exit(19); - } else exit(19); + if (!pmt_force_mode) errx(EX_USAGE, "%s", common_symbol_rule); + else return EX_USAGE; } break; /* partition lister function */ - case 'D': + case 'p': list_partitions = true; /* check combo wiewer options and progress */ if (wiew_version || wiew_help || wiew_licenses) combo_wiewers = true; @@ -194,6 +176,10 @@ int main(int argc, char *argv[]) case 'f': pmt_force_mode = true; break; + /* silent mode option */ + case 's': + pmt_silent = true; + break; /* version info option */ case 'v': wiew_version = true; @@ -215,185 +201,182 @@ int main(int argc, char *argv[]) /* for invalid options */ case '?': printf("Try `%s --help' for more information.\n", argv[0]); - exit(43); + return EX_USAGE; 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); + printf("Usage: %s [backup] flash] [format] [-l | --logical] [-c | --context] [-D | --list] [-v | --version] [--help] [-L | --license]\n", argv[0]); + exit(EX_USAGE); } } /* 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); - } + if (combo_wiewers) errx(EX_USAGE, "multiple wiewers cannot be used at the same line."); /* controller to handle viewer */ if (wiew_help) { help(); - exit(EXIT_SUCCESS); - } else if (wiew_version) + return EX_OK; + } + else if (wiew_version) { version(); - exit(EXIT_SUCCESS); - } else if (wiew_licenses) + return EX_OK; + } + else if (wiew_licenses) { licenses(); - exit(EXIT_SUCCESS); - } else if (list_partitions) + return EX_OK; + } + else if (list_partitions) { check_root(); - listpart(); - exit(EXIT_SUCCESS); + return listpart(); + } + + /* detect target mode */ + static char arg1[20]; + sprintf(arg1, "%s", argv[1]); + + if (strcmp(arg1, "backup") == 0) + { + if (argc == 2) + { + if (!pmt_force_mode) errx(EX_USAGE, "expected backup argument 2, retrieved 0."); + else return EX_USAGE; + } + + target_partition = argv[2]; + if (argc != 4) out = target_partition; + else out = argv[3]; + check_optsym(target_partition); + check_optsym(out); + + pmt_backup = true; + } + else if (strcmp(arg1, "flash") == 0) + { + if (argc == 2) + { + if (!pmt_force_mode) errx(EX_USAGE, "expected flash argument 2, retrieved 0."); + else return EX_USAGE; + } + + if (argc == 3) + { + if (!pmt_force_mode) errx(EX_USAGE, "expected flash argument 2, retrieved 0."); + else return EX_USAGE; + } + + target_flash_file = argv[2]; + target_partition = argv[3]; + check_optsym(target_flash_file); + check_optsym(target_partition); + + pmt_flash = true; + } + else if (strcmp(arg1, "format") == 0) + { + if (argc == 2) + { + if (!pmt_force_mode) errx(EX_USAGE, "expected format argument 2, retrieved 0."); + else return EX_USAGE; + } + + if (argc == 3) + { + if (!pmt_force_mode) errx(EX_USAGE, "expected format argument 2, retrieved 1."); + else return EX_USAGE; + } + + format_fs = argv[2]; + target_partition = argv[3]; + check_optsym(format_fs); + check_optsym(target_partition); + + pmt_format = true; } /* 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); - } + if (!pmt_backup && !pmt_flash && !pmt_format && !pmt_silent) errx(EX_USAGE, "missing operand.\nTry `%s --help` for more information.", argv[0]); + else return EX_USAGE; /* prevent multiple mode use */ - if (pmt_backup && pmt_flash) + if (pmt_backup && pmt_flash && pmt_format) { - if (!pmt_force_mode) - { - fprintf(stderr, "Backup and flash functions cannot be used in the same command.\n"); - exit(9); - } else exit(9); + if (!pmt_force_mode) errx(EX_USAGE, "multi functions cannot be used in the same command."); + else return EX_USAGE; } /* checks */ check_root(); - check_psf(); + check_dev_point(); 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) - { - fprintf(stderr, "%s\n", common_symbol_rule); - exit(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_force_mode) errx(EX_USAGE, "formatter: unsupported filesystem: %s", format_fs); + else return EX_USAGE; } } if (pmt_flash) { - struct stat flashf_info; - if (stat(target_flash_file, &flashf_info) != 0) + search_result = search_stat(target_flash_file, "file"); + + if (search_result == 1) { - 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); - } + if (!pmt_silent) errx(EX_UNAVAILABLE, "cannot stat `%s': %s", target_flash_file, strerror(errno)); + else return EX_UNAVAILABLE; + } + else if (search_result == -1) + { + if (!pmt_silent) errx(EX_USAGE, "`%s': is a not file.", target_flash_file); + else return EX_USAGE; } } /* custom context checker */ if (pmt_use_cust_cxt) { - struct stat cxtinfo; - if (stat(cust_cxt, &cxtinfo) == 0) + search_result = search_stat(cust_cxt, "dir"); + + if (search_result == 1) { - if (!S_ISREG(cxtinfo.st_mode)) - { - if (!pmt_force_mode) - { - fprintf(stderr, "%s: %s: is a not directory.\n", argv[0], cust_cxt); - exit(8); - } else exit(8); - } - } else { - if (!pmt_force_mode) - { - fprintf(stderr, "%s: %s: %s\n", argv[0], cust_cxt, strerror(errno)); - exit(6); - } else exit(6); + if (!pmt_silent) errx(EX_UNAVAILABLE, "cannot stat `%s': %s", cust_cxt, strerror(errno)); + else return EX_UNAVAILABLE; } - if (strstr(cust_cxt, "/dev") == NULL && !pmt_force_mode) + else if (search_result == -1) { - 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); - } + if (!pmt_silent) errx(EX_USAGE, "`%s': is a not directory.", cust_cxt); + else return EX_USAGE; + } + + if (strstr(cust_cxt, "/dev") == NULL && !pmt_force_mode) errx(EX_USAGE, ANSI_YELLOW "you're going through my wave? There's nothing about this /dev. Use force mode if you don't want this error." ANSI_RESET); } if (target_partition == NULL) { if (!pmt_force_mode) { - fprintf(stderr, "%s: required partition name.\nTry `%s --help' for more information.\n", argv[0], argv[0]); - exit(5); - } else exit(5); - } else { + if (!pmt_silent) errx(EX_USAGE, "required partition name.\nTry `%s --help' for more information.\n", argv[0]); + } + else return EX_USAGE; + } + else + { /** - * * 1 = backup mode * * 2 = flash mode * * 3 = format */ - if (pmt_backup) - { - pmt(1); - exit(EXIT_SUCCESS); - } else if (pmt_flash) - { - pmt(2); - exit(EXIT_SUCCESS); - } else if (pmt_format) - { - pmt(3); - } else { - fprintf(stderr, "%s: no target (backup or flash).\nTry `%s --help` for more information.\n", argv[0], argv[0]); - exit(3); - } + if (pmt_backup) return pmt(1); + else if (pmt_flash) return pmt(2); + else if (pmt_format) return pmt(3); + else if (!pmt_silent) errx(EX_USAGE, "no target (backup or flash).\nTry `%s --help` for more information.\n", argv[0]); } } @@ -401,4 +384,4 @@ int main(int argc, char *argv[]) } #endif -/* end of code */ +/* end of code */ \ No newline at end of file diff --git a/jni/root.c b/jni/root.c new file mode 100755 index 0000000..45d3cc5 --- /dev/null +++ b/jni/root.c @@ -0,0 +1,46 @@ +/* By YZBruh */ + +/** + * Copyright 2024 Partition Manager + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#if defined(__cplusplus) +extern "C" { +#endif + +#include +#include +#include +#include +#include +#include +#include + +/* root checker function */ +void check_root() +{ + /* a quick, easy method to verify root */ + if (getuid() != 0) + { + if (!pmt_force_mode) errx(EX_NOPERM, "root access could not be detected! Please run this binary with root."); + else exit(EX_NOPERM); + } +} + +#if defined(__cplusplus) +} +#endif /* __cplusplus */ + +/* end of code */ diff --git a/jni/tools.c b/jni/tools.c index 25dbd79..7a5347b 100755 --- a/jni/tools.c +++ b/jni/tools.c @@ -29,27 +29,28 @@ extern "C" { #include #include #include +#include +#include #include #include #define BFSIZE 1024 -extern char *out; -extern char *outdir; -extern char *format_fs; -extern char *cust_cxt; -extern char *target_partition; -extern char *target_flash_file; -extern char *partition_type; +extern char* out; +extern char* format_fs; +extern char* cust_cxt; +extern char* target_partition; +extern char* target_flash_file; +extern char* partition_type; extern bool pmt_use_logical; extern bool pmt_use_cust_cxt; extern bool pmt_logical; extern bool pmt_flash; extern bool pmt_backup; +extern bool pmt_silent; extern bool pmt_force_mode; /** - * * if progress_code is a * 1 = backup mode * @@ -58,14 +59,19 @@ extern bool pmt_force_mode; * 3 = format mode */ +/** + * it is meant to calculate the size of the quickly given file. + * its purpose is for rapid processing + */ static double -calc_flsz(char *filepath) +calc_flsz(const char* _Nonnull filepath) { static int calc_flsz_file; calc_flsz_file = open(filepath, O_RDONLY); - if (calc_flsz_file == -1) return -1; + if (calc_flsz_file == -1) return calc_flsz_file; - off_t flsz = lseek(calc_flsz_file, 0, SEEK_END); + static off_t flsz; + flsz = lseek(calc_flsz_file, 0, SEEK_END); close(calc_flsz_file); if (flsz == (off_t)-1) return -1; @@ -73,7 +79,85 @@ calc_flsz(char *filepath) return (double)flsz / (1024 * 1024); } -void pmt(unsigned short progress_code) +/** + * error that the partition is not found. + * It's for quick action. + */ +static int +partition_not_found() +{ + if (!pmt_silent) errx(EX_OSFILE, "partition not found!"); + else return EX_OSFILE; +} + +/** + * unknown partition type error. + * It's for quick action. + */ +static int +invalid_partition_type() +{ + if (!pmt_silent) errx(EX_USAGE, "invalid partition type!"); + else exit(EX_USAGE); + + return 0; +} + +/* to stop use of function type */ +#define partition_not_found partition_not_found() +#define invalid_partition_type invalid_partition_type() + +/** + * The target file is controlled by the stat function. + * Files, directories, links and blocks (disks) are for. + * If it is never found, it returns 1 value. + * If he finds 0 value is returned. + * If the desired type is not in -1 value is returned. + */ +static int +search_stat(const char* _Nonnull filepath, const char* _Nonnull stype) +{ + struct stat search_stat; + + if (stat(filepath, &search_stat) != 0) return 1; + + if (strcmp(stype, "dir") == 0) + { + if (S_ISDIR(search_stat.st_mode)) return 0; + else return -1; + } + else if (strcmp(stype, "file") == 0) + { + if (S_ISREG(search_stat.st_mode)) return 0; + else return -1; + } + else if (strcmp(stype, "blk") == 0) + { + if (S_ISBLK(search_stat.st_mode)) return 0; + else return -1; + } + else if (strcmp(stype, "link") == 0) + { + if (S_ISLNK(search_stat.st_mode)) return 0; + else return -1; + } + + return 0; +} + +/* the partitions are meant to quickly find. */ +static void +search_partition(const char* _Nonnull partition) +{ + static int partition_results = 0; + partition_results = search_stat(partition, "blk"); + + if (partition_results == 1) partition_not_found; + else if (partition_results == -1 && !pmt_silent) errx(EX_OSFILE, "the specified partition is not the block. I mean, it's not actually an episode (disc). I'm sure it needs to applaud those mistakes."); + else exit(EX_OSFILE); +} + +int pmt(unsigned short progress_code) { /* required variables */ static int srcf, targetf; @@ -83,8 +167,8 @@ void pmt(unsigned short progress_code) static char outf[512]; static char flasher_path[512]; static char buffer[BFSIZE]; - static ssize_t bytesRead; - static unsigned long long bytesCopied = 0; + static ssize_t readed_data; + static unsigned long long copied_data = 0; static unsigned long long count = 1024 * 1024 * 1024; if (progress_code == 1) @@ -93,85 +177,60 @@ void pmt(unsigned short progress_code) { if (pmt_use_cust_cxt) sprintf(backupper_path, "%s/%s", cust_cxt, target_partition); else sprintf(backupper_path, "/dev/block/by-name/%s", target_partition); - } else if (pmt_use_logical) sprintf(backupper_path, "/dev/block/mapper/%s", target_partition); - else { - if (!pmt_force_mode) - { - fprintf(stderr, "İnvalid partition type!\n"); - exit(28); - } else exit(28); } + else if (pmt_use_logical) sprintf(backupper_path, "/dev/block/mapper/%s", target_partition); + else return invalid_partition_type; - if (access(backupper_path, F_OK) == -1) - { - if (!pmt_force_mode) - { - fprintf(stderr, "Partition not found!\n"); - exit(29); - } else exit(29); - } + search_partition(backupper_path); - if (calc_flsz(backupper_path) != -1 && !pmt_force_mode) printf("Disk size of the partition to be backed up: %.2f\n", calc_flsz(backupper_path)); - else printf("%sFailed to target partition disk size%s\n", ANSI_YELLOW, ANSI_RESET); + if (calc_flsz(backupper_path) != -1 && !pmt_silent) printf("Disk size of the partition to be backed up: %.2f\n", calc_flsz(backupper_path)); + else warnx(ANSI_YELLOW "failed to get target partition disk size" ANSI_RESET); srcf = open(backupper_path, O_RDONLY); - if (srcf == -1) { - if (!pmt_force_mode) - { - fprintf(stderr, "Couldn't read: %s: %s", backupper_path, strerror(errno)); - exit(39); - } else exit(39); + if (srcf == -1) + { + if (!pmt_silent) errx(EX_OSFILE, "couldn't read: %s: %s", backupper_path, strerror(errno)); + else return EX_IOERR; } /* determine output */ - if (outdir != NULL) + if (strcmp(out, target_partition) == 0) { - if (out != NULL) sprintf(outf, "%s/%s.img", outdir, out); - else sprintf(outf, "%s/%s.img", outdir, target_partition); - } else { - if (out != NULL) sprintf(outf, "/storage/emulated/0/%s.img", out); - else sprintf(outf, "/storage/emulated/0/%s.img", target_partition); + sprintf(outf, "%s.img", target_partition); + if (!pmt_silent) warnx(ANSI_YELLOW "warning: The output file name was not specified. The output file name will be: %s" ANSI_RESET, outf); } + else sprintf(outf, "%s", target_partition); targetf = open(outf, O_WRONLY | O_CREAT | O_TRUNC, 0666); - if (targetf == -1) { - if (!pmt_force_mode) - { - fprintf(stderr, "Couldn't generate: %s: %s", outf, strerror(errno)); - exit(37); - } else exit(37); + if (targetf == -1) + { + if (!pmt_silent) errx(EX_CANTCREAT, "couldn't generate: %s: %s", outf, strerror(errno)); + else return EX_CANTCREAT; } - /* start writing */ - while ((bytesRead = read(srcf, buffer, BFSIZE)) > 0 && bytesCopied < count) + while ((readed_data = read(srcf, buffer, BFSIZE)) > 0 && copied_data < count) { - ssize_t bytesWritten = write(targetf, buffer, bytesRead); - if (bytesWritten != bytesRead) + ssize_t writed_data = write(targetf, buffer, readed_data); + if (writed_data != readed_data) { - fprintf(stderr, "Couldn't write: %s: %s", backupper_path, strerror(errno)); + if (!pmt_silent) warnx("couldn't write: %s: %s", backupper_path, strerror(errno)); close(srcf); close(targetf); - exit(81); + if (search_stat(outf, "file") == 0) remove(outf); + return EX_IOERR; } - bytesCopied += bytesWritten; + copied_data += writed_data; } /* close files */ close(srcf); close(targetf); - /* Print the output information by evaluating all situations */ - if (outdir != NULL) - { - if (out != NULL) printf("%sSuccess. Output: %s/%s.img%s\n", ANSI_GREEN, outdir, out, ANSI_RESET); - else printf("%sSuccess. Output: %s/%s.img%s\n", ANSI_GREEN, outdir, target_partition, ANSI_RESET); - } else { - if (out != NULL) printf("%sSuccess. Output: /storage/emulated/0/%s.img%s\n", ANSI_GREEN, out, ANSI_RESET); - else printf("%sSuccess. Output: /storage/emulated/0/%s.img%s\n", ANSI_GREEN, target_partition, ANSI_RESET); - } - } else if (progress_code == 2) + if (!pmt_silent) printf("%sSuccess. Output: %s%s\n", ANSI_GREEN, outf, ANSI_RESET); + } + else if (progress_code == 2) { /* determine device block */ /* for classic */ @@ -180,67 +239,57 @@ void pmt(unsigned short progress_code) if (pmt_use_cust_cxt) sprintf(flasher_path, "%s/%s", cust_cxt, target_partition); else sprintf(flasher_path, "/dev/block/by-name/%s", target_partition); /* for logical */ - } else if (pmt_use_logical) sprintf(flasher_path, "/dev/block/mapper/%s", target_partition); - else { - if (!pmt_force_mode) - { - fprintf(stderr, "İnvalid partition type!\n"); - exit(30); - } else exit(30); } + else if (pmt_use_logical) sprintf(flasher_path, "/dev/block/mapper/%s", target_partition); + else return invalid_partition_type; /* check partition */ - if (access(flasher_path, F_OK) == -1) - { - if (!pmt_force_mode) - { - fprintf(stderr, "Partition not found!\n"); - exit(31); - } else exit(31); - } + search_partition(flasher_path); if (calc_flsz(target_flash_file) != -1 && !pmt_force_mode) printf("Size of flash file: %.2f\n", calc_flsz(target_flash_file)); - else printf("%sFailed to get flash file size%s\n", ANSI_YELLOW, ANSI_RESET); + else warnx(ANSI_YELLOW "failed to get flash file size" ANSI_RESET); if (calc_flsz(target_partition) != -1 && !pmt_force_mode) printf("Disk size of the target partition: %.2f\n", calc_flsz(target_partition)); - else printf("%sFailed to get target partition disk size%s\n", ANSI_YELLOW, ANSI_RESET); + else warnx(ANSI_YELLOW "failed to get target partition disk size" ANSI_RESET); + + if (calc_flsz(target_flash_file) > calc_flsz(target_partition) && !pmt_silent) errx(EX__BASE, "size of the file to flash more than the partition size."); + else return EX__BASE; srcf = open(target_flash_file, O_RDONLY); - if (srcf == -1) { - if (!pmt_force_mode) - { - fprintf(stderr, "Couldn't read: %s: %s", target_flash_file, strerror(errno)); - exit(39); - } else exit(39); + if (srcf == -1) + { + if (!pmt_force_mode) errx(EX_NOINPUT, "couldn't read: %s: %s", target_flash_file, strerror(errno)); + else return EX_IOERR; } targetf = open(target_partition, O_WRONLY | O_CREAT | O_TRUNC, 0666); - if (targetf == -1) { - if (!pmt_force_mode) - { - fprintf(stderr, "Couldn't read: %s: %s", target_partition, strerror(errno)); - exit(37); - } else exit(37); + if (targetf == -1) + { + if (!pmt_force_mode) errx(EX_OSFILE, "couldn't read: %s: %s", target_partition, strerror(errno)); + else return EX_IOERR; } /* start writing */ - while ((bytesRead = read(srcf, buffer, BFSIZE)) > 0 && bytesCopied < count) { - ssize_t bytesWritten = write(targetf, buffer, bytesRead); - if (bytesWritten != bytesRead) { - fprintf(stderr, "Couldn't write: %s: %s", backupper_path, strerror(errno)); + while ((readed_data = read(srcf, buffer, BFSIZE)) > 0 && copied_data < count) + { + ssize_t writed_data = write(targetf, buffer, readed_data); + if (writed_data != readed_data) + { + warnx("couldn't write: %s: %s", backupper_path, strerror(errno)); close(srcf); close(targetf); - exit(81); + return EX_IOERR; } - bytesCopied += bytesWritten; + copied_data += writed_data; } close(srcf); close(targetf); if (!pmt_force_mode) printf("%sSuccess.%s\n", ANSI_GREEN, ANSI_RESET); - } else if (progress_code == 3) + } + else if (progress_code == 3) { /* generate partition extn */ if (!pmt_use_logical) @@ -248,33 +297,19 @@ void pmt(unsigned short progress_code) if (pmt_use_cust_cxt) sprintf(ppath, "%s/%s", cust_cxt, target_partition); else sprintf(ppath, "/dev/block/by-name/%s", target_partition); /* for logical */ - } else if (pmt_use_logical) sprintf(ppath, "/dev/block/mapper/%s", target_partition); - else { - if (!pmt_force_mode) - { - fprintf(stderr, "İnvalid partition type!\n"); - exit(49); - } else exit(49); } + else if (pmt_use_logical) sprintf(ppath, "/dev/block/mapper/%s", target_partition); + else return invalid_partition_type; /* check partition */ - if (access(ppath, F_OK) == -1) - { - if (!pmt_force_mode) - { - fprintf(stderr, "Partition not found!\n"); - } else exit(31); - } + search_partition(ppath); /* get target partition block size */ struct statvfs file_sys_inf; if (statvfs(ppath, &file_sys_inf) != 0) { - if (!pmt_force_mode) - { - fprintf(stderr, "The partition block size could not be obtained!\n"); - exit(49); - } else exit(49); + if (!pmt_force_mode) errx(EX_TEMPFAIL, "the partition block size could not be obtained!"); + else return EX_TEMPFAIL; } /* generate mke2fs command */ @@ -283,13 +318,11 @@ void pmt(unsigned short progress_code) /* run command */ if (system(formatter_cmd) != 0) { - if (!pmt_force_mode) - { - fprintf(stderr, "Formatting failed! There may be a chance that something has been damaged!\n"); - exit(71); - } else exit(71); + if (!pmt_force_mode) errx(EX_TEMPFAIL, "formatting failed! There may be a chance that something has been damaged!"); + else return EX_TEMPFAIL; } } + return 0; } #if defined(__cplusplus) diff --git a/jni/versioner.c b/jni/versioner.c index 06b6957..46311e6 100755 --- a/jni/versioner.c +++ b/jni/versioner.c @@ -21,24 +21,25 @@ extern "C" { #endif #include +#include #include +extern char* bin_name; + void version() { - printf("Version: %d.%d.%d (code %d%d%d) ", PMT_MAJOR, PMT_LEVEL, PMT_PATCH, PMT_MAJOR, PMT_LEVEL, PMT_PATCH); + printf("%s version %d.%d.%d (code %d%d%d) ", bin_name, PMT_MAJOR, PMT_MINOR, PMT_PATCHLEVEL, PMT_MAJOR, PMT_MINOR, PMT_PATCHLEVEL); #if __SIZEOF_POINTER__ == 4 printf("32-bit binary\n"); #elif __SIZEOF_POINTER__ == 8 printf("64-bit binary\n"); #else - printf("\n"); + printf(" binary\n"); #endif #if defined(__clang__) - printf("Compiler: clang %s\n", __clang_version__); - #elif defined(__gcc__) - printf("Compiler: gcc %s\n", __gcc_version__) + printf("Compiler: clang %d.%d.%d (NDK major r%d)\n", __clang_major__, __clang_minor__, __clang_patchlevel__, __NDK_MAJOR__); #endif printf("See licenses with -L argument.\n"); diff --git a/make-deb.sh b/make-deb.sh index a21331d..a764c4a 100755 --- a/make-deb.sh +++ b/make-deb.sh @@ -15,39 +15,65 @@ # See the License for the specific language governing permissions and # limitations under the License. +# ANSI color codes RED='\e[31m' NC='\e[0m' +# error messages abort() { - printf "${RED}$1${NC}" + if [ ! "$1" = "" ]; then + printf "${RED}${1}${NC}\n" + fi + if [ -d ${DEBUTILS_DIR}/temp ]; then + rm -rf ${DEB_DIR}/temp + fi exit 1 } -case $1 in +# detect arch flag +case "$1" in arm64-v8a) PREFIX="64" + ARM_PREFIX="-v8a" ;; armeabi-v7a) PREFIX="32" + ARM_PREFIX="" ;; *) - abort " - Error: unknown architecture flag: $1. Avaiable: arm64-v8a & armeabi-v7a\n" + abort " - Error: unknown architecture flag: $1. Avaiable: arm64-v8a & armeabi-v7a.\n" esac -VERSION="2.0.0" +case "$2" in + sudo) + SUDO="sudo" + ;; + no-sudo) + SUDO="" + ;; + *) + SUDO="" +esac + +# variables +VERSION="2.1.0" CUR_DIR=$(pwd) LIB_DIR=${CUR_DIR}/libs -ARMV8A_DIR=${OUT_DIR}/arm64-v8a -ARMV7A_DIR=${OUT_DIR}/armeabi-v7a -DEB_DIR=${OUT_DIR}/deb +ARMV8A_DIR=${LIB_DIR}/arm64-v8a +ARMV7A_DIR=${LIB_DIR}/armeabi-v7a +DEB_DIR=${CUR_DIR}/deb DEBUTILS_DIR=${CUR_DIR}/debutils DEBTERMUX_USR=${DEBUTILS_DIR}/data/data/com.termux/files/usr -chmod -R 755 * +# set file modes (all) to 755 +${SUDO} chmod -R 755 * -printf " --------- Making pmt deb package ---------\n"; +# start +printf " --------- pmt deb package maker ---------\n"; printf " - Checking all files and directories (only eededs)...\n"; + +# check all important files if [ ! -d ${DEBUTILS_DIR} ]; then abort " - Not found: ${DEBUTILS_DIR}\n" fi @@ -80,37 +106,49 @@ if [ ! -f ${DEBUTILS_DIR}/DEBIAN/control_64 ]; then fi if [ ! -f ${ARMV8A_DIR}/pmt ]; then abort " - Package not comptely builded! Please build package and try again\n" -elif [ ! -f ${ARMV7A_DIR}/pmt ]; then +elif [ ! -f ${ARMV7A_DIR}/pmt ]; then abort " - Package not comptely builded! Please build package and try again\n" fi +# generate template dir printf " - Generating template dir...\n" -mkdir -p ${DEBUTILS_DIR}/temp +${SUDO} mkdir -p ${DEBUTILS_DIR}/temp || abort +# generate out dir printf " - Generating out dir...\n" -mkdir -p ${DEB_DIR} +${SUDO} mkdir -p ${DEB_DIR} || abort +# copy files printf " - Copying files...\n" -cp -r ${DEBUTILS_DIR}/data ${DEBUTILS_DIR}/temp || exit 1 -rm -f ${DEBTERMUX_USR}/share/man/man1/dummy -rm -f ${DEBTERMUX_USR}/bin/dummy -mkdir -p ${DEBUTILS_DIR}/temp/DEBIAN +${SUDO} cp -r ${DEBUTILS_DIR}/data ${DEBUTILS_DIR}/temp || abort +${SUDO} rm -f ${DEBTERMUX_USR}/share/man/man1/dummy +${SUDO} rm -f ${DEBTERMUX_USR}/bin/dummy +${SUDO} mkdir -p ${DEBUTILS_DIR}/temp/DEBIAN || abort +# select control file printf " - Selected arm-${PREFIX} package control file.\n" -cp ${DEBUTILS_DIR}/DEBIAN/control_${PREFIX} ${DEBUTILS_DIR}/temp/DEBIAN/control || exit 1 -cp ${DEBUTILS_DIR}/mandoc/pmt.1 ${DEBTERMUX_USR}/share/man/man1 || exit 1 -if [ "${PREFIX}" = "64" ]; then - cp ${ARMV8A_DIR}/pmt ${DEBTERMUX_USR}/bin || exit 1 -elif [ "${PREFIX}" = "32" ]; then - cp ${ARMV7A_DIR}/pmt ${DEBTERMUX_USR}/bin || exit 1 +${SUDO} cp ${DEBUTILS_DIR}/DEBIAN/control_${PREFIX} ${DEBUTILS_DIR}/temp/DEBIAN/control || exit 1 +${SUDO} cp ${DEBUTILS_DIR}/mandoc/pmt.1.gz ${DEBTERMUX_USR}/share/man/man1 || abort +if [ "${PREFIX}" = "64" ]; then + ${SUDO} cp ${ARMV8A_DIR}/pmt ${DEBTERMUX_USR}/bin || abort +elif [ "${PREFIX}" = "32" ]; then + ${SUDO} cp ${ARMV7A_DIR}/pmt ${DEBTERMUX_USR}/bin || abort fi +# start dpkg-deb printf " - Starting dpkg-deb...\n" sleep 2 -chmod -R 755 * -dpkg-deb -b ${DEBUTILS_DIR}/temp ${DEB_DIR}/pmt-${VERSION}-arm${PREFIX}.deb || exit 1; -rm -rf ${DEBUTILS_DIR}/temp || exit 1; +${SUDO} chmod -R 755 * -printf " - Done! Package: ${DEB_DIR}/pmt-${VERSION}.deb\n" +# if ARM_PREFIX is '-v7a', unset ARM_PREFIX and PREFIX. Re set PREFIX +if [ "${PREFIX}" = "32" ]; then + unset PREFIX + PREFIX="eabi-v7a" +fi -# end of script \ No newline at end of file +${SUDO} dpkg-deb -b ${DEBUTILS_DIR}/temp ${DEB_DIR}/pmt-${VERSION}-arm${PREFIX}${ARM_PREFIX}.deb || abort +${SUDO} rm -rf ${DEBUTILS_DIR}/temp + +printf " - Done! Package: ${DEB_DIR}/pmt-${VERSION}-arm${PREFIX}${ARM_PREFIX}.deb\n" + +# end of script