xenstore-ls -f for find(1)-like output
authorKeir Fraser <keir.fraser@citrix.com>
Wed, 5 Dec 2007 11:07:12 +0000 (11:07 +0000)
committerKeir Fraser <keir.fraser@citrix.com>
Wed, 5 Dec 2007 11:07:12 +0000 (11:07 +0000)
The current output of xenstore-ls can be quite hard to read and it is
not very intractable for postprocessing with sort|diff and the like.

The patch below provides a -f option which produces output with the
full key pathname on each line, and which disables the value
truncation and the `.'-padding when used with -p (since these latter
two aren't likely to be very useful when values are preceded by long
pathnames).

While I was at it I added the `-s' option to the usage message, where
it was previously missing.

The results looks like this:

...
/local/domain/1 = ""
/local/domain/1/vm = "/vm/8b5fd34a-e268-fab5-9cde-c06eda21df16"
/local/domain/1/device = ""
/local/domain/1/device/vbd = ""
/local/domain/1/device/vbd/2049 = ""
/local/domain/1/device/vbd/2049/virtual-device = "2049"
/local/domain/1/device/vbd/2049/device-type = "disk"
/local/domain/1/device/vbd/2049/protocol = "x86_32-abi"
...

Signed-off-by: Ian Jackson <ian.jackson@eu.citrix.com>
tools/xenstore/xsls.c

index 061feebd57fd3013421300407ea019d4a12ebc25..cd8e3a9dacf57a5050dba1afc69866c613cfd62b 100644 (file)
@@ -11,6 +11,7 @@
 #define STRING_MAX PATH_MAX
 static int max_width = 80;
 static int desired_width = 60;
+static int show_whole_path = 0;
 
 #define TAG " = \"...\""
 #define TAG_LEN strlen(TAG)
@@ -36,23 +37,31 @@ void print_dir(struct xs_handle *h, char *path, int cur_depth, int show_perms)
         unsigned int nperms;
         int linewid;
 
-        /* Print indent and path basename */
-        for (linewid=0; linewid<cur_depth; linewid++) {
-            putchar(' ');
-        }
-        linewid += printf("%.*s",
-                          (int) (max_width - TAG_LEN - linewid), e[i]);
-
-        /* Compose fullpath and fetch value */
+        /* Compose fullpath */
         newpath_len = snprintf(newpath, sizeof(newpath), "%s%s%s", path, 
                 path[strlen(path)-1] == '/' ? "" : "/", 
                 e[i]);
+
+        /* Print indent and path basename */
+        linewid = 0;
+        if (show_whole_path) {
+            fputs(newpath, stdout);
+        } else {
+            for (; linewid<cur_depth; linewid++) {
+                putchar(' ');
+            }
+            linewid += printf("%.*s",
+                              (int) (max_width - TAG_LEN - linewid), e[i]);
+        }
+
+       /* Fetch value */
         if ( newpath_len < sizeof(newpath) ) {
             val = xs_read(h, XBT_NULL, newpath, &len);
         }
         else {
             /* Path was truncated and thus invalid */
             val = NULL;
+            len = 0;
         }
 
         /* Print value */
@@ -106,7 +115,7 @@ void print_dir(struct xs_handle *h, char *path, int cur_depth, int show_perms)
 
 void usage(int argc, char *argv[])
 {
-    fprintf(stderr, "Usage: %s [-w] [-p] [path]\n", argv[0]);
+    fprintf(stderr, "Usage: %s [-w] [-p] [-f] [-s] [path]\n", argv[0]);
 }
 
 int main(int argc, char *argv[])
@@ -122,7 +131,7 @@ int main(int argc, char *argv[])
     if (!ret)
         max_width = ws.ws_col - PAD;
 
-    while (0 < (c = getopt(argc, argv, "psw"))) {
+    while (0 < (c = getopt(argc, argv, "pswf"))) {
         switch (c) {
         case 'w':
             max_width= STRING_MAX - PAD;
@@ -134,6 +143,11 @@ int main(int argc, char *argv[])
         case 's':
             socket = 1;
             break;
+        case 'f':
+            max_width = INT_MAX/2;
+            desired_width = 0;
+            show_whole_path = 1;
+            break;
         case ':':
         case '?':
         default: