treewrite: use Lindent to reformat to kernel coding style
[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 &&
69 (!strcmp
70 (pkg->architecture,
71 vec->pkgs[i]->architecture))))) {
72 found = 1;
73 opkg_msg(DEBUG2,
74 "Duplicate for pkg=%s version=%s arch=%s.\n",
75 pkg->name, pkg->version, pkg->architecture);
76 break;
77 }
78 }
79
80 /* we didn't find one, add it */
81 if (!found) {
82 opkg_msg(DEBUG2, "Adding new pkg=%s version=%s arch=%s.\n",
83 pkg->name, pkg->version, pkg->architecture);
84 pkg_vec_insert(vec, pkg);
85 return;
86 }
87
88 /* update the one that we have */
89 opkg_msg(DEBUG2, "Merging %s %s arch=%s, set_status=%d.\n",
90 pkg->name, pkg->version, pkg->architecture, set_status);
91 if (set_status) {
92 /* This is from the status file,
93 * so need to merge with existing database */
94 pkg_merge(pkg, vec->pkgs[i]);
95 }
96
97 /* overwrite the old one */
98 pkg_deinit(vec->pkgs[i]);
99 free(vec->pkgs[i]);
100 vec->pkgs[i] = pkg;
101 }
102
103 void pkg_vec_insert(pkg_vec_t * vec, const pkg_t * pkg)
104 {
105 vec->pkgs = xrealloc(vec->pkgs, (vec->len + 1) * sizeof(pkg_t *));
106 vec->pkgs[vec->len] = (pkg_t *) pkg;
107 vec->len++;
108 }
109
110 int pkg_vec_contains(pkg_vec_t * vec, pkg_t * apkg)
111 {
112 int i;
113 for (i = 0; i < vec->len; i++)
114 if (vec->pkgs[i] == apkg)
115 return 1;
116 return 0;
117 }
118
119 void pkg_vec_sort(pkg_vec_t * vec, compare_fcn_t compar)
120 {
121 qsort(vec->pkgs, vec->len, sizeof(pkg_t *), compar);
122 }
123
124 int pkg_vec_clear_marks(pkg_vec_t * vec)
125 {
126 int npkgs = vec->len;
127 int i;
128 for (i = 0; i < npkgs; i++) {
129 pkg_t *pkg = vec->pkgs[i];
130 pkg->state_flag &= ~SF_MARKED;
131 }
132 return 0;
133 }
134
135 int pkg_vec_mark_if_matches(pkg_vec_t * vec, const char *pattern)
136 {
137 int matching_count = 0;
138 pkg_t **pkgs = vec->pkgs;
139 int npkgs = vec->len;
140 int i;
141 for (i = 0; i < npkgs; i++) {
142 pkg_t *pkg = pkgs[i];
143 if (fnmatch(pattern, pkg->name, 0) == 0) {
144 pkg->state_flag |= SF_MARKED;
145 matching_count++;
146 }
147 }
148 return matching_count;
149 }
150
151 abstract_pkg_vec_t *abstract_pkg_vec_alloc(void)
152 {
153 abstract_pkg_vec_t *vec;
154 vec = xcalloc(1, sizeof(abstract_pkg_vec_t));
155 vec->pkgs = NULL;
156 vec->len = 0;
157
158 return vec;
159 }
160
161 void abstract_pkg_vec_free(abstract_pkg_vec_t * vec)
162 {
163 if (!vec)
164 return;
165 free(vec->pkgs);
166 free(vec);
167 }
168
169 /*
170 * assumption: all names in a vector are unique
171 */
172 void abstract_pkg_vec_insert(abstract_pkg_vec_t * vec, abstract_pkg_t * pkg)
173 {
174 vec->pkgs =
175 xrealloc(vec->pkgs, (vec->len + 1) * sizeof(abstract_pkg_t *));
176 vec->pkgs[vec->len] = pkg;
177 vec->len++;
178 }
179
180 abstract_pkg_t *abstract_pkg_vec_get(abstract_pkg_vec_t * vec, int i)
181 {
182 if (vec->len > i)
183 return vec->pkgs[i];
184 else
185 return NULL;
186 }
187
188 int abstract_pkg_vec_contains(abstract_pkg_vec_t * vec, abstract_pkg_t * apkg)
189 {
190 int i;
191 for (i = 0; i < vec->len; i++)
192 if (vec->pkgs[i] == apkg)
193 return 1;
194 return 0;
195 }
196
197 void abstract_pkg_vec_sort(pkg_vec_t * vec, compare_fcn_t compar)
198 {
199 qsort(vec->pkgs, vec->len, sizeof(pkg_t *), compar);
200 }
201
202 int pkg_compare_names(const void *p1, const void *p2)
203 {
204 const pkg_t *pkg1 = *(const pkg_t **)p1;
205 const pkg_t *pkg2 = *(const pkg_t **)p2;
206 if (pkg1->name == NULL)
207 return 1;
208 if (pkg2->name == NULL)
209 return -1;
210 return (strcmp(pkg1->name, pkg2->name));
211 }