All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] xenstore-client: Add option for raw in-/output
@ 2018-07-21 14:35 Marek Marczykowski-Górecki
  2018-07-23  9:39 ` Roger Pau Monné
  2018-07-25  9:20 ` Wei Liu
  0 siblings, 2 replies; 5+ messages in thread
From: Marek Marczykowski-Górecki @ 2018-07-21 14:35 UTC (permalink / raw)
  To: xen-devel
  Cc: Simon Gaiser, Wei Liu, Ian Jackson,
	Marek Marczykowski-Górecki, xen-devel

From: Simon Gaiser <simon@invisiblethingslab.com>

Parsing/generating the escape sequences used by xenstore-client is non
trivial. So make scripting (for use in stubdom) easier by adding a raw
option.

Signed-off-by: Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
---
 tools/xenstore/xenstore_client.c | 51 +++++++++++++++++++++++++-------
 1 file changed, 41 insertions(+), 10 deletions(-)

diff --git a/tools/xenstore/xenstore_client.c b/tools/xenstore/xenstore_client.c
index 3d14d37e62..904204bc2d 100644
--- a/tools/xenstore/xenstore_client.c
+++ b/tools/xenstore/xenstore_client.c
@@ -68,6 +68,18 @@ output(const char *fmt, ...) {
     output_pos += len;
 }
 
+static void
+output_raw(const char *data, int len) {
+    if (output_pos + len > output_size) {
+        output_size += len + 1024;
+	output_buf = realloc(output_buf, output_size);
+	if (output_buf == NULL)
+	    err(1, "malloc");
+    }
+    memcpy(&output_buf[output_pos], data, len);
+    output_pos += len;
+}
+
 static void
 usage(enum mode mode, int incl_mode, const char *progname)
 {
@@ -78,10 +90,10 @@ usage(enum mode mode, int incl_mode, const char *progname)
 	errx(1, "Usage: %s <mode> [-h] [...]", progname);
     case MODE_read:
 	mstr = incl_mode ? "read " : "";
-	errx(1, "Usage: %s %s[-h] [-p] [-s] key [...]", progname, mstr);
+	errx(1, "Usage: %s %s[-h] [-p] [-s] [-R] key [...]", progname, mstr);
     case MODE_write:
 	mstr = incl_mode ? "write " : "";
-	errx(1, "Usage: %s %s[-h] [-s] key value [...]", progname, mstr);
+	errx(1, "Usage: %s %s[-h] [-s] [-R] key value [...]", progname, mstr);
     case MODE_rm:
 	mstr = incl_mode ? "rm " : "";
 	errx(1, "Usage: %s %s[-h] [-s] [-t] key [...]", progname, mstr);
@@ -293,7 +305,8 @@ do_watch(struct xs_handle *xsh, int max_events)
 
 static int
 perform(enum mode mode, int optind, int argc, char **argv, struct xs_handle *xsh,
-        xs_transaction_t xth, int prefix, int tidy, int upto, int recurse, int nr_watches)
+        xs_transaction_t xth, int prefix, int tidy, int upto, int recurse, int nr_watches,
+        int raw)
 {
     switch (mode) {
     case MODE_ls:
@@ -322,17 +335,27 @@ perform(enum mode mode, int optind, int argc, char **argv, struct xs_handle *xsh
             }
             if (prefix)
                 output("%s: ", argv[optind]);
-            output("%s\n", sanitise_value(&ebuf, val, len));
+            if (raw)
+                output_raw(val, len);
+            else
+                output("%s\n", sanitise_value(&ebuf, val, len));
             free(val);
             optind++;
             break;
         }
         case MODE_write: {
             char *val_spec = argv[optind + 1];
+            char *val;
             unsigned len;
-            expanding_buffer_ensure(&ebuf, strlen(val_spec)+1);
-            unsanitise_value(ebuf.buf, &len, val_spec);
-            if (!xs_write(xsh, xth, argv[optind], ebuf.buf, len)) {
+            if (raw) {
+                val = val_spec;
+                len = strlen(val_spec);
+            } else {
+                expanding_buffer_ensure(&ebuf, strlen(val_spec)+1);
+                unsanitise_value(ebuf.buf, &len, val_spec);
+                val = ebuf.buf;
+            }
+            if (!xs_write(xsh, xth, argv[optind], val, len)) {
                 warnx("could not write path %s", argv[optind]);
                 return 1;
             }
@@ -506,6 +529,7 @@ main(int argc, char **argv)
     int recurse = 0;
     int nr_watches = -1;
     int transaction;
+    int raw = 0;
     struct winsize ws;
     enum mode mode;
 
@@ -539,10 +563,11 @@ main(int argc, char **argv)
 	    {"upto",    0, 0, 'u'}, /* MODE_chmod */
 	    {"recurse", 0, 0, 'r'}, /* MODE_chmod */
 	    {"number",  1, 0, 'n'}, /* MODE_watch */
+	    {"raw",     0, 0, 'R'}, /* MODE_read || MODE_write */
 	    {0, 0, 0, 0}
 	};
 
-	c = getopt_long(argc - switch_argv, argv + switch_argv, "hfspturn:",
+	c = getopt_long(argc - switch_argv, argv + switch_argv, "hfspturn:R",
 			long_options, &index);
 	if (c == -1)
 	    break;
@@ -593,6 +618,12 @@ main(int argc, char **argv)
 	    else
 		usage(mode, switch_argv, argv[0]);
 	    break;
+	case 'R':
+	    if ( mode == MODE_read || mode == MODE_write )
+		raw = 1;
+	    else
+		usage(mode, switch_argv, argv[0]);
+	    break;
 	}
     }
 
@@ -646,7 +677,7 @@ again:
 	    errx(1, "couldn't start transaction");
     }
 
