src/main.c
changeset 364 66ec30bde5e5
parent 359 c9c90315ea24
child 369 f8c27fe9fe63
     1.1 --- a/src/main.c	Wed Apr 22 15:09:17 2009 +0100
     1.2 +++ b/src/main.c	Sat May 09 21:30:22 2009 +0100
     1.3 @@ -49,6 +49,10 @@
     1.4  
     1.5  #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
     1.6  
     1.7 +static int
     1.8 +update_packages(struct razor_set *system, struct razor_set *next,
     1.9 +		struct razor_relocations *relocations);
    1.10 +
    1.11  static struct razor_package_iterator *
    1.12  create_iterator_from_argv(struct razor_set *set, int argc, const char *argv[])
    1.13  {
    1.14 @@ -561,35 +565,47 @@
    1.15  static int
    1.16  command_remove(int argc, const char *argv[])
    1.17  {
    1.18 -	struct razor_set *set, *upstream;
    1.19 +	struct razor_root *root;
    1.20 +	struct razor_set *system, *upstream, *next;
    1.21  	struct razor_transaction *trans;
    1.22  	int i, errors;
    1.23  
    1.24 -	set = razor_root_open_read_only(install_root);
    1.25 -	if (set == NULL)
    1.26 +	root = razor_root_open(install_root);
    1.27 +	system = razor_root_get_system_set(root);
    1.28 +	if (system == NULL) {
    1.29 +		razor_root_close(root);
    1.30  		return 1;
    1.31 +	}
    1.32  
    1.33 -	upstream = razor_set_create();
    1.34 -	trans = razor_transaction_create(set, upstream);
    1.35 +	upstream = razor_set_create_without_root();
    1.36 +	trans = razor_transaction_create(system, upstream);
    1.37  	for (i = 0; i < argc; i++) {
    1.38 -		if (mark_packages_for_removal(trans, set, argv[i]) == 0) {
    1.39 +		if (mark_packages_for_removal(trans, system, argv[i]) == 0) {
    1.40  			fprintf(stderr, "no match for %s\n", argv[i]);
    1.41 +			razor_transaction_destroy(trans);
    1.42 +			razor_set_destroy(upstream);
    1.43 +			razor_root_close(root);
    1.44  			return 1;
    1.45  		}
    1.46  	}
    1.47  
    1.48  	razor_transaction_resolve(trans);
    1.49  	errors = razor_transaction_describe(trans);
    1.50 -	if (errors)
    1.51 +	if (errors) {
    1.52 +		razor_transaction_destroy(trans);
    1.53 +		razor_set_destroy(upstream);
    1.54 +		razor_root_close(root);
    1.55  		return 1;
    1.56 +	}
    1.57  
    1.58 -	set = razor_transaction_finish(trans);
    1.59 -	razor_set_write(set, updated_repo_filename, RAZOR_REPO_FILE_MAIN);
    1.60 -	razor_set_destroy(set);
    1.61 +	next = razor_transaction_finish(trans);
    1.62 +	update_packages(system, next, NULL);
    1.63 +	razor_root_update(root, next);
    1.64 +
    1.65 +	razor_set_destroy(next);
    1.66  	razor_set_destroy(upstream);
    1.67 -	printf("wrote system-updated.rzdb\n");
    1.68  
    1.69 -	return 0;
    1.70 +	return razor_root_commit(root);
    1.71  }
    1.72  
    1.73  static void
    1.74 @@ -812,51 +828,88 @@
    1.75  }
    1.76  
    1.77  static int
    1.78 -install_packages(struct razor_set *system, struct razor_set *next,
    1.79 -		 struct razor_relocations *relocations)
    1.80 +install_package(struct razor_set *set, struct razor_package *package,
    1.81 +		struct razor_relocations *relocations)
    1.82 +{
    1.83 +	int retval;
    1.84 +	const char *name, *version, *arch;
    1.85 +	char file[PATH_MAX];
    1.86 +	struct razor_rpm *rpm;
    1.87 +
    1.88 +	razor_package_get_details(set, package,
    1.89 +				  RAZOR_DETAIL_NAME, &name,
    1.90 +				  RAZOR_DETAIL_VERSION, &version,
    1.91 +				  RAZOR_DETAIL_ARCH, &arch,
    1.92 +				  RAZOR_DETAIL_LAST);
    1.93 +
    1.94 +	printf("install %s-%s\n", name, version);
    1.95 +
    1.96 +	snprintf(file, sizeof file,
    1.97 +		 "rpms/%s", rpm_filename(name, version, arch));
    1.98 +	rpm = razor_rpm_open(file);
    1.99 +	if (rpm == NULL) {
   1.100 +		fprintf(stderr, "failed to open rpm %s\n", file);
   1.101 +		return -1;
   1.102 +	}
   1.103 +	if (relocations)
   1.104 +		razor_rpm_set_relocations(rpm, relocations);
   1.105 +	retval = razor_rpm_install(rpm, install_root);
   1.106 +	if (retval < 0)
   1.107 +		fprintf(stderr, "failed to install rpm %s\n", file);
   1.108 +	razor_rpm_close(rpm);
   1.109 +	return retval;
   1.110 +}
   1.111 +
   1.112 +static int
   1.113 +remove_package(struct razor_set *set, struct razor_package *package)
   1.114 +{
   1.115 +	struct razor_file_iterator *fi;
   1.116 +	struct razor_package_iterator *pi;
   1.117 +	struct razor_package *p;
   1.118 +	char buffer[PATH_MAX];
   1.119 +	const char *name;
   1.120 +	int retval = 0, count;
   1.121 +
   1.122 +	fi = razor_file_iterator_create(set, package);
   1.123 +
   1.124 +	while (!retval && razor_file_iterator_next(fi, &name)) {
   1.125 +		pi = razor_package_iterator_create_for_file(set, name);
   1.126 +		count = 0;
   1.127 +		while (razor_package_iterator_next(pi, &p, RAZOR_DETAIL_LAST))
   1.128 +			count++;
   1.129 +		razor_package_iterator_destroy(pi);
   1.130 +		if (count <= 1) {
   1.131 +			snprintf(buffer, sizeof buffer, "%s%s", install_root,
   1.132 +				 name);
   1.133 +			retval = remove(buffer);
   1.134 +		}
   1.135 +	}
   1.136 +
   1.137 +	razor_file_iterator_destroy(fi);
   1.138 +	return retval;
   1.139 +}
   1.140 +
   1.141 +static int
   1.142 +update_packages(struct razor_set *system, struct razor_set *next,
   1.143 +		struct razor_relocations *relocations)
   1.144  {
   1.145  	struct razor_install_iterator *ii;
   1.146  	struct razor_package *package;
   1.147  	struct razor_set *set;
   1.148  	enum razor_install_action action;
   1.149 -	struct razor_rpm *rpm;
   1.150 -	const char *name, *version, *arch;
   1.151 -	char file[PATH_MAX];
   1.152 -	int count;
   1.153 +	int retval = 0, count;
   1.154  
   1.155  	ii = razor_set_create_install_iterator(system, next);
   1.156 -	while (razor_install_iterator_next(ii, &set, &package,
   1.157 -					   &action, &count)) {
   1.158 -		if (action == RAZOR_INSTALL_ACTION_REMOVE)
   1.159 -			continue;
   1.160 -
   1.161 -		razor_package_get_details(set, package,
   1.162 -					  RAZOR_DETAIL_NAME, &name,
   1.163 -					  RAZOR_DETAIL_VERSION, &version,
   1.164 -					  RAZOR_DETAIL_ARCH, &arch,
   1.165 -					  RAZOR_DETAIL_LAST);
   1.166 -
   1.167 -		printf("install %s-%s\n", name, version);
   1.168 -
   1.169 -		snprintf(file, sizeof file,
   1.170 -			 "rpms/%s", rpm_filename(name, version, arch));
   1.171 -		rpm = razor_rpm_open(file);
   1.172 -		if (rpm == NULL) {
   1.173 -			fprintf(stderr, "failed to open rpm %s\n", file);
   1.174 -			return -1;
   1.175 -		}
   1.176 -		if (relocations)
   1.177 -			razor_rpm_set_relocations(rpm, relocations);
   1.178 -		if (razor_rpm_install(rpm, install_root) < 0) {
   1.179 -			fprintf(stderr,
   1.180 -				"failed to install rpm %s\n", file);
   1.181 -			return -1;
   1.182 -		}
   1.183 -		razor_rpm_close(rpm);
   1.184 +	while (!retval && razor_install_iterator_next(ii, &set, &package,
   1.185 +						      &action, &count)) {
   1.186 +		if (action == RAZOR_INSTALL_ACTION_ADD)
   1.187 +			retval = install_package(set, package, relocations);
   1.188 +		else if (action == RAZOR_INSTALL_ACTION_REMOVE)
   1.189 +			retval = remove_package(set, package);
   1.190  	}
   1.191  	razor_install_iterator_destroy(ii);
   1.192  
   1.193 -	return 0;
   1.194 +	return retval;
   1.195  }
   1.196  
   1.197  static int
   1.198 @@ -949,7 +1002,7 @@
   1.199                  return 1;
   1.200          }
   1.201  
   1.202 -	install_packages(system, next, relocations);
   1.203 +	update_packages(system, next, relocations);
   1.204  
   1.205  	if (relocations)
   1.206  		razor_relocations_destroy(relocations);