@@ -102,6 +102,8 @@ cleaner_cb(struct xbps_handle *xhp, xbps_object_t obj,
102102 pkgd = xbps_pkgdb_get_pkg (xhp , binpkgver );
103103 } else {
104104 pkgd = xbps_rpool_get_pkg (xhp , binpkgver );
105+ if (!pkgd )
106+ pkgd = xbps_pkgdb_get_pkg (xhp , binpkgver );
105107 }
106108 if (pkgd ) {
107109 xbps_dictionary_get_cstring_nocopy (pkgd ,
@@ -143,20 +145,42 @@ clean_cachedir(struct xbps_handle *xhp, bool uninstalled, bool drun)
143145 DIR * dirp ;
144146 struct dirent * dp ;
145147 char * ext ;
146- int rv = 0 ;
148+ int r ;
147149
148150 // XXX: there is no public api to load the pkgdb so force it before
149151 // its done potentially concurrently by threads through the
150152 // xbps_array_foreach_cb_multi call later.
151- (void )xbps_pkgdb_get_pkg (xhp , "foo" );
153+ // XXX: same for the repository pool...
154+ if (uninstalled ) {
155+ (void )xbps_pkgdb_get_pkg (xhp , "package-that-wont-exist" );
156+ } else {
157+ (void )xbps_rpool_get_pkg (xhp , "package-that-wont-exist-so-it-loads-all-repos" );
158+ (void )xbps_pkgdb_get_pkg (xhp , "package-that-wont-exist" );
159+ }
152160
153- if (chdir (xhp -> cachedir ) == -1 )
154- return -1 ;
161+ if (chdir (xhp -> cachedir ) == -1 ) {
162+ if (errno == ENOENT )
163+ return 0 ;
164+ r = - errno ;
165+ xbps_error_printf ("failed to change to cache directory: %s: %s\n" ,
166+ xhp -> cachedir , strerror (- r ));
167+ return r ;
168+ }
155169
156- if ((dirp = opendir (xhp -> cachedir )) == NULL )
157- return 0 ;
170+ dirp = opendir ("." );
171+ if (!dirp ) {
172+ r = - errno ;
173+ xbps_error_printf ("failed to open cache directory: %s: %s\n" ,
174+ xhp -> cachedir , strerror (- r ));
175+ return r ;
176+ }
158177
159178 array = xbps_array_create ();
179+ if (!array ) {
180+ (void )closedir (dirp );
181+ return xbps_error_oom ();
182+ }
183+
160184 while ((dp = readdir (dirp )) != NULL ) {
161185 if ((strcmp (dp -> d_name , "." ) == 0 ) ||
162186 (strcmp (dp -> d_name , ".." ) == 0 ))
@@ -169,7 +193,11 @@ clean_cachedir(struct xbps_handle *xhp, bool uninstalled, bool drun)
169193 xbps_dbg_printf ("ignoring unknown file: %s\n" , dp -> d_name );
170194 continue ;
171195 }
172- xbps_array_add_cstring (array , dp -> d_name );
196+ if (!xbps_array_add_cstring (array , dp -> d_name )) {
197+ xbps_object_release (array );
198+ (void )closedir (dirp );
199+ return xbps_error_oom ();
200+ }
173201 }
174202 (void )closedir (dirp );
175203
@@ -178,8 +206,11 @@ clean_cachedir(struct xbps_handle *xhp, bool uninstalled, bool drun)
178206 .dry = drun ,
179207 .uninstalled = uninstalled ,
180208 };
181- rv = xbps_array_foreach_cb_multi (xhp , array , NULL , cleaner_cb , (void * )& data );
182- xbps_object_release (array );
209+ r = xbps_array_foreach_cb_multi (xhp , array , NULL , cleaner_cb , (void * )& data );
210+ } else {
211+ r = 0 ;
183212 }
184- return rv ;
213+
214+ xbps_object_release (array );
215+ return r ;
185216}
0 commit comments