@@ -47,8 +47,8 @@ struct item {
4747 UT_hash_handle hh ;
4848};
4949
50+ // XXX: this shouldn't be a global static...
5051static struct item * items = NULL ;
51- static xbps_array_t result ;
5252
5353static struct item *
5454lookupItem (const char * pkgn )
@@ -99,7 +99,7 @@ addDepn(struct item *item, struct item *xitem)
9999}
100100
101101static void
102- add_deps_recursive (struct item * item , bool first )
102+ add_deps_recursive (xbps_array_t result , struct item * item , bool first )
103103{
104104 struct depn * dep ;
105105 xbps_string_t str ;
@@ -108,7 +108,7 @@ add_deps_recursive(struct item *item, bool first)
108108 return ;
109109
110110 for (dep = item -> dbase ; dep ; dep = dep -> dnext )
111- add_deps_recursive (dep -> item , false);
111+ add_deps_recursive (result , dep -> item , false);
112112
113113 if (first )
114114 return ;
@@ -133,19 +133,23 @@ cleanup(void)
133133 }
134134}
135135
136+ struct deptree_ctx {
137+ struct xbps_handle * xhp ;
138+ xbps_array_t result ;
139+ bool rpool ;
140+ };
141+
136142/*
137143 * Recursively calculate all dependencies.
138144 */
139145static struct item *
140- ordered_depends (struct xbps_handle * xhp , xbps_dictionary_t pkgd , bool rpool ,
141- size_t depth )
146+ ordered_depends (struct deptree_ctx * ctx , xbps_dictionary_t pkgd , size_t depth )
142147{
143148 xbps_array_t rdeps , provides ;
144149 xbps_string_t str ;
145150 struct item * item = NULL , * xitem = NULL ;
146151 const char * pkgver = NULL , * pkgname = NULL ;
147152
148- assert (xhp );
149153 assert (pkgd );
150154
151155 rdeps = xbps_dictionary_get (pkgd , "run_depends" );
@@ -154,7 +158,7 @@ ordered_depends(struct xbps_handle *xhp, xbps_dictionary_t pkgd, bool rpool,
154158
155159 item = lookupItem (pkgname );
156160 if (item ) {
157- add_deps_recursive (item , depth == 0 );
161+ add_deps_recursive (ctx -> result , item , depth == 0 );
158162 return item ;
159163 }
160164
@@ -171,12 +175,12 @@ ordered_depends(struct xbps_handle *xhp, xbps_dictionary_t pkgd, bool rpool,
171175 char curdepname [XBPS_NAME_SIZE ];
172176
173177 xbps_array_get_cstring_nocopy (rdeps , i , & curdep );
174- if (rpool ) {
175- if ((curpkgd = xbps_rpool_get_pkg (xhp , curdep )) == NULL )
176- curpkgd = xbps_rpool_get_virtualpkg (xhp , curdep );
178+ if (ctx -> rpool ) {
179+ if ((curpkgd = xbps_rpool_get_pkg (ctx -> xhp , curdep )) == NULL )
180+ curpkgd = xbps_rpool_get_virtualpkg (ctx -> xhp , curdep );
177181 } else {
178- if ((curpkgd = xbps_pkgdb_get_pkg (xhp , curdep )) == NULL )
179- curpkgd = xbps_pkgdb_get_virtualpkg (xhp , curdep );
182+ if ((curpkgd = xbps_pkgdb_get_pkg (ctx -> xhp , curdep )) == NULL )
183+ curpkgd = xbps_pkgdb_get_virtualpkg (ctx -> xhp , curdep );
180184 /* Ignore missing local runtime dependencies, because ignorepkg */
181185 if (curpkgd == NULL )
182186 continue ;
@@ -199,10 +203,10 @@ ordered_depends(struct xbps_handle *xhp, xbps_dictionary_t pkgd, bool rpool,
199203 }
200204 xitem = lookupItem (curdepname );
201205 if (xitem ) {
202- add_deps_recursive (xitem , false);
206+ add_deps_recursive (ctx -> result , xitem , false);
203207 continue ;
204208 }
205- xitem = ordered_depends (xhp , curpkgd , rpool , depth + 1 );
209+ xitem = ordered_depends (ctx , curpkgd , depth + 1 );
206210 if (xitem == NULL ) {
207211 /* package depends on missing dependencies */
208212 xbps_dbg_printf ("%s: missing dependency '%s'\n" , pkgver , curdep );
@@ -213,10 +217,10 @@ ordered_depends(struct xbps_handle *xhp, xbps_dictionary_t pkgd, bool rpool,
213217 addDepn (item , xitem );
214218 }
215219 /* all deps were processed, add item to head */
216- if (depth > 0 && !xbps_match_string_in_array (result , item -> pkgver )) {
220+ if (depth > 0 && !xbps_match_string_in_array (ctx -> result , item -> pkgver )) {
217221 str = xbps_string_create_cstring (item -> pkgver );
218222 assert (str );
219- xbps_array_add_first (result , str );
223+ xbps_array_add_first (ctx -> result , str );
220224 xbps_object_release (str );
221225 }
222226 return item ;
@@ -225,11 +229,12 @@ ordered_depends(struct xbps_handle *xhp, xbps_dictionary_t pkgd, bool rpool,
225229xbps_array_t HIDDEN
226230xbps_get_pkg_fulldeptree (struct xbps_handle * xhp , const char * pkg , bool rpool )
227231{
232+ struct deptree_ctx ctx = {
233+ .xhp = xhp ,
234+ .rpool = rpool ,
235+ };
228236 xbps_dictionary_t pkgd ;
229237
230- result = xbps_array_create ();
231- assert (result );
232-
233238 if (rpool ) {
234239 if (((pkgd = xbps_rpool_get_pkg (xhp , pkg )) == NULL ) &&
235240 ((pkgd = xbps_rpool_get_virtualpkg (xhp , pkg )) == NULL ))
@@ -239,9 +244,20 @@ xbps_get_pkg_fulldeptree(struct xbps_handle *xhp, const char *pkg, bool rpool)
239244 ((pkgd = xbps_pkgdb_get_virtualpkg (xhp , pkg )) == NULL ))
240245 return NULL ;
241246 }
242- if (ordered_depends (xhp , pkgd , rpool , 0 ) == NULL )
247+
248+ ctx .result = xbps_array_create ();
249+ if (!ctx .result ) {
250+ xbps_error_oom ();
243251 return NULL ;
252+ }
253+
254+ if (ordered_depends (& ctx , pkgd , 0 ) == NULL )
255+ goto err ;
244256
245257 cleanup ();
246- return result ;
258+ return ctx .result ;
259+ err :
260+ cleanup ();
261+ xbps_object_release (ctx .result );
262+ return NULL ;
247263}
0 commit comments