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