Improve download error handling, don't use libcurl progress.
1.1 --- a/main.c Mon Nov 05 21:55:31 2007 -0500
1.2 +++ b/main.c Mon Nov 05 22:54:33 2007 -0500
1.3 @@ -141,6 +141,52 @@
1.4 return 0;
1.5 }
1.6
1.7 +static int
1.8 +show_progress(void *clientp,
1.9 + double dltotal, double dlnow, double ultotal, double ulnow)
1.10 +{
1.11 + const char *file = clientp;
1.12 +
1.13 + if (!dlnow < dltotal)
1.14 + fprintf(stderr, "\rdownloading %s, %dkB/%dkB",
1.15 + file, (int) dlnow / 1024, (int) dltotal / 1024);
1.16 + else
1.17 + fprintf(stderr, "\n");
1.18 +
1.19 + return 0;
1.20 +}
1.21 +
1.22 +static int
1.23 +download_if_missing(CURL *curl, const char *url, const char *file)
1.24 +{
1.25 + struct stat buf;
1.26 + char error[256];
1.27 + FILE *fp;
1.28 + CURLcode res;
1.29 + char buffer[256];
1.30 +
1.31 + curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
1.32 + curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
1.33 + curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, show_progress);
1.34 + curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, file);
1.35 +
1.36 + if (stat(file, &buf) < 0) {
1.37 + fp = fopen(file, "w");
1.38 + snprintf(buffer, sizeof buffer, "%s/%s", url, file);
1.39 + curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
1.40 + curl_easy_setopt(curl, CURLOPT_URL, buffer);
1.41 + res = curl_easy_perform(curl);
1.42 + fclose(fp);
1.43 + if (res != CURLE_OK) {
1.44 + fprintf(stderr, "curl error: %s\n", error);
1.45 + unlink(file);
1.46 + return -1;
1.47 + }
1.48 + }
1.49 +
1.50 + return 0;
1.51 +}
1.52 +
1.53 #define REPO_URL "http://download.fedora.redhat.com" \
1.54 "/pub/fedora/linux/development/i386/os/repodata"
1.55
1.56 @@ -149,33 +195,15 @@
1.57 {
1.58 struct razor_set *set;
1.59 CURL *curl;
1.60 - CURLcode res;
1.61 - FILE *fp;
1.62 - struct stat buf;
1.63
1.64 curl = curl_easy_init();
1.65 if (curl == NULL)
1.66 return 1;
1.67
1.68 - if (stat("primary.xml.gz", &buf) < 0) {
1.69 - curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
1.70 - fp = fopen("primary.xml.gz", "w");
1.71 - curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
1.72 - curl_easy_setopt(curl, CURLOPT_URL,
1.73 - REPO_URL "/primary.xml.gz");
1.74 - res = curl_easy_perform(curl);
1.75 - fclose(fp);
1.76 - }
1.77 -
1.78 - if (stat("filelist.xml.gz", &buf) < 0) {
1.79 - fp = fopen("filelists.xml.gz", "w");
1.80 - curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
1.81 - curl_easy_setopt(curl, CURLOPT_URL,
1.82 - REPO_URL "/filelists.xml.gz");
1.83 - res = curl_easy_perform(curl);
1.84 - fclose(fp);
1.85 - }
1.86 -
1.87 + if (download_if_missing(curl, REPO_URL, "primary.xml.gz") < 0)
1.88 + return -1;
1.89 + if (download_if_missing(curl, REPO_URL, "filelists.xml.gz") < 0)
1.90 + return -1;
1.91 curl_easy_cleanup(curl);
1.92
1.93 set = razor_set_create_from_yum();