adding list_upgradable
[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 "includes.h"
20 #include "opkg_install.h"
21 #include "opkg_message.h"
22
23 int opkg_upgrade_pkg(opkg_conf_t *conf, pkg_t *old)
24 {
25      pkg_t *new;
26      int cmp;
27      char *old_version, *new_version;
28
29      if (old->state_flag & SF_HOLD) {
30           opkg_message(conf, OPKG_NOTICE,
31                        "Not upgrading package %s which is marked "
32                        "hold (flags=%#x)\n", old->name, old->state_flag);
33           return 0;
34      }
35
36      new = pkg_hash_fetch_best_installation_candidate_by_name(conf, old->name, NULL);
37      if (new == NULL) {
38           old_version = pkg_version_str_alloc(old);
39           opkg_message(conf, OPKG_NOTICE,
40                        "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_message(conf, OPKG_DEBUG,
51                   "comparing visible versions of pkg %s:"
52                   "\n\t%s is installed "
53                   "\n\t%s is available "
54                   "\n\t%d was comparison result\n",
55                   old->name, old_version, new_version, cmp);
56      if (cmp == 0) {
57           opkg_message(conf, OPKG_INFO,
58                        "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_message(conf, OPKG_NOTICE,
65                        "Not downgrading package %s on %s from %s to %s.\n",
66                        old->name, old->dest->name, old_version, new_version);
67           free(old_version);
68           free(new_version);
69           return 0;
70      } else if (cmp < 0) {
71           new->dest = old->dest;
72           old->state_want = SW_DEINSTALL;
73      }
74
75     free(old_version);
76     free(new_version);
77     new->state_flag |= SF_USER;
78     return opkg_install_pkg(conf, new,1);
79 }
80
81
82
83 pkg_vec_t *opkg_upgrade_all_list_get(opkg_conf_t *conf) {
84     pkg_vec_t *all, *ans;
85     int i;
86
87     /* ensure all data is valid */
88     pkg_info_preinstall_check (conf);
89
90     all = pkg_vec_alloc ();
91     ans = pkg_vec_alloc ();
92     pkg_hash_fetch_all_installed (&conf->pkg_hash, all);
93     for (i = 0; i < all->len; i++)
94     {
95         pkg_t *old, *new;
96         int cmp;
97
98         old = all->pkgs[i];
99         new = pkg_hash_fetch_best_installation_candidate_by_name(conf, old->name, NULL);
100
101         if (new == NULL)
102             continue;
103
104         cmp = pkg_compare_versions(old, new);
105
106         if ( cmp < 0 )
107             pkg_vec_insert(ans, old);
108     }
109     pkg_vec_free (all);
110     return ans;
111 }