gutils: Strip all trailing slahes in load_user_special_dirs

Do what the comment states and strip all trailing slashes. Also, do not
strip the trailing slash if it's the only character left, i.e. if it
denotes the root directory.
This commit is contained in:
Tobias Stoeckmann
2025-09-11 19:22:04 +02:00
parent 2e88bd5fdd
commit 43d9e50654
2 changed files with 8 additions and 5 deletions

View File

@@ -2379,12 +2379,11 @@ load_user_special_dirs (void)
*d = 0;
d = p;
/* remove trailing slashes */
len = strlen (d);
if (d[len - 1] == '/')
for (len = strlen (d); len > 1 && d[len - 1] == '/'; len--)
d[len - 1] = 0;
if (is_relative)
{
const gchar *home_dir = g_get_home_dir_unlocked ();

View File

@@ -209,7 +209,8 @@ test_load_user_special_dirs (void)
result = g_file_set_contents (user_dirs_file,
"XDG_DESKTOP_DIR = \"/root\"\nXDG_DESKTOP_DIR = \"$HOMER/Desktop\"\n"
"XDG_DOCUMENTS_DIR = \"$HOME\"\n"
"XDG_DOWNLOAD_DIR = \"$HOME/Downloads\"\n",
"XDG_DOWNLOAD_DIR = \"$HOME/Downloads\"\n"
"XDG_MUSIC_DIR = \"///\"\n",
-1, NULL);
g_assert_true (result);
g_free (user_dirs_file);
@@ -226,6 +227,9 @@ test_load_user_special_dirs (void)
dir = g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD);
g_assert_cmpstr (dir, ==, expected);
g_free (expected);
dir = g_get_user_special_dir (G_USER_DIRECTORY_MUSIC);
g_assert_cmpstr (dir, ==, "/");
}
else
{