|
| 1 | +From 69fd960a8d5a5aad6874cc11be6dc258ae7eef23 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Lennart Poettering <lennart@poettering.net> |
| 3 | +Date: Mon, 19 May 2025 12:58:52 +0200 |
| 4 | +Subject: [PATCH 1/4] path-util: add flavour of path_startswith() that leaves a |
| 5 | + leading slash in place |
| 6 | + |
| 7 | +(cherry picked from commit ee19edbb9f3455db3f750089082f3e5a925e3a0c) |
| 8 | +(cherry picked from commit 20021e7686426052e3a7505425d7e12085feb2a6) |
| 9 | +--- |
| 10 | + src/basic/fs-util.c | 2 +- |
| 11 | + src/basic/mkdir.c | 4 ++-- |
| 12 | + src/basic/path-util.c | 39 ++++++++++++++++++++++++++++----------- |
| 13 | + src/basic/path-util.h | 10 ++++++++-- |
| 14 | + src/test/test-path-util.c | 16 ++++++++++++++++ |
| 15 | + 5 files changed, 55 insertions(+), 16 deletions(-) |
| 16 | + |
| 17 | +diff --git a/src/basic/fs-util.c b/src/basic/fs-util.c |
| 18 | +index 552986f546..8b2f6cc087 100644 |
| 19 | +--- a/src/basic/fs-util.c |
| 20 | ++++ b/src/basic/fs-util.c |
| 21 | +@@ -67,7 +67,7 @@ int rmdir_parents(const char *path, const char *stop) { |
| 22 | + assert(*slash == '/'); |
| 23 | + *slash = '\0'; |
| 24 | + |
| 25 | +- if (path_startswith_full(stop, p, /* accept_dot_dot= */ false)) |
| 26 | ++ if (path_startswith_full(stop, p, /* flags= */ 0)) |
| 27 | + return 0; |
| 28 | + |
| 29 | + if (rmdir(p) < 0 && errno != ENOENT) |
| 30 | +diff --git a/src/basic/mkdir.c b/src/basic/mkdir.c |
| 31 | +index 6e2b94d024..454739679c 100644 |
| 32 | +--- a/src/basic/mkdir.c |
| 33 | ++++ b/src/basic/mkdir.c |
| 34 | +@@ -92,7 +92,7 @@ int mkdir_parents_internal(const char *prefix, const char *path, mode_t mode, ui |
| 35 | + assert(_mkdirat != mkdirat); |
| 36 | + |
| 37 | + if (prefix) { |
| 38 | +- p = path_startswith_full(path, prefix, /* accept_dot_dot= */ false); |
| 39 | ++ p = path_startswith_full(path, prefix, /* flags= */ 0); |
| 40 | + if (!p) |
| 41 | + return -ENOTDIR; |
| 42 | + } else |
| 43 | +@@ -137,7 +137,7 @@ int mkdir_parents_internal(const char *prefix, const char *path, mode_t mode, ui |
| 44 | + |
| 45 | + s[n] = '\0'; |
| 46 | + |
| 47 | +- if (!prefix || !path_startswith_full(prefix, path, /* accept_dot_dot= */ false)) { |
| 48 | ++ if (!prefix || !path_startswith_full(prefix, path, /* flags= */ 0)) { |
| 49 | + r = mkdir_safe_internal(path, mode, uid, gid, flags, _mkdirat); |
| 50 | + if (r < 0 && r != -EEXIST) |
| 51 | + return r; |
| 52 | +diff --git a/src/basic/path-util.c b/src/basic/path-util.c |
| 53 | +index 4c952d863c..9b44d5735c 100644 |
| 54 | +--- a/src/basic/path-util.c |
| 55 | ++++ b/src/basic/path-util.c |
| 56 | +@@ -424,8 +424,8 @@ int path_simplify_and_warn( |
| 57 | + return 0; |
| 58 | + } |
| 59 | + |
| 60 | +-char *path_startswith_full(const char *path, const char *prefix, bool accept_dot_dot) { |
| 61 | +- assert(path); |
| 62 | ++char* path_startswith_full(const char *original_path, const char *prefix, PathStartWithFlags flags) { |
| 63 | ++ assert(original_path); |
| 64 | + assert(prefix); |
| 65 | + |
| 66 | + /* Returns a pointer to the start of the first component after the parts matched by |
| 67 | +@@ -438,28 +438,45 @@ char *path_startswith_full(const char *path, const char *prefix, bool accept_dot |
| 68 | + * Returns NULL otherwise. |
| 69 | + */ |
| 70 | + |
| 71 | ++ const char *path = original_path; |
| 72 | ++ |
| 73 | + if ((path[0] == '/') != (prefix[0] == '/')) |
| 74 | + return NULL; |
| 75 | + |
| 76 | + for (;;) { |
| 77 | + const char *p, *q; |
| 78 | +- int r, k; |
| 79 | ++ int m, n; |
| 80 | + |
| 81 | +- r = path_find_first_component(&path, accept_dot_dot, &p); |
| 82 | +- if (r < 0) |
| 83 | ++ m = path_find_first_component(&path, FLAGS_SET(flags, PATH_STARTSWITH_ACCEPT_DOT_DOT), &p); |
| 84 | ++ if (m < 0) |
| 85 | + return NULL; |
| 86 | + |
| 87 | +- k = path_find_first_component(&prefix, accept_dot_dot, &q); |
| 88 | +- if (k < 0) |
| 89 | ++ n = path_find_first_component(&prefix, FLAGS_SET(flags, PATH_STARTSWITH_ACCEPT_DOT_DOT), &q); |
| 90 | ++ if (n < 0) |
| 91 | + return NULL; |
| 92 | + |
| 93 | +- if (k == 0) |
| 94 | +- return (char*) (p ?: path); |
| 95 | ++ if (n == 0) { |
| 96 | ++ if (!p) |
| 97 | ++ p = path; |
| 98 | ++ |
| 99 | ++ if (FLAGS_SET(flags, PATH_STARTSWITH_RETURN_LEADING_SLASH)) { |
| 100 | ++ |
| 101 | ++ if (p <= original_path) |
| 102 | ++ return NULL; |
| 103 | ++ |
| 104 | ++ p--; |
| 105 | ++ |
| 106 | ++ if (*p != '/') |
| 107 | ++ return NULL; |
| 108 | ++ } |
| 109 | ++ |
| 110 | ++ return (char*) p; |
| 111 | ++ } |
| 112 | + |
| 113 | +- if (r != k) |
| 114 | ++ if (m != n) |
| 115 | + return NULL; |
| 116 | + |
| 117 | +- if (!strneq(p, q, r)) |
| 118 | ++ if (!strneq(p, q, m)) |
| 119 | + return NULL; |
| 120 | + } |
| 121 | + } |
| 122 | +diff --git a/src/basic/path-util.h b/src/basic/path-util.h |
| 123 | +index 518f3340bf..763d1fe1a1 100644 |
| 124 | +--- a/src/basic/path-util.h |
| 125 | ++++ b/src/basic/path-util.h |
| 126 | +@@ -57,9 +57,15 @@ char* path_make_absolute(const char *p, const char *prefix); |
| 127 | + int safe_getcwd(char **ret); |
| 128 | + int path_make_absolute_cwd(const char *p, char **ret); |
| 129 | + int path_make_relative(const char *from, const char *to, char **ret); |
| 130 | +-char *path_startswith_full(const char *path, const char *prefix, bool accept_dot_dot) _pure_; |
| 131 | ++ |
| 132 | ++typedef enum PathStartWithFlags { |
| 133 | ++ PATH_STARTSWITH_ACCEPT_DOT_DOT = 1U << 0, |
| 134 | ++ PATH_STARTSWITH_RETURN_LEADING_SLASH = 1U << 1, |
| 135 | ++} PathStartWithFlags; |
| 136 | ++ |
| 137 | ++char* path_startswith_full(const char *path, const char *prefix, PathStartWithFlags flags) _pure_; |
| 138 | + static inline char* path_startswith(const char *path, const char *prefix) { |
| 139 | +- return path_startswith_full(path, prefix, true); |
| 140 | ++ return path_startswith_full(path, prefix, PATH_STARTSWITH_ACCEPT_DOT_DOT); |
| 141 | + } |
| 142 | + int path_compare(const char *a, const char *b) _pure_; |
| 143 | + |
| 144 | +diff --git a/src/test/test-path-util.c b/src/test/test-path-util.c |
| 145 | +index b9c4ef4126..c9794244d7 100644 |
| 146 | +--- a/src/test/test-path-util.c |
| 147 | ++++ b/src/test/test-path-util.c |
| 148 | +@@ -541,6 +541,22 @@ TEST(path_startswith) { |
| 149 | + test_path_startswith_one("/foo/bar/barfoo/", "/fo", NULL, NULL); |
| 150 | + } |
| 151 | + |
| 152 | ++static void test_path_startswith_return_leading_slash_one(const char *path, const char *prefix, const char *expected) { |
| 153 | ++ const char *p; |
| 154 | ++ |
| 155 | ++ log_debug("/* %s(%s, %s) */", __func__, path, prefix); |
| 156 | ++ |
| 157 | ++ p = path_startswith_full(path, prefix, PATH_STARTSWITH_RETURN_LEADING_SLASH); |
| 158 | ++ assert_se(streq(p, expected)); |
| 159 | ++} |
| 160 | ++ |
| 161 | ++TEST(path_startswith_return_leading_slash) { |
| 162 | ++ test_path_startswith_return_leading_slash_one("/foo/bar", "/", "/foo/bar"); |
| 163 | ++ test_path_startswith_return_leading_slash_one("/foo/bar", "/foo", "/bar"); |
| 164 | ++ test_path_startswith_return_leading_slash_one("/foo/bar", "/foo/bar", NULL); |
| 165 | ++ test_path_startswith_return_leading_slash_one("/foo/bar/", "/foo/bar", "/"); |
| 166 | ++} |
| 167 | ++ |
| 168 | + static void test_prefix_root_one(const char *r, const char *p, const char *expected) { |
| 169 | + _cleanup_free_ char *s = NULL; |
| 170 | + const char *t; |
| 171 | +-- |
| 172 | +2.51.0 |
| 173 | + |
0 commit comments