src/main.c
changeset 455 df914f383f5c
parent 447 0a5e583393e1
child 457 51a084acef49
     1.1 --- a/src/main.c	Tue Sep 09 15:27:12 2014 +0100
     1.2 +++ b/src/main.c	Thu Oct 09 17:27:41 2014 +0100
     1.3 @@ -32,9 +32,16 @@
     1.4  #include <fcntl.h>
     1.5  #include <dirent.h>
     1.6  #include <limits.h>
     1.7 +#ifdef MSWIN_API
     1.8 +#include <windows.h>
     1.9 +#include <shlwapi.h>
    1.10 +#endif
    1.11  #ifdef HAVE_CURL
    1.12  #include <curl/curl.h>
    1.13  #endif
    1.14 +#if !defined(HAVE_CURL) && !defined(MSWIN_API)
    1.15 +#include <ctype.h>
    1.16 +#endif
    1.17  #include <fnmatch.h>
    1.18  #include <errno.h>
    1.19  #include <getopt.h>
    1.20 @@ -607,6 +614,71 @@
    1.21  				      RAZOR_PROPERTY_PROVIDES);
    1.22  }
    1.23  
    1.24 +#ifndef HAVE_CURL
    1.25 +static int
    1.26 +download_local(const char *url, const char *file)
    1.27 +{
    1.28 +	FILE *wfp, *rfp;
    1.29 +	char buffer[256], *ptr, *local;
    1.30 +	size_t nb, n;
    1.31 +
    1.32 +	local = razor_path_from_url(url);
    1.33 +
    1.34 +	if (local == NULL) {
    1.35 +		fprintf(stderr,
    1.36 +			"%s: download manually (curl not available)\n",
    1.37 +			file);
    1.38 +		return -1;
    1.39 +	} else {
    1.40 +		rfp = fopen(local, "rb");
    1.41 +		if (rfp == NULL) {
    1.42 +			perror(local);
    1.43 +			free(local);
    1.44 +			return -1;
    1.45 +		}
    1.46 +
    1.47 +		wfp = fopen(file, "wb");
    1.48 +		if (wfp == NULL) {
    1.49 +			perror(file);
    1.50 +			fclose(rfp);
    1.51 +			free(local);
    1.52 +			return -1;
    1.53 +		}
    1.54 +
    1.55 +		while((nb = fread(buffer, 1, sizeof(buffer), rfp)) > 0) {
    1.56 +			ptr = buffer;
    1.57 +			while (nb > 0 && (n = fwrite(ptr, 1, nb, wfp)) > 0) {
    1.58 +				ptr += n;
    1.59 +				nb -= n;
    1.60 +			}
    1.61 +
    1.62 +			if (nb != 0) {
    1.63 +				perror(file);
    1.64 +				fclose(wfp);
    1.65 +				fclose(rfp);
    1.66 +				unlink(file);
    1.67 +				free(local);
    1.68 +				return -1;
    1.69 +			}
    1.70 +		}
    1.71 +
    1.72 +		if (ferror(rfp)) {
    1.73 +			perror(local);
    1.74 +			fclose(wfp);
    1.75 +			fclose(rfp);
    1.76 +			unlink(file);
    1.77 +			free(local);
    1.78 +			return -1;
    1.79 +		}
    1.80 +
    1.81 +		fclose(wfp);
    1.82 +		fclose(rfp);
    1.83 +		free(local);
    1.84 +		return 0;
    1.85 +	}
    1.86 +}
    1.87 +#endif /* !HAVE_CURL */
    1.88 +
    1.89  #ifdef HAVE_CURL
    1.90  static int
    1.91  show_progress(void *clientp,
    1.92 @@ -620,65 +692,74 @@
    1.93  
    1.94  	return 0;
    1.95  }
    1.96 -#endif	/* HAVE_CURL */
    1.97  
    1.98  static int
    1.99 -download_if_missing(const char *url, const char *file)
   1.100 +download_with_curl(const char *url, const char *file)
   1.101  {
   1.102 -#ifndef HAVE_CURL
   1.103 -	return 1;
   1.104 -#else
   1.105 +	FILE *fp;
   1.106  	CURL *curl;
   1.107 -	struct stat buf;
   1.108  	char error[256];
   1.109 -	FILE *fp;
   1.110  	CURLcode res;
   1.111  	long response;
   1.112  
   1.113  	curl = curl_easy_init();
   1.114 -	if (curl == NULL)
   1.115 -		return 1;
   1.116 +	if (curl == NULL) {
   1.117 +		fprintf(stderr,
   1.118 +			"%s: download manually (curl failed)\n", file);
   1.119 +		return -1;
   1.120 +	}
   1.121  
   1.122  	curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
   1.123  	curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
   1.124  	curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, show_progress);
   1.125  	curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, file);
   1.126  
   1.127 -	if (stat(file, &buf) < 0) {
   1.128 -		fp = fopen(file, "wb");
   1.129 -		if (fp == NULL) {
   1.130 -			fprintf(stderr,
   1.131 -				"failed to open %s for writing\n", file);
   1.132 -			return -1;
   1.133 -		}
   1.134 -		curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
   1.135 -		curl_easy_setopt(curl, CURLOPT_URL, url);
   1.136 -		res = curl_easy_perform(curl);
   1.137 -		fclose(fp);
   1.138 -		if (res != CURLE_OK) {
   1.139 -			fprintf(stderr, "curl error: %s\n", error);
   1.140 -			unlink(file);
   1.141 -			return -1;
   1.142 -		}
   1.143 -		res = curl_easy_getinfo(curl,
   1.144 -					CURLINFO_RESPONSE_CODE, &response);
   1.145 -		if (res != CURLE_OK) {
   1.146 -			fprintf(stderr, "curl error: %s\n", error);
   1.147 -                        unlink(file);
   1.148 -                        return -1;
   1.149 -		}
   1.150 -		if (response != 200) {
   1.151 -			fprintf(stderr, " - failed %ld\n", response);
   1.152 -                        unlink(file);
   1.153 -                        return -1;
   1.154 -		}
   1.155 -		fprintf(stderr, "\n");
   1.156 +	fp = fopen(file, "wb");
   1.157 +	if (fp == NULL) {
   1.158 +		perror(file);
   1.159 +		return -1;
   1.160  	}
   1.161 -
   1.162 +	curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
   1.163 +	curl_easy_setopt(curl, CURLOPT_URL, url);
   1.164 +	res = curl_easy_perform(curl);
   1.165 +	fclose(fp);
   1.166 +	if (res != CURLE_OK) {
   1.167 +		fprintf(stderr, "curl error: %s\n", error);
   1.168 +		unlink(file);
   1.169 +		return -1;
   1.170 +	}
   1.171 +	res = curl_easy_getinfo(curl,
   1.172 +				CURLINFO_RESPONSE_CODE, &response);
   1.173 +	if (res != CURLE_OK) {
   1.174 +		fprintf(stderr, "curl error: %s\n", error);
   1.175 +		unlink(file);
   1.176 +		return -1;
   1.177 +	}
   1.178 +	if (response != 200) {
   1.179 +		fprintf(stderr, " - failed %ld\n", response);
   1.180 +		unlink(file);
   1.181 +		return -1;
   1.182 +	}
   1.183 +	fprintf(stderr, "\n");
   1.184  	curl_easy_cleanup(curl);
   1.185  
   1.186  	return 0;
   1.187 +}
   1.188  #endif	/* HAVE_CURL */
   1.189 +
   1.190 +static int
   1.191 +download_if_missing(const char *url, const char *file)
   1.192 +{
   1.193 +	struct stat buf;
   1.194 +
   1.195 +	if (stat(file, &buf) >= 0)
   1.196 +		return 0;
   1.197 +
   1.198 +#ifndef HAVE_CURL
   1.199 +	return download_local(url, file);
   1.200 +#else
   1.201 +	return download_with_curl(url, file);
   1.202 +#endif
   1.203  }
   1.204  
   1.205  #define YUM_URL "http://download.fedora.redhat.com" \
   1.206 @@ -704,7 +785,7 @@
   1.207  		return 1;
   1.208  	}
   1.209  
   1.210 -	printf("downloading from %s.\n", yum_url);
   1.211 +	printf("downloading from '%s'.\n", yum_url);
   1.212  	snprintf(buffer, sizeof buffer,
   1.213  		 "%s/repodata/primary.xml.gz", yum_url);
   1.214  	if (download_if_missing(buffer, "primary.xml.gz") < 0)
   1.215 @@ -1450,7 +1531,6 @@
   1.216  		razor_transaction_destroy(trans);
   1.217  		razor_set_unref(upstream);
   1.218  		razor_root_close(root);
   1.219 -		razor_atomic_destroy(atomic);
   1.220  		if (relocations)
   1.221  			razor_relocations_destroy(relocations);
   1.222                  return 1;