Remove some code duplication, check for realloc failure.
[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 *)calloc(1, 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           pkg_vec_insert(vec, pkg);
79           return pkg;
80      }
81      /* update the one that we have */
82      else {
83           opkg_message(conf, OPKG_DEBUG2, "Function: %s. calling pkg_merge for pkg=%s version=%s arch=%s",
84                         __FUNCTION__, pkg->name, pkg->version, pkg->architecture);
85           if (set_status) {
86                /* this is from the status file, so need to merge with existing database */
87                opkg_message(conf, OPKG_DEBUG2, " with set_status\n");
88                pkg_merge(vec->pkgs[i], pkg, set_status);
89                /* XXX: CLEANUP: It's not so polite to free something here
90                   that was passed in from above. */
91                pkg_deinit(pkg);
92                free(pkg);
93           } else {
94                opkg_message(conf, OPKG_DEBUG2, " WITHOUT set_status\n");
95                /* just overwrite the old one */
96                pkg_deinit(vec->pkgs[i]);
97                free(vec->pkgs[i]);
98                vec->pkgs[i] = pkg;
99           }
100           return vec->pkgs[i];
101      }
102 }
103
104 void pkg_vec_insert(pkg_vec_t *vec, const pkg_t *pkg)
105 {
106     pkg_t **tmp;
107     tmp = realloc(vec->pkgs, (vec->len + 1) * sizeof(pkg_t *));
108     if (tmp == NULL) {
109         fprintf(stderr, "%s: %s\n", __FUNCTION__, strerror(errno));
110         return;
111     }
112     vec->pkgs = tmp;
113     vec->pkgs[vec->len] = (pkg_t *)pkg;
114     vec->len++;
115 }
116
117 int pkg_vec_contains(pkg_vec_t *vec, pkg_t *apkg)
118 {
119      int i;
120      for (i = 0; i < vec->len; i++)
121           if (vec->pkgs[i] == apkg)
122                return 1;
123      return 0;
124 }
125
126 void pkg_vec_sort(pkg_vec_t *vec, compare_fcn_t compar)
127 {
128      qsort(vec->pkgs, vec->len, sizeof(pkg_t *), compar);
129 }
130
131 int pkg_vec_clear_marks(pkg_vec_t *vec)
132 {
133      int npkgs = vec->len;
134      int i;
135      for (i = 0; i < npkgs; i++) {
136           pkg_t *pkg = vec->pkgs[i];
137           pkg->state_flag &= ~SF_MARKED;
138      }
139      return 0;
140 }
141
142 int pkg_vec_mark_if_matches(pkg_vec_t *vec, const char *pattern)
143 {
144      int matching_count = 0;
145      pkg_t **pkgs = vec->pkgs;
146      int npkgs = vec->len;
147      int i;
148      for (i = 0; i < npkgs; i++) {
149           pkg_t *pkg = pkgs[i];
150           if (fnmatch(pattern, pkg->name, 0)==0) {
151                pkg->state_flag |= SF_MARKED;
152                matching_count++;
153           }
154      }
155      return matching_count;
156 }
157
158
159 abstract_pkg_vec_t * abstract_pkg_vec_alloc(void)
160 {
161     abstract_pkg_vec_t * vec ; 
162     vec = (abstract_pkg_vec_t *)calloc(1, sizeof(abstract_pkg_vec_t));
163     if (!vec) {
164       fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
165       return NULL;
166     }
167     vec->pkgs = NULL;
168     vec->len = 0;
169
170     return vec;
171 }
172
173 void abstract_pkg_vec_free(abstract_pkg_vec_t *vec)
174 {
175     if (!vec)
176       return;
177     free(vec->pkgs);
178     free(vec);
179 }
180
181 /*
182  * assumption: all names in a vector are unique
183  */
184 void abstract_pkg_vec_insert(abstract_pkg_vec_t *vec, abstract_pkg_t *pkg)
185 {
186     abstract_pkg_t **tmp;
187     tmp = realloc(vec->pkgs, (vec->len + 1) * sizeof(abstract_pkg_t *));
188     if (tmp == NULL) {
189         fprintf(stderr, "%s: %s\n", __FUNCTION__, strerror(errno));
190         return;
191     }
192     vec->pkgs = tmp;
193     vec->pkgs[vec->len] = pkg;
194     vec->len++;
195 }
196
197 abstract_pkg_t * abstract_pkg_vec_get(abstract_pkg_vec_t *vec, int i)
198 {
199     if (vec->len > i) 
200         return vec->pkgs[i];
201     else
202         return NULL;
203 }
204
205 int abstract_pkg_vec_contains(abstract_pkg_vec_t *vec, abstract_pkg_t *apkg)
206 {
207      int i;
208      for (i = 0; i < vec->len; i++)
209           if (vec->pkgs[i] == apkg)
210                return 1;
211      return 0;
212 }
213
214 void abstract_pkg_vec_sort(pkg_vec_t *vec, compare_fcn_t compar)
215 {
216      qsort(vec->pkgs, vec->len, sizeof(pkg_t *), compar);
217 }
218
219 int pkg_compare_names(const void *p1, const void *p2)
220 {
221   const pkg_t *pkg1 = *(const pkg_t **)p1;
222   const pkg_t *pkg2 = *(const pkg_t **)p2;
223   if (pkg1->name == NULL)
224     return 1;
225   if (pkg2->name == NULL)
226     return -1;
227   return(strcmp(pkg1->name, pkg2->name));
228 }
229