Initial stab at untangling the #include maze. Probably needs a second pass.
[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 "xregex.h"
22 #include "pkg.h"
23 #include "opkg_message.h"
24 #include "libbb/libbb.h"
25
26 pkg_vec_t * pkg_vec_alloc(void)
27 {
28 pkg_vec_t * vec = xcalloc(1, sizeof(pkg_vec_t));
29 vec->pkgs = NULL;
30 vec->len = 0;
31
32 return vec;
33 }
34
35 void pkg_vec_free(pkg_vec_t *vec)
36 {
37 if (!vec)
38 return;
39
40 if (vec->pkgs)
41 free(vec->pkgs);
42
43 free(vec);
44 }
45
46 /*
47 * assumption: all names in a vector are identical
48 * assumption: all version strings are trimmed,
49 * so identical versions have identical version strings,
50 * implying identical packages; let's marry these
51 */
52 void pkg_vec_insert_merge(pkg_vec_t *vec, pkg_t *pkg, int set_status)
53 {
54 int i;
55 int found = 0;
56
57 /* look for a duplicate pkg by name, version, and architecture */
58 for (i = 0; i < vec->len; i++){
59 opkg_msg(DEBUG2, "%s %s arch=%s vs. %s %s arch=%s.\n",
60 pkg->name, pkg->version, pkg->architecture,
61 vec->pkgs[i]->name, vec->pkgs[i]->version,
62 vec->pkgs[i]->architecture);
63 if ((strcmp(pkg->name, vec->pkgs[i]->name) == 0)
64 && (pkg_compare_versions(pkg, vec->pkgs[i]) == 0)
65 && (strcmp(pkg->architecture, vec->pkgs[i]->architecture) == 0)) {
66 found = 1;
67 opkg_msg(DEBUG2, "Duplicate for pkg=%s version=%s arch=%s.\n",
68 pkg->name, pkg->version, pkg->architecture);
69 break;
70 }
71 }
72
73 /* we didn't find one, add it */
74 if (!found){
75 opkg_msg(DEBUG2, "Adding new pkg=%s version=%s arch=%s.\n",
76 pkg->name, pkg->version, pkg->architecture);
77 pkg_vec_insert(vec, pkg);
78 return;
79 }
80
81 /* update the one that we have */
82 opkg_msg(DEBUG2, "Merging %s %s arch=%s, set_status=%d.\n",
83 pkg->name, pkg->version, pkg->architecture, set_status);
84 if (set_status) {
85 /* This is from the status file,
86 * so need to merge with existing database */
87 pkg_merge(pkg, vec->pkgs[i]);
88 }
89
90 /* overwrite the old one */
91 pkg_deinit(vec->pkgs[i]);
92 free(vec->pkgs[i]);
93 vec->pkgs[i] = pkg;
94 }
95
96 void pkg_vec_insert(pkg_vec_t *vec, const pkg_t *pkg)
97 {
98 vec->pkgs = xrealloc(vec->pkgs, (vec->len + 1) * sizeof(pkg_t *));
99 vec->pkgs[vec->len] = (pkg_t *)pkg;
100 vec->len++;
101 }
102
103 int pkg_vec_contains(pkg_vec_t *vec, pkg_t *apkg)
104 {
105 int i;
106 for (i = 0; i < vec->len; i++)
107 if (vec->pkgs[i] == apkg)
108 return 1;
109 return 0;
110 }
111
112 void pkg_vec_sort(pkg_vec_t *vec, compare_fcn_t compar)
113 {
114 qsort(vec->pkgs, vec->len, sizeof(pkg_t *), compar);
115 }
116
117 int pkg_vec_clear_marks(pkg_vec_t *vec)
118 {
119 int npkgs = vec->len;
120 int i;
121 for (i = 0; i < npkgs; i++) {
122 pkg_t *pkg = vec->pkgs[i];
123 pkg->state_flag &= ~SF_MARKED;
124 }
125 return 0;
126 }
127
128 int pkg_vec_mark_if_matches(pkg_vec_t *vec, const char *pattern)
129 {
130 int matching_count = 0;
131 pkg_t **pkgs = vec->pkgs;
132 int npkgs = vec->len;
133 int i;
134 for (i = 0; i < npkgs; i++) {
135 pkg_t *pkg = pkgs[i];
136 if (fnmatch(pattern, pkg->name, 0)==0) {
137 pkg->state_flag |= SF_MARKED;
138 matching_count++;
139 }
140 }
141 return matching_count;
142 }
143
144
145 abstract_pkg_vec_t * abstract_pkg_vec_alloc(void)
146 {
147 abstract_pkg_vec_t * vec ;
148 vec = xcalloc(1, sizeof(abstract_pkg_vec_t));
149 vec->pkgs = NULL;
150 vec->len = 0;
151
152 return vec;
153 }
154
155 void abstract_pkg_vec_free(abstract_pkg_vec_t *vec)
156 {
157 if (!vec)
158 return;
159 free(vec->pkgs);
160 free(vec);
161 }
162
163 /*
164 * assumption: all names in a vector are unique
165 */
166 void abstract_pkg_vec_insert(abstract_pkg_vec_t *vec, abstract_pkg_t *pkg)
167 {
168 vec->pkgs = xrealloc(vec->pkgs, (vec->len + 1) * sizeof(abstract_pkg_t *));
169 vec->pkgs[vec->len] = pkg;
170 vec->len++;
171 }
172
173 abstract_pkg_t * abstract_pkg_vec_get(abstract_pkg_vec_t *vec, int i)
174 {
175 if (vec->len > i)
176 return vec->pkgs[i];
177 else
178 return NULL;
179 }
180
181 int abstract_pkg_vec_contains(abstract_pkg_vec_t *vec, abstract_pkg_t *apkg)
182 {
183 int i;
184 for (i = 0; i < vec->len; i++)
185 if (vec->pkgs[i] == apkg)
186 return 1;
187 return 0;
188 }
189
190 void abstract_pkg_vec_sort(pkg_vec_t *vec, compare_fcn_t compar)
191 {
192 qsort(vec->pkgs, vec->len, sizeof(pkg_t *), compar);
193 }
194
195 int pkg_compare_names(const void *p1, const void *p2)
196 {
197 const pkg_t *pkg1 = *(const pkg_t **)p1;
198 const pkg_t *pkg2 = *(const pkg_t **)p2;
199 if (pkg1->name == NULL)
200 return 1;
201 if (pkg2->name == NULL)
202 return -1;
203 return(strcmp(pkg1->name, pkg2->name));
204 }
205