diff options
author | Andreas Schneider <asn@samba.org> | 2014-02-17 11:24:33 +0100 |
---|---|---|
committer | Andreas Schneider <asn@samba.org> | 2014-02-19 18:22:30 +0100 |
commit | 995118484f7c1ef92b5539567cf878acc1c48b0a (patch) | |
tree | 86c51e4a9b7cc049aceffb158b645504428d0cd8 /source3/client | |
parent | 3b207dc0f3ef642ec5f16bc1dbce7c018c89cf55 (diff) | |
download | samba-995118484f7c1ef92b5539567cf878acc1c48b0a.tar.gz |
s3-clitar: Simplify is_subpath().
Signed-off-by: Andreas Schneider <asn@samba.org>
Signed-off-by: David Disseldorp <ddiss@samba.org>
Diffstat (limited to 'source3/client')
-rw-r--r-- | source3/client/clitar.c | 59 |
1 files 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; } /** |