7d42a29fd477df6408304f5e55ca34e53a13114c
[oweals/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
24 pkg_vec_t * pkg_vec_alloc(void)
25 {
26     pkg_vec_t * vec = (pkg_vec_t *)malloc(sizeof(pkg_vec_t));
27     if (!vec) {
28       fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
29       return NULL;
30     }
31     vec->pkgs = NULL;
32     vec->len = 0;
33
34     return vec;
35 }
36
37 void pkg_vec_free(pkg_vec_t *vec)
38 {
39     if (!vec)
40       return;
41
42     if (vec->pkgs)
43       free(vec->pkgs);
44
45     free(vec);
46 }
47
48 /*
49  * assumption: all names in a vector are identical
50  * assumption: all version strings are trimmed,
51  *             so identical versions have identical version strings,
52  *             implying identical packages; let's marry these
53  */
54 pkg_t *pkg_vec_insert_merge(pkg_vec_t *vec, pkg_t *pkg, int set_status,opkg_conf_t *conf)
55 {
56      int i;
57      int found = 0;
58
59      /* look for a duplicate pkg by name, version, and architecture */
60      for (i = 0; i < vec->len; i++){
61          opkg_message(conf, OPKG_DEBUG2, "Function: %s. Found pkg=%s version=%s arch=%s cmp=%s version=%s arch=%s \n", 
62                       __FUNCTION__, pkg->name, pkg->version, pkg->architecture, 
63                        vec->pkgs[i]->name, vec->pkgs[i]->version,vec->pkgs[i]->architecture );
64           if ((strcmp(pkg->name, vec->pkgs[i]->name) == 0)
65               && (pkg_compare_versions(pkg, vec->pkgs[i]) == 0)
66               && (strcmp(pkg->architecture, vec->pkgs[i]->architecture) == 0)) {
67                found  = 1;
68                opkg_message(conf, OPKG_DEBUG2, "Function: %s. Found duplicate for pkg=%s version=%s arch=%s\n",
69                              __FUNCTION__, pkg->name, pkg->version, pkg->architecture);
70                break;
71           }
72      }
73
74      /* we didn't find one, add it */
75      if (!found){   
76          opkg_message(conf, OPKG_DEBUG2, "Function: %s. Adding new pkg=%s version=%s arch=%s\n",
77                       __FUNCTION__, pkg->name, pkg->version, pkg->architecture);
78
79           vec->pkgs = (pkg_t **)realloc(vec->pkgs, (vec->len + 1) * sizeof(pkg_t *));
80           vec->pkgs[vec->len] = pkg;
81           vec->len++;
82           return pkg;
83      }
84      /* update the one that we have */
85      else {
86           opkg_message(conf, OPKG_DEBUG2, "Function: %s. calling pkg_merge for pkg=%s version=%s arch=%s",
87                         __FUNCTION__, pkg->name, pkg->version, pkg->architecture);
88           if (set_status) {
89                /* this is from the status file, so need to merge with existing database */
90                opkg_message(conf, OPKG_DEBUG2, " with set_status\n");
91                pkg_merge(vec->pkgs[i], pkg, set_status);
92                /* XXX: CLEANUP: It's not so polite to free something here
93                   that was passed in from above. */
94                pkg_deinit(pkg);
95                free(pkg);
96           } else {
97                opkg_message(conf, OPKG_DEBUG2, " WITHOUT set_status\n");
98                /* just overwrite the old one */
99                pkg_deinit(vec->pkgs[i]);
100                free(vec->pkgs[i]);
101                vec->pkgs[i] = pkg;
102           }
103           return vec->pkgs[i];
104      }
105 }
106
107 void pkg_vec_insert(pkg_vec_t *vec, const pkg_t *pkg)
108 {
109      int found = 0;
110
111 #if 0
112      /* look for a duplicate pkg by name, version, and architecture */
113      for (i = 0; i < vec->len; i++)
114           if ((strcmp(pkg->name, vec->pkgs[i]->name) == 0)
115               && (pkg_compare_versions(pkg, vec->pkgs[i]) == 0)
116               && (strcmp(pkg->architecture, vec->pkgs[i]->name) == 0)) {
117                found = 1;
118                break;
119           }
120 #endif
121
122      /* we didn't find one, add it */
123      if(!found){   
124           vec->pkgs = (pkg_t **)realloc(vec->pkgs, (vec->len + 1) * sizeof(pkg_t *));
125           *(const pkg_t **)&vec->pkgs[vec->len] = pkg;
126           vec->len++;
127      }
128 }
129
130 int pkg_vec_contains(pkg_vec_t *vec, pkg_t *apkg)
131 {
132      int i;
133      for (i = 0; i < vec->len; i++)
134           if (vec->pkgs[i] == apkg)
135                return 1;
136      return 0;
137 }
138
139 void pkg_vec_sort(pkg_vec_t *vec, compare_fcn_t compar)
140 {
141      qsort(vec->pkgs, vec->len, sizeof(pkg_t *), compar);
142 }
143
144 int pkg_vec_clear_marks(pkg_vec_t *vec)
145 {
146      int npkgs = vec->len;
147      int i;
148      for (i = 0; i < npkgs; i++) {
149           pkg_t *pkg = vec->pkgs[i];
150           pkg->state_flag &= ~SF_MARKED;
151      }
152      return 0;
153 }
154
155 int pkg_vec_mark_if_matches(pkg_vec_t *vec, const char *pattern)
156 {
157      int matching_count = 0;
158      pkg_t **pkgs = vec->pkgs;
159      int npkgs = vec->len;
160      int i;
161      for (i = 0; i < npkgs; i++) {
162           pkg_t *pkg = pkgs[i];
163           if (fnmatch(pattern, pkg->name, 0)==0) {
164                pkg->state_flag |= SF_MARKED;
165                matching_count++;
166           }
167      }
168      return matching_count;
169 }
170
171
172 abstract_pkg_vec_t * abstract_pkg_vec_alloc(void)
173 {
174     abstract_pkg_vec_t * vec ; 
175     vec = (abstract_pkg_vec_t *)malloc(sizeof(abstract_pkg_vec_t));
176     if (!vec) {
177       fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
178       return NULL;
179     }
180     vec->pkgs = NULL;
181     vec->len = 0;
182
183     return vec;
184 }
185
186 void abstract_pkg_vec_free(abstract_pkg_vec_t *vec)
187 {
188     if (!vec)
189       return;
190     free(vec->pkgs);
191     free(vec);
192 }
193
194 /*
195  * assumption: all names in a vector are unique
196  */
197 void abstract_pkg_vec_insert(abstract_pkg_vec_t *vec, abstract_pkg_t *pkg)
198 {
199
200 #if 0
201     /* look for a duplicate pkg by name */
202     for(i = 0; i < vec->len; i++)
203         if (strcmp(pkg->name, vec->pkgs[i]->name) == 0)
204             break;
205
206     /* we didn't find one, add it */
207     if(i == vec->len){   
208 #endif
209         vec->pkgs = 
210           (abstract_pkg_t **)
211             realloc(vec->pkgs, (vec->len + 1) * sizeof(abstract_pkg_t *));
212         vec->pkgs[vec->len] = pkg;
213         vec->len++;
214 #if 0
215     }
216 #endif
217 }
218
219 abstract_pkg_t * abstract_pkg_vec_get(abstract_pkg_vec_t *vec, int i)
220 {
221     if (vec->len > i) 
222         return vec->pkgs[i];
223     else
224         return NULL;
225 }
226
227 int abstract_pkg_vec_contains(abstract_pkg_vec_t *vec, abstract_pkg_t *apkg)
228 {
229      int i;
230      for (i = 0; i < vec->len; i++)
231           if (vec->pkgs[i] == apkg)
232                return 1;
233      return 0;
234 }
235
236 void abstract_pkg_vec_sort(pkg_vec_t *vec, compare_fcn_t compar)
237 {
238      qsort(vec->pkgs, vec->len, sizeof(pkg_t *), compar);
239 }
240