[PATCH 10/20] tools/xenstore: replace watch->relative_path with a prefix length

Juergen Gross posted 20 patches 3 years, 3 months ago
There is a newer version of this series
[PATCH 10/20] tools/xenstore: replace watch->relative_path with a prefix length
Posted by Juergen Gross 3 years, 3 months ago
Instead of storing a pointer to the path which is prepended to
relative paths in struct watch, just use the length of the prepended
path.

It should be noted that the now removed special case of the
relative path being "" in get_watch_path() can't happen at all.

Signed-off-by: Juergen Gross <jgross@suse.com>
---
 tools/xenstore/xenstored_watch.c | 26 +++++++-------------------
 1 file changed, 7 insertions(+), 19 deletions(-)

diff --git a/tools/xenstore/xenstored_watch.c b/tools/xenstore/xenstored_watch.c
index 75748ac109..8bed967c56 100644
--- a/tools/xenstore/xenstored_watch.c
+++ b/tools/xenstore/xenstored_watch.c
@@ -39,8 +39,8 @@ struct watch
 	/* Current outstanding events applying to this watch. */
 	struct list_head events;
 
-	/* Is this relative to connnection's implicit path? */
-	const char *relative_path;
+	/* Offset into path for skipping prefix (used for relative paths). */
+	unsigned int prefix_len;
 
 	char *token;
 	char *node;
@@ -66,15 +66,7 @@ static bool is_child(const char *child, const char *parent)
 
 static const char *get_watch_path(const struct watch *watch, const char *name)
 {
-	const char *path = name;
-
-	if (watch->relative_path) {
-		path += strlen(watch->relative_path);
-		if (*path == '/') /* Could be "" */
-			path++;
-	}
-
-	return path;
+	return name + watch->prefix_len;
 }
 
 /*
@@ -211,10 +203,7 @@ static struct watch *add_watch(struct connection *conn, char *path, char *token,
 			      no_quota_check))
 		goto nomem;
 
-	if (relative)
-		watch->relative_path = get_implicit_path(conn);
-	else
-		watch->relative_path = NULL;
+	watch->prefix_len = relative ? strlen(get_implicit_path(conn)) + 1 : 0;
 
 	INIT_LIST_HEAD(&watch->events);
 
@@ -316,7 +305,6 @@ const char *dump_state_watches(FILE *fp, struct connection *conn,
 	struct watch *watch;
 	struct xs_state_watch sw;
 	struct xs_state_record_header head;
-	const char *path;
 
 	head.type = XS_STATE_TYPE_WATCH;
 
@@ -324,8 +312,7 @@ const char *dump_state_watches(FILE *fp, struct connection *conn,
 		head.length = sizeof(sw);
 
 		sw.conn_id = conn_id;
-		path = get_watch_path(watch, watch->node);
-		sw.path_length = strlen(path) + 1;
+		sw.path_length = strlen(watch->node + watch->prefix_len) + 1;
 		sw.token_length = strlen(watch->token) + 1;
 		head.length += sw.path_length + sw.token_length;
 		head.length = ROUNDUP(head.length, 3);
@@ -334,7 +321,8 @@ const char *dump_state_watches(FILE *fp, struct connection *conn,
 		if (fwrite(&sw, sizeof(sw), 1, fp) != 1)
 			return "Dump watch state error";
 
-		if (fwrite(path, sw.path_length, 1, fp) != 1)
+		if (fwrite(watch->node + watch->prefix_len, sw.path_length,
+			   1, fp) != 1)
 			return "Dump watch path error";
 		if (fwrite(watch->token, sw.token_length, 1, fp) != 1)
 			return "Dump watch token error";
-- 
2.35.3
Re: [PATCH 10/20] tools/xenstore: replace watch->relative_path with a prefix length
Posted by Julien Grall 3 years, 2 months ago
Hi Juergen,

On 01/11/2022 15:28, Juergen Gross wrote:
> @@ -324,8 +312,7 @@ const char *dump_state_watches(FILE *fp, struct connection *conn,
>   		head.length = sizeof(sw);
>   
>   		sw.conn_id = conn_id;
> -		path = get_watch_path(watch, watch->node);
> -		sw.path_length = strlen(path) + 1;
> +		sw.path_length = strlen(watch->node + watch->prefix_len) + 1;

Why are you open-coding get_watch_path()?

>   		sw.token_length = strlen(watch->token) + 1;
>   		head.length += sw.path_length + sw.token_length;
>   		head.length = ROUNDUP(head.length, 3);
> @@ -334,7 +321,8 @@ const char *dump_state_watches(FILE *fp, struct connection *conn,
>   		if (fwrite(&sw, sizeof(sw), 1, fp) != 1)
>   			return "Dump watch state error";
>   
> -		if (fwrite(path, sw.path_length, 1, fp) != 1)
> +		if (fwrite(watch->node + watch->prefix_len, sw.path_length,
> +			   1, fp) != 1)
>   			return "Dump watch path error";
>   		if (fwrite(watch->token, sw.token_length, 1, fp) != 1)
>   			return "Dump watch token error";

Cheers,

-- 
Julien Grall
Re: [PATCH 10/20] tools/xenstore: replace watch->relative_path with a prefix length
Posted by Juergen Gross 3 years, 1 month ago
On 01.12.22 22:51, Julien Grall wrote:
> Hi Juergen,
> 
> On 01/11/2022 15:28, Juergen Gross wrote:
>> @@ -324,8 +312,7 @@ const char *dump_state_watches(FILE *fp, struct connection 
>> *conn,
>>           head.length = sizeof(sw);
>>           sw.conn_id = conn_id;
>> -        path = get_watch_path(watch, watch->node);
>> -        sw.path_length = strlen(path) + 1;
>> +        sw.path_length = strlen(watch->node + watch->prefix_len) + 1;
> 
> Why are you open-coding get_watch_path()?

Will fix it.


Juergen