From a2a5aacf63d7f8016de1591c3160e99af9879ff1 Mon Sep 17 00:00:00 2001 From: anon Date: Fri, 24 Jan 2025 14:58:02 +0100 Subject: [PATCH] implement custom deletion --- README.md | 2 +- source/file_utils.c | 28 ++++++++++++++++++++++++---- source/file_utils.h | 2 +- test/CMDTEST_vimdir.rb | 16 ++++++++++++++++ test/trash.sh | 2 ++ 5 files changed, 44 insertions(+), 6 deletions(-) create mode 100755 test/trash.sh diff --git a/README.md b/README.md index 9c8e07c..0aa02a7 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,6 @@ its (changed) value is ignored. - [X] change owner - [ ] swapping - [ ] copying -- [ ] specify the deletion method (so trash can be supported) +- [X] specify the deletion method (so trash can be supported) - [X] use `${VIMDIREDITOR}` - [X] `NO_COLOR` / color diff --git a/source/file_utils.c b/source/file_utils.c index 7492ad0..5cf6496 100644 --- a/source/file_utils.c +++ b/source/file_utils.c @@ -19,6 +19,8 @@ extern char * trim_trailing_slashes(char * path) { return path; } +static const char * custom_rm = NULL; + int (*mytouch)(const char *filename) = NULL; int (*mydelete)(const char *filename) = NULL; int (*mychmod)(const char *filename, mode_t mode) = NULL; @@ -36,8 +38,9 @@ static int moist_delete(const char * filename); static int moist_chmod(const char * filename, mode_t mode); static int moist_chown(const char * filename, const char * owner, const char * group); static int moist_move(const char * filename, const char * newname); +int init_file_utils(bool is_dry_run, const char * custom_rm_) { + custom_rm = custom_rm_; -int init_file_utils(bool is_dry_run) { if (is_dry_run) { mytouch = dry_touch; mydelete = dry_delete; @@ -62,6 +65,8 @@ int deinit_file_utis() { mychown = NULL; mymove = NULL; + custom_rm = NULL; + return 0; } @@ -173,9 +178,24 @@ int moist_touch(const char * filename) { static int moist_delete(const char * filename) { - if (unlink(filename) != 0) { - errorn(E_FILE_DELETE, filename); - return 1; + if (custom_rm) { + size_t cmd_len = strlen(custom_rm) + sizeof(' ') + strlen(filename) + 1; + char cmd[cmd_len]; + + snprintf(cmd, cmd_len, "%s %s", custom_rm, filename); + + int result = system(cmd); + if (result == 127 + || result == -1 + || (WIFEXITED(result) && WEXITSTATUS(result) != 0)) { + errorn(E_FILE_DELETE, filename); + return 1; + } + } else { + //if (unlink(filename) != 0) { + // errorn(E_FILE_DELETE, filename); + // return 1; + //} } return 0; } diff --git a/source/file_utils.h b/source/file_utils.h index 675f2eb..c4c9177 100644 --- a/source/file_utils.h +++ b/source/file_utils.h @@ -4,7 +4,7 @@ #include #include -extern int init_file_utils(bool is_dry_run); +extern int init_file_utils(bool is_dry_run, const char * custom_rm_); extern int deinit_file_utis(); extern char * trim_trailing_slashes(char * path); diff --git a/test/CMDTEST_vimdir.rb b/test/CMDTEST_vimdir.rb index f755396..fa2dce0 100644 --- a/test/CMDTEST_vimdir.rb +++ b/test/CMDTEST_vimdir.rb @@ -66,6 +66,7 @@ class CMDTEST_mydir < Cmdtest::Testcase import_file "test/replacer.sh", "./" import_file "test/saver.sh", "./" import_file "test/memoryhole.sh", "./" + import_file "test/trash.sh", "./" import_directory "test/mydir/", "./mydir/" end @@ -152,6 +153,21 @@ class CMDTEST_mydir < Cmdtest::Testcase end end + def test_del_custom + File.write('target.txt', + [ + "000\t./mydir/.gitkeep", + "002\t./mydir/script.sh" + ].join("\n") + ) + + cmd "VIMDIRRM=./trash.sh EDITOR=./replacer.sh vimdir ./mydir/" do + exit_zero + created_files ["vimdir_test_file.vimdir", "mydir/file.txt.trash"] + removed_files ["target.txt", "mydir/file.txt"] + end + end + def test_swapped_order_noop File.write('target.txt', [ diff --git a/test/trash.sh b/test/trash.sh new file mode 100755 index 0000000..5b5f0bb --- /dev/null +++ b/test/trash.sh @@ -0,0 +1,2 @@ +#!/bin/sh +mv $1 $1.trash