|
| 1 | +From 0247730a13284be4bb1ccd16181b34ba94b2da04 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 | 2 +- |
| 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, 54 insertions(+), 15 deletions(-) |
| 16 | + |
| 17 | +diff --git a/src/basic/fs-util.c b/src/basic/fs-util.c |
| 18 | +index ee38e0266a..cc89f48fb7 100644 |
| 19 | +--- a/src/basic/fs-util.c |
| 20 | ++++ b/src/basic/fs-util.c |
| 21 | +@@ -65,7 +65,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 c770e5ed32..7bc73361a5 100644 |
| 32 | +--- a/src/basic/mkdir.c |
| 33 | ++++ b/src/basic/mkdir.c |
| 34 | +@@ -155,7 +155,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 | +diff --git a/src/basic/path-util.c b/src/basic/path-util.c |
| 44 | +index 6810bf66aa..e73f5d708e 100644 |
| 45 | +--- a/src/basic/path-util.c |
| 46 | ++++ b/src/basic/path-util.c |
| 47 | +@@ -403,8 +403,8 @@ char* path_simplify_full(char *path, PathSimplifyFlags flags) { |
| 48 | + return path; |
| 49 | + } |
| 50 | + |
| 51 | +-char* path_startswith_full(const char *path, const char *prefix, bool accept_dot_dot) { |
| 52 | +- assert(path); |
| 53 | ++char* path_startswith_full(const char *original_path, const char *prefix, PathStartWithFlags flags) { |
| 54 | ++ assert(original_path); |
| 55 | + assert(prefix); |
| 56 | + |
| 57 | + /* Returns a pointer to the start of the first component after the parts matched by |
| 58 | +@@ -417,28 +417,45 @@ char* path_startswith_full(const char *path, const char *prefix, bool accept_dot |
| 59 | + * Returns NULL otherwise. |
| 60 | + */ |
| 61 | + |
| 62 | ++ const char *path = original_path; |
| 63 | ++ |
| 64 | + if ((path[0] == '/') != (prefix[0] == '/')) |
| 65 | + return NULL; |
| 66 | + |
| 67 | + for (;;) { |
| 68 | + const char *p, *q; |
| 69 | +- int r, k; |
| 70 | ++ int m, n; |
| 71 | + |
| 72 | +- r = path_find_first_component(&path, accept_dot_dot, &p); |
| 73 | +- if (r < 0) |
| 74 | ++ m = path_find_first_component(&path, FLAGS_SET(flags, PATH_STARTSWITH_ACCEPT_DOT_DOT), &p); |
| 75 | ++ if (m < 0) |
| 76 | + return NULL; |
| 77 | + |
| 78 | +- k = path_find_first_component(&prefix, accept_dot_dot, &q); |
| 79 | +- if (k < 0) |
| 80 | ++ n = path_find_first_component(&prefix, FLAGS_SET(flags, PATH_STARTSWITH_ACCEPT_DOT_DOT), &q); |
| 81 | ++ if (n < 0) |
| 82 | + return NULL; |
| 83 | + |
| 84 | +- if (k == 0) |
| 85 | +- return (char*) (p ?: path); |
| 86 | ++ if (n == 0) { |
| 87 | ++ if (!p) |
| 88 | ++ p = path; |
| 89 | ++ |
| 90 | ++ if (FLAGS_SET(flags, PATH_STARTSWITH_RETURN_LEADING_SLASH)) { |
| 91 | ++ |
| 92 | ++ if (p <= original_path) |
| 93 | ++ return NULL; |
| 94 | ++ |
| 95 | ++ p--; |
| 96 | ++ |
| 97 | ++ if (*p != '/') |
| 98 | ++ return NULL; |
| 99 | ++ } |
| 100 | ++ |
| 101 | ++ return (char*) p; |
| 102 | ++ } |
| 103 | + |
| 104 | +- if (r != k) |
| 105 | ++ if (m != n) |
| 106 | + return NULL; |
| 107 | + |
| 108 | +- if (!strneq(p, q, r)) |
| 109 | ++ if (!strneq(p, q, m)) |
| 110 | + return NULL; |
| 111 | + } |
| 112 | + } |
| 113 | +diff --git a/src/basic/path-util.h b/src/basic/path-util.h |
| 114 | +index 6d943e967f..e0ec05f4db 100644 |
| 115 | +--- a/src/basic/path-util.h |
| 116 | ++++ b/src/basic/path-util.h |
| 117 | +@@ -53,9 +53,15 @@ int safe_getcwd(char **ret); |
| 118 | + int path_make_absolute_cwd(const char *p, char **ret); |
| 119 | + int path_make_relative(const char *from, const char *to, char **ret); |
| 120 | + int path_make_relative_parent(const char *from_child, const char *to, char **ret); |
| 121 | +-char* path_startswith_full(const char *path, const char *prefix, bool accept_dot_dot) _pure_; |
| 122 | ++ |
| 123 | ++typedef enum PathStartWithFlags { |
| 124 | ++ PATH_STARTSWITH_ACCEPT_DOT_DOT = 1U << 0, |
| 125 | ++ PATH_STARTSWITH_RETURN_LEADING_SLASH = 1U << 1, |
| 126 | ++} PathStartWithFlags; |
| 127 | ++ |
| 128 | ++char* path_startswith_full(const char *path, const char *prefix, PathStartWithFlags flags) _pure_; |
| 129 | + static inline char* path_startswith(const char *path, const char *prefix) { |
| 130 | +- return path_startswith_full(path, prefix, true); |
| 131 | ++ return path_startswith_full(path, prefix, PATH_STARTSWITH_ACCEPT_DOT_DOT); |
| 132 | + } |
| 133 | + |
| 134 | + int path_compare(const char *a, const char *b) _pure_; |
| 135 | +diff --git a/src/test/test-path-util.c b/src/test/test-path-util.c |
| 136 | +index f5a425689a..5b164facc0 100644 |
| 137 | +--- a/src/test/test-path-util.c |
| 138 | ++++ b/src/test/test-path-util.c |
| 139 | +@@ -754,6 +754,22 @@ TEST(path_startswith) { |
| 140 | + test_path_startswith_one("/foo/bar/barfoo/", "/fo", NULL, NULL); |
| 141 | + } |
| 142 | + |
| 143 | ++static void test_path_startswith_return_leading_slash_one(const char *path, const char *prefix, const char *expected) { |
| 144 | ++ const char *p; |
| 145 | ++ |
| 146 | ++ log_debug("/* %s(%s, %s) */", __func__, path, prefix); |
| 147 | ++ |
| 148 | ++ p = path_startswith_full(path, prefix, PATH_STARTSWITH_RETURN_LEADING_SLASH); |
| 149 | ++ assert_se(streq(p, expected)); |
| 150 | ++} |
| 151 | ++ |
| 152 | ++TEST(path_startswith_return_leading_slash) { |
| 153 | ++ test_path_startswith_return_leading_slash_one("/foo/bar", "/", "/foo/bar"); |
| 154 | ++ test_path_startswith_return_leading_slash_one("/foo/bar", "/foo", "/bar"); |
| 155 | ++ test_path_startswith_return_leading_slash_one("/foo/bar", "/foo/bar", NULL); |
| 156 | ++ test_path_startswith_return_leading_slash_one("/foo/bar/", "/foo/bar", "/"); |
| 157 | ++} |
| 158 | ++ |
| 159 | + static void test_prefix_root_one(const char *r, const char *p, const char *expected) { |
| 160 | + _cleanup_free_ char *s = NULL; |
| 161 | + const char *t; |
| 162 | +-- |
| 163 | +2.51.0 |
| 164 | + |
0 commit comments