libopkg: add support for signature checking through usign
[oweals/opkg-lede.git] / libopkg / opkg.c
index fdb2fe38284170f19277f5af6a6523719db77bf7..dbb904a7be6123abc954ed239c571582b439328d 100644 (file)
@@ -112,14 +112,20 @@ curl_progress_cb(struct _curl_cb_data *cb_data, double t, /* dltotal */
 }
 
 
+static struct opkg_conf saved_conf;
 /*** Public API ***/
 
 int
 opkg_new()
 {
+       saved_conf = *conf;
+
        if (opkg_conf_init())
                goto err0;
 
+       if (opkg_conf_load())
+               goto err0;
+
        if (pkg_hash_load_feeds())
                goto err1;
 
@@ -147,20 +153,9 @@ opkg_free(void)
 int
 opkg_re_read_config_files(void)
 {
-       pkg_hash_deinit();
-       pkg_hash_init();
-
-       if (pkg_hash_load_feeds())
-               goto err;
-
-       if (pkg_hash_load_status_files())
-               goto err;
-
-       return 0;
-
-err:
-       pkg_hash_deinit();
-       return -1;
+       opkg_free();
+       *conf = saved_conf;
+       return opkg_new();
 }
 
 void
@@ -244,7 +239,7 @@ opkg_set_option(char *option, void *value)
 /**
  * @brief libopkg API: Install package
  * @param package_name The name of package in which is going to install
- * @param progress_callback The callback function that report the status to caller. 
+ * @param progress_callback The callback function that report the status to caller.
  */
 int
 opkg_install_package(const char *package_name,
@@ -347,7 +342,7 @@ opkg_install_package(const char *package_name,
 
                err = opkg_download(url, pkg->local_filename,
                                    (curl_progress_func) curl_progress_cb,
-                                   &cb_data);
+                                   &cb_data, 0);
                free(url);
 
                if (err) {
@@ -523,6 +518,10 @@ opkg_upgrade_all(opkg_progress_callback_t progress_callback, void *user_data)
        if (err)
                return 1;
 
+       /* write out status files and file lists */
+       opkg_conf_write_status_files();
+       pkg_write_changed_filelists();
+
        pdata.pkg = NULL;
        progress(pdata, 100);
        return 0;
@@ -593,55 +592,14 @@ opkg_update_package_lists(opkg_progress_callback_t progress_callback,
                                      src->gzip ? "Packages.gz" : "Packages");
 
                sprintf_alloc(&list_file_name, "%s/%s", lists_dir, src->name);
-               if (src->gzip) {
-                       FILE *in, *out;
-                       struct _curl_cb_data cb_data;
-                       char *tmp_file_name = NULL;
 
-                       sprintf_alloc(&tmp_file_name, "%s/%s.gz", tmp,
-                                     src->name);
-
-                       opkg_msg(INFO, "Downloading %s to %s...\n", url,
-                                       tmp_file_name);
-
-                       cb_data.cb = progress_callback;
-                       cb_data.progress_data = &pdata;
-                       cb_data.user_data = user_data;
-                       cb_data.start_range =
-                           100 * sources_done / sources_list_count;
-                       cb_data.finish_range =
-                           100 * (sources_done + 1) / sources_list_count;
-
-                       err = opkg_download(url, tmp_file_name,
-                                         (curl_progress_func) curl_progress_cb,
-                                         &cb_data);
-
-                       if (err == 0) {
-                               opkg_msg(INFO, "Inflating %s...\n",
-                                               tmp_file_name);
-                               in = fopen(tmp_file_name, "r");
-                               out = fopen(list_file_name, "w");
-                               if (in && out)
-                                       unzip(in, out);
-                               else
-                                       err = 1;
-                               if (in)
-                                       fclose(in);
-                               if (out)
-                                       fclose(out);
-                               unlink(tmp_file_name);
-                       }
-                       free(tmp_file_name);
-               } else
-                       err = opkg_download(url, list_file_name, NULL, NULL);
-
-               if (err) {
+               if (opkg_download(url, list_file_name, NULL, NULL, 0)) {
                        opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
                        result = -1;
                }
                free(url);
 
-#if defined(HAVE_GPGME) || defined(HAVE_OPENSSL)
+#if defined(HAVE_GPGME) || defined(HAVE_OPENSSL) || defined(HAVE_USIGN)
                if (conf->check_signature) {
                        char *sig_file_name;
                        /* download detached signitures to verify the package lists */
@@ -660,7 +618,7 @@ opkg_update_package_lists(opkg_progress_callback_t progress_callback,
                        /* make sure there is no existing signature file */
                        unlink(sig_file_name);
 
-                       err = opkg_download(url, sig_file_name, NULL, NULL);
+                       err = opkg_download(url, sig_file_name, NULL, NULL, 0);
                        if (err) {
                                opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
                        } else {
@@ -678,7 +636,6 @@ opkg_update_package_lists(opkg_progress_callback_t progress_callback,
                                }
                        }
                        free(sig_file_name);
-                       free(list_file_name);
                        free(url);
                }
 #else
@@ -686,6 +643,7 @@ opkg_update_package_lists(opkg_progress_callback_t progress_callback,
                                " has not been enabled in this build\n",
                                list_file_name);
 #endif
+               free(list_file_name);
 
                sources_done++;
                progress(pdata, 100 * sources_done / sources_list_count);
@@ -756,7 +714,7 @@ opkg_list_upgradable_packages(opkg_package_callback_t callback, void *user_data)
 
        head = prepare_upgrade_list();
        for (node = active_list_next(head, head); node;
-            active_list_next(head, node)) {
+            node = active_list_next(head, node)) {
                old = list_entry(node, pkg_t, list);
                new = pkg_hash_fetch_best_installation_candidate_by_name(old->name);
                if (new == NULL)
@@ -771,6 +729,7 @@ pkg_t *
 opkg_find_package(const char *name, const char *ver, const char *arch,
                const char *repo)
 {
+       int pkg_found = 0;
        pkg_t *pkg = NULL;
        pkg_vec_t *all;
        int i;
@@ -808,17 +767,18 @@ opkg_find_package(const char *name, const char *ver, const char *arch,
                }
 
                /* match found */
+               pkg_found = 1;
                break;
        }
 
        pkg_vec_free(all);
 
-       return pkg;
+       return pkg_found ? pkg : NULL;
 }
 
 /**
  * @brief Check the accessibility of repositories.
- * @return return how many repositories cannot access. 0 means all okay. 
+ * @return return how many repositories cannot access. 0 means all okay.
  */
 int
 opkg_repository_accessibility_check(void)
@@ -863,7 +823,7 @@ opkg_repository_accessibility_check(void)
                iter1 = str_list_pop(src);
                repositories--;
 
-               if (opkg_download(iter1->data, "/dev/null", NULL, NULL))
+               if (opkg_download(iter1->data, "/dev/null", NULL, NULL, 0))
                        ret++;
                str_list_elt_deinit(iter1);
        }