@@ -56,11 +56,31 @@ struct rpmdbstate {
5656};
5757
5858
59+ static inline int
60+ access_rootdir (struct rpmdbstate * state , const char * dir , int mode )
61+ {
62+ if (state -> rootdir )
63+ {
64+ char * path = solv_dupjoin (state -> rootdir , dir , 0 );
65+ int r = access (path , mode );
66+ free (path );
67+ return r ;
68+ }
69+ return access (dir , mode );
70+ }
71+
72+ static void
73+ detect_ostree (struct rpmdbstate * state )
74+ {
75+ state -> is_ostree = access_rootdir (state , "/var/lib/rpm" , W_OK ) == -1 &&
76+ access_rootdir (state , "/usr/share/rpm/Packages" , R_OK ) == 0 ? 1 : -1 ;
77+ }
78+
5979static int
6080stat_database_name (struct rpmdbstate * state , char * dbname , struct stat * statbuf , int seterror )
6181{
6282 char * dbpath ;
63- dbpath = solv_dupjoin (state -> rootdir , state -> is_ostree ? "/usr/share/rpm/" : "/var/lib/rpm/" , dbname );
83+ dbpath = solv_dupjoin (state -> rootdir , state -> is_ostree > 0 ? "/usr/share/rpm/" : "/var/lib/rpm/" , dbname );
6484 if (stat (dbpath , statbuf ))
6585 {
6686 if (seterror )
@@ -75,6 +95,8 @@ stat_database_name(struct rpmdbstate *state, char *dbname, struct stat *statbuf,
7595static int
7696stat_database (struct rpmdbstate * state , struct stat * statbuf )
7797{
98+ if (!state -> is_ostree )
99+ detect_ostree (state );
78100 return stat_database_name (state , "Packages" , statbuf , 1 );
79101}
80102
@@ -160,14 +182,15 @@ opendbenv(struct rpmdbstate *state)
160182 dbenv -> set_thread_count (dbenv , 8 );
161183#endif
162184 dbpath = solv_dupjoin (rootdir , "/var/lib/rpm" , 0 );
185+ state -> is_ostree = -1 ;
163186 if (access (dbpath , W_OK ) == -1 )
164187 {
165188 free (dbpath );
166189 dbpath = solv_dupjoin (rootdir , "/usr/share/rpm/Packages" , 0 );
167190 if (access (dbpath , R_OK ) == 0 )
168191 state -> is_ostree = 1 ;
169192 free (dbpath );
170- dbpath = solv_dupjoin (rootdir , state -> is_ostree ? "/usr/share/rpm" : "/var/lib/rpm" , 0 );
193+ dbpath = solv_dupjoin (rootdir , state -> is_ostree > 0 ? "/usr/share/rpm" : "/var/lib/rpm" , 0 );
171194 r = dbenv -> open (dbenv , dbpath , DB_CREATE |DB_PRIVATE |DB_INIT_MPOOL , 0 );
172195 }
173196 else
@@ -209,6 +232,12 @@ closedbenv(struct rpmdbstate *state)
209232 uint32_t eflags = 0 ;
210233#endif
211234
235+ if (state -> db )
236+ {
237+ state -> db -> close (state -> db , 0 );
238+ state -> db = 0 ;
239+ }
240+ state -> pkgdbopened = 0 ;
212241 if (!state -> dbenv )
213242 return ;
214243#if defined(FEDORA ) || defined(MAGEIA )
@@ -264,16 +293,6 @@ openpkgdb(struct rpmdbstate *state)
264293 return 1 ;
265294}
266295
267- static void
268- closepkgdb (struct rpmdbstate * state )
269- {
270- if (!state -> db )
271- return ;
272- state -> db -> close (state -> db , 0 );
273- state -> db = 0 ;
274- state -> pkgdbopened = 0 ;
275- }
276-
277296/* get the rpmdbids of all installed packages from the Name index database.
278297 * This is much faster then querying the big Packages database */
279298static struct rpmdbentry *
@@ -464,6 +483,8 @@ count_headers(struct rpmdbstate *state)
464483static int
465484pkgdb_cursor_open (struct rpmdbstate * state )
466485{
486+ if (state -> pkgdbopened != 1 && !openpkgdb (state ))
487+ return -1 ;
467488 if (state -> db -> cursor (state -> db , NULL , & state -> dbc , 0 ))
468489 return pool_error (state -> pool , -1 , "db->cursor failed" );
469490 return 0 ;
@@ -497,3 +518,20 @@ pkgdb_cursor_getrpm(struct rpmdbstate *state)
497518 return 0 ; /* no more entries */
498519}
499520
521+ static int
522+ hash_name_index (struct rpmdbstate * state , Chksum * chk )
523+ {
524+ char * dbpath ;
525+ int fd , l ;
526+ char buf [4096 ];
527+
528+ dbpath = solv_dupjoin (state -> rootdir , state -> is_ostree > 0 ? "/usr/share/rpm/" : "/var/lib/rpm/" , "Name" );
529+ if ((fd = open (dbpath , O_RDONLY )) < 0 )
530+ return -1 ;
531+ while ((l = read (fd , buf , sizeof (buf ))) > 0 )
532+ solv_chksum_add (chk , buf , l );
533+ close (fd );
534+ return 0 ;
535+ }
536+
537+
0 commit comments