ia64/xen-unstable

changeset 17409:b982ab47285e

xenstore: merge xenstore-ls into the multicall binary.

This is done separately from moving the other utilities into the
mutlicall since ls is slightly different and was already separate.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Wed Apr 09 13:31:19 2008 +0100 (2008-04-09)
parents 7259de99f7fd
children cb1f41538756
files tools/xenstore/Makefile tools/xenstore/xenstore_client.c tools/xenstore/xsls.c
line diff
     1.1 --- a/tools/xenstore/Makefile	Wed Apr 09 13:30:37 2008 +0100
     1.2 +++ b/tools/xenstore/Makefile	Wed Apr 09 13:31:19 2008 +0100
     1.3 @@ -13,7 +13,7 @@ CFLAGS += -Wp,-MD,.$(@F).d
     1.4  DEP    = .*.d
     1.5  
     1.6  CLIENTS := xenstore-exists xenstore-list xenstore-read xenstore-rm xenstore-chmod
     1.7 -CLIENTS += xenstore-write
     1.8 +CLIENTS += xenstore-write xenstore-ls
     1.9  
    1.10  XENSTORED_OBJS = xenstored_core.o xenstored_watch.o xenstored_domain.o xenstored_transaction.o xs_lib.o talloc.o utils.o tdb.o hashtable.o
    1.11  
    1.12 @@ -27,14 +27,14 @@ ifneq ($(XENSTORE_STATIC_CLIENTS),y)
    1.13  LIBXENSTORE := libxenstore.so
    1.14  else
    1.15  LIBXENSTORE := libxenstore.a
    1.16 -xenstore xenstore-control xenstore-ls: CFLAGS += -static
    1.17 +xenstore xenstore-control: CFLAGS += -static
    1.18  endif
    1.19  
    1.20  .PHONY: all
    1.21  all: libxenstore.so libxenstore.a xenstored clients xs_tdb_dump 
    1.22  
    1.23  .PHONY: clients
    1.24 -clients: xenstore $(CLIENTS) xenstore-control xenstore-ls
    1.25 +clients: xenstore $(CLIENTS) xenstore-control
    1.26  
    1.27  ifeq ($(CONFIG_SunOS),y)
    1.28  xenstored_probes.h: xenstored_probes.d
    1.29 @@ -60,9 +60,6 @@ xenstore: xenstore_client.o $(LIBXENSTOR
    1.30  xenstore-control: xenstore_control.o $(LIBXENSTORE)
    1.31  	$(CC) $(CFLAGS) $(LDFLAGS) $< -L. -lxenstore $(SOCKET_LIBS) -o $@
    1.32  
    1.33 -xenstore-ls: xsls.o $(LIBXENSTORE)
    1.34 -	$(CC) $(CFLAGS) $(LDFLAGS) $< -L. -lxenstore $(SOCKET_LIBS) -o $@
    1.35 -
    1.36  xs_tdb_dump: xs_tdb_dump.o utils.o tdb.o talloc.o
    1.37  	$(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@
    1.38  
    1.39 @@ -83,7 +80,7 @@ libxenstore.a: xs.o xs_lib.o
    1.40  clean:
    1.41  	rm -f *.a *.o *.opic *.so* xenstored_probes.h
    1.42  	rm -f xenstored xs_random xs_stress xs_crashme
    1.43 -	rm -f xs_tdb_dump xenstore-control xenstore-ls
    1.44 +	rm -f xs_tdb_dump xenstore-control
    1.45  	rm -f xenstore $(CLIENTS)
    1.46  	$(RM) $(DEP)
    1.47  
    1.48 @@ -108,7 +105,6 @@ install: all
    1.49  	set -e ; for c in $(CLIENTS) ; do \
    1.50  		ln -f $(DESTDIR)/usr/bin/xenstore $(DESTDIR)/usr/bin/$${c} ; \
    1.51  	done
    1.52 -	$(INSTALL_PROG) xenstore-ls $(DESTDIR)$(BINDIR)
    1.53  	$(INSTALL_DIR) $(DESTDIR)$(LIBDIR)
    1.54  	$(INSTALL_PROG) libxenstore.so.$(MAJOR).$(MINOR) $(DESTDIR)$(LIBDIR)
    1.55  	ln -sf libxenstore.so.$(MAJOR).$(MINOR) $(DESTDIR)$(LIBDIR)/libxenstore.so.$(MAJOR)
     2.1 --- a/tools/xenstore/xenstore_client.c	Wed Apr 09 13:30:37 2008 +0100
     2.2 +++ b/tools/xenstore/xenstore_client.c	Wed Apr 09 13:31:19 2008 +0100
     2.3 @@ -16,8 +16,12 @@
     2.4  #include <stdio.h>
     2.5  #include <stdlib.h>
     2.6  #include <string.h>
     2.7 +#include <termios.h>
     2.8 +#include <unistd.h>
     2.9  #include <xs.h>
    2.10  
    2.11 +#include <sys/ioctl.h>
    2.12 +
    2.13  #define PATH_SEP '/'
    2.14  #define MAX_PATH_LEN 256
    2.15  
    2.16 @@ -28,6 +32,7 @@ enum mode {
    2.17      MODE_chmod,
    2.18      MODE_exists,
    2.19      MODE_list,
    2.20 +    MODE_ls,
    2.21      MODE_read,
    2.22      MODE_rm,
    2.23      MODE_write,
    2.24 @@ -84,6 +89,9 @@ usage(enum mode mode, int incl_mode, con
    2.25      case MODE_list:
    2.26  	mstr = mstr ? : incl_mode ? "list " : "";
    2.27  	errx(1, "Usage: %s %s[-h] [-s] key [...]", progname, mstr);
    2.28 +    case MODE_ls:
    2.29 +	mstr = mstr ? : incl_mode ? "ls " : "";
    2.30 +	errx(1, "Usage: %s %s[-h] [-s] [path]", progname, mstr);
    2.31      case MODE_chmod:
    2.32  	mstr = incl_mode ? "chmod " : "";
    2.33  	errx(1, "Usage: %s %s[-h] [-s] key <mode [modes...]>", progname, mstr);
    2.34 @@ -103,6 +111,111 @@ do_rm(char *path, struct xs_handle *xsh,
    2.35      }
    2.36  }
    2.37  
    2.38 +#define STRING_MAX XENSTORE_ABS_PATH_MAX+1024
    2.39 +static int max_width = 80;
    2.40 +static int desired_width = 60;
    2.41 +static int show_whole_path = 0;
    2.42 +
    2.43 +#define TAG " = \"...\""
    2.44 +#define TAG_LEN strlen(TAG)
    2.45 +
    2.46 +#define MIN(a, b) (((a) < (b))? (a) : (b))
    2.47 +
    2.48 +void do_ls(struct xs_handle *h, char *path, int cur_depth, int show_perms)
    2.49 +{
    2.50 +    static struct expanding_buffer ebuf;
    2.51 +    char **e;
    2.52 +    char newpath[STRING_MAX], *val;
    2.53 +    int newpath_len;
    2.54 +    int i;
    2.55 +    unsigned int num, len;
    2.56 +
    2.57 +    e = xs_directory(h, XBT_NULL, path, &num);
    2.58 +    if (e == NULL)
    2.59 +        err(1, "xs_directory (%s)", path);
    2.60 +
    2.61 +    for (i = 0; i<num; i++) {
    2.62 +        char buf[MAX_STRLEN(unsigned int)+1];
    2.63 +        struct xs_permissions *perms;
    2.64 +        unsigned int nperms;
    2.65 +        int linewid;
    2.66 +
    2.67 +        /* Compose fullpath */
    2.68 +        newpath_len = snprintf(newpath, sizeof(newpath), "%s%s%s", path, 
    2.69 +                path[strlen(path)-1] == '/' ? "" : "/", 
    2.70 +                e[i]);
    2.71 +
    2.72 +        /* Print indent and path basename */
    2.73 +        linewid = 0;
    2.74 +        if (show_whole_path) {
    2.75 +            fputs(newpath, stdout);
    2.76 +        } else {
    2.77 +            for (; linewid<cur_depth; linewid++) {
    2.78 +                putchar(' ');
    2.79 +            }
    2.80 +            linewid += printf("%.*s",
    2.81 +                              (int) (max_width - TAG_LEN - linewid), e[i]);
    2.82 +        }
    2.83 +
    2.84 +	/* Fetch value */
    2.85 +        if ( newpath_len < sizeof(newpath) ) {
    2.86 +            val = xs_read(h, XBT_NULL, newpath, &len);
    2.87 +        }
    2.88 +        else {
    2.89 +            /* Path was truncated and thus invalid */
    2.90 +            val = NULL;
    2.91 +            len = 0;
    2.92 +        }
    2.93 +
    2.94 +        /* Print value */
    2.95 +        if (val == NULL) {
    2.96 +            printf(":\n");
    2.97 +        }
    2.98 +        else {
    2.99 +            if (max_width < (linewid + len + TAG_LEN)) {
   2.100 +                printf(" = \"%.*s\\...\"",
   2.101 +                       (int)(max_width - TAG_LEN - linewid),
   2.102 +		       sanitise_value(&ebuf, val, len));
   2.103 +            }
   2.104 +            else {
   2.105 +                linewid += printf(" = \"%s\"",
   2.106 +				  sanitise_value(&ebuf, val, len));
   2.107 +                if (show_perms) {
   2.108 +                    putchar(' ');
   2.109 +                    for (linewid++;
   2.110 +                         linewid < MIN(desired_width, max_width);
   2.111 +                         linewid++)
   2.112 +                        putchar((linewid & 1)? '.' : ' ');
   2.113 +                }
   2.114 +            }
   2.115 +        }
   2.116 +        free(val);
   2.117 +
   2.118 +        if (show_perms) {
   2.119 +            perms = xs_get_permissions(h, XBT_NULL, newpath, &nperms);
   2.120 +            if (perms == NULL) {
   2.121 +                warn("\ncould not access permissions for %s", e[i]);
   2.122 +            }
   2.123 +            else {
   2.124 +                int i;
   2.125 +                fputs("  (", stdout);
   2.126 +                for (i = 0; i < nperms; i++) {
   2.127 +                    if (i)
   2.128 +                        putchar(',');
   2.129 +                    xs_perm_to_string(perms+i, buf, sizeof(buf));
   2.130 +                    fputs(buf, stdout);
   2.131 +                }
   2.132 +                putchar(')');
   2.133 +            }
   2.134 +        }
   2.135 +
   2.136 +        putchar('\n');
   2.137 +            
   2.138 +        do_ls(h, newpath, cur_depth+1, show_perms); 
   2.139 +    }
   2.140 +    free(e);
   2.141 +}
   2.142 +
   2.143  static void
   2.144  do_chmod(char *path, struct xs_permissions *perms, int nperms, int upto,
   2.145  	 int recurse, struct xs_handle *xsh, xs_transaction_t xth)
   2.146 @@ -154,6 +267,19 @@ static int
   2.147  perform(enum mode mode, int optind, int argc, char **argv, struct xs_handle *xsh,
   2.148          xs_transaction_t xth, int prefix, int tidy, int upto, int recurse)
   2.149  {
   2.150 +    switch (mode) {
   2.151 +    case MODE_ls:
   2.152 +	if (optind == argc)
   2.153 +	{
   2.154 +	    optind=0;
   2.155 +	    argc=1;
   2.156 +	    argv[0] = "/";
   2.157 +	}
   2.158 +	break;
   2.159 +    default:
   2.160 +	break;
   2.161 +    }
   2.162 +
   2.163      while (optind < argc) {
   2.164          switch (mode) {
   2.165          case MODE_unknown:
   2.166 @@ -256,8 +382,13 @@ perform(enum mode mode, int optind, int 
   2.167                  output("%s\n", list[i]);
   2.168              }
   2.169              free(list);
   2.170 -            optind++;
   2.171 -            break;
   2.172 +	    optind++;
   2.173 +	    break;
   2.174 +	}
   2.175 +	case MODE_ls: {
   2.176 +	    do_ls(xsh, argv[optind], 0, prefix);
   2.177 + 	    optind++;
   2.178 + 	    break;
   2.179          }
   2.180          case MODE_chmod: {
   2.181              struct xs_permissions perms[MAX_PERMS];
   2.182 @@ -311,6 +442,8 @@ static enum mode lookup_mode(const char 
   2.183  	return MODE_exists;
   2.184      else if (strcmp(m, "list") == 0)
   2.185  	return MODE_list;
   2.186 +    else if (strcmp(m, "ls") == 0)
   2.187 +	return MODE_ls;
   2.188      else if (strcmp(m, "rm") == 0)
   2.189  	return MODE_rm;
   2.190      else if (strcmp(m, "write") == 0)
   2.191 @@ -332,6 +465,7 @@ main(int argc, char **argv)
   2.192      int upto = 0;
   2.193      int recurse = 0;
   2.194      int transaction;
   2.195 +    struct winsize ws;
   2.196      enum mode mode;
   2.197  
   2.198      const char *_command = strrchr(argv[0], '/');
   2.199 @@ -365,7 +499,7 @@ main(int argc, char **argv)
   2.200  	    {0, 0, 0, 0}
   2.201  	};
   2.202  
   2.203 -	c = getopt_long(argc - switch_argv, argv + switch_argv, "hsptur",
   2.204 +	c = getopt_long(argc - switch_argv, argv + switch_argv, "fhsptur",
   2.205  			long_options, &index);
   2.206  	if (c == -1)
   2.207  	    break;
   2.208 @@ -374,11 +508,20 @@ main(int argc, char **argv)
   2.209  	case 'h':
   2.210  	    usage(mode, switch_argv, argv[0]);
   2.211  	    /* NOTREACHED */
   2.212 +        case 'f':
   2.213 +	    if ( mode == MODE_read || mode == MODE_list || mode == MODE_ls ) {
   2.214 +		max_width = INT_MAX/2;
   2.215 +		desired_width = 0;
   2.216 +		show_whole_path = 1;
   2.217 +	    } else {
   2.218 +		usage(mode, switch_argv, argv[0]);
   2.219 +	    }
   2.220 +            break;
   2.221          case 's':
   2.222              socket = 1;
   2.223              break;
   2.224  	case 'p':
   2.225 -	    if ( mode == MODE_read || mode == MODE_list )
   2.226 +	    if ( mode == MODE_read || mode == MODE_list || mode == MODE_ls )
   2.227  		prefix = 1;
   2.228  	    else
   2.229  		usage(mode, switch_argv, argv[0]);
   2.230 @@ -404,13 +547,20 @@ main(int argc, char **argv)
   2.231  	}
   2.232      }
   2.233  
   2.234 -    if (optind == argc) {
   2.235 -	usage(mode, switch_argv, argv[0]);
   2.236 -	/* NOTREACHED */
   2.237 -    }
   2.238 -    if (mode == MODE_write && (argc - switch_argv - optind) % 2 == 1) {
   2.239 -	usage(mode, switch_argv, argv[0]);
   2.240 -	/* NOTREACHED */
   2.241 +    switch (mode) {
   2.242 +    case MODE_ls:
   2.243 +	break;
   2.244 +    case MODE_write:
   2.245 +	if ((argc - switch_argv - optind) % 2 == 1) {
   2.246 +	    usage(mode, switch_argv, argv[0]);
   2.247 +	    /* NOTREACHED */
   2.248 +	}
   2.249 +	/* DROP-THRU */
   2.250 +    default:
   2.251 +	if (optind == argc - switch_argv) {
   2.252 +	    usage(mode, switch_argv, argv[0]);
   2.253 +	    /* NOTREACHED */
   2.254 +	}
   2.255      }
   2.256  
   2.257      switch (mode) {
   2.258 @@ -420,11 +570,22 @@ main(int argc, char **argv)
   2.259      case MODE_write:
   2.260  	transaction = (argc - switch_argv - optind) > 2;
   2.261  	break;
   2.262 +    case MODE_ls:
   2.263 +	transaction = 0;
   2.264 +	break;
   2.265      default:
   2.266  	transaction = 1;
   2.267  	break;
   2.268      }
   2.269  
   2.270 +    if ( mode == MODE_ls )
   2.271 +    {
   2.272 +	memset(&ws, 0, sizeof(ws));
   2.273 +	ret = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
   2.274 +	if (!ret)
   2.275 +	    max_width = ws.ws_col - 2;
   2.276 +    }
   2.277 +
   2.278      xsh = socket ? xs_daemon_open() : xs_domain_open();
   2.279      if (xsh == NULL)
   2.280  	err(1, socket ? "xs_daemon_open" : "xs_domain_open");
     3.1 --- a/tools/xenstore/xsls.c	Wed Apr 09 13:30:37 2008 +0100
     3.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.3 @@ -1,171 +0,0 @@
     3.4 -#include <stdio.h>
     3.5 -#include <stdlib.h>
     3.6 -#include <string.h>
     3.7 -#include <err.h>
     3.8 -#include <xs.h>
     3.9 -#include <getopt.h>
    3.10 -#include <unistd.h>
    3.11 -#include <sys/ioctl.h>
    3.12 -#include <termios.h>
    3.13 -
    3.14 -#define STRING_MAX XENSTORE_ABS_PATH_MAX+1024
    3.15 -static int max_width = 80;
    3.16 -static int desired_width = 60;
    3.17 -static int show_whole_path = 0;
    3.18 -
    3.19 -#define TAG " = \"...\""
    3.20 -#define TAG_LEN strlen(TAG)
    3.21 -
    3.22 -#define MIN(a, b) (((a) < (b))? (a) : (b))
    3.23 -
    3.24 -void print_dir(struct xs_handle *h, char *path, int cur_depth, int show_perms)
    3.25 -{
    3.26 -    static struct expanding_buffer ebuf;
    3.27 -    char **e;
    3.28 -    char newpath[STRING_MAX], *val;
    3.29 -    int newpath_len;
    3.30 -    int i;
    3.31 -    unsigned int num, len;
    3.32 -
    3.33 -    e = xs_directory(h, XBT_NULL, path, &num);
    3.34 -    if (e == NULL)
    3.35 -        err(1, "xs_directory (%s)", path);
    3.36 -
    3.37 -    for (i = 0; i<num; i++) {
    3.38 -        char buf[MAX_STRLEN(unsigned int)+1];
    3.39 -        struct xs_permissions *perms;
    3.40 -        unsigned int nperms;
    3.41 -        int linewid;
    3.42 -
    3.43 -        /* Compose fullpath */
    3.44 -        newpath_len = snprintf(newpath, sizeof(newpath), "%s%s%s", path, 
    3.45 -                path[strlen(path)-1] == '/' ? "" : "/", 
    3.46 -                e[i]);
    3.47 -
    3.48 -        /* Print indent and path basename */
    3.49 -        linewid = 0;
    3.50 -        if (show_whole_path) {
    3.51 -            fputs(newpath, stdout);
    3.52 -        } else {
    3.53 -            for (; linewid<cur_depth; linewid++) {
    3.54 -                putchar(' ');
    3.55 -            }
    3.56 -            linewid += printf("%.*s",
    3.57 -                              (int) (max_width - TAG_LEN - linewid), e[i]);
    3.58 -        }
    3.59 -
    3.60 -	/* Fetch value */
    3.61 -        if ( newpath_len < sizeof(newpath) ) {
    3.62 -            val = xs_read(h, XBT_NULL, newpath, &len);
    3.63 -        }
    3.64 -        else {
    3.65 -            /* Path was truncated and thus invalid */
    3.66 -            val = NULL;
    3.67 -            len = 0;
    3.68 -        }
    3.69 -
    3.70 -        /* Print value */
    3.71 -        if (val == NULL) {
    3.72 -            printf(":\n");
    3.73 -        }
    3.74 -        else {
    3.75 -            if (max_width < (linewid + len + TAG_LEN)) {
    3.76 -                printf(" = \"%.*s\\...\"",
    3.77 -                       (int)(max_width - TAG_LEN - linewid),
    3.78 -		       sanitise_value(&ebuf, val, len));
    3.79 -            }
    3.80 -            else {
    3.81 -                linewid += printf(" = \"%s\"",
    3.82 -				  sanitise_value(&ebuf, val, len));
    3.83 -                if (show_perms) {
    3.84 -                    putchar(' ');
    3.85 -                    for (linewid++;
    3.86 -                         linewid < MIN(desired_width, max_width);
    3.87 -                         linewid++)
    3.88 -                        putchar((linewid & 1)? '.' : ' ');
    3.89 -                }
    3.90 -            }
    3.91 -        }
    3.92 -        free(val);
    3.93 -
    3.94 -        if (show_perms) {
    3.95 -            perms = xs_get_permissions(h, XBT_NULL, newpath, &nperms);
    3.96 -            if (perms == NULL) {
    3.97 -                warn("\ncould not access permissions for %s", e[i]);
    3.98 -            }
    3.99 -            else {
   3.100 -                int i;
   3.101 -                fputs("  (", stdout);
   3.102 -                for (i = 0; i < nperms; i++) {
   3.103 -                    if (i)
   3.104 -                        putchar(',');
   3.105 -                    xs_perm_to_string(perms+i, buf, sizeof(buf));
   3.106 -                    fputs(buf, stdout);
   3.107 -                }
   3.108 -                putchar(')');
   3.109 -            }
   3.110 -        }
   3.111 -
   3.112 -        putchar('\n');
   3.113 -            
   3.114 -        print_dir(h, newpath, cur_depth+1, show_perms); 
   3.115 -    }
   3.116 -    free(e);
   3.117 -}
   3.118 -
   3.119 -void usage(int argc, char *argv[])
   3.120 -{
   3.121 -    fprintf(stderr, "Usage: %s [-w] [-p] [-f] [-s] [path]\n", argv[0]);
   3.122 -}
   3.123 -
   3.124 -int main(int argc, char *argv[])
   3.125 -{
   3.126 -    struct winsize ws;
   3.127 -    int ret, c, socket = 0, show_perm = 0;
   3.128 -    struct xs_handle *xsh;
   3.129 -
   3.130 -#define PAD 2
   3.131 -
   3.132 -    memset(&ws, 0, sizeof(ws));
   3.133 -    ret = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
   3.134 -    if (!ret)
   3.135 -        max_width = ws.ws_col - PAD;
   3.136 -
   3.137 -    while (0 < (c = getopt(argc, argv, "pswf"))) {
   3.138 -        switch (c) {
   3.139 -        case 'w':
   3.140 -            max_width= STRING_MAX - PAD;
   3.141 -            desired_width = 0;
   3.142 -            break;
   3.143 -        case 'p':
   3.144 -            show_perm = 1;
   3.145 -            break;
   3.146 -        case 's':
   3.147 -            socket = 1;
   3.148 -            break;
   3.149 -        case 'f':
   3.150 -            max_width = INT_MAX/2;
   3.151 -            desired_width = 0;
   3.152 -            show_whole_path = 1;
   3.153 -            break;
   3.154 -        case ':':
   3.155 -        case '?':
   3.156 -        default:
   3.157 -            usage(argc, argv);
   3.158 -            return 0;
   3.159 -        }
   3.160 -    }
   3.161 -
   3.162 -    /* Adjust the width here to avoid argument order dependency */
   3.163 -    if ( show_perm ) {
   3.164 -        max_width -= 16;
   3.165 -    }
   3.166 -
   3.167 -    xsh = socket ? xs_daemon_open() : xs_domain_open();
   3.168 -    if (xsh == NULL)
   3.169 -        err(1, socket ? "xs_daemon_open" : "xs_domain_open");
   3.170 -
   3.171 -    print_dir(xsh, (argc - optind) == 1 ? argv[optind] : "/", 0, show_perm);
   3.172 -
   3.173 -    return 0;
   3.174 -}