Minor cleanup for opkg_upgrade.c
[project/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 "includes.h"
20 #include "opkg_install.h"
21 #include "opkg_message.h"
22
23 int
24 opkg_upgrade_pkg(opkg_conf_t *conf, pkg_t *old)
25 {
26 pkg_t *new;
27 int cmp;
28 char *old_version, *new_version;
29
30 if (old->state_flag & SF_HOLD) {
31 opkg_message(conf, OPKG_NOTICE,
32 "Not upgrading package %s which is marked "
33 "hold (flags=%#x)\n", old->name, old->state_flag);
34 return 0;
35 }
36
37 new = pkg_hash_fetch_best_installation_candidate_by_name(conf, old->name, NULL);
38 if (new == NULL) {
39 old_version = pkg_version_str_alloc(old);
40 opkg_message(conf, OPKG_NOTICE,
41 "Assuming locally installed package %s (%s) "
42 "is up to date.\n", old->name, old_version);
43 free(old_version);
44 return 0;
45 }
46
47 old_version = pkg_version_str_alloc(old);
48 new_version = pkg_version_str_alloc(new);
49
50 cmp = pkg_compare_versions(old, new);
51 opkg_message(conf, OPKG_DEBUG,
52 "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_message(conf, OPKG_INFO,
59 "Package %s (%s) installed in %s is up to date.\n",
60 old->name, old_version, old->dest->name);
61 free(old_version);
62 free(new_version);
63 return 0;
64 } else if (cmp > 0) {
65 opkg_message(conf, OPKG_NOTICE,
66 "Not downgrading package %s on %s from %s to %s.\n",
67 old->name, old->dest->name, old_version, new_version);
68 free(old_version);
69 free(new_version);
70 return 0;
71 } else if (cmp < 0) {
72 new->dest = old->dest;
73 old->state_want = SW_DEINSTALL;
74 }
75
76 free(old_version);
77 free(new_version);
78 new->state_flag |= SF_USER;
79 return opkg_install_pkg(conf, new,1);
80 }
81
82
83 static void
84 pkg_hash_check_installed_pkg_helper(const char *pkg_name, void *entry,
85 void *data)
86 {
87 struct active_list *head = (struct active_list *) data;
88 abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry;
89 pkg_vec_t *pkg_vec = ab_pkg->pkgs;
90 int j;
91
92 if (!pkg_vec)
93 return;
94
95 for (j = 0; j < pkg_vec->len; j++) {
96 pkg_t *pkg = pkg_vec->pkgs[j];
97 if (pkg->state_status == SS_INSTALLED
98 || pkg->state_status == SS_UNPACKED)
99 active_list_add(head, &pkg->list);
100 }
101 }
102
103 struct active_list *
104 prepare_upgrade_list(opkg_conf_t *conf)
105 {
106 struct active_list *head = active_list_head_new();
107 struct active_list *all = active_list_head_new();
108 struct active_list *node=NULL;
109
110 /* ensure all data is valid */
111 pkg_info_preinstall_check (conf);
112
113 hash_table_foreach(&conf->pkg_hash, pkg_hash_check_installed_pkg_helper, all);
114 for (node=active_list_next(all,all); node; node = active_list_next(all, node)) {
115 pkg_t *old, *new;
116 int cmp;
117
118 old = list_entry(node, pkg_t, list);
119 new = pkg_hash_fetch_best_installation_candidate_by_name(conf, old->name, NULL);
120
121 if (new == NULL)
122 continue;
123
124 cmp = pkg_compare_versions(old, new);
125
126 if ( cmp < 0 ) {
127 node = active_list_move_node(all, head, &old->list);
128 }
129 }
130 active_list_head_delete(all);
131 return head;
132 }