-    ret = perform(mode, optind, argc - switch_argv, argv + switch_argv, xsh, xth, prefix, tidy, upto, recurse, nr_watches);
+    ret = perform(mode, optind, argc - switch_argv, argv + switch_argv, xsh, xth, prefix, tidy, upto, recurse, nr_watches, raw);
 
     if (transaction && !xs_transaction_end(xsh, xth, ret)) {
 	if (ret == 0 && errno == EAGAIN) {
@@ -657,7 +688,7 @@ again:
     }
 
     if (output_pos)
-	printf("%s", output_buf);
+        fwrite(output_buf, 1, output_pos, stdout);
 
     free(output_buf);
     free(ebuf.buf);
-- 
2.17.1


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] xenstore-client: Add option for raw in-/output
  2018-07-21 14:35 [PATCH] xenstore-client: Add option for raw in-/output Marek Marczykowski-Górecki
@ 2018-07-23  9:39 ` Roger Pau Monné
  2018-07-23 10:10   ` Marek Marczykowski-Górecki
  2018-07-25  9:20 ` Wei Liu
  1 sibling, 1 reply; 5+ messages in thread
From: Roger Pau Monné @ 2018-07-23  9:39 UTC (permalink / raw)
  To: Marek Marczykowski-Górecki
  Cc: Simon Gaiser, Ian Jackson, Wei Liu, xen-devel, xen-devel

On Sat, Jul 21, 2018 at 04:35:37PM +0200, Marek Marczykowski-Górecki wrote:
> From: Simon Gaiser <simon@invisiblethingslab.com>
> 
> Parsing/generating the escape sequences used by xenstore-client is non
> trivial. So make scripting (for use in stubdom) easier by adding a raw
> option.

Should a description for this option be added to the man pages?

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] xenstore-client: Add option for raw in-/output
  2018-07-23  9:39 ` Roger Pau Monné
@ 2018-07-23 10:10   ` Marek Marczykowski-Górecki
  2018-07-25  9:21     ` Wei Liu
  0 siblings, 1 reply; 5+ messages in thread
From: Marek Marczykowski-Górecki @ 2018-07-23 10:10 UTC (permalink / raw)
  To: Roger Pau Monné
  Cc: Simon Gaiser, Ian Jackson, Wei Liu, xen-devel, xen-devel


