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