1 /* pkg_vec.c - the opkg package management system
5 Copyright (C) 2002 Compaq Computer Corporation
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.
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.
22 #include "opkg_message.h"
23 #include "libbb/libbb.h"
25 pkg_vec_t *pkg_vec_alloc(void)
27 pkg_vec_t *vec = xcalloc(1, sizeof(pkg_vec_t));
34 void pkg_vec_free(pkg_vec_t * vec)
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
51 void pkg_vec_insert_merge(pkg_vec_t * vec, pkg_t * pkg, int set_status)
55 char *pkg_version = pkg_get_string(pkg, PKG_VERSION);
56 char *pkg_architecture = pkg_get_architecture(pkg);
57 char *vec_architecture;
59 /* look for a duplicate pkg by name, version, and architecture */
60 for (i = 0; i < vec->len; i++) {
61 vec_architecture = pkg_get_architecture(vec->pkgs[i]);
63 opkg_msg(DEBUG2, "%s %s arch=%s vs. %s %s arch=%s.\n",
64 pkg->name, pkg_version, pkg_architecture,
65 vec->pkgs[i]->name, pkg_get_string(vec->pkgs[i], PKG_VERSION),
67 /* if the name,ver,arch matches, or the name matches and the
68 * package is marked deinstall/hold */
69 if ((!strcmp(pkg->name, vec->pkgs[i]->name))
70 && ((pkg->state_want == SW_DEINSTALL
71 && (pkg->state_flag & SF_HOLD))
72 || ((pkg_compare_versions(pkg, vec->pkgs[i]) == 0)
73 && (!strcmp(pkg_architecture, vec_architecture))))) {
76 "Duplicate for pkg=%s version=%s arch=%s.\n",
77 pkg->name, pkg_version, pkg_architecture);
82 /* we didn't find one, add it */
84 opkg_msg(DEBUG2, "Adding new pkg=%s version=%s arch=%s.\n",
85 pkg->name, pkg_version, pkg_architecture);
86 pkg_vec_insert(vec, pkg);
90 /* update the one that we have */
91 opkg_msg(DEBUG2, "Merging %s %s arch=%s, set_status=%d.\n",
92 pkg->name, pkg_version, pkg_architecture, set_status);
94 /* This is from the status file,
95 * so need to merge with existing database */
96 pkg_merge(pkg, vec->pkgs[i]);
99 /* overwrite the old one */
100 pkg_deinit(vec->pkgs[i]);
105 void pkg_vec_insert(pkg_vec_t * vec, const pkg_t * pkg)
107 vec->pkgs = xrealloc(vec->pkgs, (vec->len + 1) * sizeof(pkg_t *));
108 vec->pkgs[vec->len] = (pkg_t *) pkg;
112 int pkg_vec_contains(pkg_vec_t * vec, pkg_t * apkg)
115 for (i = 0; i < vec->len; i++)
116 if (vec->pkgs[i] == apkg)
121 void pkg_vec_sort(pkg_vec_t * vec, compare_fcn_t compar)
123 qsort(vec->pkgs, vec->len, sizeof(pkg_t *), compar);
126 int pkg_vec_clear_marks(pkg_vec_t * vec)
128 int npkgs = vec->len;
130 for (i = 0; i < npkgs; i++) {
131 pkg_t *pkg = vec->pkgs[i];
132 pkg->state_flag &= ~SF_MARKED;
137 int pkg_vec_mark_if_matches(pkg_vec_t * vec, const char *pattern)
139 int matching_count = 0;
140 pkg_t **pkgs = vec->pkgs;
141 int npkgs = vec->len;
143 for (i = 0; i < npkgs; i++) {
144 pkg_t *pkg = pkgs[i];
145 if (fnmatch(pattern, pkg->name, 0) == 0) {
146 pkg->state_flag |= SF_MARKED;
150 return matching_count;
153 abstract_pkg_vec_t *abstract_pkg_vec_alloc(void)
155 abstract_pkg_vec_t *vec;
156 vec = xcalloc(1, sizeof(abstract_pkg_vec_t));
163 void abstract_pkg_vec_free(abstract_pkg_vec_t * vec)
172 * assumption: all names in a vector are unique
174 void abstract_pkg_vec_insert(abstract_pkg_vec_t * vec, abstract_pkg_t * pkg)
177 xrealloc(vec->pkgs, (vec->len + 1) * sizeof(abstract_pkg_t *));
178 vec->pkgs[vec->len] = pkg;
182 abstract_pkg_t *abstract_pkg_vec_get(abstract_pkg_vec_t * vec, int i)
190 int abstract_pkg_vec_contains(abstract_pkg_vec_t * vec, abstract_pkg_t * apkg)
193 for (i = 0; i < vec->len; i++)
194 if (vec->pkgs[i] == apkg)
199 void abstract_pkg_vec_sort(abstract_pkg_vec_t * vec, compare_fcn_t compar)
201 qsort(vec->pkgs, vec->len, sizeof(pkg_t *), compar);
204 int pkg_compare_names(const void *p1, const void *p2)
206 const pkg_t *pkg1 = *(const pkg_t **)p1;
207 const pkg_t *pkg2 = *(const pkg_t **)p2;
208 if (pkg1->name == NULL)
210 if (pkg2->name == NULL)
212 return (strcmp(pkg1->name, pkg2->name));