libopkg: remove unused progress parameters from opkg_download()
[oweals/opkg-lede.git] / libopkg / opkg_download.c
index 4dab8092f5001e5e94cc02e70edd235ec7849d7a..db4c90f2a69b81ca2a1d2fa52187471822ae3443 100644 (file)
@@ -1,5 +1,5 @@
 /* vi: set noexpandtab sw=4 sts=4: */
-/* opkg_download.c - the itsy package management system
+/* opkg_download.c - the opkg package management system
 
    Carl D. Worth
 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.
 */
-#include "config.h"
-#include <curl/curl.h>
-#ifdef HAVE_GPGME
-#include <gpgme.h>
-#endif
 
-#include "opkg.h"
+#include <sys/wait.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <libgen.h>
+
 #include "opkg_download.h"
 #include "opkg_message.h"
-#include "opkg_state.h"
 
 #include "sprintf_alloc.h"
 #include "xsystem.h"
 #include "file_util.h"
-#include "str_util.h"
+#include "opkg_defines.h"
+#include "libbb/libbb.h"
 
-#include "libopkg.h"
-opkg_download_progress_callback opkg_cb_download_progress = NULL;
+static int str_starts_with(const char *str, const char *prefix)
+{
+       return (strncmp(str, prefix, strlen(prefix)) == 0);
+}
 
 int
-curl_progress_func (char* url,
-                   double t, /* dltotal */
-                   double d, /* dlnow */
-                   double ultotal,
-                   double ulnow)
+opkg_download(const char *src, const char *dest_file_name,
+              const short hide_error)
 {
-    int i;
-    int p = (t) ? d*100/t : 0;
-
-    if (opkg_cb_download_progress)
-    {
-       static int prev = -1;
+       int err = 0;
 
-       /* don't report the same percentage multiple times
-        * (this can occur due to rounding) */
-       if (prev == p)
-           return 0;
-       prev = p;
+       char *src_basec = xstrdup(src);
+       char *src_base = basename(src_basec);
+       char *tmp_file_location;
 
-       opkg_cb_download_progress (p, url);
-       return 0;
-    }
+       opkg_msg(NOTICE, "Downloading %s\n", src);
 
-    /* skip progress bar if we haven't done started yet
-     * this prevents drawing the progress bar if we receive an error such as
-     * file not found */
-    if (t == 0)
-       return 0;
+       if (str_starts_with(src, "file:")) {
+               const char *file_src = src + 5;
+               opkg_msg(INFO, "Copying %s to %s...", file_src, dest_file_name);
+               err = file_copy(file_src, dest_file_name);
+               opkg_msg(INFO, "Done.\n");
+               free(src_basec);
+               return err;
+       }
 
-    printf ("\r%3d%% |", p);
-    for (i = 1; i < 73; i++)
-    {
-       if (i <= p * 0.73)
-           printf ("=");
-       else
-           printf ("-");
-    }
-    printf ("|");
-    fflush(stdout);
-    return 0;
-}
+       sprintf_alloc(&tmp_file_location, "%s/%s", conf->tmp_dir, src_base);
+       free(src_basec);
+       err = unlink(tmp_file_location);
+       if (err && errno != ENOENT) {
+               opkg_perror(ERROR, "Failed to unlink %s", tmp_file_location);
+               free(tmp_file_location);
+               return -1;
+       }
 
-int opkg_download(opkg_conf_t *conf, const char *src, const char *dest_file_name)
-{
-    int err = 0;
-
-    char *src_basec = strdup(src);
-    char *src_base = basename(src_basec);
-    char *tmp_file_location;
-
-    opkg_message(conf,OPKG_NOTICE,"Downloading %s\n", src);
-       
-    fflush(stdout);
-    
-    if (str_starts_with(src, "file:")) {
-       int ret;
-       const char *file_src = src + 5;
-       opkg_message(conf,OPKG_INFO,"Copying %s to %s...", file_src, dest_file_name);
-       ret = file_copy(src + 5, dest_file_name);
-       opkg_message(conf,OPKG_INFO,"Done\n");
-       return ret;
-    }
-
-    sprintf_alloc(&tmp_file_location, "%s/%s", conf->tmp_dir, src_base);
-    err = unlink(tmp_file_location);
-    if (err && errno != ENOENT) {
-       opkg_message(conf,OPKG_ERROR, "%s: ERROR: failed to unlink %s: %s\n",
-               __FUNCTION__, tmp_file_location, strerror(errno));
-       free(tmp_file_location);
-       return errno;
-    }
-
-    if (conf->http_proxy) {
-       opkg_message(conf,OPKG_DEBUG,"Setting environment variable: http_proxy = %s\n", conf->http_proxy);
-       setenv("http_proxy", conf->http_proxy, 1);
-    }
-    if (conf->ftp_proxy) {
-       opkg_message(conf,OPKG_DEBUG,"Setting environment variable: ftp_proxy = %s\n", conf->ftp_proxy);
-       setenv("ftp_proxy", conf->ftp_proxy, 1);
-    }
-    if (conf->no_proxy) {
-       opkg_message(conf,OPKG_DEBUG,"Setting environment variable: no_proxy = %s\n", conf->no_proxy);
-       setenv("no_proxy", conf->no_proxy, 1);
-    }
-
-    CURL *curl;
-    CURLcode res;
-    FILE * file = fopen (tmp_file_location, "w");
-
-    curl = curl_easy_init ();
-    if (curl)
-    {
-       curl_easy_setopt (curl, CURLOPT_URL, src);
-       curl_easy_setopt (curl, CURLOPT_WRITEDATA, file);
-       curl_easy_setopt (curl, CURLOPT_NOPROGRESS, 0);
-       curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, src);
-       curl_easy_setopt (curl, CURLOPT_PROGRESSFUNCTION, curl_progress_func);
-       curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
-       if (conf->http_proxy || conf->ftp_proxy)
-       {
-           char *userpwd;
-           sprintf_alloc (&userpwd, "%s:%s", conf->proxy_user,
-                   conf->proxy_passwd);
-           curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, userpwd);
-           free (userpwd);
+       if (conf->http_proxy) {
+               opkg_msg(DEBUG,
+                        "Setting environment variable: http_proxy = %s.\n",
+                        conf->http_proxy);
+               setenv("http_proxy", conf->http_proxy, 1);
+       }
+       if (conf->ftp_proxy) {
+               opkg_msg(DEBUG,
+                        "Setting environment variable: ftp_proxy = %s.\n",
+                        conf->ftp_proxy);
+               setenv("ftp_proxy", conf->ftp_proxy, 1);
+       }
+       if (conf->no_proxy) {
+               opkg_msg(DEBUG,
+                        "Setting environment variable: no_proxy = %s.\n",
+                        conf->no_proxy);
+               setenv("no_proxy", conf->no_proxy, 1);
        }
-       res = curl_easy_perform (curl);
-       curl_easy_cleanup (curl);
-       fclose (file);
-       if (res)
+
        {
-           long error_code;
-           curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &error_code);
-           opkg_message(conf, OPKG_ERROR, "Failed to download %s, error %d\n", src, error_code);
-           return res;
+               int res;
+               const char *argv[8];
+               int i = 0;
+
+               argv[i++] = "wget";
+               argv[i++] = "-q";
+               if (conf->http_proxy || conf->ftp_proxy) {
+                       argv[i++] = "-Y";
+                       argv[i++] = "on";
+               }
+               argv[i++] = "-O";
+               argv[i++] = tmp_file_location;
+               argv[i++] = src;
+               argv[i++] = NULL;
+               res = xsystem(argv);
+
+               if (res) {
+                       opkg_msg(ERROR,
+                                "Failed to download %s, wget returned %d.\n",
+                                src, res);
+                       if (res == 4)
+                               opkg_msg(ERROR,
+                                        "Check your network settings and connectivity.\n\n");
+                       free(tmp_file_location);
+                       return -1;
+               }
        }
 
