diff options
author | Sébastien Helleu <flashcode@flashtux.org> | 2024-02-10 10:23:40 +0100 |
---|---|---|
committer | Sébastien Helleu <flashcode@flashtux.org> | 2024-02-10 10:23:40 +0100 |
commit | caa51160dad483be2dfc323856e366331e9d0630 (patch) | |
tree | 30fcb525474a1de7a37c135eb1eed6e0e2f32474 | |
parent | 5c6e6f43d112941e077c87777a773bfc070a180d (diff) | |
download | weechat-caa51160dad483be2dfc323856e366331e9d0630.zip |
core: remove trailing directory separators in home directories (closes #2070)
-rw-r--r-- | ChangeLog.adoc | 1 | ||||
-rw-r--r-- | src/core/wee-dir.c | 28 | ||||
-rw-r--r-- | tests/unit/core/test-core-dir.cpp | 38 |
3 files changed, 67 insertions, 0 deletions
diff --git a/ChangeLog.adoc b/ChangeLog.adoc index 51a6b8eb6..f536b11d3 100644 --- a/ChangeLog.adoc +++ b/ChangeLog.adoc @@ -24,6 +24,7 @@ New features:: Bug fixes:: + * core: remove trailing directory separators in home directories (issue #2070) * irc: fix random date displayed when a received message contains tags but no "time" (issue #2064) * scripts: fix crash on script unload when a hook is created in a buffer close callback (issue #2067) diff --git a/src/core/wee-dir.c b/src/core/wee-dir.c index 373868c82..d3c6ed426 100644 --- a/src/core/wee-dir.c +++ b/src/core/wee-dir.c @@ -630,6 +630,29 @@ use_xdg: } /* + * Removes trailing separators in path, which is modified in place + * (each trailing separator is replaced by NUL char). + * + * Example on Linux: "/home/xxx/" => "/home/xxx". + */ + +void +dir_remove_trailing_separators (char *path) +{ + int length; + + if (!path || !path[0]) + return; + + length = strlen (path); + while ((length > 1) && (path[length - 1] == DIR_SEPARATOR_CHAR)) + { + path[length - 1] = '\0'; + length--; + } +} + +/* * Creates a home directory. * * Returns: @@ -681,6 +704,11 @@ dir_create_home_dirs () if (!dir_find_home_dirs ()) goto error; + dir_remove_trailing_separators (weechat_config_dir); + dir_remove_trailing_separators (weechat_data_dir); + dir_remove_trailing_separators (weechat_cache_dir); + dir_remove_trailing_separators (weechat_runtime_dir); + rc = dir_create_home_dir (weechat_config_dir); if (rc && (strcmp (weechat_config_dir, weechat_data_dir) != 0)) rc = dir_create_home_dir (weechat_data_dir); diff --git a/tests/unit/core/test-core-dir.cpp b/tests/unit/core/test-core-dir.cpp index 01aec5998..eee3093c5 100644 --- a/tests/unit/core/test-core-dir.cpp +++ b/tests/unit/core/test-core-dir.cpp @@ -29,6 +29,8 @@ extern "C" #include <sys/time.h> #include "src/core/wee-dir.h" #include "src/core/wee-string.h" + +extern void dir_remove_trailing_separators (char *path); } TEST_GROUP(CoreDir) @@ -164,6 +166,42 @@ TEST(CoreDir, SetHomePath) /* * Tests functions: + * dir_remove_trailing_separators + */ + +TEST(CoreDir, RemoveTrailingSeparators) +{ + char path[128]; + + dir_remove_trailing_separators (NULL); + + snprintf (path, sizeof (path), ""); + dir_remove_trailing_separators (path); + STRCMP_EQUAL("", path); + + snprintf (path, sizeof (path), "/"); + dir_remove_trailing_separators (path); + STRCMP_EQUAL("/", path); + + snprintf (path, sizeof (path), "///"); + dir_remove_trailing_separators (path); + STRCMP_EQUAL("/", path); + + snprintf (path, sizeof (path), "/tmp"); + dir_remove_trailing_separators (path); + STRCMP_EQUAL("/tmp", path); + + snprintf (path, sizeof (path), "/tmp/"); + dir_remove_trailing_separators (path); + STRCMP_EQUAL("/tmp", path); + + snprintf (path, sizeof (path), "/tmp///////"); + dir_remove_trailing_separators (path); + STRCMP_EQUAL("/tmp", path); +} + +/* + * Tests functions: * dir_create_home_temp_dir */ |