libopkg: clarify messages and errors related to downloads
[oweals/opkg-lede.git] / libopkg / opkg_download.c
index 81ce53d22251d94db3d5fa30126d53b84c8b6aee..3f86462b9a7729b8d00c9bea38d5d633dac2a3ad 100644 (file)
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.
 */
+
 #include "config.h"
 
-#include "includes.h"
+#include <sys/wait.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <libgen.h>
+
 #include "opkg_download.h"
 #include "opkg_message.h"
 
@@ -78,7 +83,7 @@ str_starts_with(const char *str, const char *prefix)
 
 int
 opkg_download(const char *src, const char *dest_file_name,
-       curl_progress_func cb, void *data)
+       curl_progress_func cb, void *data, const short hide_error)
 {
     int err = 0;
 
@@ -86,19 +91,19 @@ opkg_download(const char *src, const char *dest_file_name,
     char *src_base = basename(src_basec);
     char *tmp_file_location;
 
-    free(src_basec);
+    opkg_msg(NOTICE,"Downloading %s\n", src);
 
-    opkg_msg(NOTICE,"Downloading %s.\n", src);
-       
     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;
     }
 
     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);
@@ -138,10 +143,10 @@ opkg_download(const char *src, const char *dest_file_name,
        {
            long error_code;
            curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &error_code);
-           opkg_msg(ERROR, "Failed to download %s: %s.\n",
+           opkg_msg(hide_error?DEBUG2:ERROR, "Failed to download %s: %s.\n",
                    src, curl_easy_strerror(res));
            free(tmp_file_location);
-           return res;
+           return -1;
        }
 
     }
@@ -170,8 +175,10 @@ opkg_download(const char *src, const char *dest_file_name,
 
       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 res;
+       return -1;
       }
     }
 #endif
@@ -192,10 +199,17 @@ opkg_download_cache(const char *src, const char *dest_file_name,
     int err = 0;
 
     if (!conf->cache || str_starts_with(src, "file:")) {
-       err = opkg_download(src, dest_file_name, cb, data);
+       err = opkg_download(src, dest_file_name, cb, data, 0);
        goto out1;
     }
 
+    if(!file_is_dir(conf->cache)){
+           opkg_msg(ERROR, "%s is not a directory.\n",
+                           conf->cache);
+           err = 1;
+           goto out1;
+    }
+
     for (p = cache_name; *p; p++)
        if (*p == '/')
            *p = ',';   /* looks nicer than | or # */
@@ -204,10 +218,23 @@ opkg_download_cache(const char *src, const char *dest_file_name,
     if (file_exists(cache_location))
        opkg_msg(NOTICE, "Copying %s.\n", cache_location);
     else {
-       err = opkg_download(src, cache_location, cb, data);
-       if (err) {
-           (void) unlink(cache_location);
-           goto out2;
+       /* 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, cb, data, 0);
+           if (err) {
+              (void) unlink(cache_location);
+              goto out2;
+         }
        }
     }
 
@@ -241,7 +268,7 @@ opkg_download_pkg(pkg_t *pkg, const char *dir)
 
     sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
 
-    /* XXX: BUG: The pkg->filename might be something like
+    /* The pkg->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. */
@@ -259,7 +286,7 @@ opkg_download_pkg(pkg_t *pkg, const char *dir)
 }
 
 /*
- * 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(const char *url, char **namep)
@@ -276,7 +303,7 @@ opkg_prepare_url_for_install(const char *url, char **namep)
          char *file_base = basename(file_basec);
 
          sprintf_alloc(&tmp_file, "%s/%s", conf->tmp_dir, file_base);
-         err = opkg_download(url, tmp_file, NULL, NULL);
+         err = opkg_download(url, tmp_file, NULL, NULL, 0);
          if (err)
               return err;
 
@@ -307,10 +334,10 @@ opkg_prepare_url_for_install(const char *url, char **namep)
      pkg->dest = conf->default_dest;
      pkg->state_want = SW_INSTALL;
      pkg->state_flag |= SF_PREFER;
-     hash_insert_pkg(pkg, 1);  
+     hash_insert_pkg(pkg, 1);
 
      if (namep) {
-         *namep = pkg->name;
+         *namep = xstrdup(pkg->name);
      }
      return 0;
 }
@@ -318,7 +345,28 @@ opkg_prepare_url_for_install(const char *url, char **namep)
 int
 opkg_verify_file (char *text_file, char *sig_file)
 {
-#if defined HAVE_GPGME
+#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;
+#elif defined HAVE_GPGME
     if (conf->check_signature == 0 )
         return 0;
     int status = -1;
@@ -328,14 +376,16 @@ opkg_verify_file (char *text_file, char *sig_file)
     gpgme_verify_result_t result;
     gpgme_signature_t s;
     char *trusted_path = NULL;
-    
+
+    gpgme_check_version (NULL);
+
     err = gpgme_new (&ctx);
 
     if (err)
        return -1;
 
     sprintf_alloc(&trusted_path, "%s/%s", conf->offline_root, "/etc/opkg/trusted.gpg");
-    err = gpgme_data_new_from_file (&key, trusted_path, 1); 
+    err = gpgme_data_new_from_file (&key, trusted_path, 1);
     free (trusted_path);
     if (err)
     {
@@ -349,14 +399,14 @@ opkg_verify_file (char *text_file, char *sig_file)
     }
     gpgme_data_release (key);
 
-    err = gpgme_data_new_from_file (&sig, sig_file, 1); 
+    err = gpgme_data_new_from_file (&sig, sig_file, 1);
     if (err)
     {
        gpgme_release (ctx);
        return -1;
     }
 
-    err = gpgme_data_new_from_file (&text, text_file, 1); 
+    err = gpgme_data_new_from_file (&text, text_file, 1);
     if (err)
     {
         gpgme_data_release (sig);