main.c
changeset 195 7a53d1711083
parent 193 2d0d5e7dd439
child 196 b38fc517ea04
     1.1 --- a/main.c	Sun Apr 06 23:36:45 2008 -0400
     1.2 +++ b/main.c	Mon Apr 07 00:02:01 2008 -0400
     1.3 @@ -245,13 +245,18 @@
     1.4  }
     1.5  
     1.6  static int
     1.7 -download_if_missing(CURL *curl, const char *url, const char *file)
     1.8 +download_if_missing(const char *url, const char *file)
     1.9  {
    1.10 +	CURL *curl;
    1.11  	struct stat buf;
    1.12  	char error[256];
    1.13  	FILE *fp;
    1.14  	CURLcode res;
    1.15  
    1.16 +	curl = curl_easy_init();
    1.17 +	if (curl == NULL)
    1.18 +		return 1;
    1.19 +
    1.20  	curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
    1.21  	curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
    1.22  	curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, show_progress);
    1.23 @@ -271,6 +276,8 @@
    1.24  		fprintf(stderr, "\n");
    1.25  	}
    1.26  
    1.27 +	curl_easy_cleanup(curl);
    1.28 +
    1.29  	return 0;
    1.30  }
    1.31  
    1.32 @@ -281,21 +288,13 @@
    1.33  command_import_yum(int argc, const char *argv[])
    1.34  {
    1.35  	struct razor_set *set;
    1.36 -	CURL *curl;
    1.37  
    1.38 -	curl = curl_easy_init();
    1.39 -	if (curl == NULL)
    1.40 -		return 1;
    1.41 -
    1.42 -	if (download_if_missing(curl,
    1.43 -				REPO_URL "/repodata/primary.xml.gz",
    1.44 +	if (download_if_missing(REPO_URL "/repodata/primary.xml.gz",
    1.45  				"primary.xml.gz") < 0)
    1.46  		return -1;
    1.47 -	if (download_if_missing(curl,
    1.48 -				REPO_URL "/repodata/filelists.xml.gz",
    1.49 +	if (download_if_missing(REPO_URL "/repodata/filelists.xml.gz",
    1.50  				"filelists.xml.gz") < 0)
    1.51  		return -1;
    1.52 -	curl_easy_cleanup(curl);
    1.53  
    1.54  	set = razor_set_create_from_yum();
    1.55  	if (set == NULL)
    1.56 @@ -473,49 +472,57 @@
    1.57  	return 0;
    1.58  }
    1.59  
    1.60 -static struct razor_set *
    1.61 -create_set_from_rpms(int argc, const char *argv[])
    1.62 +static void
    1.63 +download_package(const char *name,
    1.64 +		 const char *old_version,
    1.65 +		 const char *new_version,
    1.66 +		 const char *arch,
    1.67 +		 void *data)
    1.68  {
    1.69 -	struct razor_importer *importer;
    1.70 +	char file[PATH_MAX], url[256];
    1.71 +
    1.72 +	if (old_version)
    1.73 +		return;
    1.74 +
    1.75 +	snprintf(url, sizeof url,
    1.76 +		 REPO_URL "/Packages/%s-%s.%s.rpm", name, new_version, arch);
    1.77 +	snprintf(file, sizeof file,
    1.78 +		 "rpms/%s-%s.%s.rpm", name, new_version, arch);
    1.79 +	if (download_if_missing(url, file) < 0)
    1.80 +		fprintf(stderr, "failed to download %s\n", name);
    1.81 +}
    1.82 +
    1.83 +static void
    1.84 +install_package(const char *name,
    1.85 +		const char *old_version,
    1.86 +		const char *new_version,
    1.87 +		const char *arch,
    1.88 +		void *data)
    1.89 +{
    1.90 +	const char *root = data;
    1.91 +	char file[PATH_MAX];
    1.92  	struct razor_rpm *rpm;
    1.93 -	int i;
    1.94  
    1.95 -	importer = razor_importer_new();
    1.96 -	for (i = 0; i < argc; i++) {
    1.97 -		rpm = razor_rpm_open(argv[i]);
    1.98 -		if (rpm == NULL) {
    1.99 -			fprintf(stderr,
   1.100 -				"failed to open rpm \"%s\"\n", argv[i]);
   1.101 -			continue;
   1.102 -		}
   1.103 -		if (razor_importer_add_rpm(importer, rpm)) {
   1.104 -			fprintf(stderr, "couldn't import %s\n", argv[i]);
   1.105 -			break;
   1.106 -		}
   1.107 -		razor_rpm_close(rpm);
   1.108 +	if (old_version) {
   1.109 +		printf("removing %s %s not handled\n", name, old_version);
   1.110 +		return;
   1.111  	}
   1.112  
   1.113 -	return razor_importer_finish(importer);
   1.114 -}
   1.115 +	printf("install %s %s\n", name, new_version);
   1.116 +	snprintf(file, sizeof file,
   1.117 +		 "rpms/%s-%s.%s.rpm", name, new_version, arch);
   1.118  
   1.119 -static char **
   1.120 -list_packages(int count, struct razor_set *set)
   1.121 -{
   1.122 -	struct razor_package_iterator *pi;
   1.123 -	struct razor_package *package;
   1.124 -	const char *name, *version, *arch;
   1.125 -	char **packages;
   1.126 -	int i;
   1.127 -
   1.128 -	packages = malloc(count * sizeof *packages);
   1.129 -	pi = razor_package_iterator_create(set);
   1.130 -	i = 0;
   1.131 -	while (razor_package_iterator_next(pi, &package,
   1.132 -					   &name, &version, &arch))
   1.133 -		packages[i++] = strdup(name);
   1.134 -	razor_package_iterator_destroy(pi);
   1.135 -
   1.136 -	return packages;
   1.137 + 	rpm = razor_rpm_open(file);
   1.138 +	if (rpm == NULL) {
   1.139 +		fprintf(stderr, "failed to open rpm %s\n", file);
   1.140 +		return;
   1.141 +	}
   1.142 +	if (razor_rpm_install(rpm, root) < 0) {
   1.143 +		fprintf(stderr,
   1.144 +			"failed to install rpm %s\n", file);
   1.145 +		return;
   1.146 +	}
   1.147 +	razor_rpm_close(rpm);
   1.148  }
   1.149  
   1.150  static int
   1.151 @@ -523,25 +530,18 @@
   1.152  {
   1.153  	struct razor_set *system, *upstream, *next;
   1.154  	struct razor_transaction *trans;
   1.155 -	struct razor_rpm *rpm;
   1.156 -	const char *filename;
   1.157 -	char path[PATH_MAX], new_path[PATH_MAX], **packages;
   1.158 -	int errors, i;
   1.159 +	char path[PATH_MAX], new_path[PATH_MAX];
   1.160 +	CURL *curl;
   1.161 +	int errors;
   1.162  
   1.163 -	upstream = create_set_from_rpms(argc, argv);
   1.164 +	upstream = razor_set_open(rawhide_repo_filename);
   1.165  	snprintf(path, sizeof path,
   1.166  		 "%s%s/%s", root, razor_root_path, system_repo_filename);
   1.167  	system = razor_set_open(path);
   1.168 -	if (system == NULL) {
   1.169 -		fprintf(stderr, "couldn't open system package database\n");
   1.170 -		return -1;
   1.171 -	}
   1.172 -
   1.173 -	packages = list_packages(argc, upstream);
   1.174 +	if (system == NULL || upstream == NULL)
   1.175 +		return 1;
   1.176  	trans = razor_transaction_create(system, upstream,
   1.177 -					 argc, (const char **)packages,
   1.178 -					 0, NULL);
   1.179 -	free(packages);
   1.180 +					 argc, argv, 0, NULL);
   1.181  	errors = razor_transaction_describe(trans);
   1.182  	if (errors)
   1.183  		return 1;
   1.184 @@ -558,29 +558,22 @@
   1.185  	 * up front here or fail if it already exists. */
   1.186  	snprintf(new_path, sizeof new_path,
   1.187  		 "%s%s/%s", root, razor_root_path, next_repo_filename);
   1.188 -	razor_set_write(next, path);
   1.189 +
   1.190 +	razor_set_write(next, new_path);
   1.191 +	printf("wrote %s\n", new_path);
   1.192 +
   1.193 +	curl = curl_easy_init();
   1.194 +	if (curl == NULL)
   1.195 +		return 1;
   1.196 +	razor_set_diff(system, next, download_package, curl);	
   1.197 +	curl_easy_cleanup(curl);
   1.198 +
   1.199 +	razor_set_diff(system, next, install_package, (void *) root);
   1.200  
   1.201  	razor_set_destroy(next);
   1.202  	razor_set_destroy(system);
   1.203  	razor_set_destroy(upstream);
   1.204  
   1.205 -	printf("wrote %s\n", new_path);
   1.206 -
   1.207 -	for (i = 0; i < argc; i++) {
   1.208 -		filename = argv[i];
   1.209 -		rpm = razor_rpm_open(argv[i]);
   1.210 -		if (rpm == NULL) {
   1.211 -			fprintf(stderr, "failed to open rpm %s\n", filename);
   1.212 -			return -1;
   1.213 -		}
   1.214 -		if (razor_rpm_install(rpm, root) < 0) {
   1.215 -			fprintf(stderr,
   1.216 -				"failed to install rpm %s\n", filename);
   1.217 -			return -1;
   1.218 -		}
   1.219 -		razor_rpm_close(rpm);
   1.220 -	}	
   1.221 -
   1.222  	/* Make it so. */
   1.223  	rename(new_path, path);
   1.224  	printf("renamed %s to %s\n", new_path, path);
   1.225 @@ -636,11 +629,6 @@
   1.226  	struct razor_package *package;
   1.227  	const char *pattern = argv[0], *name, *version, *arch;
   1.228  	char url[256], file[256];
   1.229 -	CURL *curl;
   1.230 -
   1.231 -	curl = curl_easy_init();
   1.232 -	if (curl == NULL)
   1.233 -		return 1;
   1.234  
   1.235  	set = razor_set_open(rawhide_repo_filename);
   1.236  	pi = razor_package_iterator_create(set);
   1.237 @@ -653,12 +641,11 @@
   1.238  			 REPO_URL "/Packages/%s-%s.i386.rpm", name, version);
   1.239  		snprintf(file, sizeof file,
   1.240  			 "rpms/%s-%s.i386.rpm", name, version);
   1.241 -		if (download_if_missing(curl, url, file) < 0)
   1.242 +		if (download_if_missing(url, file) < 0)
   1.243  			fprintf(stderr, "failed to download %s\n", name);
   1.244  	}
   1.245  	razor_package_iterator_destroy(pi);
   1.246  	razor_set_destroy(set);
   1.247 -	curl_easy_cleanup(curl);
   1.248  
   1.249  	return 0;
   1.250  }