Remove trailing whitespace. Sorry if this breaks your patches.
[oweals/opkg-lede.git] / libopkg / pkg.c
index e54182500d8798e05f09c29292902bf9ff661e70..38816d4af6034e8d391e3b696ffc5452e7444b15 100644 (file)
    General Public License for more details.
 */
 
-#include "includes.h"
-#include <ctype.h>
-#include <alloca.h>
+#include "config.h"
+
+#include <stdio.h>
 #include <string.h>
-#include <stdbool.h>
-#include <errno.h>
+#include <ctype.h>
+#include <unistd.h>
 
 #include "pkg.h"
 
@@ -95,7 +95,7 @@ pkg_init(pkg_t *pkg)
      pkg->recommends_str = NULL;
      pkg->suggests_count = 0;
      pkg->recommends_count = 0;
-     
+
      active_list_init(&pkg->list);
 
      pkg->conflicts = NULL;
@@ -103,7 +103,7 @@ pkg_init(pkg_t *pkg)
 
      pkg->replaces = NULL;
      pkg->replaces_count = 0;
-    
+
      pkg->pre_depends_count = 0;
      pkg->pre_depends_str = NULL;
      pkg->provides_count = 0;
@@ -188,7 +188,7 @@ pkg_deinit(pkg_t *pkg)
        if (pkg->description)
                free(pkg->description);
        pkg->description = NULL;
-       
+
        pkg->state_want = SW_UNKNOWN;
        pkg->state_flag = SF_OK;
        pkg->state_status = SS_NOT_INSTALLED;
@@ -221,11 +221,11 @@ pkg_deinit(pkg_t *pkg)
 
        pkg->pre_depends_count = 0;
        pkg->provides_count = 0;
-       
+
        if (pkg->filename)
                free(pkg->filename);
        pkg->filename = NULL;
-       
+
        if (pkg->local_filename)
                free(pkg->local_filename);
        pkg->local_filename = NULL;
