Allow blacklisting a package in the status file.
[project/opkg-lede.git] / libopkg / pkg_vec.c
1 /* pkg_vec.c - the opkg package management system
2
3 Steven M. Ayer
4
5 Copyright (C) 2002 Compaq Computer Corporation
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2, or (at
10 your option) any later version.
11
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16 */
17
18 #include <stdio.h>
19 #include <fnmatch.h>
20
21 #include "pkg.h"
22 #include "opkg_message.h"
23 #include "libbb/libbb.h"
24
25 pkg_vec_t * pkg_vec_alloc(void)
26 {
27 pkg_vec_t * vec = xcalloc(1, sizeof(pkg_vec_t));
28 vec->pkgs = NULL;
29 vec->len = 0;
30
31 return vec;
32 }
33
34 void pkg_vec_free(pkg_vec_t *vec)
35 {
36 if (!vec)
37 return;
38
39 if (vec->pkgs)
40 free(vec->pkgs);
41
42 free(vec);
43 }
44
45 /*
46 * assumption: all names in a vector are identical
47 * assumption: all version strings are trimmed,
48 * so identical versions have identical version strings,
49 * implying identical packages; let's marry these
50 */
51 void pkg_vec_insert_merge(pkg_vec_t *vec, pkg_t *pkg, int set_status)
52 {
53 int i;
54 int found = 0;
55
56 /* look for a duplicate pkg by name, version, and architecture */
57 for (i = 0; i < vec->len; i++){
58 opkg_msg(DEBUG2, "%s %s arch=%s vs. %s %s arch=%s.\n",
59 pkg->name, pkg->version, pkg->architecture,
60 vec->pkgs[i]->name, vec->pkgs[i]->version,
61 vec->pkgs[i]->architecture);
62 /* if the name,ver,arch matches, or the name matches and the
63 * package is marked deinstall/hold */
64 if ((!strcmp(pkg->name, vec->pkgs[i]->name))
65 && ((pkg->state_want == SW_DEINSTALL
66 && (pkg->state_flag & SF_HOLD))
67 || ((pkg_compare_versions(pkg, vec->pkgs[i]) == 0)
68 && (!strcmp(pkg->architecture, vec->pkgs[i]->architecture))))) {
69 found = 1;
70 opkg_msg(DEBUG2, "Duplicate for pkg=%s version=%s arch=%s.\n",
71 pkg->name, pkg->version, pkg->architecture);
72 break;
73 }
74 }
75
76 /* we didn't find one, add it */
77 if (!found){
78 opkg_msg(DEBUG2, "Adding new pkg=%s version=%s arch=%s.\n",
79 pkg->name, pkg->version, pkg->architecture);
80 pkg_vec_insert(vec, pkg);
81 return;
82 }
83
84 /* update the one that we have */
85 opkg_msg(DEBUG2, "Merging %s %s arch=%s, set_status=%d.\n",
86 pkg->name, pkg->version, pkg->architecture, set_status);
87 if (set_status) {
88 /* This is from the status file,
89 * so need to merge with existing database */
90 pkg_merge(pkg, vec->pkgs[i]);
91 }
92
93 /* overwrite the old one */
94 pkg_deinit(vec->pkgs[i]);
95 free(vec->pkgs[i]);
96 vec->pkgs[i] = pkg;
97 }
98
99 void pkg_vec_insert(pkg_vec_t *vec, const pkg_t *pkg)
100 {
101 vec->pkgs = xrealloc(vec->pkgs, (vec->len + 1) * sizeof(pkg_t *));
102 vec->pkgs[vec->len] = (pkg_t *)pkg;
103 vec->len++;
104 }
105
106 int pkg_vec_contains(pkg_vec_t *vec, pkg_t *apkg)
107 {
108 int i;
109 for (i = 0; i < vec->len; i++)
110 if (vec->pkgs[i] == apkg)
111 return 1;
112 return 0;
113 }
114
115 void pkg_vec_sort(pkg_vec_t *vec, compare_fcn_t compar)
116 {
117 qsort(vec->pkgs, vec->len, sizeof(pkg_t *), compar);
118 }
119
120 int pkg_vec_clear_marks(pkg_vec_t *vec)
121 {
122 int npkgs = vec->len;
123 int i;
124 for (i = 0; i < npkgs; i++) {
125 pkg_t *pkg = vec->pkgs[i];
126 pkg->state_flag &= ~SF_MARKED;
127 }
128 return 0;
129 }
130
131 int pkg_vec_mark_if_matches(pkg_vec_t *vec, const char *pattern)
132 {
133 int matching_count = 0;
134 pkg_t **pkgs = vec->pkgs;
135 int npkgs = vec->len;
136 int i;
137 for (i = 0; i < npkgs; i++) {
138 pkg_t *pkg = pkgs[i];
139 if (fnmatch(pattern, pkg->name, 0)==0) {
140 pkg->state_flag |= SF_MARKED;
141 matching_count++;
142 }
143 }
144 return matching_count;
145 }
146
147
148 abstract_pkg_vec_t * abstract_pkg_vec_alloc(void)
149 {
150 abstract_pkg_vec_t * vec ;
151 vec = xcalloc(1, sizeof(abstract_pkg_vec_t));
152 vec->pkgs = NULL;
153 vec->len = 0;
154
155 return vec;
156 }
157
158 void abstract_pkg_vec_free(abstract_pkg_vec_t *vec)
159 {
160 if (!vec)
161 return;
162 free(vec->pkgs);
163 free(vec);
164 }
165
166 /*
167 * assumption: all names in a vector are unique
168 */
169 void abstract_pkg_vec_insert(abstract_pkg_vec_t *vec, abstract_pkg_t *pkg)
170 {
171 vec->pkgs = xrealloc(vec->pkgs, (vec->len + 1) * sizeof(abstract_pkg_t *));
172 vec->pkgs[vec->len] = pkg;
173 vec->len++;
174 }
175
176 abstract_pkg_t * abstract_pkg_vec_get(abstract_pkg_vec_t *vec, int i)
177 {
178 if (vec->len > i)
179 return vec->pkgs[i];
180 else
181 return NULL;
182 }
183
184 int abstract_pkg_vec_contains(abstract_pkg_vec_t *vec, abstract_pkg_t *apkg)
185 {
186 int i;
187 for (i = 0; i < vec->len; i++)
188 if (vec->pkgs[i] == apkg)
189 return 1;
190 return 0;
191 }
192
193 void abstract_pkg_vec_sort(pkg_vec_t *vec, compare_fcn_t compar)
194 {
195 qsort(vec->pkgs, vec->len, sizeof(pkg_t *), compar);
196 }
197
198 int pkg_compare_names(const void *p1, const void *p2)
199 {
200 const pkg_t *pkg1 = *(const pkg_t **)p1;
201 const pkg_t *pkg2 = *(const pkg_t **)p2;
202 if (pkg1->name == NULL)
203 return 1;
204 if (pkg2->name == NULL)
205 return -1;
206 return(strcmp(pkg1->name, pkg2->name));
207 }
208