From 995118484f7c1ef92b5539567cf878acc1c48b0a Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Mon, 17 Feb 2014 11:24:33 +0100 Subject: [PATCH] s3-clitar: Simplify is_subpath(). Signed-off-by: Andreas Schneider Signed-off-by: David Disseldorp --- source3/client/clitar.c | 59 ++++++++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 23 deletions(-) diff --git a/source3/client/clitar.c b/source3/client/clitar.c index bad7eac4ad8..0f6b6a80522 100644 --- a/source3/client/clitar.c +++ b/source3/client/clitar.c @@ -1433,29 +1433,42 @@ static const char* skip_useless_char_in_path(const char *p) */ static bool is_subpath(const char *sub, const char *full) { - const char *full_copy = full; - - while (*full && *sub && - (*full == *sub || tolower_m(*full) == tolower_m(*sub) || - (*full == '\\' && *sub=='/') || (*full == '/' && *sub=='\\'))) { - full++; sub++; - } - - /* if full has a trailing slash, it compared equal, so full is an "initial" - string of sub. - */ - if (!*full && full != full_copy && (*(full-1) == '/' || *(full-1) == '\\')) - return true; - - /* ignore trailing slash on full */ - if (!*sub && (*full == '/' || *full == '\\') && !*(full+1)) - return true; - - /* check for full is an "initial" string of sub */ - if ((*sub == '/' || *sub == '\\') && !*full) - return true; - - return *full == *sub; + TALLOC_CTX *tmp_ctx = PANIC_IF_NULL(talloc_new(NULL)); + int len = 0; + char *f, *s; + + f = PANIC_IF_NULL(strlower_talloc(tmp_ctx, full)); + string_replace(f, '\\', '/'); + s = PANIC_IF_NULL(strlower_talloc(tmp_ctx, sub)); + string_replace(s, '\\', '/'); + + /* find the point where sub and full diverge */ + while ((*f != '\0') && (*s != '\0') && (*f == *s)) { + f++; + s++; + len++; + } + + if ((*f == '\0') && (*s == '\0')) { + return true; /* sub and full match */ + } + + if ((*f == '\0') && (len > 0) && (*(f - 1) == '/')) { + /* sub diverges from full at path separator */ + return true; + } + + if ((*s == '\0') && (strcmp(f, "/") == 0)) { + /* full diverges from sub with trailing slash only */ + return true; + } + + if ((*s == '/') && (*f == '\0')) { + /* sub diverges from full with extra path component */ + return true; + } + + return false; } /** -- 2.11.4.GIT