ncdu-zig/src/delete.c

250 lines
5.5 KiB
C

/* ncdu - NCurses Disk Usage
Copyright (c) 2007-2010 Yoran Heling
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "global.h"
#include <string.h>
#include <errno.h>
#include <unistd.h>
#define DS_CONFIRM 0
#define DS_PROGRESS 1
#define DS_FAILED 2
compll_t root, nextsel, curdir;
char noconfirm = 0,
ignoreerr = 0,
state, seloption;
int lasterrno;
void delete_draw_confirm() {
nccreate(6, 60, "Confirm delete");
ncprint(1, 2, "Are you sure you want to delete \"%s\"%c",
cropstr(DR(root)->name, 21), DR(root)->flags & FF_DIR ? ' ' : '?');
if(DR(root)->flags & FF_DIR)
ncprint(2, 18, "and all of its contents?");
if(seloption == 0)
attron(A_REVERSE);
ncaddstr(4, 15, "yes");
attroff(A_REVERSE);
if(seloption == 1)
attron(A_REVERSE);
ncaddstr(4, 24, "no");
attroff(A_REVERSE);
if(seloption == 2)
attron(A_REVERSE);
ncaddstr(4, 31, "don't ask me again");
attroff(A_REVERSE);
ncmove(4, seloption == 0 ? 15 : seloption == 1 ? 24 : 31);
}
void delete_draw_progress() {
nccreate(6, 60, "Deleting...");
ncaddstr(1, 2, cropstr(getpath(curdir), 47));
ncaddstr(4, 41, "Press q to abort");
}
void delete_draw_error() {
nccreate(6, 60, "Error!");
ncprint(1, 2, "Can't delete %s:", cropstr(getpath(curdir), 42));
ncaddstr(2, 4, strerror(lasterrno));
if(seloption == 0)
attron(A_REVERSE);
ncaddstr(4, 14, "abort");
attroff(A_REVERSE);
if(seloption == 1)
attron(A_REVERSE);
ncaddstr(4, 23, "ignore");
attroff(A_REVERSE);
if(seloption == 2)
attron(A_REVERSE);
ncaddstr(4, 33, "ignore all");
attroff(A_REVERSE);
}
void delete_draw() {
browse_draw();
switch(state) {
case DS_CONFIRM: delete_draw_confirm(); break;
case DS_PROGRESS: delete_draw_progress(); break;
case DS_FAILED: delete_draw_error(); break;
}
}
int delete_key(int ch) {
/* confirm */
if(state == DS_CONFIRM)
switch(ch) {
case KEY_LEFT:
case 'h':
if(--seloption < 0)
seloption = 0;
break;
case KEY_RIGHT:
case 'l':
if(++seloption > 2)
seloption = 2;
break;
case '\n':
if(seloption == 1)
return 1;
if(seloption == 2)
noconfirm++;
state = DS_PROGRESS;
break;
case 'q':
return 1;
}
/* processing deletion */
else if(state == DS_PROGRESS)
switch(ch) {
case 'q':
return 1;
}
/* error */
else if(state == DS_FAILED)
switch(ch) {
case KEY_LEFT:
case 'h':
if(--seloption < 0)
seloption = 0;
break;
case KEY_RIGHT:
case 'l':
if(++seloption > 2)
seloption = 2;
break;
case 10:
if(seloption == 0)
return 1;
if(seloption == 2)
ignoreerr++;
state = DS_PROGRESS;
break;
case 'q':
return 1;
}
return 0;
}
int delete_dir(compll_t dr) {
compll_t nxt, cur;
int r;
/* check for input or screen resizes */
curdir = dr;
if(input_handle(1))
return 1;
/* do the actual deleting */
if(DR(dr)->flags & FF_DIR) {
if((r = chdir(DR(dr)->name)) < 0)
goto delete_nxt;
if(DR(dr)->sub) {
nxt = DR(dr)->sub;
while(nxt) {
cur = nxt;
nxt = DR(cur)->next;
if(delete_dir(cur))
return 1;
}
}
if((r = chdir("..")) < 0)
goto delete_nxt;
r = DR(dr)->sub ? 0 : rmdir(DR(dr)->name);
} else
r = unlink(DR(dr)->name);
delete_nxt:
/* error occured, ask user what to do */
if(r == -1 && !ignoreerr) {
state = DS_FAILED;
lasterrno = errno;
curdir = dr;
while(state == DS_FAILED)
if(input_handle(0))
return 1;
} else if(!(DR(dr)->flags & FF_DIR && DR(dr)->sub)) {
freedir(dr);
return 0;
}
return root == dr ? 1 : 0;
}
void delete_process() {
/* confirm */
seloption = 1;
while(state == DS_CONFIRM && !noconfirm)
if(input_handle(0)) {
browse_init(root);
return;
}
/* chdir */
if(path_chdir(getpath(DR(root)->parent)) < 0) {
state = DS_FAILED;
lasterrno = errno;
while(state == DS_FAILED)
if(input_handle(0))
return;
}
/* delete */
seloption = 0;
state = DS_PROGRESS;
if(delete_dir(root))
browse_init(root);
else {
DW(nextsel)->flags |= FF_BSEL;
browse_init(nextsel);
dirlist_top(-4);
}
}
void delete_init(compll_t dr, compll_t s) {
state = DS_CONFIRM;
root = curdir = dr;
pstate = ST_DEL;
nextsel = s;
}