From 3d34b453dd0a1031543b2d2770553a4f065ee1be Mon Sep 17 00:00:00 2001 From: anon Date: Thu, 30 Jan 2025 13:44:57 +0100 Subject: [PATCH] fix filename space related issues --- source/directive.c | 2 +- source/file_utils.c | 15 +++++--- test/CMDTEST_vimdir.rb | 49 ++++++++++++++++++++++++++ test/myspacedir/laptop/with a spacebar | 0 test/myspacedir/my space.txt | 1 + test/myspacedir/space bar/wine | 0 6 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 test/myspacedir/laptop/with a spacebar create mode 100644 test/myspacedir/my space.txt create mode 100644 test/myspacedir/space bar/wine diff --git a/source/directive.c b/source/directive.c index 49fa360..cbf986a 100644 --- a/source/directive.c +++ b/source/directive.c @@ -290,7 +290,7 @@ int execute_directive_file(FILE * f) { // Name (move) if (!entry->is_mentioned) { - CHECK_FORMAT(1, sscanf(sp, "%s\n", buffer)); + CHECK_FORMAT(1, sscanf(sp, "%[^\n]", buffer)); size_t len = strlen(buffer); if (buffer[len-1] == '/') { buffer[len-1] = '\0'; diff --git a/source/file_utils.c b/source/file_utils.c index 5654cf3..f1f2d82 100644 --- a/source/file_utils.c +++ b/source/file_utils.c @@ -227,10 +227,15 @@ int moist_delete(const char * filename) { if (access(filename, F_OK)) { return 0; } if (custom_rm) { - size_t cmd_len = strlen(custom_rm) + sizeof(' ') + strlen(filename) + 1; + size_t cmd_len = strlen(custom_rm) + + sizeof(' ') + + sizeof('\'')*2 + + strlen(filename) + + 1 + ; char cmd[cmd_len]; - snprintf(cmd, cmd_len, "%s %s", custom_rm, filename); + snprintf(cmd, cmd_len, "%s '%s'", custom_rm, filename); int result = system(cmd); if (result == 127 @@ -298,13 +303,13 @@ int moist_copy(const char * filename, const char * newname) { // Is using system for copying terrible? yes. // Do I have know a better solution thats not filled with footguns? no. size_t cmd_len = strlen("cp -a") - + sizeof(' ') + strlen(filename) - + sizeof(' ') + strlen(newname) + + sizeof(' ') + sizeof('\'')*2 + strlen(filename) + + sizeof(' ') + sizeof('\'')*2 + strlen(newname) + 1 ; char cmd[cmd_len]; - snprintf(cmd, cmd_len, "cp -a %s %s", filename, newname); + snprintf(cmd, cmd_len, "cp -a '%s' '%s'", filename, newname); int result = system(cmd); if (result == 127 diff --git a/test/CMDTEST_vimdir.rb b/test/CMDTEST_vimdir.rb index 246c01d..76a02b7 100644 --- a/test/CMDTEST_vimdir.rb +++ b/test/CMDTEST_vimdir.rb @@ -465,3 +465,52 @@ class CMDTEST_myswapdir < Cmdtest::Testcase end end end + + + +# ___ _ _ +# / __|_ __ __ _ __ ___ __| (_)_ _ +# \__ \ '_ \/ _` / _/ -_) _` | | '_| +# |___/ .__/\__,_\__\___\__,_|_|_| +# |_| +class CMDTEST_myswapdir < Cmdtest::Testcase + def setup + import_file "test/replacer.sh", "./" + import_file "test/saver.sh", "./" + import_directory "test/myspacedir/", "./myspacedir/" + end + + def test_space_contents + expected = [ + "000\t./myspacedir/laptop/", + "001\t./myspacedir/laptop/with a spacebar", + "002\t./myspacedir/my space.txt", + "003\t./myspacedir/space bar/", + "004\t./myspacedir/space bar/wine", + ] + + cmd "EDITOR=./saver.sh vimdir -n -r ./myspacedir/" do + exit_zero + created_files ["output.txt"] + file_equal "output.txt", expected + end + end + + def test_space_copy + File.write('target.txt', + [ + "000\t./myspacedir/laptop/", + "001\t./myspacedir/laptop/with a spacebar", + "002\t./myspacedir/my space.txt", + "002\t./myspacedir/myspace.txt", + "003\t./myspacedir/space bar/", + "004\t./myspacedir/space bar/wine", + ].join("\n") + ) + + cmd "EDITOR=./replacer.sh vimdir -r ./myspacedir/" do + exit_zero + created_files ["myspacedir/myspace.txt"] + end + end +end diff --git a/test/myspacedir/laptop/with a spacebar b/test/myspacedir/laptop/with a spacebar new file mode 100644 index 0000000..e69de29 diff --git a/test/myspacedir/my space.txt b/test/myspacedir/my space.txt new file mode 100644 index 0000000..8834002 --- /dev/null +++ b/test/myspacedir/my space.txt @@ -0,0 +1 @@ +wait, that still exists? diff --git a/test/myspacedir/space bar/wine b/test/myspacedir/space bar/wine new file mode 100644 index 0000000..e69de29