summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDaan De Meyer <daan.j.demeyer@gmail.com>2023-08-17 14:55:03 +0200
committerGitHub <noreply@github.com>2023-08-17 14:55:03 +0200
commitbe7d808c1c865a57c919d0dc5ffd6418dcc632f3 (patch)
treeb30e0ccce69a3a32d8f865423f3d2302437d26ed /src
parentMerge pull request #28751 from yuwata/mount-revert (diff)
parentrepart: Make sure we keep trailing slashes in ExcludeFiles= (diff)
downloadsystemd-be7d808c1c865a57c919d0dc5ffd6418dcc632f3.tar.xz
systemd-be7d808c1c865a57c919d0dc5ffd6418dcc632f3.zip
Merge pull request #28869 from DaanDeMeyer/repart-trailing
repart: Make sure we keep trailing slashes in ExcludeFiles=
Diffstat (limited to 'src')
-rw-r--r--src/basic/path-util.c9
-rw-r--r--src/basic/path-util.h9
-rw-r--r--src/partition/repart.c2
-rw-r--r--src/shared/parse-helpers.c2
-rw-r--r--src/shared/parse-helpers.h7
-rw-r--r--src/test/test-path-util.c43
6 files changed, 44 insertions, 28 deletions
diff --git a/src/basic/path-util.c b/src/basic/path-util.c
index a4c9d332f0..7204c80e79 100644
--- a/src/basic/path-util.c
+++ b/src/basic/path-util.c
@@ -344,8 +344,8 @@ char **path_strv_resolve_uniq(char **l, const char *root) {
return strv_uniq(l);
}
-char *path_simplify(char *path) {
- bool add_slash = false;
+char *path_simplify_full(char *path, PathSimplifyFlags flags) {
+ bool add_slash = false, keep_trailing_slash;
char *f = ASSERT_PTR(path);
int r;
@@ -359,6 +359,8 @@ char *path_simplify(char *path) {
if (isempty(path))
return path;
+ keep_trailing_slash = FLAGS_SET(flags, PATH_SIMPLIFY_KEEP_TRAILING_SLASH) && endswith(path, "/");
+
if (path_is_absolute(path))
f++;
@@ -388,6 +390,9 @@ char *path_simplify(char *path) {
if (f == path)
*f++ = '.';
+ if (*(f-1) != '/' && keep_trailing_slash)
+ *f++ = '/';
+
*f = '\0';
return path;
}
diff --git a/src/basic/path-util.h b/src/basic/path-util.h
index 038cd8226d..507aec27c5 100644
--- a/src/basic/path-util.h
+++ b/src/basic/path-util.h
@@ -76,7 +76,14 @@ char* path_extend_internal(char **x, ...);
#define path_extend(x, ...) path_extend_internal(x, __VA_ARGS__, POINTER_MAX)
#define path_join(...) path_extend_internal(NULL, __VA_ARGS__, POINTER_MAX)
-char* path_simplify(char *path);
+typedef enum PathSimplifyFlags {
+ PATH_SIMPLIFY_KEEP_TRAILING_SLASH = 1 << 0,
+} PathSimplifyFlags;
+
+char *path_simplify_full(char *path, PathSimplifyFlags flags);
+static inline char* path_simplify(char *path) {
+ return path_simplify_full(path, 0);
+}
static inline bool path_equal_ptr(const char *a, const char *b) {
return !!a == !!b && (!a || path_equal(a, b));
diff --git a/src/partition/repart.c b/src/partition/repart.c
index 06494dd5b9..198e698c79 100644
--- a/src/partition/repart.c
+++ b/src/partition/repart.c
@@ -1477,7 +1477,7 @@ static int config_parse_exclude_files(
return 0;
}
- r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
+ r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE|PATH_KEEP_TRAILING_SLASH, unit, filename, line, lvalue);
if (r < 0)
return 0;
diff --git a/src/shared/parse-helpers.c b/src/shared/parse-helpers.c
index f48baf7146..9664b9c773 100644
--- a/src/shared/parse-helpers.c
+++ b/src/shared/parse-helpers.c
@@ -40,7 +40,7 @@ int path_simplify_and_warn(
lvalue, fatal ? "" : ", ignoring", path);
}
- path_simplify(path);
+ path_simplify_full(path, flag & PATH_KEEP_TRAILING_SLASH ? PATH_SIMPLIFY_KEEP_TRAILING_SLASH : 0);
if (!path_is_valid(path))
return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EINVAL),
diff --git a/src/shared/parse-helpers.h b/src/shared/parse-helpers.h
index 38a47e85c3..3e4ad3c0a1 100644
--- a/src/shared/parse-helpers.h
+++ b/src/shared/parse-helpers.h
@@ -4,9 +4,10 @@
#include <stdint.h>
enum {
- PATH_CHECK_FATAL = 1 << 0, /* If not set, then error message is appended with 'ignoring'. */
- PATH_CHECK_ABSOLUTE = 1 << 1,
- PATH_CHECK_RELATIVE = 1 << 2,
+ PATH_CHECK_FATAL = 1 << 0, /* If not set, then error message is appended with 'ignoring'. */
+ PATH_CHECK_ABSOLUTE = 1 << 1,
+ PATH_CHECK_RELATIVE = 1 << 2,
+ PATH_KEEP_TRAILING_SLASH = 1 << 3,
};
int path_simplify_and_warn(
diff --git a/src/test/test-path-util.c b/src/test/test-path-util.c
index 31e2a3d296..8c3a27f228 100644
--- a/src/test/test-path-util.c
+++ b/src/test/test-path-util.c
@@ -48,11 +48,11 @@ TEST(path) {
assert_se(!path_equal_ptr(NULL, "/a"));
}
-static void test_path_simplify_one(const char *in, const char *out) {
+static void test_path_simplify_one(const char *in, const char *out, PathSimplifyFlags flags) {
char *p;
p = strdupa_safe(in);
- path_simplify(p);
+ path_simplify_full(p, flags);
log_debug("/* test_path_simplify(%s) → %s (expected: %s) */", in, p, out);
assert_se(streq(p, out));
}
@@ -61,34 +61,37 @@ TEST(path_simplify) {
_cleanup_free_ char *hoge = NULL, *hoge_out = NULL;
char foo[NAME_MAX * 2];
- test_path_simplify_one("", "");
- test_path_simplify_one("aaa/bbb////ccc", "aaa/bbb/ccc");
- test_path_simplify_one("//aaa/.////ccc", "/aaa/ccc");
- test_path_simplify_one("///", "/");
- test_path_simplify_one("///.//", "/");
- test_path_simplify_one("///.//.///", "/");
- test_path_simplify_one("////.././///../.", "/../..");
- test_path_simplify_one(".", ".");
- test_path_simplify_one("./", ".");
- test_path_simplify_one(".///.//./.", ".");
- test_path_simplify_one(".///.//././/", ".");
+ test_path_simplify_one("", "", 0);
+ test_path_simplify_one("aaa/bbb////ccc", "aaa/bbb/ccc", 0);
+ test_path_simplify_one("//aaa/.////ccc", "/aaa/ccc", 0);
+ test_path_simplify_one("///", "/", 0);
+ test_path_simplify_one("///", "/", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
+ test_path_simplify_one("///.//", "/", 0);
+ test_path_simplify_one("///.//.///", "/", 0);
+ test_path_simplify_one("////.././///../.", "/../..", 0);
+ test_path_simplify_one(".", ".", 0);
+ test_path_simplify_one("./", ".", 0);
+ test_path_simplify_one("./", "./", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
+ test_path_simplify_one(".///.//./.", ".", 0);
+ test_path_simplify_one(".///.//././/", ".", 0);
test_path_simplify_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/.",
- "/aaa/.bbb/../c./d.dd/..eeee");
+ "/aaa/.bbb/../c./d.dd/..eeee", 0);
test_path_simplify_one("//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
- "/aaa/.bbb/../c./d.dd/..eeee/..");
+ "/aaa/.bbb/../c./d.dd/..eeee/..", 0);
test_path_simplify_one(".//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
- "aaa/.bbb/../c./d.dd/..eeee/..");
+ "aaa/.bbb/../c./d.dd/..eeee/..", 0);
test_path_simplify_one("..//./aaa///.//./.bbb/..///c.//d.dd///..eeee/..",
- "../aaa/.bbb/../c./d.dd/..eeee/..");
+ "../aaa/.bbb/../c./d.dd/..eeee/..", 0);
+ test_path_simplify_one("abc///", "abc/", PATH_SIMPLIFY_KEEP_TRAILING_SLASH);
memset(foo, 'a', sizeof(foo) -1);
char_array_0(foo);
- test_path_simplify_one(foo, foo);
+ test_path_simplify_one(foo, foo, 0);
hoge = strjoin("/", foo);
assert_se(hoge);
- test_path_simplify_one(hoge, hoge);
+ test_path_simplify_one(hoge, hoge, 0);
hoge = mfree(hoge);
hoge = strjoin("a////.//././//./b///././/./c/////././//./", foo, "//.//////d/e/.//f/");
@@ -97,7 +100,7 @@ TEST(path_simplify) {
hoge_out = strjoin("a/b/c/", foo, "//.//////d/e/.//f/");
assert_se(hoge_out);
- test_path_simplify_one(hoge, hoge_out);
+ test_path_simplify_one(hoge, hoge_out, 0);
}
static void test_path_compare_one(const char *a, const char *b, int expected) {