From 3607a75236b37abe350bf52f4700355be963a60d Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Fri, 13 Jan 2012 16:54:11 +0000 Subject: xenstore: New function xs_path_is_subpath This utility function compares two paths, textually and reports whether one is a subpath (a child path) of the other. Signed-off-by: Ian Jackson Acked-by: Ian Campbell Committed-by: Ian Jackson --- tools/xenstore/xs.c | 17 +++++++++++++++++ tools/xenstore/xs.h | 7 +++++++ 2 files changed, 24 insertions(+) (limited to 'tools/xenstore') diff --git a/tools/xenstore/xs.c b/tools/xenstore/xs.c index 8e54fe01c9..0a01675a78 100644 --- a/tools/xenstore/xs.c +++ b/tools/xenstore/xs.c @@ -950,6 +950,23 @@ char *xs_get_domain_path(struct xs_handle *h, unsigned int domid) return xs_single(h, XBT_NULL, XS_GET_DOMAIN_PATH, domid_str, NULL); } +bool xs_path_is_subpath(const char *parent, const char *child) +{ + size_t childlen = strlen(child); + size_t parentlen = strlen(parent); + + if (childlen < parentlen) + return false; + + if (memcmp(child, parent, parentlen)) + return false; + + if (childlen > parentlen && child[parentlen] != '/') + return false; + + return true; +} + bool xs_is_domain_introduced(struct xs_handle *h, unsigned int domid) { char *domain = single_with_domid(h, XS_IS_DOMAIN_INTRODUCED, domid); diff --git a/tools/xenstore/xs.h b/tools/xenstore/xs.h index 63f535dd55..8d49e50e46 100644 --- a/tools/xenstore/xs.h +++ b/tools/xenstore/xs.h @@ -207,6 +207,13 @@ bool xs_release_domain(struct xs_handle *h, unsigned int domid); */ char *xs_get_domain_path(struct xs_handle *h, unsigned int domid); +/* Returns true if child is either equal to parent, or a node underneath + * parent; or false otherwise. Done by string comparison, so relative and + * absolute pathnames never in a parent/child relationship by this + * definition. Cannot fail. + */ +bool xs_path_is_subpath(const char *parent, const char *child); + /* Return whether the domain specified has been introduced to xenstored. */ bool xs_is_domain_introduced(struct xs_handle *h, unsigned int domid); -- cgit v1.2.3