ia64/xen-unstable

changeset 7353:41c970cf82ba

Fix the handling of EAGAIN from the transaction_end; we were failing to do any
work if the transaction caused a retry, because optind had been obliterated.
Achieve this by splitting the main body of the loop off to a separate function,
meaning that we get rid of the goto out at the same time.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@ewan
date Wed Oct 12 12:03:03 2005 +0100 (2005-10-12)
parents b719806d1265
children ffbee4415bf7
files tools/xenstore/xenstore_client.c
line diff
     1.1 --- a/tools/xenstore/xenstore_client.c	Wed Oct 12 11:51:52 2005 +0100
     1.2 +++ b/tools/xenstore/xenstore_client.c	Wed Oct 12 12:03:03 2005 +0100
     1.3 @@ -4,6 +4,7 @@
     1.4   * this archive for more details.
     1.5   *
     1.6   * Copyright (C) 2005 by Christian Limpach
     1.7 + * Copyright (C) 2005 XenSource Ltd.
     1.8   *
     1.9   */
    1.10  
    1.11 @@ -28,16 +29,71 @@ usage(const char *progname)
    1.12  #endif
    1.13  }
    1.14  
    1.15 +
    1.16 +static int
    1.17 +perform(int optind, int argc, char **argv, struct xs_handle *xsh,
    1.18 +        struct xs_transaction_handle *xth, int prefix)
    1.19 +{
    1.20 +    while (optind < argc) {
    1.21 +#if defined(CLIENT_read)
    1.22 +	char *val = xs_read(xsh, xth, argv[optind], NULL);
    1.23 +	if (val == NULL) {
    1.24 +	    warnx("couldn't read path %s", argv[optind]);
    1.25 +	    return 1;
    1.26 +	}
    1.27 +	if (prefix)
    1.28 +	    printf("%s: ", argv[optind]);
    1.29 +	printf("%s\n", val);
    1.30 +	free(val);
    1.31 +	optind++;
    1.32 +#elif defined(CLIENT_write)
    1.33 +	if (!xs_write(xsh, xth, argv[optind], argv[optind + 1],
    1.34 +                      strlen(argv[optind + 1]))) {
    1.35 +	    warnx("could not write path %s", argv[optind]);
    1.36 +	    return 1;
    1.37 +	}
    1.38 +	optind += 2;
    1.39 +#elif defined(CLIENT_rm)
    1.40 +	if (!xs_rm(xsh, xth, argv[optind])) {
    1.41 +	    warnx("could not remove path %s", argv[optind]);
    1.42 +	    return 1;
    1.43 +	}
    1.44 +	optind++;
    1.45 +#elif defined(CLIENT_exists)
    1.46 +	char *val = xs_read(xsh, xth, argv[optind], NULL);
    1.47 +	if (val == NULL) {
    1.48 +	    return 1;
    1.49 +	}
    1.50 +	free(val);
    1.51 +	optind++;
    1.52 +#elif defined(CLIENT_list)
    1.53 +	unsigned int i, num;
    1.54 +	char **list = xs_directory(xsh, xth, argv[optind], &num);
    1.55 +	if (list == NULL) {
    1.56 +	    warnx("could not list path %s", argv[optind]);
    1.57 +	    return 1;
    1.58 +	}
    1.59 +	for (i = 0; i < num; i++) {
    1.60 +	    if (prefix)
    1.61 +		printf("%s/", argv[optind]);
    1.62 +	    printf("%s\n", list[i]);
    1.63 +	}
    1.64 +	free(list);
    1.65 +	optind++;
    1.66 +#endif
    1.67 +    }
    1.68 +
    1.69 +    return 0;
    1.70 +}
    1.71 +
    1.72 +
    1.73  int
    1.74  main(int argc, char **argv)
    1.75  {
    1.76      struct xs_handle *xsh;
    1.77      struct xs_transaction_handle *xth;
    1.78 -    bool success;
    1.79      int ret = 0, socket = 0;
    1.80 -#if defined(CLIENT_read) || defined(CLIENT_list)
    1.81      int prefix = 0;
    1.82 -#endif
    1.83  
    1.84      while (1) {
    1.85  	int c, index = 0;
    1.86 @@ -93,65 +149,9 @@ main(int argc, char **argv)
    1.87      if (xth == NULL)
    1.88  	errx(1, "couldn't start transaction");
    1.89  
    1.90 -    while (optind < argc) {
    1.91 -#if defined(CLIENT_read)
    1.92 -	char *val = xs_read(xsh, xth, argv[optind], NULL);
    1.93 -	if (val == NULL) {
    1.94 -	    warnx("couldn't read path %s", argv[optind]);
    1.95 -	    ret = 1;
    1.96 -	    goto out;
    1.97 -	}
    1.98 -	if (prefix)
    1.99 -	    printf("%s: ", argv[optind]);
   1.100 -	printf("%s\n", val);
   1.101 -	free(val);
   1.102 -	optind++;
   1.103 -#elif defined(CLIENT_write)
   1.104 -	success = xs_write(xsh, xth, argv[optind], argv[optind + 1],
   1.105 -			   strlen(argv[optind + 1]));
   1.106 -	if (!success) {
   1.107 -	    warnx("could not write path %s", argv[optind]);
   1.108 -	    ret = 1;
   1.109 -	    goto out;
   1.110 -	}
   1.111 -	optind += 2;
   1.112 -#elif defined(CLIENT_rm)
   1.113 -	success = xs_rm(xsh, xth, argv[optind]);
   1.114 -	if (!success) {
   1.115 -	    warnx("could not remove path %s", argv[optind]);
   1.116 -	    ret = 1;
   1.117 -	    goto out;
   1.118 -	}
   1.119 -	optind++;
   1.120 -#elif defined(CLIENT_exists)
   1.121 -	char *val = xs_read(xsh, xth, argv[optind], NULL);
   1.122 -	if (val == NULL) {
   1.123 -	    ret = 1;
   1.124 -	    goto out;
   1.125 -	}
   1.126 -	free(val);
   1.127 -	optind++;
   1.128 -#elif defined(CLIENT_list)
   1.129 -	unsigned int i, num;
   1.130 -	char **list = xs_directory(xsh, xth, argv[optind], &num);
   1.131 -	if (list == NULL) {
   1.132 -	    warnx("could not list path %s", argv[optind]);
   1.133 -	    ret = 1;
   1.134 -	    goto out;
   1.135 -	}
   1.136 -	for (i = 0; i < num; i++) {
   1.137 -	    if (prefix)
   1.138 -		printf("%s/", argv[optind]);
   1.139 -	    printf("%s\n", list[i]);
   1.140 -	}
   1.141 -	free(list);
   1.142 -	optind++;
   1.143 -#endif
   1.144 -    }
   1.145 +    ret = perform(optind, argc, argv, xsh, xth, prefix);
   1.146  
   1.147 - out:
   1.148 -    success = xs_transaction_end(xsh, xth, ret ? true : false);
   1.149 -    if (!success) {
   1.150 +    if (!xs_transaction_end(xsh, xth, ret)) {
   1.151  	if (ret == 0 && errno == EAGAIN)
   1.152  	    goto again;
   1.153  	errx(1, "couldn't end transaction");