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 pkg_t
*pkg_vec_insert_merge(pkg_vec_t
*vec
, pkg_t
*pkg
, int set_status
,opkg_conf_t
*conf
)
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)) {
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
);
71 /* we didn't find one, add it */
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
);
78 /* update the one that we have */
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
);
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. */
91 opkg_message(conf
, OPKG_DEBUG2
, " WITHOUT set_status\n");
92 /* just overwrite the old one */
93 pkg_deinit(vec
->pkgs
[i
]);
101 void pkg_vec_insert(pkg_vec_t
*vec
, const pkg_t
*pkg
)
103 vec
->pkgs
= xrealloc(vec
->pkgs
, (vec
->len
+ 1) * sizeof(pkg_t
*));
104 vec
->pkgs
[vec
->len
] = (pkg_t
*)pkg
;
108 int pkg_vec_contains(pkg_vec_t
*vec
, pkg_t
*apkg
)
111 for (i
= 0; i
< vec
->len
; i
++)
112 if (vec
->pkgs
[i
] == apkg
)
117 void pkg_vec_sort(pkg_vec_t
*vec
, compare_fcn_t compar
)
119 qsort(vec
->pkgs
, vec
->len
, sizeof(pkg_t
*), compar
);
122 int pkg_vec_clear_marks(pkg_vec_t
*vec
)
124 int npkgs
= vec
->len
;
126 for (i
= 0; i
< npkgs
; i
++) {
127 pkg_t
*pkg
= vec
->pkgs
[i
];
128 pkg
->state_flag
&= ~SF_MARKED
;
133 int pkg_vec_mark_if_matches(pkg_vec_t
*vec
, const char *pattern
)
135 int matching_count
= 0;
136 pkg_t
**pkgs
= vec
->pkgs
;
137 int npkgs
= vec
->len
;
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
;
146 return matching_count
;
150 abstract_pkg_vec_t
* abstract_pkg_vec_alloc(void)
152 abstract_pkg_vec_t
* vec
;
153 vec
= xcalloc(1, sizeof(abstract_pkg_vec_t
));
160 void abstract_pkg_vec_free(abstract_pkg_vec_t
*vec
)
169 * assumption: all names in a vector are unique
171 void abstract_pkg_vec_insert(abstract_pkg_vec_t
*vec
, abstract_pkg_t
*pkg
)
173 vec
->pkgs
= xrealloc(vec
->pkgs
, (vec
->len
+ 1) * sizeof(abstract_pkg_t
*));
174 vec
->pkgs
[vec
->len
] = pkg
;
178 abstract_pkg_t
* abstract_pkg_vec_get(abstract_pkg_vec_t
*vec
, int i
)
186 int abstract_pkg_vec_contains(abstract_pkg_vec_t
*vec
, abstract_pkg_t
*apkg
)
189 for (i
= 0; i
< vec
->len
; i
++)
190 if (vec
->pkgs
[i
] == apkg
)
195 void abstract_pkg_vec_sort(pkg_vec_t
*vec
, compare_fcn_t compar
)
197 qsort(vec
->pkgs
, vec
->len
, sizeof(pkg_t
*), compar
);
200 int pkg_compare_names(const void *p1
, const void *p2
)
202 const pkg_t
*pkg1
= *(const pkg_t
**)p1
;
203 const pkg_t
*pkg2
= *(const pkg_t
**)p2
;
204 if (pkg1
->name
== NULL
)
206 if (pkg2
->name
== NULL
)
208 return(strcmp(pkg1
->name
, pkg2
->name
));