summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2022-11-30 11:40:54 +0100
committerYorhel <git@yorhel.nl>2022-11-30 11:44:08 +0100
commit6dd159d4898b43ef61749cc45f2f71437ab5b0e3 (patch)
tree7a5fddd1ba7296d0aded56d5d7e26bf534f3700e
parent49e4b061a56eeb319e3037f89252678c779156d0 (diff)
Backport --(enable|disable)-(shell|delete|refresh) from 2.x
Only remaining options missing from this C implementation are --graph-style (sounds doable, but pretty low priority) and --shared-column (unlikely to happen).
-rw-r--r--doc/ncdu.pod34
-rw-r--r--src/browser.c24
-rw-r--r--src/global.h6
-rw-r--r--src/main.c17
4 files changed, 51 insertions, 30 deletions
diff --git a/doc/ncdu.pod b/doc/ncdu.pod
index 5d18805..f00d378 100644
--- a/doc/ncdu.pod
+++ b/doc/ncdu.pod
@@ -167,22 +167,29 @@ decreased to once every 2 seconds with C<-q> or C<--slow-ui-updates>. This
feature can be used to save bandwidth over remote connections. This option has
no effect when C<-0> is used.
-=item B<-r>
+=item B<--enable-shell>, B<--disable-shell>
+
+Enable or disable shell spawning from the browser. This feature is enabled by
+default when scanning a live directory and disabled when importing from file.
+
+=item B<--enable-delete>, B<--disable-delete>
-Read-only mode. This will disable the built-in file deletion feature. This
-option has no effect when C<-o> is used, because there will not be a browser
-interface in that case. It has no effect when C<-f> is used, either, because
-the deletion feature is disabled in that case anyway.
+Enable or disable the built-in file deletion feature. This feature is enabled
+by default when scanning a live directory and disabled when importing from
+file. Explicitly disabling the deletion feature can work as a safeguard to
+prevent accidental data loss.
-WARNING: This option will only prevent deletion through the file browser. It is
-still possible to spawn a shell from ncdu and delete or modify files from
-there. To disable that feature as well, pass the C<-r> option twice (see
-C<-rr>).
+=item B<--enable-refresh>, B<--disable-refresh>
-=item B<-rr>
+Enable or disable directory refreshing from the browser. This feature is
+enabled by default when scanning a live directory and disabled when importing
+from file.
-In addition to C<-r>, this will also disable the shell spawning feature of the
-file browser.
+=item B<-r>
+
+Read-only mode. When given once, this is an alias for C<--disable-delete>, when
+given twice it will also add C<--disable-shell>, thus ensuring that there is no
+way to modify the file system from within ncdu.
=item B<--si>, B<--no-si>
@@ -272,6 +279,9 @@ starting with C<#> are ignored. Example configuration file:
# Always enable extended mode
-e
+ # Disable file deletion
+ --disable-delete
+
# Exclude .git directories
--exclude .git
diff --git a/src/browser.c b/src/browser.c
index 91d7757..2e29ee8 100644
--- a/src/browser.c
+++ b/src/browser.c
@@ -272,7 +272,7 @@ void browse_draw(void) {
addstrc(UIC_HD, " for help");
if(dir_import_active)
mvaddstr(0, wincols-10, "[imported]");
- else if(read_only)
+ else if(!can_delete)
mvaddstr(0, wincols-11, "[read-only]");
/* second line - the path */
@@ -323,7 +323,7 @@ void browse_draw(void) {
if(message) {
nccreate(6, 60, "Message");
ncaddstr(2, 2, message);
- ncaddstr(4, 34, "Press any key to continue");
+ ncaddstr(4, 33, "Press any key to continue");
}
/* draw information window */
@@ -488,8 +488,8 @@ int browse_key(int ch) {
/* and other stuff */
case 'r':
- if(dir_import_active) {
- message = "Directory imported from file, won't refresh.";
+ if(!can_refresh) {
+ message = "Directory refresh feature disabled.";
break;
}
if(dirlist_par) {
@@ -527,14 +527,12 @@ int browse_key(int ch) {
info_show = 0;
break;
case 'd':
- if(read_only >= 1 || dir_import_active) {
- message = read_only >= 1
- ? "File deletion disabled in read-only mode."
- : "File deletion not available for imported directories.";
- break;
- }
if(sel == NULL || sel == dirlist_parent)
break;
+ if(!can_delete) {
+ message = "Deletion feature disabled.";
+ break;
+ }
info_show = 0;
if((t = dirlist_get(1)) == sel)
if((t = dirlist_get(-1)) == sel || t == dirlist_parent)
@@ -542,10 +540,8 @@ int browse_key(int ch) {
delete_init(sel, t);
break;
case 'b':
- if(read_only >= 2 || dir_import_active) {
- message = read_only >= 2
- ? "Shell feature disabled in read-only mode."
- : "Shell feature not available for imported directories.";
+ if(!can_shell) {
+ message = "Shell feature disabled.";
break;
}
shell_init();
diff --git a/src/global.h b/src/global.h
index 573e685..9d86958 100644
--- a/src/global.h
+++ b/src/global.h
@@ -95,8 +95,10 @@ struct dir_ext {
/* program state */
extern int pstate;
-/* read-only flag, 1+ = disable deletion, 2+ = also disable shell */
-extern int read_only;
+/* enabled features */
+extern int can_delete;
+extern int can_shell;
+extern int can_refresh;
/* minimum screen update interval when calculating, in ms */
extern long update_delay;
/* filter directories with CACHEDIR.TAG */
diff --git a/src/main.c b/src/main.c
index 222514d..cc2ac30 100644
--- a/src/main.c
+++ b/src/main.c
@@ -35,7 +35,9 @@
int pstate;
-int read_only = 0;
+int can_delete = -1;
+int can_shell = -1;
+int can_refresh = -1;
long update_delay = 100;
int cachedir_tags = 0;
int extended_info = 0;
@@ -197,7 +199,14 @@ static int arg_option(void) {
else if(OPT("--cross-file-system")) dir_scan_smfs = 0;
else if(OPT("-e") || OPT("--extended")) extended_info = 1;
else if(OPT("--no-extended")) extended_info = 0;
- else if(OPT("-r")) read_only++;
+ else if(OPT("-r") && !can_delete) can_shell = 0;
+ else if(OPT("-r")) can_delete = 0;
+ else if(OPT("--enable-shell")) can_shell = 1;
+ else if(OPT("--disable-shell")) can_shell = 0;
+ else if(OPT("--enable-delete")) can_delete = 1;
+ else if(OPT("--disable-delete")) can_delete = 0;
+ else if(OPT("--enable-refresh")) can_refresh = 1;
+ else if(OPT("--disable-refresh")) can_refresh = 0;
else if(OPT("--show-hidden")) dirlist_hidden = 0;
else if(OPT("--hide-hidden")) dirlist_hidden = 1;
else if(OPT("--show-itemcount")) show_items = 1;
@@ -406,6 +415,10 @@ static void argv_parse(int argc, char **argv) {
* feedback when exporting to stdout. */
if(dir_ui == -1)
dir_ui = export && strcmp(export, "-") == 0 ? 0 : export ? 1 : 2;
+
+ if(can_delete == -1) can_delete = import ? 0 : 1;
+ if(can_shell == -1) can_shell = import ? 0 : 1;
+ if(can_refresh == -1) can_refresh = import ? 0 : 1;
}