Introduce install/remove iterators.
authorKristian H?gsberg <krh@redhat.com>
Fri Jun 20 21:56:43 2008 -0400 (2008-06-20)
changeset 254ccb1c11968ab
parent 253 338a577cdfd2
child 255 5cd6aa72bbd5
Introduce install/remove iterators.

These iterator constructors lets you pass in two sets and creates
an iterator for the packages to remove or the packages to install.
The iterators will step through the packages in a sequence that respects
the pre, post, preun and postun modifiers.

Right now, the install order isn't actually implemented, this patch just
implements the API changes and updates the applications.
librazor/razor.c
librazor/razor.h
src/main.c
     1.1 --- a/librazor/razor.c	Fri Jun 20 21:38:29 2008 -0400
     1.2 +++ b/librazor/razor.c	Fri Jun 20 21:56:43 2008 -0400
     1.3 @@ -463,3 +463,55 @@
     1.4  	razor_package_iterator_destroy(pi1);
     1.5  	razor_package_iterator_destroy(pi2);
     1.6  }
     1.7 +
     1.8 +static void
     1.9 +add_new_package(enum razor_diff_action action,
    1.10 +		struct razor_package *package,
    1.11 +		const char *name,
    1.12 +		const char *version,
    1.13 +		const char *arch,
    1.14 +		void *data)
    1.15 +{
    1.16 +	if (action == RAZOR_DIFF_ACTION_ADD)
    1.17 +		razor_package_query_add_package(data, package);
    1.18 +}
    1.19 +
    1.20 +struct razor_package_iterator *
    1.21 +razor_set_create_remove_iterator(struct razor_set *set,
    1.22 +				 struct razor_set *next)
    1.23 +{
    1.24 +	struct razor_package_query *query;
    1.25 +	struct razor_package_iterator *pi;
    1.26 +
    1.27 +	query = razor_package_query_create(set);
    1.28 +	razor_set_diff(next, set, add_new_package, query);
    1.29 +
    1.30 +	pi = razor_package_query_finish(query);
    1.31 +
    1.32 +	/* FIXME: We need to figure out the right install order here,
    1.33 +	 * so the post and pre scripts can run. */
    1.34 +
    1.35 +	/* sort */
    1.36 +
    1.37 +	return pi;
    1.38 +}
    1.39 +
    1.40 +struct razor_package_iterator *
    1.41 +razor_set_create_install_iterator(struct razor_set *set,
    1.42 +				  struct razor_set *next)
    1.43 +{
    1.44 +	struct razor_package_query *query;
    1.45 +	struct razor_package_iterator *pi;
    1.46 +
    1.47 +	query = razor_package_query_create(next);
    1.48 +	razor_set_diff(set, next, add_new_package, query);
    1.49 +
    1.50 +	pi = razor_package_query_finish(query);
    1.51 +
    1.52 +	/* FIXME: We need to figure out the right install order here,
    1.53 +	 * so the post and pre scripts can run. */
    1.54 +
    1.55 +	/* sort */
    1.56 +
    1.57 +	return pi;
    1.58 +}
     2.1 --- a/librazor/razor.h	Fri Jun 20 21:38:29 2008 -0400
     2.2 +++ b/librazor/razor.h	Fri Jun 20 21:56:43 2008 -0400
     2.3 @@ -121,6 +121,12 @@
     2.4  void
     2.5  razor_set_diff(struct razor_set *set, struct razor_set *upstream,
     2.6  	       razor_diff_callback_t callback, void *data);
     2.7 +struct razor_package_iterator *
     2.8 +razor_set_create_remove_iterator(struct razor_set *set,
     2.9 +				 struct razor_set *next);
    2.10 +struct razor_package_iterator *
    2.11 +razor_set_create_install_iterator(struct razor_set *set,
    2.12 +				  struct razor_set *next);
    2.13  
    2.14  /* Package transactions */
    2.15  
     3.1 --- a/src/main.c	Fri Jun 20 21:38:29 2008 -0400
     3.2 +++ b/src/main.c	Fri Jun 20 21:56:43 2008 -0400
     3.3 @@ -578,74 +578,86 @@
     3.4  	return 0;
     3.5  }
     3.6  
     3.7 -static void
     3.8 -download_package(enum razor_diff_action action,
     3.9 -		 struct razor_package *package,
    3.10 -		 const char *name,
    3.11 -		 const char *version,
    3.12 -		 const char *arch,
    3.13 -		 void *data)
    3.14 +static const char *
    3.15 +rpm_filename(const char *name, const char *version, const char *arch)
    3.16  {
    3.17 -	char file[PATH_MAX], url[256];
    3.18 -	const char *v;
    3.19 -	int *errors = data;
    3.20 -
    3.21 -	if (action != RAZOR_DIFF_ACTION_ADD)
    3.22 -		return;
    3.23 -
    3.24 -	/* Skip epoch */
    3.25 +	static char file[PATH_MAX];
    3.26 + 	const char *v;
    3.27 + 
    3.28 + 	/* Skip epoch */
    3.29  	v = strchr(version, ':');
    3.30 -	if (v != NULL)
    3.31 -		v = v + 1;
    3.32 -	else
    3.33 + 	if (v != NULL)
    3.34 + 		v = v + 1;
    3.35 + 	else
    3.36  		v = version;
    3.37  
    3.38 -	snprintf(url, sizeof url,
    3.39 -		 "%s/Packages/%s-%s.%s.rpm", yum_url, name, v, arch);
    3.40 -	snprintf(file, sizeof file,
    3.41 -		 "rpms/%s-%s.%s.rpm", name, v, arch);
    3.42 -	if (download_if_missing(url, file) < 0)
    3.43 -		(*errors)++;
    3.44 +	snprintf(file, sizeof file, "%s-%s.%s.rpm", name, v, arch);
    3.45 +
    3.46 +	return file;
    3.47  }
    3.48  
    3.49 -static void
    3.50 -install_package(enum razor_diff_action action,
    3.51 -		struct razor_package *package,
    3.52 -		const char *name,
    3.53 -		const char *version,
    3.54 -		const char *arch,
    3.55 -		void *data)
    3.56 +static int
    3.57 +download_packages(struct razor_set *system, struct razor_set *next)
    3.58  {
    3.59 -	const char *v, *root = data;
    3.60 +	struct razor_package_iterator *pi;
    3.61 +	struct razor_package *package;
    3.62 +	const char *name, *version, *arch;
    3.63 +	char file[PATH_MAX], url[256];
    3.64 +	int errors;
    3.65 + 
    3.66 +	pi = razor_set_create_install_iterator(system, next);
    3.67 +	errors = 0;
    3.68 +	while (razor_package_iterator_next(pi, &package,
    3.69 +					   &name, &version, &arch)) {
    3.70 +		snprintf(url, sizeof url,
    3.71 +			 "%s/Packages/%s",
    3.72 +			 yum_url, rpm_filename(name, version, arch));
    3.73 +		snprintf(file, sizeof file,
    3.74 +			 "rpms/%s", rpm_filename(name, version, arch));
    3.75 +		if (download_if_missing(url, file) < 0)
    3.76 +			errors++;
    3.77 +	}
    3.78 +	razor_package_iterator_destroy(pi);
    3.79 +
    3.80 +	if (errors > 0) {
    3.81 +		fprintf(stderr, "failed to download %d packages\n", errors);
    3.82 +                return -1;
    3.83 +        }
    3.84 +
    3.85 +	return 0;
    3.86 +}
    3.87 +
    3.88 +static int
    3.89 +install_packages(struct razor_set *system, struct razor_set *next)
    3.90 +{
    3.91 +	struct razor_package_iterator *pi;
    3.92 +	struct razor_package *package;
    3.93 +	struct razor_rpm *rpm;
    3.94 +	const char *name, *version, *arch;
    3.95  	char file[PATH_MAX];
    3.96 -	struct razor_rpm *rpm;
    3.97  
    3.98 -	if (action == RAZOR_DIFF_ACTION_REMOVE) {
    3.99 -		printf("removing %s %s not handled\n", name, version);
   3.100 -		return;
   3.101 +	pi = razor_set_create_install_iterator(system, next);
   3.102 +	while (razor_package_iterator_next(pi, &package,
   3.103 +					   &name, &version, &arch)) {
   3.104 +		printf("install %s-%s\n", name, version);
   3.105 +
   3.106 +		snprintf(file, sizeof file,
   3.107 +			 "rpms/%s", rpm_filename(name, version, arch));
   3.108 +		rpm = razor_rpm_open(file);
   3.109 +		if (rpm == NULL) {
   3.110 +			fprintf(stderr, "failed to open rpm %s\n", file);
   3.111 +			return -1;
   3.112 +		}
   3.113 +		if (razor_rpm_install(rpm, install_root) < 0) {
   3.114 +			fprintf(stderr,
   3.115 +				"failed to install rpm %s\n", file);
   3.116 +			return -1;
   3.117 +		}
   3.118 +		razor_rpm_close(rpm);
   3.119  	}
   3.120 +	razor_package_iterator_destroy(pi);
   3.121  
   3.122 -	/* Skip epoch */
   3.123 -	v = strchr(version, ':');
   3.124 -	if (v != NULL)
   3.125 -		v = v + 1;
   3.126 -	else
   3.127 -		v = version;
   3.128 -
   3.129 -	printf("install %s %s\n", name, v);
   3.130 -	snprintf(file, sizeof file, "rpms/%s-%s.%s.rpm", name, v, arch);
   3.131 -
   3.132 - 	rpm = razor_rpm_open(file);
   3.133 -	if (rpm == NULL) {
   3.134 -		fprintf(stderr, "failed to open rpm %s\n", file);
   3.135 -		return;
   3.136 -	}
   3.137 -	if (razor_rpm_install(rpm, root) < 0) {
   3.138 -		fprintf(stderr,
   3.139 -			"failed to install rpm %s\n", file);
   3.140 -		return;
   3.141 -	}
   3.142 -	razor_rpm_close(rpm);
   3.143 +	return 0;
   3.144  }
   3.145  
   3.146  static int
   3.147 @@ -692,17 +704,12 @@
   3.148  		return 1;
   3.149  	}
   3.150  
   3.151 -	errors = 0;
   3.152 -	razor_set_diff(system, next, download_package, &errors);
   3.153 -	if (errors > 0) {
   3.154 -		fprintf(stderr, "failed to download %d packages\n", errors);
   3.155 +	if (download_packages(system, next) < 0) {
   3.156  		razor_root_close(root);
   3.157                  return 1;
   3.158          }
   3.159  
   3.160 -	/* FIXME: We need to figure out the right install order here,
   3.161 -	 * so the post and pre scripts can run. */
   3.162 -	razor_set_diff(system, next, install_package, (void *) install_root);
   3.163 +	install_packages(system, next);
   3.164  
   3.165  	razor_set_destroy(next);
   3.166  	razor_set_destroy(upstream);