path-util: retain directory-ness of a trailing "/." in path_simplify()

With PATH_SIMPLIFY_KEEP_TRAILING_SLASH path_simplify() retains a
trailing slash, since it indicates that the path must refer to a
directory. A trailing "/." indicates the same, but so far it was simply
dropped, losing that information. Turn it into a trailing slash instead.

(A trailing ".." is not dropped by path_simplify(), hence needs no
special handling.)
This commit is contained in:
Lennart Poettering authored and Lennart Poettering committed 2026-11-05 18:54:40 +01:00
1 parent bc97dd19ef
commit fd39acb784
2 files changed
+10 -1

No files matched your search

+3 -1
View File
@@ -369,7 +369,9 @@ char* path_simplify_full(char *path, PathSimplifyFlags flags) {
if (isempty(path))
return path;
keep_trailing_slash = FLAGS_SET(flags, PATH_SIMPLIFY_KEEP_TRAILING_SLASH) && endswith(path, "/");
/* A trailing "/." is dropped below just like a trailing slash, but carries the same meaning
* (i.e. "this must be a directory"), hence turn it into a trailing slash too, if requested. */
keep_trailing_slash = FLAGS_SET(flags, PATH_SIMPLIFY_KEEP_TRAILING_SLASH) && ENDSWITH_SET(path, "/", "/.");
absolute = path_is_absolute(path);
f += absolute; /* Keep leading /, if present. */
+7
View File
@@ -198,6 +198,13 @@ TEST(path_simplify) {
test_path_simplify_one("/../abc///..", "/abc/..", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
test_path_simplify_one("/../abc///../", "/abc/../", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
test_path_simplify_one("/../abc///../", "/abc/..", /* flags= */ 0);
test_path_simplify_one("/abc/.", "/abc/", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
test_path_simplify_one("/abc/./", "/abc/", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
test_path_simplify_one("/abc//.//.", "/abc/", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
test_path_simplify_one("/abc/.", "/abc", /* flags= */ 0);
test_path_simplify_one("/.", "/", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
test_path_simplify_one("./.", "./", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
test_path_simplify_one(".", ".", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
test_path_simplify_one("/../../abc", "/abc", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
test_path_simplify_one("/../../abc///", "/abc/", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);