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 ///////////////////////////////////////////////////////////////////
64 * Root directory for all operations.
69 * Directory that contains the rpmdb.
76 * Constructor. There's no rpmdb access until @ref initDatabase
87 * @return Root directory for all operations (empty if not initialized).
89 const Pathname & root() const
95 * @return Directory that contains the rpmdb (empty if not initialized).
97 const Pathname & dbPath() const
103 * @return Whether we are initialized.
105 bool initialized() const
107 return( ! _root.empty() );
111 * Prepare access to the rpm database below \a root_r.
113 * An optional argument denotes the root directory for all operations. If
114 * an empty Pathname is given the default (\c/) is used.
116 * Calling initDatabase a second time with different arguments will return
117 * an error but leave the database in it's original state.
119 * If the database already exists and \c doRebuild_r is true,
120 * \ref rebuildDatabase is called.
122 * \throws RpmException
124 void initDatabase( Pathname root_r = Pathname(), bool doRebuild_r = false );
127 * Block further access to the rpm database and go back to uninitialized
128 * state. On update: Decides what to do with any converted database
129 * (see @ref initDatabase).
131 * \throws RpmException
134 void closeDatabase();
137 * Rebuild the rpm database (rpm --rebuilddb).
139 * \throws RpmException
142 void rebuildDatabase();
145 * Import ascii armored public key in file pubkey_r.
147 * \throws RpmException
150 void importPubkey( const PublicKey & pubkey_r );
153 * Remove a public key from the rpm database
155 * \throws RpmException
158 void removePubkey( const PublicKey & pubkey_r );
161 * Return the long ids of all installed public keys.
163 std::list<PublicKey> pubkeys() const;
166 * Return the edition of all installed public keys.
168 std::set<Edition> pubkeyEditions() const;
170 ///////////////////////////////////////////////////////////////////
172 // Direct RPM database retrieval via librpm.
174 ///////////////////////////////////////////////////////////////////
178 * return complete file list for installed package name_r (in FileInfo.filename)
179 * if edition_r != Edition::noedition, check for exact edition
180 * if full==true, fill all attributes of FileInfo
182 std::list<FileInfo> fileList( const std::string & name_r, const Edition & edition_r ) const;
185 * Return true if at least one package owns a certain file (name_r empty)
186 * Return true if package name_r owns file file_r (name_r nonempty).
188 bool hasFile( const std::string & file_r, const std::string & name_r = "" ) const;
191 * Return name of package owning file
192 * or empty string if no installed package owns file
194 std::string whoOwnsFile( const std::string & file_r ) const;
197 * Return true if at least one package provides a certain tag.
199 bool hasProvides( const std::string & tag_r ) const;
202 * Return true if at least one package requires a certain tag.
204 bool hasRequiredBy( const std::string & tag_r ) const;
207 * Return true if at least one package conflicts with a certain tag.
209 bool hasConflicts( const std::string & tag_r ) const;
212 * Return true if package is installed.
214 bool hasPackage( const std::string & name_r ) const;
217 * Return true if package is installed in a certain edition.
219 bool hasPackage( const std::string & name_r, const Edition & ed_r ) const;
222 * Get an installed packages data from rpmdb. Package is
223 * identified by name. Data returned via result are NULL,
224 * if packge is not installed (PMError is not set), or RPM database
225 * could not be read (PMError is set).
227 * \throws RpmException
229 * FIXME this and following comment
232 void getData( const std::string & name_r,
233 RpmHeader::constPtr & result_r ) const;
236 * Get an installed packages data from rpmdb. Package is
237 * identified by name and edition. Data returned via result are NULL,
238 * if packge is not installed (PMError is not set), or RPM database
239 * could not be read (PMError is set).
241 * \throws RpmException
244 void getData( const std::string & name_r, const Edition & ed_r,
245 RpmHeader::constPtr & result_r ) const;
247 ///////////////////////////////////////////////////////////////////
249 ///////////////////////////////////////////////////////////////////
251 /** Sync mode for \ref syncTrustedKeys */
252 enum SyncTrustedKeyBits
254 SYNC_TO_KEYRING = 1<<0, //!< export rpm trusted keys into zypp trusted keyring
255 SYNC_FROM_KEYRING = 1<<1, //!< import zypp trusted keys into rpm database.
256 SYNC_BOTH = SYNC_TO_KEYRING | SYNC_FROM_KEYRING
259 * Sync trusted keys stored in rpm database and zypp trusted keyring.
261 void syncTrustedKeys( SyncTrustedKeyBits mode_r = SYNC_BOTH );
263 * iterates through zypp keyring and import all non existant keys
266 void importZyppKeyRingTrustedKeys();
268 * insert all rpm trusted keys into zypp trusted keyring
270 void exportTrustedKeysInZyppKeyRing();
274 * The connection to the rpm process.
276 ExternalProgram *process;
278 typedef std::vector<const char*> RpmArgVec;
281 * Run rpm with the specified arguments and handle stderr.
282 * @param n_opts The number of arguments
283 * @param options Array of the arguments, @ref n_opts elements
284 * @param stderr_disp How to handle stderr, merged with stdout by default
286 * \throws RpmException
289 void run_rpm( const RpmArgVec& options,
290 ExternalProgram::Stderr_Disposition stderr_disp =
291 ExternalProgram::Stderr_To_Stdout);
295 * Read a line from the general rpm query
297 bool systemReadLine(std::string &line);
300 * Return the exit status of the general rpm process,
301 * closing the connection if not already done.
306 * Forcably kill the system process
311 * The exit code of the rpm process, or -1 if not yet known.
316 * Error message from running rpm as external program.
317 * Use only if something fail.
319 std::string error_message;
321 /** /var/adm/backup */
322 Pathname _backuppath;
324 /** create package backups? */
325 bool _packagebackups;
328 * handle rpm messages like "/etc/testrc saved as /etc/testrc.rpmorig"
330 * @param line rpm output starting with warning:
331 * @param name name of package, appears in subject line
332 * @param typemsg " saved as " or " created as "
333 * @param difffailmsg what to put into mail if diff failed, must contain two %s for the two files
334 * @param diffgenmsg what to put into mail if diff succeeded, must contain two %s for the two files
336 void processConfigFiles(const std::string& line,
337 const std::string& name,
339 const char* difffailmsg,
340 const char* diffgenmsg);
345 typedef std::set<std::string> FileList;
348 * checkPackage result
351 enum CheckPackageResult
353 CHK_OK = 0, /*!< Signature is OK. */
354 CHK_NOTFOUND = 1, /*!< Signature is unknown type. */
355 CHK_FAIL = 2, /*!< Signature does not verify. */
356 CHK_NOTTRUSTED = 3, /*!< Signature is OK, but key is not trusted. */
357 CHK_NOKEY = 4, /*!< Public key is unavailable. */
358 CHK_ERROR = 5, /*!< File does not exist or can't be opened. */
359 CHK_NOSIG = 6, /*!< File has no gpg signature (only digests). */
362 /** Detailed rpm signature check log messages
363 * A single multiline message if \ref CHK_OK. Otherwise each message line
364 * together with it's \ref CheckPackageResult.
366 struct CheckPackageDetail : std::vector<std::pair<CheckPackageResult,std::string>>
370 * Check signature of rpm file on disk (legacy version returning CHK_OK if file is unsigned, like 'rpm -K')
372 * @param path_r which file to check
373 * @param detail_r Return detailed rpm log messages
375 * @return CheckPackageResult (CHK_OK if file is unsigned)
377 * \see also \ref checkPackageSignature
379 CheckPackageResult checkPackage( const Pathname & path_r, CheckPackageDetail & detail_r );
380 /** \overload Ignoring the \a details_r */
381 CheckPackageResult checkPackage( const Pathname & path_r );
384 * Check signature of rpm file on disk (strict check returning CHK_NOSIG if file is unsigned).
386 * @param path_r which file to check
387 * @param detail_r Return detailed rpm log messages
389 * @return CheckPackageResult (CHK_NOSIG if file is unsigned)
391 * \see also \ref checkPackage
393 CheckPackageResult checkPackageSignature( const Pathname & path_r, CheckPackageDetail & detail_r );
395 /** install rpm package
397 * @param filename file to install
398 * @param flags which rpm options to use
402 * \throws RpmException
405 void installPackage ( const Pathname & filename, RpmInstFlags flags = RPMINST_NONE );
407 /** remove rpm package
409 * @param name_r Name of the rpm package to remove.
410 * @param iflags which rpm options to use
414 * \throws RpmException
417 void removePackage( const std::string & name_r, RpmInstFlags flags = RPMINST_NONE );
418 void removePackage( Package::constPtr package, RpmInstFlags flags = RPMINST_NONE );
421 * get backup dir for rpm config files
424 Pathname getBackupPath (void)
430 * create tar.gz of all changed files in a Package
432 * @param packageName name of the Package to backup
436 bool backupPackage(const std::string& packageName);
439 * queries file for name and then calls above backupPackage
440 * function. For convenience.
442 * @param filename rpm file that is about to be installed
444 bool backupPackage(const Pathname& filename);
447 * set path where package backups are stored
451 void setBackupPath(const Pathname& path);
454 * whether to create package backups during install or
457 * @param yes true or false
459 void createPackageBackups(bool yes)
461 _packagebackups = yes;
465 * determine which files of an installed package have been
468 * @param fileList (output) where to store modified files
469 * @param packageName name of package to query
471 * @return false if package couln't be queried for some
474 bool queryChangedFiles(FileList & fileList, const std::string& packageName);
481 virtual std::ostream & dumpOn( std::ostream & str ) const;
484 void doRemovePackage( const std::string & name_r, RpmInstFlags flags, callback::SendReport<RpmRemoveReport> & report );
485 void doInstallPackage( const Pathname & filename, RpmInstFlags flags, callback::SendReport<RpmInstallReport> & report );
486 void doRebuildDatabase(callback::SendReport<RebuildDBReport> & report);
489 /** \relates RpmDb::CheckPackageResult Stream output */
490 std::ostream & operator<<( std::ostream & str, RpmDb::CheckPackageResult obj );
492 /** \relates RpmDb::checkPackageDetail Stream output */
493 std::ostream & operator<<( std::ostream & str, const RpmDb::CheckPackageDetail & obj );
496 } // namespace target
499 #endif // ZYPP_TARGET_RPM_RPMDB_H