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/RpmFlags.h>
30 #include <zypp/target/rpm/RpmHeader.h>
31 #include <zypp/target/rpm/RpmCallbacks.h>
32 #include <zypp/ZYppCallbacks.h>
41 ///////////////////////////////////////////////////////////////////
45 * @short Interface to the rpm program
47 class RpmDb : public base::ReferenceCounted, private base::NonCopyable
54 typedef class InstTargetError Error;
56 ///////////////////////////////////////////////////////////////////
60 ///////////////////////////////////////////////////////////////////
63 enum DbStateInfoBits {
66 DbStateInfoBits _dbStateInfo;
70 * Root directory for all operations.
75 * Directory that contains the rpmdb.
82 * Constructor. There's no rpmdb access until @ref initDatabase
93 * @return Root directory for all operations (empty if not initialized).
95 const Pathname & root() const
101 * @return Directory that contains the rpmdb (empty if not initialized).
103 const Pathname & dbPath() const
109 * @return Whether we are initialized.
111 bool initialized() const
113 return( ! _root.empty() );
117 * Prepare access to the rpm database at \c/var/lib/rpm.
119 * An optional argument denotes the root directory for all operations. If
120 * an empty Pathname is given the default (\c/) is used.
122 * Calling initDatabase a second time with different arguments will return
123 * an error but leave the database in it's original state.
125 * If the database already exists and \c doRebuild_r is true,
126 * \ref rebuildDatabase is called.
128 * \throws RpmException
130 void initDatabase( Pathname root_r = Pathname(), bool doRebuild_r = false );
133 * Block further access to the rpm database and go back to uninitialized
134 * state. On update: Decides what to do with any converted database
135 * (see @ref initDatabase).
137 * \throws RpmException
140 void closeDatabase();
143 * Rebuild the rpm database (rpm --rebuilddb).
145 * \throws RpmException
148 void rebuildDatabase();
151 * Import ascii armored public key in file pubkey_r.
153 * \throws RpmException
156 void importPubkey( const PublicKey & pubkey_r );
159 * Remove a public key from the rpm database
161 * \throws RpmException
164 void removePubkey( const PublicKey & pubkey_r );
167 * Return the long ids of all installed public keys.
169 std::list<PublicKey> pubkeys() const;
172 * Return the edition of all installed public keys.
174 std::set<Edition> pubkeyEditions() const;
176 ///////////////////////////////////////////////////////////////////
178 // Direct RPM database retrieval via librpm.
180 ///////////////////////////////////////////////////////////////////
184 * return complete file list for installed package name_r (in FileInfo.filename)
185 * if edition_r != Edition::noedition, check for exact edition
186 * if full==true, fill all attributes of FileInfo
188 std::list<FileInfo> fileList( const std::string & name_r, const Edition & edition_r ) const;
191 * Return true if at least one package owns a certain file (name_r empty)
192 * Return true if package name_r owns file file_r (name_r nonempty).
194 bool hasFile( const std::string & file_r, const std::string & name_r = "" ) const;
197 * Return name of package owning file
198 * or empty string if no installed package owns file
200 std::string whoOwnsFile( const std::string & file_r ) const;
203 * Return true if at least one package provides a certain tag.
205 bool hasProvides( const std::string & tag_r ) const;
208 * Return true if at least one package requires a certain tag.
210 bool hasRequiredBy( const std::string & tag_r ) const;
213 * Return true if at least one package conflicts with a certain tag.
215 bool hasConflicts( const std::string & tag_r ) const;
218 * Return true if package is installed.
220 bool hasPackage( const std::string & name_r ) const;
223 * Return true if package is installed in a certain edition.
225 bool hasPackage( const std::string & name_r, const Edition & ed_r ) const;
228 * Get an installed packages data from rpmdb. Package is
229 * identified by name. Data returned via result are NULL,
230 * if packge is not installed (PMError is not set), or RPM database
231 * could not be read (PMError is set).
233 * \throws RpmException
235 * FIXME this and following comment
238 void getData( const std::string & name_r,
239 RpmHeader::constPtr & result_r ) const;
242 * Get an installed packages data from rpmdb. Package is
243 * identified by name and edition. Data returned via result are NULL,
244 * if packge is not installed (PMError is not set), or RPM database
245 * could not be read (PMError is set).
247 * \throws RpmException
250 void getData( const std::string & name_r, const Edition & ed_r,
251 RpmHeader::constPtr & result_r ) const;
253 ///////////////////////////////////////////////////////////////////
255 ///////////////////////////////////////////////////////////////////
257 /** Sync mode for \ref syncTrustedKeys */
258 enum SyncTrustedKeyBits
260 SYNC_TO_KEYRING = 1<<0, //!< export rpm trusted keys into zypp trusted keyring
261 SYNC_FROM_KEYRING = 1<<1, //!< import zypp trusted keys into rpm database.
262 SYNC_BOTH = SYNC_TO_KEYRING | SYNC_FROM_KEYRING
265 * Sync trusted keys stored in rpm database and zypp trusted keyring.
267 void syncTrustedKeys( SyncTrustedKeyBits mode_r = SYNC_BOTH );
269 * iterates through zypp keyring and import all non existant keys
272 void importZyppKeyRingTrustedKeys();
274 * insert all rpm trusted keys into zypp trusted keyring
276 void exportTrustedKeysInZyppKeyRing();
280 * The connection to the rpm process.
282 ExternalProgram *process;
284 typedef std::vector<const char*> RpmArgVec;
287 * Run rpm with the specified arguments and handle stderr.
288 * @param n_opts The number of arguments
289 * @param options Array of the arguments, @ref n_opts elements
290 * @param stderr_disp How to handle stderr, merged with stdout by default
292 * \throws RpmException
295 void run_rpm( const RpmArgVec& options,
296 ExternalProgram::Stderr_Disposition stderr_disp =
297 ExternalProgram::Stderr_To_Stdout);
301 * Read a line from the general rpm query
303 bool systemReadLine(std::string &line);
306 * Return the exit status of the general rpm process,
307 * closing the connection if not already done.
312 * Forcably kill the system process
317 * The exit code of the rpm process, or -1 if not yet known.
322 * Error message from running rpm as external program.
323 * Use only if something fail.
325 std::string error_message;
327 /** /var/adm/backup */
328 Pathname _backuppath;
330 /** create package backups? */
331 bool _packagebackups;
334 /** whether <_root>/<WARNINGMAILPATH> was already created */
338 * handle rpm messages like "/etc/testrc saved as /etc/testrc.rpmorig"
340 * @param line rpm output starting with warning:
341 * @param name name of package, appears in subject line
342 * @param typemsg " saved as " or " created as "
343 * @param difffailmsg what to put into mail if diff failed, must contain two %s for the two files
344 * @param diffgenmsg what to put into mail if diff succeeded, must contain two %s for the two files
346 void processConfigFiles(const std::string& line,
347 const std::string& name,
349 const char* difffailmsg,
350 const char* diffgenmsg);
355 typedef std::set<std::string> FileList;
358 * checkPackage result
361 enum CheckPackageResult
363 CHK_OK = 0, /*!< Signature is OK. */
364 CHK_NOTFOUND = 1, /*!< Signature is unknown type. */
365 CHK_FAIL = 2, /*!< Signature does not verify. */
366 CHK_NOTTRUSTED = 3, /*!< Signature is OK, but key is not trusted. */
367 CHK_NOKEY = 4, /*!< Public key is unavailable. */
368 CHK_ERROR = 5, /*!< File does not exist or can't be opened. */
369 CHK_NOSIG = 6, /*!< File has no gpg signature (only digests). */
372 /** Detailed rpm signature check log messages
373 * A single multiline message if \ref CHK_OK. Otherwise each message line
374 * together with it's \ref CheckPackageResult.
376 struct CheckPackageDetail : std::vector<std::pair<CheckPackageResult,std::string>>
380 * Check signature of rpm file on disk (legacy version returning CHK_OK if file is unsigned, like 'rpm -K')
382 * @param path_r which file to check
383 * @param detail_r Return detailed rpm log messages
385 * @return CheckPackageResult (CHK_OK if file is unsigned)
387 * \see also \ref checkPackageSignature
389 CheckPackageResult checkPackage( const Pathname & path_r, CheckPackageDetail & detail_r );
390 /** \overload Ignoring the \a details_r */
391 CheckPackageResult checkPackage( const Pathname & path_r );
394 * Check signature of rpm file on disk (strict check returning CHK_NOSIG if file is unsigned).
396 * @param path_r which file to check
397 * @param detail_r Return detailed rpm log messages
399 * @return CheckPackageResult (CHK_NOSIG if file is unsigned)
401 * \see also \ref checkPackage
403 CheckPackageResult checkPackageSignature( const Pathname & path_r, CheckPackageDetail & detail_r );
405 /** install rpm package
407 * @param filename file to install
408 * @param flags which rpm options to use
412 * \throws RpmException
415 void installPackage ( const Pathname & filename, RpmInstFlags flags = RPMINST_NONE );
417 /** remove rpm package
419 * @param name_r Name of the rpm package to remove.
420 * @param iflags which rpm options to use
424 * \throws RpmException
427 void removePackage( const std::string & name_r, RpmInstFlags flags = RPMINST_NONE );
428 void removePackage( Package::constPtr package, RpmInstFlags flags = RPMINST_NONE );
431 * get backup dir for rpm config files
434 Pathname getBackupPath (void)
440 * create tar.gz of all changed files in a Package
442 * @param packageName name of the Package to backup
446 bool backupPackage(const std::string& packageName);
449 * queries file for name and then calls above backupPackage
450 * function. For convenience.
452 * @param filename rpm file that is about to be installed
454 bool backupPackage(const Pathname& filename);
457 * set path where package backups are stored
461 void setBackupPath(const Pathname& path);
464 * whether to create package backups during install or
467 * @param yes true or false
469 void createPackageBackups(bool yes)
471 _packagebackups = yes;
475 * determine which files of an installed package have been
478 * @param fileList (output) where to store modified files
479 * @param packageName name of package to query
481 * @return false if package couln't be queried for some
484 bool queryChangedFiles(FileList & fileList, const std::string& packageName);
491 virtual std::ostream & dumpOn( std::ostream & str ) const;
494 void doRemovePackage( const std::string & name_r, RpmInstFlags flags, callback::SendReport<RpmRemoveReport> & report );
495 void doInstallPackage( const Pathname & filename, RpmInstFlags flags, callback::SendReport<RpmInstallReport> & report );
496 void doRebuildDatabase(callback::SendReport<RebuildDBReport> & report);
499 /** \relates RpmDb::CheckPackageResult Stream output */
500 std::ostream & operator<<( std::ostream & str, RpmDb::CheckPackageResult obj );
502 /** \relates RpmDb::checkPackageDetail Stream output */
503 std::ostream & operator<<( std::ostream & str, const RpmDb::CheckPackageDetail & obj );
506 } // namespace target
509 #endif // ZYPP_TARGET_RPM_RPMDB_H