[-- Attachment #1.1: Type: text/plain, Size: 708 bytes --]

On Mon, Jul 23, 2018 at 11:39:05AM +0200, Roger Pau Monné wrote:
> On Sat, Jul 21, 2018 at 04:35:37PM +0200, Marek Marczykowski-Górecki wrote:
> > From: Simon Gaiser <simon@invisiblethingslab.com>
> > 
> > Parsing/generating the escape sequences used by xenstore-client is non
> > trivial. So make scripting (for use in stubdom) easier by adding a raw
> > option.
> 
> Should a description for this option be added to the man pages?

Yes, but I didn't found one for xenstore-read/xenstore-write. Where is
it?

-- 
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

[-- Attachment #2: Type: text/plain, Size: 157 bytes --]

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] xenstore-client: Add option for raw in-/output
  2018-07-21 14:35 [PATCH] xenstore-client: Add option for raw in-/output Marek Marczykowski-Górecki
  2018-07-23  9:39 ` Roger Pau Monné
@ 2018-07-25  9:20 ` Wei Liu
  1 sibling, 0 replies; 5+ messages in thread
From: Wei Liu @ 2018-07-25  9:20 UTC (permalink / raw)
  To: Marek Marczykowski-Górecki
  Cc: Simon Gaiser, Wei Liu, Ian Jackson, xen-devel, xen-devel

On Sat, Jul 21, 2018 at 04:35:37PM +0200, Marek Marczykowski-Górecki wrote:
> From: Simon Gaiser <simon@invisiblethingslab.com>
> 
> Parsing/generating the escape sequences used by xenstore-client is non
> trivial. So make scripting (for use in stubdom) easier by adding a raw
> option.
> 
> Signed-off-by: Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
> ---
>  tools/xenstore/xenstore_client.c | 51 +++++++++++++++++++++++++-------
>  1 file changed, 41 insertions(+), 10 deletions(-)
> 
> diff --git a/tools/xenstore/xenstore_client.c b/tools/xenstore/xenstore_client.c
> index 3d14d37e62..904204bc2d 100644
> --- a/tools/xenstore/xenstore_client.c
> +++ b/tools/xenstore/xenstore_client.c
> @@ -68,6 +68,18 @@ output(const char *fmt, ...) {
>      output_pos += len;
>  }
>  
> +static void
> +output_raw(const char *data, int len) {

Please put '{' on a new line.

(And output() should be fixed too, but that's another patch)

> +    if (output_pos + len > output_size) {
> +        output_size += len + 1024;
> +	output_buf = realloc(output_buf, output_size);
> +	if (output_buf == NULL)
> +	    err(1, "malloc");

Indentation is wrong.

> +    }

Also, this is repetitive. Please factor out a function (expand_buffer?
ensure_buffer?) and use it in output and output_raw.

Using expanding_buffer_ensure in xs.c will probably require a bit more
refactoring than necessary, so I wouldn't ask you to do that.

The code looks fine.

Wei.

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] xenstore-client: Add option for raw in-/output
  2018-07-23 10:10   ` Marek Marczykowski-Górecki
@ 2018-07-25  9:21     ` Wei Liu
  0 siblings, 0 replies; 5+ messages in thread
From: Wei Liu @ 2018-07-25  9:21 UTC (permalink / raw)
  To: Marek Marczykowski-Górecki
  Cc: Wei Liu, Ian Jackson, xen-devel, Simon Gaiser, xen-devel,
	Roger Pau Monné

On Mon, Jul 23, 2018 at 12:10:13PM +0200, Marek Marczykowski-Górecki wrote:
> On Mon, Jul 23, 2018 at 11:39:05AM +0200, Roger Pau Monné wrote:
> > On Sat, Jul 21, 2018 at 04:35:37PM +0200, Marek Marczykowski-Górecki wrote:
> > > From: Simon Gaiser <simon@invisiblethingslab.com>
> > > 
> > > Parsing/generating the escape sequences used by xenstore-client is non
> > > trivial. So make scripting (for use in stubdom) easier by adding a raw
> > > option.
> > 
> > Should a description for this option be added to the man pages?
> 
> Yes, but I didn't found one for xenstore-read/xenstore-write. Where is
> it?

Strangely the manpage is missing. You're welcome to add one. You can
start by copying xenstore-ls.pod.1 I suppose.

Wei.

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2018-07-25  9:21 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-07-21 14:35 [PATCH] xenstore-client: Add option for raw in-/output Marek Marczykowski-Górecki
2018-07-23  9:39 ` Roger Pau Monné
2018-07-23 10:10   ` Marek Marczykowski-Górecki
2018-07-25  9:21     ` Wei Liu
2018-07-25  9:20 ` Wei Liu

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.