@@ -274,15 +274,17 @@ pkg_init_from_file(pkg_t *pkg, const char *filename)
 {
        int fd, err = 0;
        FILE *control_file;
-       char *control_path;
+       char *control_path, *tmp;
 
        pkg_init(pkg);
 
        pkg->local_filename = xstrdup(filename);
 
-       sprintf_alloc(&control_path, "%s/%s.control.XXXXXX", 
+       tmp = xstrdup(filename);
+       sprintf_alloc(&control_path, "%s/%s.control.XXXXXX",
                         conf->tmp_dir,
-                        basename(filename));
+                        basename(tmp));
+       free(tmp);
        fd = mkstemp(control_path);
        if (fd == -1) {
                opkg_perror(ERROR, "Failed to make temp file %s", control_path);
@@ -307,8 +309,13 @@ pkg_init_from_file(pkg_t *pkg, const char *filename)
 
        rewind(control_file);
 
-       if (pkg_parse_from_stream(pkg, control_file, 0))
+       if ((err = pkg_parse_from_stream(pkg, control_file, 0))) {
+               if (err == 1) {
+                       opkg_msg(ERROR, "Malformed package file %s.\n",
+                               filename);
+               }
                err = -1;
+       }
 
 err2:
        fclose(control_file);
@@ -634,8 +641,8 @@ pkg_formatted_field(FILE *fp, pkg_t *pkg, const char *field)
                fprintf(fp, "Conffiles:\n");
               for (iter = nv_pair_list_first(&pkg->conffiles); iter; iter = nv_pair_list_next(&pkg->conffiles, iter)) {
                    if (((conffile_t *)iter->data)->name && ((conffile_t *)iter->data)->value) {
-                         fprintf(fp, " %s %s\n", 
-                                 ((conffile_t *)iter->data)->name, 
+                         fprintf(fp, " %s %s\n",
+                                 ((conffile_t *)iter->data)->name,
                                  ((conffile_t *)iter->data)->value);
                    }
               }
@@ -997,7 +1004,7 @@ pkg_name_version_and_architecture_compare(const void *p1, const void *p2)
               a->name, b->name);
        return 0;
      }
-       
+
      namecmp = strcmp(a->name, b->name);
      if (namecmp)
          return namecmp;
@@ -1065,6 +1072,7 @@ pkg_get_installed_files(pkg_t *pkg)
      char *line;
      char *installed_file_name;
      unsigned int rootdirlen = 0;
+     int list_from_package;
 
      pkg->installed_files_ref_cnt++;
 
@@ -1074,17 +1082,23 @@ pkg_get_installed_files(pkg_t *pkg)
 
      pkg->installed_files = str_list_alloc();
 
-     /* For uninstalled packages, get the file list directly from the package.
-       For installed packages, look at the package.list file in the database.
-     */
-     if (pkg->state_status == SS_NOT_INSTALLED || pkg->dest == NULL) {
+     /*
+      * For installed packages, look at the package.list file in the database.
+      * For uninstalled packages, get the file list directly from the package.
+      */
+     if (pkg->state_status == SS_NOT_INSTALLED || pkg->dest == NULL)
+            list_from_package = 1;
+     else
+            list_from_package = 0;
+
+     if (list_from_package) {
          if (pkg->local_filename == NULL) {
               return pkg->installed_files;
          }
          /* XXX: CLEANUP: Maybe rewrite this to avoid using a temporary
             file. In other words, change deb_extract so that it can
             simply return the file list as a char *[] rather than
-            insisting on writing in to a FILE * as it does now. */
+            insisting on writing it to a FILE * as it does now. */
          sprintf_alloc(&list_file_name, "%s/%s.list.XXXXXX",
                                          conf->tmp_dir, pkg->name);
          fd = mkstemp(list_file_name);
@@ -1133,14 +1147,14 @@ pkg_get_installed_files(pkg_t *pkg)
 
      while (1) {
          char *file_name;
-       
+
          line = file_read_line_alloc(list_file);
          if (line == NULL) {
               break;
          }
          file_name = line;
 
-         if (pkg->state_status == SS_NOT_INSTALLED || pkg->dest == NULL) {
+         if (list_from_package) {
               if (*file_name == '.') {
                    file_name++;
               }
@@ -1166,7 +1180,7 @@ pkg_get_installed_files(pkg_t *pkg)
 
      fclose(list_file);
 
-     if (pkg->state_status == SS_NOT_INSTALLED || pkg->dest == NULL) {
+     if (list_from_package) {
          unlink(list_file_name);
          free(list_file_name);
      }
@@ -1313,54 +1327,12 @@ pkg_arch_supported(pkg_t *pkg)
      return 0;
 }
 
-static int
-pkg_get_arch_priority(const char *archname)
-{
-     nv_pair_list_elt_t *l;
-
-     list_for_each_entry(l , &conf->arch_list.head, node) {
-         nv_pair_t *nv = (nv_pair_t *)l->data;
-         if (strcmp(nv->name, archname) == 0) {
-              int priority = strtol(nv->value, NULL, 0);
-              return priority;
-         }
-     }
-     return 0;
-}
-
 void
 pkg_info_preinstall_check(void)
 {
      int i;
-     pkg_vec_t *available_pkgs = pkg_vec_alloc();
      pkg_vec_t *installed_pkgs = pkg_vec_alloc();
 
-     opkg_msg(INFO, "Updating arch priority for each package.\n");
-     pkg_hash_fetch_available(available_pkgs);
-     /* update arch_priority for each package */
-     for (i = 0; i < available_pkgs->len; i++) {
-         pkg_t *pkg = available_pkgs->pkgs[i];
-         int arch_priority = 1;
-         if (!pkg)
-              continue;
-         arch_priority = pkg_get_arch_priority(pkg->architecture);
-         pkg->arch_priority = arch_priority;
-     }
-
-     for (i = 0; i < available_pkgs->len; i++) {
-         pkg_t *pkg = available_pkgs->pkgs[i];
-         if (!pkg->arch_priority && (pkg->state_flag || (pkg->state_want != SW_UNKNOWN))) {
-              /* clear flags and want for any uninstallable package */
-              opkg_msg(DEBUG, "Clearing state_want and state_flag for pkg=%s "
-                              "(arch_priority=%d flag=%d want=%d)\n", 
-                              pkg->name, pkg->arch_priority,
-                              pkg->state_flag, pkg->state_want);
-              pkg->state_want = SW_UNKNOWN;
-              pkg->state_flag = 0;
-         }
-     }
-     pkg_vec_free(available_pkgs);
-
      /* update the file owner data structure */
      opkg_msg(INFO, "Updating file owner list.\n");
      pkg_hash_fetch_all_installed(installed_pkgs);
@@ -1373,8 +1345,8 @@ pkg_info_preinstall_check(void)
                               "files for pkg %s.\n", pkg->name);
               break;
          }
-         for (iter = str_list_first(installed_files), niter = str_list_next(installed_files, iter); 
-                  iter; 
+         for (iter = str_list_first(installed_files), niter = str_list_next(installed_files, iter);
+                  iter;
                   iter = niter, niter = str_list_next(installed_files, iter)) {
               char *installed_file = (char *) iter->data;
               file_hash_set_file_owner(installed_file, pkg);