Allow use of opkg internals by libopkg users. Not intended to be permanent.
[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_upgrade.h"
22 #include "opkg_message.h"
23
24 int
25 opkg_upgrade_pkg(pkg_t *old)
26 {
27 pkg_t *new;
28 int cmp;
29 char *old_version, *new_version;
30
31 if (old->state_flag & SF_HOLD) {
32 opkg_msg(NOTICE, "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(old->name);
38 if (new == NULL) {
39 old_version = pkg_version_str_alloc(old);
40 opkg_msg(NOTICE, "Assuming locally installed package %s (%s) "
41 "is up to date.\n", old->name, old_version);
42 free(old_version);
43 return 0;
44 }
45
46 old_version = pkg_version_str_alloc(old);
47 new_version = pkg_version_str_alloc(new);
48
49 cmp = pkg_compare_versions(old, new);
50 opkg_msg(DEBUG, "Comparing visible versions of pkg %s:"
51 "\n\t%s is installed "
52 "\n\t%s is available "
53 "\n\t%d was comparison result\n",
54 old->name, old_version, new_version, cmp);
55 if (cmp == 0) {
56 opkg_msg(INFO, "Package %s (%s) installed in %s is up to date.\n",
57 old->name, old_version, old->dest->name);
58 free(old_version);
59 free(new_version);
60 return 0;
61 } else if (cmp > 0) {
62 opkg_msg(NOTICE, "Not downgrading package %s on %s from %s to %s.\n",
63 old->name, old->dest->name, old_version, new_version);
64 free(old_version);
65 free(new_version);
66 return 0;
67 } else if (cmp < 0) {
68 new->dest = old->dest;
69 old->state_want = SW_DEINSTALL;
70 }
71
72 free(old_version);
73 free(new_version);
74 new->state_flag |= SF_USER;
75 return opkg_install_pkg(new,1);
76 }
77
78
79 static void
80 pkg_hash_check_installed_pkg_helper(const char *pkg_name, void *entry,
81 void *data)
82 {
83 struct active_list *head = (struct active_list *) data;
84 abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry;
85 pkg_vec_t *pkg_vec = ab_pkg->pkgs;
86 int j;
87
88 if (!pkg_vec)
89 return;
90
91 for (j = 0; j < pkg_vec->len; j++) {
92 pkg_t *pkg = pkg_vec->pkgs[j];
93 if (pkg->state_status == SS_INSTALLED
94 || pkg->state_status == SS_UNPACKED)
95 active_list_add(head, &pkg->list);
96 }
97 }
98
99 struct active_list *
100 prepare_upgrade_list(void)
101 {
102 struct active_list *head = active_list_head_new();
103 struct active_list *all = active_list_head_new();
104 struct active_list *node=NULL;
105
106 /* ensure all data is valid */
107 pkg_info_preinstall_check();
108
109 hash_table_foreach(&conf->pkg_hash, pkg_hash_check_installed_pkg_helper, all);
110 for (node=active_list_next(all,all); node; node = active_list_next(all, node)) {
111 pkg_t *old, *new;
112 int cmp;
113
114 old = list_entry(node, pkg_t, list);
115 new = pkg_hash_fetch_best_installation_candidate_by_name(old->name);
116
117 if (new == NULL)
118 continue;
119
120 cmp = pkg_compare_versions(old, new);
121
122 if ( cmp < 0 ) {
123 node = active_list_move_node(all, head, &old->list);
124 }
125 }
126 active_list_head_delete(all);
127 return head;
128 }