diff options
| author | 2024-02-01 02:36:12 +0100 | |
|---|---|---|
| committer | 2024-02-01 02:36:12 +0100 | |
| commit | 73b6fd183872096f20e6d1007429546a317a7cb1 (patch) | |
| tree | 719d87b8bd8b74be134fb774a90fbe921ae3d39e | |
| parent | 3c88fafdb79070bd714347d822bb1e034ea76254 (diff) | |
| download | libimobiledevice-73b6fd183872096f20e6d1007429546a317a7cb1.tar.gz libimobiledevice-73b6fd183872096f20e6d1007429546a317a7cb1.tar.bz2 | |
tools/afcclient: Allow removing non-empty directories with -r
| -rw-r--r-- | tools/afcclient.c | 116 |
1 files changed, 84 insertions, 32 deletions
diff --git a/tools/afcclient.c b/tools/afcclient.c index 3975b31..9bcd77b 100644 --- a/tools/afcclient.c +++ b/tools/afcclient.c | |||
| @@ -114,6 +114,36 @@ static void print_usage(int argc, char **argv, int is_error) | |||
| 114 | ); | 114 | ); |
| 115 | } | 115 | } |
| 116 | 116 | ||
| 117 | #ifndef HAVE_READLINE | ||
| 118 | #ifdef WIN32 | ||
| 119 | #define BS_CC '\b' | ||
| 120 | #else | ||
| 121 | #define BS_CC 0x7f | ||
| 122 | #define getch getchar | ||
| 123 | #endif | ||
| 124 | static void get_input(char *buf, int maxlen) | ||
| 125 | { | ||
| 126 | int len = 0; | ||
| 127 | int c; | ||
| 128 | |||
| 129 | while ((c = getch())) { | ||
| 130 | if ((c == '\r') || (c == '\n')) { | ||
| 131 | break; | ||
| 132 | } | ||
| 133 | if (isprint(c)) { | ||
| 134 | if (len < maxlen-1) | ||
| 135 | buf[len++] = c; | ||
| 136 | } else if (c == BS_CC) { | ||
| 137 | if (len > 0) { | ||
| 138 | fputs("\b \b", stdout); | ||
| 139 | len--; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | } | ||
| 143 | buf[len] = 0; | ||
| 144 | } | ||
| 145 | #endif | ||
| 146 | |||
| 117 | #define OPT_DOCUMENTS 1 | 147 | #define OPT_DOCUMENTS 1 |
| 118 | #define OPT_CONTAINER 2 | 148 | #define OPT_CONTAINER 2 |
| 119 | 149 | ||
| @@ -576,15 +606,67 @@ static void handle_link(afc_client_t afc, int argc, char** argv) | |||
| 576 | } | 606 | } |
| 577 | } | 607 | } |
| 578 | 608 | ||
| 609 | static int ask_yesno(const char* prompt) | ||
| 610 | { | ||
| 611 | int ret = 0; | ||
| 612 | #ifdef HAVE_READLINE | ||
| 613 | char* result = readline(prompt); | ||
| 614 | if (result && result[0] == 'y') { | ||
| 615 | ret = 1; | ||
| 616 | } | ||
| 617 | #else | ||
| 618 | char cmdbuf[2] = {0, }; | ||
| 619 | printf("%s", prompt); | ||
| 620 | fflush(stdout); | ||
| 621 | get_input(cmdbuf, sizeof(cmdbuf)); | ||
| 622 | if (cmdbuf[0] == 'y') { | ||
| 623 | ret = 1; | ||
| 624 | } | ||
| 625 | #endif | ||
| 626 | #ifdef HAVE_READLINE | ||
| 627 | free(result); | ||
| 628 | #endif | ||
| 629 | return ret; | ||
| 630 | } | ||
| 631 | |||
| 579 | static void handle_remove(afc_client_t afc, int argc, char** argv) | 632 | static void handle_remove(afc_client_t afc, int argc, char** argv) |
| 580 | { | 633 | { |
| 581 | for (int i = 0; i < argc; i++) { | 634 | int recursive = 0; |
| 635 | int force = 0; | ||
| 636 | int i = 0; | ||
| 637 | for (i = 0; i < argc; i++) { | ||
| 638 | if (!strcmp(argv[i], "--")) { | ||
| 639 | i++; | ||
| 640 | break; | ||
| 641 | } else if (!strcmp(argv[i], "-r")) { | ||
| 642 | recursive = 1; | ||
| 643 | } else if (!strcmp(argv[i], "-f")) { | ||
| 644 | force = 1; | ||
| 645 | } else if (!strcmp(argv[i], "-rf") || !strcmp(argv[i], "-fr")) { | ||
| 646 | recursive = 1; | ||
| 647 | force = 1; | ||
| 648 | } else { | ||
| 649 | break; | ||
| 650 | } | ||
| 651 | } | ||
| 652 | if (recursive && !force) { | ||
| 653 | if (!ask_yesno("WARNING: This operation will remove all contents of the given path(s). Continue? [y/N] ")) { | ||
| 654 | printf("Aborted.\n"); | ||
| 655 | return; | ||
| 656 | } | ||
| 657 | } | ||
| 658 | for ( ; i < argc; i++) { | ||
| 582 | char* abspath = get_absolute_path(argv[i]); | 659 | char* abspath = get_absolute_path(argv[i]); |
| 583 | if (!abspath) { | 660 | if (!abspath) { |
| 584 | printf("Error: Invalid argument '%s'\n", argv[i]); | 661 | printf("Error: Invalid argument '%s'\n", argv[i]); |
| 585 | continue; | 662 | continue; |
| 586 | } | 663 | } |
| 587 | afc_error_t err = afc_remove_path(afc, abspath); | 664 | afc_error_t err; |
| 665 | if (recursive) { | ||
| 666 | err = afc_remove_path_and_contents(afc, abspath); | ||
| 667 | } else { | ||
| 668 | err = afc_remove_path(afc, abspath); | ||
| 669 | } | ||
| 588 | if (err != AFC_E_SUCCESS) { | 670 | if (err != AFC_E_SUCCESS) { |
| 589 | printf("Error: Failed to remove '%s': %s (%d)\n", argv[i], afc_strerror(err), err); | 671 | printf("Error: Failed to remove '%s': %s (%d)\n", argv[i], afc_strerror(err), err); |
| 590 | } | 672 | } |
| @@ -857,36 +939,6 @@ static void handle_cd(afc_client_t afc, int argc, char** argv) | |||
| 857 | curdir_len = strlen(curdir); | 939 | curdir_len = strlen(curdir); |
| 858 | } | 940 | } |
| 859 | 941 | ||
| 860 | #ifndef HAVE_READLINE | ||
| 861 | #ifdef WIN32 | ||
| 862 | #define BS_CC '\b' | ||
| 863 | #else | ||
| 864 | #define BS_CC 0x7f | ||
| 865 | #define getch getchar | ||
| 866 | #endif | ||
| 867 | static void get_input(char *buf, int maxlen) | ||
| 868 | { | ||
| 869 | int len = 0; | ||
| 870 | int c; | ||
| 871 | |||
| 872 | while ((c = getch())) { | ||
| 873 | if ((c == '\r') || (c == '\n')) { | ||
| 874 | break; | ||
| 875 | } | ||
| 876 | if (isprint(c)) { | ||
| 877 | if (len < maxlen-1) | ||
| 878 | buf[len++] = c; | ||
| 879 | } else if (c == BS_CC) { | ||
| 880 | if (len > 0) { | ||
| 881 | fputs("\b \b", stdout); | ||
| 882 | len--; | ||
| 883 | } | ||
| 884 | } | ||
| 885 | } | ||
| 886 | buf[len] = 0; | ||
| 887 | } | ||
| 888 | #endif | ||
| 889 | |||
| 890 | static void parse_cmdline(int* p_argc, char*** p_argv, const char* cmdline) | 942 | static void parse_cmdline(int* p_argc, char*** p_argv, const char* cmdline) |
| 891 | { | 943 | { |
| 892 | char **argv = NULL; | 944 | char **argv = NULL; |
