WARNING - OLD ARCHIVES

This is an archived copy of the Xen.org mailing list, which we have preserved to ensure that existing links to archives are not broken. The live archive, which contains the latest emails, can be found at http://lists.xen.org/
   
 
 
Xen 
 
Home Products Support Community News
 
   
 

xen-devel

[Xen-devel] [PATCH] xenstore-ls permission display

To: xen-devel@xxxxxxxxxxxxxxxxxxx
Subject: [Xen-devel] [PATCH] xenstore-ls permission display
From: Michael LeMay <mdlemay@xxxxxxxxxxxxxx>
Date: Wed, 02 Aug 2006 08:07:33 -0400
Delivery-date: Wed, 02 Aug 2006 05:11:37 -0700
Envelope-to: www-data@xxxxxxxxxxxxxxxxxx
List-help: <mailto:xen-devel-request@lists.xensource.com?subject=help>
List-id: Xen developer discussion <xen-devel.lists.xensource.com>
List-post: <mailto:xen-devel@lists.xensource.com>
List-subscribe: <http://lists.xensource.com/cgi-bin/mailman/listinfo/xen-devel>, <mailto:xen-devel-request@lists.xensource.com?subject=subscribe>
List-unsubscribe: <http://lists.xensource.com/cgi-bin/mailman/listinfo/xen-devel>, <mailto:xen-devel-request@lists.xensource.com?subject=unsubscribe>
Sender: xen-devel-bounces@xxxxxxxxxxxxxxxxxxx
This patch enhances xenstore-ls to optionally display permissions.  It
also performs an ioctl to determine the current width of the terminal.
Unfortunately, it doesn't set the width perfectly all the time, but it
should at least be an improvement upon the current situation.


Signed-off-by: Michael LeMay <mdlemay@xxxxxxxxxxxxxx>

---

 tools/xenstore/xsls.c |  103 ++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 92 insertions(+), 11 deletions(-)

diff -r 2353a28247b1 -r edfce48620e9 tools/xenstore/xsls.c
--- a/tools/xenstore/xsls.c     Tue Aug 01 08:47:33 2006 -0400
+++ b/tools/xenstore/xsls.c     Tue Aug 01 10:35:08 2006 -0400
@@ -3,8 +3,19 @@
 #include <string.h>
 #include <err.h>
 #include <xs.h>
+#include <getopt.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
 
-void print_dir(struct xs_handle *h, char *path, int cur_depth)
+static int max_width = 80;
+static int desired_width = 60;
+
+#define TAG " = \"...\""
+#define TAG_LEN strlen(TAG)
+
+#define MIN(a, b) (((a) < (b))? (a) : (b))
+
+void print_dir(struct xs_handle *h, char *path, int cur_depth, int show_perms)
 {
     char **e;
     char newpath[512], *val;
@@ -16,33 +27,103 @@ void print_dir(struct xs_handle *h, char
         err(1, "xs_directory (%s)", path);
 
     for (i = 0; i<num; i++) {
-        int j;
-        for (j=0; j<cur_depth; j++) printf(" ");
-        printf("%s", e[i]);
+        char buf[MAX_STRLEN(unsigned int)+1];
+        struct xs_permissions *perms;
+        unsigned int nperms;
+        int linewid;
+
+        for (linewid=0; linewid<cur_depth; linewid++) putchar(' ');
+        linewid += printf("%.*s",
+                          (int) (max_width - TAG_LEN - linewid), e[i]);
         sprintf(newpath, "%s%s%s", path, 
                 path[strlen(path)-1] == '/' ? "" : "/", 
                 e[i]);
         val = xs_read(h, XBT_NULL, newpath, &len);
-        if (val == NULL)
+        if (val == NULL) {
             printf(":\n");
-        else if ((unsigned)len > (151 - strlen(e[i])))
-            printf(" = \"%.*s...\"\n", (int)(148 - strlen(e[i])), val);
-        else
-            printf(" = \"%s\"\n", val);
+        }
+        else {
+            if (max_width < (linewid + len + TAG_LEN)) {
+                printf(" = \"%.*s...\"",
+                       (int)(max_width - TAG_LEN - linewid), val);
+            }
+            else {
+                linewid += printf(" = \"%s\"", val);
+                if (show_perms) {
+                    putchar(' ');
+                    for (linewid++;
+                         linewid < MIN(desired_width, max_width);
+                         linewid++)
+                        putchar((linewid & 1)? '.' : ' ');
+                }
+            }
+        }
         free(val);
-        print_dir(h, newpath, cur_depth+1); 
+
+        if (show_perms) {
+            perms = xs_get_permissions(h, XBT_NULL, newpath, &nperms);
+            if (perms == NULL) {
+                warn("\ncould not access permissions for %s", e[i]);
+            }
+            else {
+                int i;
+                fputs("  (", stdout);
+                for (i = 0; i < nperms; i++) {
+                    if (i)
+                        putchar(',');
+                    xs_perm_to_string(perms+i, buf);
+                    fputs(buf, stdout);
+                }
+                putchar(')');
+            }
+        }
+
+        putchar('\n');
+            
+        print_dir(h, newpath, cur_depth+1, show_perms); 
     }
     free(e);
 }
 
+void usage(int argc, char *argv[])
+{
+    fprintf(stderr, "Usage: %s [-p] [path]\n", argv[0]);
+}
+
 int main(int argc, char *argv[])
 {
+    struct winsize ws;
+    int ret;
+    int c;
+    int show_perm = 0;
+
     struct xs_handle *xsh = xs_daemon_open();
 
     if (xsh == NULL)
         err(1, "xs_daemon_open");
 
-    print_dir(xsh, argc == 1 ? "/" : argv[1], 0);
+#define PAD 2
+
+    memset(&ws, 0, sizeof(ws));
+    ret = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
+    if (!ret)
+        max_width = ws.ws_col - PAD;
+
+    while (0 < (c = getopt(argc, argv, "p"))) {
+        switch (c) {
+        case 'p':
+            show_perm = 1;
+            max_width -= 16;
+            break;
+        case ':':
+        case '?':
+        default:
+            usage(argc, argv);
+            return 0;
+        }
+    }
+
+    print_dir(xsh, (argc - optind) == 1 ? argv[optind] : "/", 0, show_perm);
 
     return 0;
 }



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

<Prev in Thread] Current Thread [Next in Thread>
  • [Xen-devel] [PATCH] xenstore-ls permission display, Michael LeMay <=