Mailing List Archive

[PATCH 15/18] 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 <ian.jackson@eu.citrix.com>
---
tools/xenstore/xs.c | 17 +++++++++++++++++
tools/xenstore/xs.h | 7 +++++++
2 files changed, 24 insertions(+), 0 deletions(-)

diff --git a/tools/xenstore/xs.c b/tools/xenstore/xs.c
index 8e54fe0..0a01675 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 63f535d..8d49e50 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);
--
1.7.2.5


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xensource.com
http://lists.xensource.com/xen-devel
Re: [PATCH 15/18] xenstore: New function xs_path_is_subpath [ In reply to ]
On Fri, 2011-12-09 at 18:54 +0000, Ian Jackson wrote:
> 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 <ian.jackson@eu.citrix.com>
> ---
> tools/xenstore/xs.c | 17 +++++++++++++++++
> tools/xenstore/xs.h | 7 +++++++
> 2 files changed, 24 insertions(+), 0 deletions(-)
>
> diff --git a/tools/xenstore/xs.c b/tools/xenstore/xs.c
> index 8e54fe0..0a01675 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;

It took me a second to figure that this last statement was preventing
false positives from sibling directories where one is a substring of the
other. Worth a comment?

Doesn't the correctness of this depend on whether parent has a trailing
slash or not though?

parent = "foo" len = 3
child = "foo/bar"

parent = "foo/" len = 4
child = "foo/bar

Does adding
if parent[parentlen] == '/' parentlen--;
beforehand help?

> +
> + 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 63f535d..8d49e50 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);



_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xensource.com
http://lists.xensource.com/xen-devel
Re: [PATCH 15/18] xenstore: New function xs_path_is_subpath [ In reply to ]
Ian Campbell writes ("Re: [Xen-devel] [PATCH 15/18] xenstore: New function xs_path_is_subpath"):
> On Fri, 2011-12-09 at 18:54 +0000, Ian Jackson wrote:
> > + if (childlen > parentlen && child[parentlen] != '/')
> > + return false;
>
> It took me a second to figure that this last statement was preventing
> false positives from sibling directories where one is a substring of the
> other. Worth a comment?

Probably, yes.

> Doesn't the correctness of this depend on whether parent has a trailing
> slash or not though?

We don't allow trailing slashes. They don't work in xenstore
operations. However, there is one exception: "/". If parent is "/"
the algorithm is wrong.

So docs/misc/xenstore.txt needs updating to clarify the former and my
new function needs the latter fixing.

These are the perils of taking code that was correct in situ and
making a general function out of it, I guess ...

Ian.

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xensource.com
http://lists.xensource.com/xen-devel