1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/target/rpm/RpmDb.h
15 #ifndef ZYPP_TARGET_RPM_RPMDB_H
16 #define ZYPP_TARGET_RPM_RPMDB_H
23 #include "zypp/Pathname.h"
24 #include "zypp/ExternalProgram.h"
26 #include "zypp/Package.h"
27 #include "zypp/KeyRing.h"
29 #include "zypp/target/rpm/RpmHeader.h"
30 #include "zypp/target/rpm/RpmCallbacks.h"
31 #include "zypp/ZYppCallbacks.h"
40 ///////////////////////////////////////////////////////////////////
44 * @short Interface to the rpm program
46 class RpmDb : public base::ReferenceCounted, private base::NonCopyable
53 typedef class InstTargetError Error;
55 ///////////////////////////////////////////////////////////////////
59 ///////////////////////////////////////////////////////////////////
62 enum DbStateInfoBits {
63 DbSI_NO_INIT = 0x0000,
64 DbSI_HAVE_V4 = 0x0001,
65 DbSI_MADE_V4 = 0x0002,
66 DbSI_MODIFIED_V4 = 0x0004,
67 DbSI_HAVE_V3 = 0x0008,
68 DbSI_HAVE_V3TOV4 = 0x0010,
69 DbSI_MADE_V3TOV4 = 0x0020
72 friend std::ostream & operator<<( std::ostream & str, const DbStateInfoBits & obj );
74 void dbsi_set( DbStateInfoBits & val_r, const unsigned & bits_r ) const
76 val_r = (DbStateInfoBits)(val_r | bits_r);
78 void dbsi_clr( DbStateInfoBits & val_r, const unsigned & bits_r ) const
80 val_r = (DbStateInfoBits)(val_r & ~bits_r);
82 bool dbsi_has( const DbStateInfoBits & val_r, const unsigned & bits_r ) const
84 return( (val_r & bits_r) == bits_r );
90 DbStateInfoBits _dbStateInfo;
93 * Root directory for all operations.
98 * Directory that contains the rpmdb.
103 * Internal helper for @ref initDatabase.
105 * \throws RpmException
108 void internal_initDatabase( const Pathname & root_r, const Pathname & dbPath_r,
109 DbStateInfoBits & info_r );
112 * Remove the rpm4 database in dbdir_r and optionally any backup created
115 static void removeV4( const Pathname & dbdir_r, bool v3backup_r );
118 * Remove the rpm3 database in dbdir_r. Create a backup copy named
119 * packages.rpm3 if it does not already exist.
121 static void removeV3( const Pathname & dbdir_r, bool v3backup_r );
124 * Called before the database is modified by installPackage/removePackage.
125 * Invalidates Packages list and moves away any old database.
127 void modifyDatabase();
132 * Constructor. There's no rpmdb access until @ref initDatabase
143 * timestamp of the rpm database (last modification)
145 Date timestamp() const;
148 * @return Root directory for all operations (empty if not initialized).
150 const Pathname & root() const
156 * @return Directory that contains the rpmdb (empty if not initialized).
158 const Pathname & dbPath() const
164 * @return Whether we are initialized.
166 bool initialized() const
168 return( ! _root.empty() );
172 * Prepare access to the rpm database. Optional arguments may denote the
173 * root directory for all operations and the directory (below root) that
174 * contains the rpmdb (usg. you won't need to set this).
176 * On empty Pathnames the default is used:
179 * dbPath: /var/lib/rpm
182 * Calling initDatabase a second time with different arguments will return
183 * an error but leave the database in it's original state.
185 * Converting an old batabase is done if necessary. On update: The converted
186 * database will be removed by @ref closeDatabase, if it was not modified
187 * (no packages were installed or deleted). Otherwise the new database
188 * is kept, and the old one is removed.
190 * If the database alredy exists and \c doRebuild_r is true, \ref rebuildDatabase
193 * \throws RpmException
196 void initDatabase( Pathname root_r = Pathname(),
197 Pathname dbPath_r = Pathname(),
198 bool doRebuild_r = false );
201 * Block further access to the rpm database and go back to uninitialized
202 * state. On update: Decides what to do with any converted database
203 * (see @ref initDatabase).
205 * \throws RpmException
208 void closeDatabase();
211 * Rebuild the rpm database (rpm --rebuilddb).
213 * \throws RpmException
216 void rebuildDatabase();
219 * Import ascii armored public key in file pubkey_r.
221 * \throws RpmException
224 void importPubkey( const PublicKey & pubkey_r );
227 * Remove a public key from the rpm database
229 * \throws RpmException
232 void removePubkey( const PublicKey & pubkey_r );
235 * Return the long ids of all installed public keys.
237 std::list<PublicKey> pubkeys() const;
240 * Return the edition of all installed public keys.
242 std::set<Edition> pubkeyEditions() const;
244 ///////////////////////////////////////////////////////////////////
246 // Cached RPM database retrieval via librpm.
248 ///////////////////////////////////////////////////////////////////
253 Packages & _packages;
255 std::set<std::string> _filerequires;
260 * @return Whether the list of installed packages is valid, or
261 * you'd better reread it. (<B>NOTE:</B> returns valid, if not
264 bool packagesValid() const;
267 * If necessary build, and return the list of all installed packages.
269 const std::list<Package::Ptr> & getPackages();
271 ///////////////////////////////////////////////////////////////////
273 // Direct RPM database retrieval via librpm.
275 ///////////////////////////////////////////////////////////////////
279 * return complete file list for installed package name_r (in FileInfo.filename)
280 * if edition_r != Edition::noedition, check for exact edition
281 * if full==true, fill all attributes of FileInfo
283 std::list<FileInfo> fileList( const std::string & name_r, const Edition & edition_r ) const;
286 * Return true if at least one package owns a certain file (name_r empty)
287 * Return true if package name_r owns file file_r (name_r nonempty).
289 bool hasFile( const std::string & file_r, const std::string & name_r = "" ) const;
292 * Return name of package owning file
293 * or empty string if no installed package owns file
295 std::string whoOwnsFile( const std::string & file_r ) const;
298 * Return true if at least one package provides a certain tag.
300 bool hasProvides( const std::string & tag_r ) const;
303 * Return true if at least one package requires a certain tag.
305 bool hasRequiredBy( const std::string & tag_r ) const;
308 * Return true if at least one package conflicts with a certain tag.
310 bool hasConflicts( const std::string & tag_r ) const;
313 * Return true if package is installed.
315 bool hasPackage( const std::string & name_r ) const;
318 * Return true if package is installed in a certain edition.
320 bool hasPackage( const std::string & name_r, const Edition & ed_r ) const;
323 * Get an installed packages data from rpmdb. Package is
324 * identified by name. Data returned via result are NULL,
325 * if packge is not installed (PMError is not set), or RPM database
326 * could not be read (PMError is set).
328 * \throws RpmException
330 * FIXME this and following comment
333 void getData( const std::string & name_r,
334 RpmHeader::constPtr & result_r ) const;
337 * Get an installed packages data from rpmdb. Package is
338 * identified by name and edition. Data returned via result are NULL,
339 * if packge is not installed (PMError is not set), or RPM database
340 * could not be read (PMError is set).
342 * \throws RpmException
345 void getData( const std::string & name_r, const Edition & ed_r,
346 RpmHeader::constPtr & result_r ) const;
350 * Create a package from RpmHeader
351 * return NULL on error
354 static Package::Ptr makePackageFromHeader( const RpmHeader::constPtr header,
355 std::set<std::string> * filerequires,
356 const Pathname & location, Repository repo );
358 ///////////////////////////////////////////////////////////////////
360 ///////////////////////////////////////////////////////////////////
363 * iterates through zypp keyring and import all non existant keys
366 void importZyppKeyRingTrustedKeys();
368 * insert all rpm trusted keys into zypp trusted keyring
370 void exportTrustedKeysInZyppKeyRing();
373 * The connection to the rpm process.
375 ExternalProgram *process;
377 typedef std::vector<const char*> RpmArgVec;
380 * Run rpm with the specified arguments and handle stderr.
381 * @param n_opts The number of arguments
382 * @param options Array of the arguments, @ref n_opts elements
383 * @param stderr_disp How to handle stderr, merged with stdout by default
385 * \throws RpmException
388 void run_rpm( const RpmArgVec& options,
389 ExternalProgram::Stderr_Disposition stderr_disp =
390 ExternalProgram::Stderr_To_Stdout);
394 * Read a line from the general rpm query
396 bool systemReadLine(std::string &line);
399 * Return the exit status of the general rpm process,
400 * closing the connection if not already done.
405 * Forcably kill the system process
410 * The exit code of the rpm process, or -1 if not yet known.
415 * Error message from running rpm as external program.
416 * Use only if something fail.
418 std::string error_message;
420 /** /var/adm/backup */
421 Pathname _backuppath;
423 /** create package backups? */
424 bool _packagebackups;
426 /** whether <_root>/<WARNINGMAILPATH> was already created */
430 * handle rpm messages like "/etc/testrc saved as /etc/testrc.rpmorig"
432 * @param line rpm output starting with warning:
433 * @param name name of package, appears in subject line
434 * @param typemsg " saved as " or " created as "
435 * @param difffailmsg what to put into mail if diff failed, must contain two %s for the two files
436 * @param diffgenmsg what to put into mail if diff succeeded, must contain two %s for the two files
438 void processConfigFiles(const std::string& line,
439 const std::string& name,
441 const char* difffailmsg,
442 const char* diffgenmsg);
447 typedef std::set<std::string> FileList;
450 * Bits representing rpm installation options, useable as or
453 * @see installPackage(), removePackage()
457 RPMINST_NONE = 0x0000,
458 RPMINST_NODOCS = 0x0001,
459 RPMINST_NOSCRIPTS = 0x0002,
460 RPMINST_FORCE = 0x0004,
461 RPMINST_NODEPS = 0x0008,
462 RPMINST_IGNORESIZE = 0x0010,
463 RPMINST_JUSTDB = 0x0020,
464 RPMINST_NODIGEST = 0x0040,
465 RPMINST_NOSIGNATURE= 0x0080,
466 RPMINST_NOUPGRADE = 0x0100,
467 RPMINST_TEST = 0x0200
471 * checkPackage result
474 enum checkPackageResult
476 CHK_OK = 0, /*!< Signature is OK. */
477 CHK_NOTFOUND = 1, /*!< Signature is unknown type. */
478 CHK_FAIL = 2, /*!< Signature does not verify. */
479 CHK_NOTTRUSTED = 3, /*!< Signature is OK, but key is not trusted. */
480 CHK_NOKEY = 4, /*!< Public key is unavailable. */
481 CHK_ERROR = 5 /*!< File does not exist or can't be opened. */
485 * Check signature of rpm file on disk.
487 * @param filename which file to check
489 * @return checkPackageResult
491 checkPackageResult checkPackage( const Pathname & path_r );
493 /** install rpm package
495 * @param filename file to install
496 * @param flags which rpm options to use
500 * \throws RpmException
503 void installPackage (const Pathname& filename, unsigned flags = 0 );
505 /** remove rpm package
507 * @param name_r Name of the rpm package to remove.
508 * @param iflags which rpm options to use
512 * \throws RpmException
515 void removePackage(const std::string & name_r, unsigned flags = 0);
516 void removePackage(Package::constPtr package, unsigned flags = 0);
519 * get backup dir for rpm config files
522 Pathname getBackupPath (void)
528 * create tar.gz of all changed files in a Package
530 * @param packageName name of the Package to backup
534 bool backupPackage(const std::string& packageName);
537 * queries file for name and then calls above backupPackage
538 * function. For convenience.
540 * @param filename rpm file that is about to be installed
542 bool backupPackage(const Pathname& filename);
545 * set path where package backups are stored
549 void setBackupPath(const Pathname& path);
552 * whether to create package backups during install or
555 * @param yes true or false
557 void createPackageBackups(bool yes)
559 _packagebackups = yes;
563 * determine which files of an installed package have been
566 * @param fileList (output) where to store modified files
567 * @param packageName name of package to query
569 * @return false if package couln't be queried for some
572 bool queryChangedFiles(FileList & fileList, const std::string& packageName);
579 virtual std::ostream & dumpOn( std::ostream & str ) const;
582 void doRemovePackage( const std::string & name_r, unsigned flags, callback::SendReport<RpmRemoveReport> & report );
583 void doInstallPackage( const Pathname & filename, unsigned flags, callback::SendReport<RpmInstallReport> & report );
584 const std::list<Package::Ptr> & doGetPackages(callback::SendReport<ScanDBReport> & report);
585 void doRebuildDatabase(callback::SendReport<RebuildDBReport> & report);
591 } // namespace target
594 #endif // ZYPP_TARGET_RPM_RPMDB_H