-    }
-    else
-       return -1;
+       err = file_move(tmp_file_location, dest_file_name);
+
+       free(tmp_file_location);
 
-    /* if no custom progress handler was set, we need to clear the default progress bar */
-    if (!opkg_cb_download_progress)
-        printf ("\n");
+       return err;
+}
 
-    err = file_move(tmp_file_location, dest_file_name);
+static int
+opkg_download_cache(const char *src, const char *dest_file_name)
+{
+       char *cache_name = xstrdup(src);
+       char *cache_location, *p;
+       int err = 0;
 
-    free(tmp_file_location);
-    free(src_basec);
+       if (!conf->cache || str_starts_with(src, "file:")) {
+               err = opkg_download(src, dest_file_name, 0);
+               goto out1;
+       }
 
-    if (err) {
-       return err;
-    }
+       if (!file_is_dir(conf->cache)) {
+               opkg_msg(ERROR, "%s is not a directory.\n", conf->cache);
+               err = 1;
+               goto out1;
+       }
 
-    return 0;
+       for (p = cache_name; *p; p++)
+               if (*p == '/')
+                       *p = ',';       /* looks nicer than | or # */
+
+       sprintf_alloc(&cache_location, "%s/%s", conf->cache, cache_name);
+       if (file_exists(cache_location))
+               opkg_msg(NOTICE, "Copying %s.\n", cache_location);
+       else {
+               /* cache file with funky name not found, try simple name */
+               free(cache_name);
+               char *filename = strrchr(dest_file_name, '/');
+               if (filename)
+                       cache_name = xstrdup(filename + 1);     // strip leading '/'
+               else
+                       cache_name = xstrdup(dest_file_name);
+               free(cache_location);
+               sprintf_alloc(&cache_location, "%s/%s", conf->cache,
+                             cache_name);
+               if (file_exists(cache_location))
+                       opkg_msg(NOTICE, "Copying %s.\n", cache_location);
+               else {
+                       err = opkg_download(src, cache_location, 0);
+                       if (err) {
+                               (void)unlink(cache_location);
+                               goto out2;
+                       }
+               }
+       }
+
+       err = file_copy(cache_location, dest_file_name);
+
+out2:
+       free(cache_location);
+out1:
+       free(cache_name);
+       return err;
 }
 
-int opkg_download_pkg(opkg_conf_t *conf, pkg_t *pkg, const char *dir)
+int opkg_download_pkg(pkg_t * pkg, const char *dir)
 {
-    int err;
-    char *url;
-    char *pkgid;
+       int err;
+       char *url;
+       char *local_filename;
+       char *stripped_filename;
+       char *filename;
+
+       if (pkg->src == NULL) {
+               opkg_msg(ERROR,
+                        "Package %s is not available from any configured src.\n",
+                        pkg->name);
+               return -1;
+       }
+
+       filename = pkg_get_string(pkg, PKG_FILENAME);
 
-    if (pkg->src == NULL) {
-       opkg_message(conf,OPKG_ERROR, "ERROR: Package %s (parent %s) is not available from any configured src.\n",
-               pkg->name, pkg->parent->name);
-       return -1;
-    }
+       if (filename == NULL) {
+               opkg_msg(ERROR,
+                        "Package %s does not have a valid filename field.\n",
+                        pkg->name);
+               return -1;
+       }
+
+       sprintf_alloc(&url, "%s/%s", pkg->src->value, filename);
 
-    sprintf_alloc (&pkgid, "%s;%s;%s;", pkg->name, pkg->version, pkg->architecture);
-    opkg_set_current_state (conf, OPKG_STATE_DOWNLOADING_PKG, pkgid);
-    free (pkgid);
+       /* The filename might be something like
+          "../../foo.opk". While this is correct, and exactly what we
+          want to use to construct url above, here we actually need to
+          use just the filename part, without any directory. */
 
-    sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
+       stripped_filename = strrchr(filename, '/');
+       if (!stripped_filename)
+               stripped_filename = filename;
 
-    /* XXX: BUG: The pkg->filename might be something like
-       "../../foo.ipk". While this is correct, and exactly what we
-       want to use to construct url above, here we actually need to
-       use just the filename part, without any directory. */
-    sprintf_alloc(&pkg->local_filename, "%s/%s", dir, pkg->filename);
+       sprintf_alloc(&local_filename, "%s/%s", dir, stripped_filename);
+       pkg_set_string(pkg, PKG_LOCAL_FILENAME, local_filename);
 
-    err = opkg_download(conf, url, pkg->local_filename);
-    free(url);
+       err = opkg_download_cache(url, local_filename);
+       free(url);
 
-    opkg_set_current_state (conf, OPKG_STATE_NONE, NULL);
-    return err;
+       return err;
 }
 
 /*
- * Downloads file from url, installs in package database, return package name. 
+ * Downloads file from url, installs in package database, return package name.
  */
