From ae66dd92e559c71a2100a4fbf2a4779f39859d10 Mon Sep 17 00:00:00 2001 From: "Anthony G. Basile" Date: Wed, 10 Jul 2013 12:59:12 -0400 Subject: [PATCH] src/udev/mkdir.c: import path_startswith() from upstream Signed-off-by: Anthony G. Basile --- UPSTREAM.notes | 7 ++++--- src/udev/mkdir.c | 34 ++++++++++++++++++++++++++++++++++ src/udev/mkdir.h | 1 + 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/UPSTREAM.notes b/UPSTREAM.notes index 12c135939..2067c60ed 100644 --- a/UPSTREAM.notes +++ b/UPSTREAM.notes @@ -12,8 +12,9 @@ 2013-07-10 * Add "hwdb: allow list of lookup keys per given record" - * Bring src/udev/mkdir.c in line with upstreams use of *_internal() with prefix * Import format_timespan() from upstream's src/shared/time-util.c. Note: our src/libudev/util.c is derived from upstream's src/shared/util.c minus unneeded functions - plus three functions from src/shared/time-util.c: now(), timespec_load() and - timespan(). + plus three functions from src/shared/time-util.c: now(), timespec_load() and timespan(). + * Bring src/udev/mkdir.c in line with upstreams use of *_internal() with prefix. Also + import path_startswith() from upstream's src/shared/path-util.c. This is now needed + in our src/udev/mkdir.c in the new *_internal() functions. diff --git a/src/udev/mkdir.c b/src/udev/mkdir.c index c775bfa0c..420a9eb1b 100644 --- a/src/udev/mkdir.c +++ b/src/udev/mkdir.c @@ -39,6 +39,40 @@ static int is_dir(const char* path) { return S_ISDIR(st.st_mode); } + +char* path_startswith(const char *path, const char *prefix) { + assert(path); + assert(prefix); + + if ((path[0] == '/') != (prefix[0] == '/')) + return NULL; + + for (;;) { + size_t a, b; + + path += strspn(path, "/"); + prefix += strspn(prefix, "/"); + + if (*prefix == 0) + return (char*) path; + + if (*path == 0) + return NULL; + + a = strcspn(path, "/"); + b = strcspn(prefix, "/"); + + if (a != b) + return NULL; + + if (memcmp(path, prefix, a) != 0) + return NULL; + + path += a; + prefix += b; + } +} + static int mkdir_parents_internal(const char *prefix, const char *path, mode_t mode, bool apply) { const char *p, *e; int r; diff --git a/src/udev/mkdir.h b/src/udev/mkdir.h index db3141ef5..24ffa6364 100644 --- a/src/udev/mkdir.h +++ b/src/udev/mkdir.h @@ -22,6 +22,7 @@ along with systemd; If not, see . ***/ +char* path_startswith(const char *path, const char *prefix) _pure_; int mkdir_parents(const char *path, mode_t mode); int mkdir_parents_label(const char *path, mode_t mode); int mkdir_p(const char *path, mode_t mode);