Remove dead code, sprintf_alloc() cannot fail. Opkg will exit instead.
[oweals/opkg-lede.git] / libopkg / opkg_upgrade.c
1 /* opkg_upgrade.c - the opkg package management system
2
3    Carl D. Worth
4    Copyright (C) 2001 University of Southern California
5
6    Copyright (C) 2003 Daniele Nicolodi <daniele@grinta.net>
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17 */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21
22 #include "opkg_install.h"
23 #include "opkg_upgrade.h"
24 #include "opkg_message.h"
25
26 int
27 opkg_upgrade_pkg(pkg_t *old)
28 {
29      pkg_t *new;
30      int cmp;
31      char *old_version, *new_version;
32
33      if (old->state_flag & SF_HOLD) {
34           opkg_msg(NOTICE, "Not upgrading package %s which is marked "
35                        "hold (flags=%#x).\n", old->name, old->state_flag);
36           return 0;
37      }
38
39      new = pkg_hash_fetch_best_installation_candidate_by_name(old->name);
40      if (new == NULL) {
41           old_version = pkg_version_str_alloc(old);
42           opkg_msg(NOTICE, "Assuming locally installed package %s (%s) "
43                        "is up to date.\n", old->name, old_version);
44           free(old_version);
45           return 0;
46      }
47
48      old_version = pkg_version_str_alloc(old);
49      new_version = pkg_version_str_alloc(new);
50
51      cmp = pkg_compare_versions(old, new);
52      opkg_msg(DEBUG, "Comparing visible versions of pkg %s:"
53                   "\n\t%s is installed "
54                   "\n\t%s is available "
55                   "\n\t%d was comparison result\n",
56                   old->name, old_version, new_version, cmp);
57      if (cmp == 0) {
58           opkg_msg(INFO, "Package %s (%s) installed in %s is up to date.\n",
59                        old->name, old_version, old->dest->name);
60           free(old_version);
61           free(new_version);
62           return 0;
63      } else if (cmp > 0) {
64           opkg_msg(NOTICE, "Not downgrading package %s on %s from %s to %s.\n",
65                        old->name, old->dest->name, old_version, new_version);
66           free(old_version);
67           free(new_version);
68           return 0;
69      } else if (cmp < 0) {
70           new->dest = old->dest;
71           old->state_want = SW_DEINSTALL;
72      }
73
74     free(old_version);
75     free(new_version);
76     new->state_flag |= SF_USER;
77     return opkg_install_pkg(new,1);
78 }
79
80
81 static void
82 pkg_hash_check_installed_pkg_helper(const char *pkg_name, void *entry,
83                 void *data)
84 {
85         struct active_list *head = (struct active_list *) data;
86         abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry;
87         pkg_vec_t *pkg_vec = ab_pkg->pkgs;
88         int j;
89
90         if (!pkg_vec)
91                 return;
92
93         for (j = 0; j < pkg_vec->len; j++) {
94                 pkg_t *pkg = pkg_vec->pkgs[j];
95                 if (pkg->state_status == SS_INSTALLED
96                                 || pkg->state_status == SS_UNPACKED)
97                         active_list_add(head, &pkg->list);
98         }
99 }
100
101 struct active_list *
102 prepare_upgrade_list(void)
103 {
104     struct active_list *head = active_list_head_new();
105     struct active_list *all = active_list_head_new();
106     struct active_list *node=NULL;
107
108     /* ensure all data is valid */
109     pkg_info_preinstall_check();
110
111     hash_table_foreach(&conf->pkg_hash, pkg_hash_check_installed_pkg_helper, all);
112     for (node=active_list_next(all,all); node; node = active_list_next(all, node)) {
113         pkg_t *old, *new;
114         int cmp;
115
116         old = list_entry(node, pkg_t, list);
117         new = pkg_hash_fetch_best_installation_candidate_by_name(old->name);
118
119         if (new == NULL)
120             continue;
121
122         cmp = pkg_compare_versions(old, new);
123
124         if ( cmp < 0 ) {
125            node = active_list_move_node(all, head, &old->list);
126         }
127     }
128     active_list_head_delete(all);
129     return head;
130 }