libopkg: support https_proxy
[project/opkg-lede.git] / libopkg / opkg_download.c
index 373f2319e0b32c3ab5b06ad4c98e36438f9f6d27..af91f12a429ff33994c6da37d55cee45bcef97af 100644 (file)
@@ -55,16 +55,11 @@ int opkg_verify_integrity(pkg_t *pkg, const char *filename)
        pkg_expected_size = pkg_get_int(pkg, PKG_SIZE);
 
        if (pkg_expected_size > 0 && pkg_stat.st_size != pkg_expected_size) {
-               if (!conf->force_checksum) {
-                       opkg_msg(ERROR,
-                                "Package size mismatch: %s is %lld bytes, expecting %lld bytes\n",
-                                pkg->name, (long long int)pkg_stat.st_size, pkg_expected_size);
-                       return -1;
-               } else {
-                       opkg_msg(NOTICE,
-                                "Ignored %s size mismatch.\n",
-                                pkg->name);
-               }
+               opkg_msg(INFO,
+                        "Package size mismatch: %s is %lld bytes, expecting %lld bytes\n",
+                        pkg->name, (long long int)pkg_stat.st_size, pkg_expected_size);
+               err = -1;
+               goto out;
        }
 
        /* Check for md5 values */
@@ -72,17 +67,11 @@ int opkg_verify_integrity(pkg_t *pkg, const char *filename)
        if (pkg_md5) {
                file_md5 = file_md5sum_alloc(filename);
                if (file_md5 && strcmp(file_md5, pkg_md5)) {
-                       if (!conf->force_checksum) {
-                               opkg_msg(ERROR, "Package %s md5sum mismatch. "
-                                        "Either the opkg or the package index are corrupt. "
-                                        "Try 'opkg update'.\n", pkg->name);
-                               free(file_md5);
-                               return -1;
-                       } else {
-                               opkg_msg(NOTICE,
-                                        "Ignored %s md5sum mismatch.\n",
-                                        pkg->name);
-                       }
+                       opkg_msg(INFO, "Package %s md5sum mismatch.\n",
+                                pkg->name);
+                       err = -1;
+                       free(file_md5);
+                       goto out;
                }
                if (file_md5)
                        free(file_md5);
@@ -93,23 +82,17 @@ int opkg_verify_integrity(pkg_t *pkg, const char *filename)
        if (pkg_sha256) {
                file_sha256 = file_sha256sum_alloc(filename);
                if (file_sha256 && strcmp(file_sha256, pkg_sha256)) {
-                       if (!conf->force_checksum) {
-                               opkg_msg(ERROR,
-                                        "Package %s sha256sum mismatch. "
-                                        "Either the opkg or the package index are corrupt. "
-                                        "Try 'opkg update'.\n", pkg->name);
-                               free(file_sha256);
-                               return -1;
-                       } else {
-                               opkg_msg(NOTICE,
-                                        "Ignored %s sha256sum mismatch.\n",
-                                        pkg->name);
-                       }
+                       opkg_msg(INFO, "Package %s sha256sum mismatch.\n",
+                                pkg->name);
+                       err = -1;
+                       free(file_sha256);
+                       goto out;
                }
                if (file_sha256)
                        free(file_sha256);
        }
 
+out:
        return err;
 }
 
@@ -150,6 +133,12 @@ opkg_download(const char *src, const char *dest_file_name,
                         conf->http_proxy);
                setenv("http_proxy", conf->http_proxy, 1);
        }
+       if (conf->https_proxy) {
+               opkg_msg(DEBUG,
+                        "Setting environment variable: https_proxy = %s.\n",
+                        conf->https_proxy);
+               setenv("https_proxy", conf->https_proxy, 1);
+       }
        if (conf->ftp_proxy) {
                opkg_msg(DEBUG,
                         "Setting environment variable: ftp_proxy = %s.\n",
@@ -177,7 +166,7 @@ opkg_download(const char *src, const char *dest_file_name,
                        argv[i++] = "--timeout";
                        argv[i++] = conf->http_timeout;
                }
-               if (conf->http_proxy || conf->ftp_proxy) {
+               if (conf->http_proxy || conf->https_proxy || conf->ftp_proxy) {
                        argv[i++] = "-Y";
                        argv[i++] = "on";
                }
@@ -206,6 +195,17 @@ opkg_download(const char *src, const char *dest_file_name,
        return err;
 }
 
+static char* get_cache_filename(const char *dest_file_name)
+{
+       char *cache_name;
+       char *filename = strrchr(dest_file_name, '/');
+       if (filename)
+               cache_name = xstrdup(filename + 1);     // strip leading '/'
+       else
+               cache_name = xstrdup(dest_file_name);
+       return cache_name;
+}
+
 static int
 opkg_download_cache(const char *src, const char *dest_file_name)
 {
@@ -223,11 +223,7 @@ opkg_download_cache(const char *src, const char *dest_file_name)
                goto out1;
        }
 
-       char *filename = strrchr(dest_file_name, '/');
-       if (filename)
-               cache_name = xstrdup(filename + 1);     // strip leading '/'
-       else
-               cache_name = xstrdup(dest_file_name);
+       cache_name = get_cache_filename(dest_file_name);
        sprintf_alloc(&cache_location, "%s/%s", conf->cache, cache_name);
        if (file_exists(cache_location))
                opkg_msg(NOTICE, "Copying %s.\n", cache_location);
@@ -256,6 +252,8 @@ int opkg_download_pkg(pkg_t * pkg, const char *dir)
        char *stripped_filename;
        char *urlencoded_path;
        char *filename;
+       char *cache_name;
+       char *cache_location;
 
        if (pkg->src == NULL) {
                opkg_msg(ERROR,
@@ -289,6 +287,23 @@ int opkg_download_pkg(pkg_t * pkg, const char *dir)
        sprintf_alloc(&local_filename, "%s/%s", dir, stripped_filename);
        pkg_set_string(pkg, PKG_LOCAL_FILENAME, local_filename);
 
+       /* Invalidate/remove cached package if it has an incorrect checksum. */
+       if (conf->cache) {
+               cache_name = get_cache_filename(local_filename);
+               sprintf_alloc(&cache_location, "%s/%s", conf->cache, cache_name);
+               free(cache_name);
+               if (file_exists(cache_location)) {
+                       err = opkg_verify_integrity(pkg, cache_location);
+                       if (err) {
+                               opkg_msg(NOTICE,
+                                        "Removing %s from cache because it has incorrect checksum.\n",
+                                        pkg->name);
+                               unlink(cache_location);
+                       }
+               }
+               free(cache_location);
+       }
+
        err = opkg_download_cache(url, local_filename);
        free(url);