-int opkg_prepare_url_for_install(opkg_conf_t *conf, const char *url, char **namep)
+int opkg_prepare_url_for_install(const char *url, char **namep)
 {
-     int err = 0;
-     pkg_t *pkg;
-     pkg = pkg_new();
-     if (pkg == NULL)
-         return ENOMEM;
-
-     if (str_starts_with(url, "http://")
-        || str_starts_with(url, "ftp://")) {
-         char *tmp_file;
-         char *file_basec = strdup(url);
-         char *file_base = basename(file_basec);
-
-         sprintf_alloc(&tmp_file, "%s/%s", conf->tmp_dir, file_base);
-         err = opkg_download(conf, url, tmp_file);
-         if (err)
-              return err;
-
-         err = pkg_init_from_file(pkg, tmp_file);
-         if (err)
-              return err;
-         pkg->local_filename = strdup(tmp_file);
-
-         free(tmp_file);
-         free(file_basec);
-
-     } else if (strcmp(&url[strlen(url) - 4], OPKG_PKG_EXTENSION) == 0
-               || strcmp(&url[strlen(url) - 4], DPKG_PKG_EXTENSION) == 0) {
-
-         err = pkg_init_from_file(pkg, url);
-         if (err)
-              return err;
-         pkg->local_filename = strdup(url);
-         opkg_message(conf, OPKG_DEBUG2, "Package %s provided by hand (%s).\n", pkg->name,pkg->local_filename);
-          pkg->provided_by_hand = 1;
-
-     } else {
-       pkg_deinit(pkg);
-       free(pkg);
-       return 0;
-     }
-
-     if (!pkg->architecture) {
-         opkg_message(conf, OPKG_ERROR, "Package %s has no Architecture defined.\n", pkg->name);
-         return -EINVAL;
-     }
-
-     pkg->dest = conf->default_dest;
-     pkg->state_want = SW_INSTALL;
-     pkg->state_flag |= SF_PREFER;
-     pkg = hash_insert_pkg(&conf->pkg_hash, pkg, 1,conf);  
-     if ( pkg == NULL ){
-        fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
-        return 0;
-     }
-     if (namep) {
-         *namep = strdup(pkg->name);
-     }
-     return 0;
+       int err = 0;
+       pkg_t *pkg;
+       abstract_pkg_t *ab_pkg;
+
+       pkg = pkg_new();
+
+       if (str_starts_with(url, "http://")
+           || str_starts_with(url, "ftp://")) {
+               char *tmp_file;
+               char *file_basec = xstrdup(url);
+               char *file_base = basename(file_basec);
+
+               sprintf_alloc(&tmp_file, "%s/%s", conf->tmp_dir, file_base);
+               err = opkg_download(url, tmp_file, 0);
+               if (err)
+                       return err;
+
+               err = pkg_init_from_file(pkg, tmp_file);
+               if (err)
+                       return err;
+
+               free(tmp_file);
+               free(file_basec);
+
+       } else if (strcmp(&url[strlen(url) - 4], OPKG_PKG_EXTENSION) == 0
+                  || strcmp(&url[strlen(url) - 4], IPKG_PKG_EXTENSION) == 0
+                  || strcmp(&url[strlen(url) - 4], DPKG_PKG_EXTENSION) == 0) {
+
+               err = pkg_init_from_file(pkg, url);
+               if (err)
+                       return err;
+               opkg_msg(DEBUG2, "Package %s provided by hand (%s).\n",
+                        pkg->name, pkg_get_string(pkg, PKG_LOCAL_FILENAME));
+               pkg->provided_by_hand = 1;
+
+       } else {
+               ab_pkg = ensure_abstract_pkg_by_name(url);
+
+               if (!(ab_pkg->state_flag & SF_NEED_DETAIL)) {
+                       opkg_msg(DEBUG, "applying abpkg flag to %s\n", ab_pkg->name);
+                       ab_pkg->state_flag |= SF_NEED_DETAIL;
+               }
+
+               pkg_deinit(pkg);
+               free(pkg);
+               return 0;
+       }
+
+       pkg->dest = conf->default_dest;
+       pkg->state_want = SW_INSTALL;
+       pkg->state_flag |= SF_PREFER;
+       hash_insert_pkg(pkg, 1);
+
+       if (namep) {
+               *namep = xstrdup(pkg->name);
+       }
+       return 0;
 }
 
