Skip to content

Commit 86d3c3f

Browse files
committed
tools/afcclient: Allow removing non-empty directories with -r
1 parent 3c88faf commit 86d3c3f

File tree

1 file changed

+84
-32
lines changed

1 file changed

+84
-32
lines changed

tools/afcclient.c

Lines changed: 84 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,36 @@ static void print_usage(int argc, char **argv, int is_error)
114114
);
115115
}
116116

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+
117147
#define OPT_DOCUMENTS 1
118148
#define OPT_CONTAINER 2
119149

@@ -576,15 +606,67 @@ static void handle_link(afc_client_t afc, int argc, char** argv)
576606
}
577607
}
578608

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(cmd);
628+
#endif
629+
return ret;
630+
}
631+
579632
static void handle_remove(afc_client_t afc, int argc, char** argv)
580633
{
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++) {
582659
char* abspath = get_absolute_path(argv[i]);
583660
if (!abspath) {
584661
printf("Error: Invalid argument '%s'\n", argv[i]);
585662
continue;
586663
}
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+
}
588670
if (err != AFC_E_SUCCESS) {
589671
printf("Error: Failed to remove '%s': %s (%d)\n", argv[i], afc_strerror(err), err);
590672
}
@@ -857,36 +939,6 @@ static void handle_cd(afc_client_t afc, int argc, char** argv)
857939
curdir_len = strlen(curdir);
858940
}
859941

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-
890942
static void parse_cmdline(int* p_argc, char*** p_argv, const char* cmdline)
891943
{
892944
char **argv = NULL;

0 commit comments

Comments
 (0)