-int
-opkg_verify_file (opkg_conf_t *conf, char *text_file, char *sig_file)
+int opkg_verify_file(char *text_file, char *sig_file)
 {
-#ifdef HAVE_GPGME
-    int status = -1;
-    gpgme_ctx_t ctx;
-    gpgme_data_t sig, text;
-    gpgme_error_t err = -1;
-    gpgme_verify_result_t result;
-    gpgme_signature_t s;
-    
-    err = gpgme_new (&ctx);
-
-    if (err)
-       return -1;
-
-    err = gpgme_data_new_from_file (&sig, sig_file, 1); 
-    if (err)
-       return -1;
-
-    err = gpgme_data_new_from_file (&text, text_file, 1); 
-    if (err)
-       return -1;
-
-    err = gpgme_op_verify (ctx, sig, text, NULL);
-
-    result = gpgme_op_verify_result (ctx);
-
-    /* see if any of the signitures matched */
-    s = result->signatures;
-    while (s)
-    {
-       status = gpg_err_code (s->status);
-       if (status == GPG_ERR_NO_ERROR)
-           break;
-       s = s->next;
-    }
-
-    gpgme_data_release (sig);
-    gpgme_data_release (text);
-    gpgme_release (ctx);
-
-    return status;
+#if defined HAVE_USIGN
+       int status = -1;
+       int pid;
+
+       if (conf->check_signature == 0)
+               return 0;
+
+       pid = fork();
+       if (pid < 0)
+               return -1;
+
+       if (!pid) {
+               execl("/usr/sbin/opkg-key", "opkg-key", "verify", sig_file,
+                     text_file, NULL);
+               exit(255);
+       }
+
+       waitpid(pid, &status, 0);
+       if (!WIFEXITED(status) || WEXITSTATUS(status))
+               return -1;
+
+       return 0;
 #else
-    opkg_message (conf, OPKG_NOTICE, "Signature check for %s was skipped because GPG support was not enabled in this build\n");
-    return 0;
+       /* mute `unused variable' warnings. */
+       (void)sig_file;
+       (void)text_file;
+       (void)conf;
+       return 0;
 #endif
 }