merge REFACTORING-10_3 back to trunk
[platform/upstream/libzypp.git] / zypp / target / rpm / RpmDb.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/target/rpm/RpmDb.h
10  *
11 */
12
13 // -*- C++ -*-
14
15 #ifndef ZYPP_TARGET_RPM_RPMDB_H
16 #define ZYPP_TARGET_RPM_RPMDB_H
17
18 #include <iosfwd>
19 #include <list>
20 #include <vector>
21 #include <string>
22
23 #include "zypp/Pathname.h"
24 #include "zypp/ExternalProgram.h"
25
26 #include "zypp/Package.h"
27 #include "zypp/KeyRing.h"
28
29 #include "zypp/target/rpm/RpmHeader.h"
30 #include "zypp/target/rpm/RpmCallbacks.h"
31 #include "zypp/ZYppCallbacks.h"
32
33 namespace zypp
34 {
35 namespace target
36 {
37 namespace rpm
38 {
39
40 ///////////////////////////////////////////////////////////////////
41 //
42 //      CLASS NAME : RpmDb
43 /**
44  * @short Interface to the rpm program
45  **/
46 class RpmDb : public base::ReferenceCounted, private base::NonCopyable
47 {
48 public:
49
50   /**
51    * Default error class
52    **/
53   typedef class InstTargetError Error;
54
55   ///////////////////////////////////////////////////////////////////
56   //
57   // INITALISATION
58   //
59   ///////////////////////////////////////////////////////////////////
60 private:
61
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
70   };
71
72   friend std::ostream & operator<<( std::ostream & str, const DbStateInfoBits & obj );
73
74   void dbsi_set( DbStateInfoBits & val_r, const unsigned & bits_r ) const
75   {
76     val_r = (DbStateInfoBits)(val_r | bits_r);
77   }
78   void dbsi_clr( DbStateInfoBits & val_r, const unsigned & bits_r ) const
79   {
80     val_r = (DbStateInfoBits)(val_r & ~bits_r);
81   }
82   bool dbsi_has( const DbStateInfoBits & val_r, const unsigned & bits_r ) const
83   {
84     return( (val_r & bits_r) == bits_r );
85   }
86
87   /**
88    * Internal state info
89    **/
90   DbStateInfoBits _dbStateInfo;
91
92   /**
93    * Root directory for all operations.
94    **/
95   Pathname _root;
96
97   /**
98    * Directory that contains the rpmdb.
99    **/
100   Pathname _dbPath;
101
102   /**
103    * Internal helper for @ref initDatabase.
104    *
105    * \throws RpmException
106    *
107    **/
108   void internal_initDatabase( const Pathname & root_r, const Pathname & dbPath_r,
109                               DbStateInfoBits & info_r );
110
111   /**
112    * Remove the rpm4 database in dbdir_r and optionally any backup created
113    * on conversion.
114    **/
115   static void removeV4( const Pathname & dbdir_r, bool v3backup_r );
116
117   /**
118    * Remove the rpm3 database in dbdir_r. Create a backup copy named
119    * packages.rpm3 if it does not already exist.
120    **/
121   static void removeV3( const Pathname & dbdir_r, bool v3backup_r );
122
123   /**
124    * Called before the database is modified by installPackage/removePackage.
125    * Invalidates Packages list and moves away any old database.
126    **/
127   void modifyDatabase();
128
129 public:
130
131   /**
132    * Constructor. There's no rpmdb access until @ref initDatabase
133    * was called.
134    **/
135   RpmDb();
136
137   /**
138    * Destructor.
139    **/
140   ~RpmDb();
141
142   /**
143    * timestamp of the rpm database (last modification)
144    */
145   Date timestamp() const;
146
147   /**
148    * @return Root directory for all operations (empty if not initialized).
149    **/
150   const Pathname & root() const
151   {
152     return _root;
153   }
154
155   /**
156    * @return Directory that contains the rpmdb (empty if not initialized).
157    **/
158   const Pathname & dbPath() const
159   {
160     return _dbPath;
161   }
162
163   /**
164    * @return Whether we are initialized.
165    **/
166   bool initialized() const
167   {
168     return( ! _root.empty() );
169   }
170
171   /**
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).
175    *
176    * On empty Pathnames the default is used:
177    * <PRE>
178    *     root:   /
179    *     dbPath: /var/lib/rpm
180    * </PRE>
181    *
182    * Calling initDatabase a second time with different arguments will return
183    * an error but leave the database in it's original state.
184    *
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.
189    *
190    * \throws RpmException
191    *
192    **/
193   void initDatabase( Pathname root_r = Pathname(),
194                      Pathname dbPath_r = Pathname() );
195
196   /**
197    * Block further access to the rpm database and go back to uninitialized
198    * state. On update: Decides what to do with any converted database
199    * (see @ref initDatabase).
200    *
201    * \throws RpmException
202    *
203    **/
204   void closeDatabase();
205
206   /**
207    * Rebuild the rpm database (rpm --rebuilddb).
208    *
209    * \throws RpmException
210    *
211    **/
212   void rebuildDatabase();
213
214   /**
215    * Import ascii armored public key in file pubkey_r.
216    *
217    * \throws RpmException
218    *
219    **/
220   void importPubkey( const PublicKey & pubkey_r );
221
222   /**
223    * Return the long ids of all installed public keys.
224    **/
225   std::list<PublicKey> pubkeys() const;
226
227   /**
228    * Return the edition of all installed public keys.
229    **/
230   std::set<Edition> pubkeyEditions() const;
231
232   ///////////////////////////////////////////////////////////////////
233   //
234   // Cached RPM database retrieval via librpm.
235   //
236   ///////////////////////////////////////////////////////////////////
237 private:
238
239   class Packages;
240
241   Packages & _packages;
242
243   std::set<std::string> _filerequires;
244
245 public:
246
247   /**
248    * @return Whether the list of installed packages is valid, or
249    * you'd better reread it. (<B>NOTE:</B> returns valid, if not
250    * initialized).
251    **/
252   bool packagesValid() const;
253
254   /**
255    * If necessary build, and return the list of all installed packages.
256    **/
257   const std::list<Package::Ptr> & getPackages();
258
259   ///////////////////////////////////////////////////////////////////
260   //
261   // Direct RPM database retrieval via librpm.
262   //
263   ///////////////////////////////////////////////////////////////////
264 public:
265
266   /**
267    * return complete file list for installed package name_r (in FileInfo.filename)
268    * if edition_r != Edition::noedition, check for exact edition
269    * if full==true, fill all attributes of FileInfo
270    **/
271   std::list<FileInfo> fileList( const std::string & name_r, const Edition & edition_r ) const;
272
273   /**
274    * Return true if at least one package owns a certain file (name_r empty)
275    * Return true if package name_r owns file file_r (name_r nonempty).
276    **/
277   bool hasFile( const std::string & file_r, const std::string & name_r = "" ) const;
278
279   /**
280    * Return name of package owning file
281    * or empty string if no installed package owns file
282    **/
283   std::string whoOwnsFile( const std::string & file_r ) const;
284
285   /**
286    * Return true if at least one package provides a certain tag.
287    **/
288   bool hasProvides( const std::string & tag_r ) const;
289
290   /**
291    * Return true if at least one package requires a certain tag.
292    **/
293   bool hasRequiredBy( const std::string & tag_r ) const;
294
295   /**
296    * Return true if at least one package conflicts with a certain tag.
297    **/
298   bool hasConflicts( const std::string & tag_r ) const;
299
300   /**
301    * Return true if package is installed.
302    **/
303   bool hasPackage( const std::string & name_r ) const;
304
305   /**
306    * Return true if package is installed in a certain edition.
307    **/
308   bool hasPackage( const std::string & name_r, const Edition & ed_r ) const;
309
310   /**
311    * Get an installed packages data from rpmdb. Package is
312    * identified by name. Data returned via result are NULL,
313    * if packge is not installed (PMError is not set), or RPM database
314    * could not be read (PMError is set).
315    *
316    * \throws RpmException
317    *
318    * FIXME this and following comment
319    *
320    **/
321   void getData( const std::string & name_r,
322                 RpmHeader::constPtr & result_r ) const;
323
324   /**
325    * Get an installed packages data from rpmdb. Package is
326    * identified by name and edition. Data returned via result are NULL,
327    * if packge is not installed (PMError is not set), or RPM database
328    * could not be read (PMError is set).
329    *
330    * \throws RpmException
331    *
332    **/
333   void getData( const std::string & name_r, const Edition & ed_r,
334                 RpmHeader::constPtr & result_r ) const;
335
336
337   /**
338    * Create a package from RpmHeader
339    * return NULL on error
340    **/
341
342   static Package::Ptr makePackageFromHeader( const RpmHeader::constPtr header,
343                                              std::set<std::string> * filerequires,
344                                              const Pathname & location, Repository repo );
345
346   ///////////////////////////////////////////////////////////////////
347   //
348   ///////////////////////////////////////////////////////////////////
349 public:
350   /**
351    * iterates through zypp keyring and import all non existant keys
352    * into rpm keyring
353    */
354   void importZyppKeyRingTrustedKeys();
355   /**
356    * insert all rpm trusted keys into zypp trusted keyring
357    */
358   void exportTrustedKeysInZyppKeyRing();
359 private:
360   /**
361    * The connection to the rpm process.
362   */
363   ExternalProgram *process;
364
365   typedef std::vector<const char*> RpmArgVec;
366
367   /**
368    * Run rpm with the specified arguments and handle stderr.
369    * @param n_opts The number of arguments
370    * @param options Array of the arguments, @ref n_opts elements
371    * @param stderr_disp How to handle stderr, merged with stdout by default
372    *
373    * \throws RpmException
374    *
375    **/
376   void run_rpm( const RpmArgVec& options,
377                 ExternalProgram::Stderr_Disposition stderr_disp =
378                   ExternalProgram::Stderr_To_Stdout);
379
380
381   /**
382    * Read a line from the general rpm query
383   */
384   bool systemReadLine(std::string &line);
385
386   /**
387    * Return the exit status of the general rpm process,
388    * closing the connection if not already done.
389   */
390   int systemStatus();
391
392   /**
393    * Forcably kill the system process
394   */
395   void systemKill();
396
397   /**
398    * The exit code of the rpm process, or -1 if not yet known.
399   */
400   int exit_code;
401
402   /** /var/adm/backup */
403   Pathname _backuppath;
404
405   /** create package backups? */
406   bool _packagebackups;
407
408   /** whether <_root>/<WARNINGMAILPATH> was already created */
409   bool _warndirexists;
410
411   /**
412    * handle rpm messages like "/etc/testrc saved as /etc/testrc.rpmorig"
413    *
414    * @param line rpm output starting with warning:
415    * @param name name of package, appears in subject line
416    * @param typemsg " saved as " or " created as "
417    * @param difffailmsg what to put into mail if diff failed, must contain two %s for the two files
418    * @param diffgenmsg what to put into mail if diff succeeded, must contain two %s for the two files
419    * */
420   void processConfigFiles(const std::string& line,
421                           const std::string& name,
422                           const char* typemsg,
423                           const char* difffailmsg,
424                           const char* diffgenmsg);
425
426
427 public:
428
429   typedef std::set<std::string> FileList;
430
431   /**
432    * Bits representing rpm installation options, useable as or
433    * combination
434    *
435    * @see installPackage(), removePackage()
436    * */
437   enum RpmInstFlag
438   {
439     RPMINST_NONE       = 0x0000,
440     RPMINST_NODOCS     = 0x0001,
441     RPMINST_NOSCRIPTS  = 0x0002,
442     RPMINST_FORCE      = 0x0004,
443     RPMINST_NODEPS     = 0x0008,
444     RPMINST_IGNORESIZE = 0x0010,
445     RPMINST_JUSTDB     = 0x0020,
446     RPMINST_NODIGEST   = 0x0040,
447     RPMINST_NOSIGNATURE= 0x0080,
448     RPMINST_NOUPGRADE  = 0x0100,
449     RPMINST_TEST         = 0x0200
450   };
451
452   /**
453    * checkPackage result
454    * @see checkPackage
455    * */
456   enum checkPackageResult
457   {
458     CHK_OK            = 0, /*!< Signature is OK. */
459     CHK_NOTFOUND      = 1, /*!< Signature is unknown type. */
460     CHK_FAIL          = 2, /*!< Signature does not verify. */
461     CHK_NOTTRUSTED    = 3, /*!< Signature is OK, but key is not trusted. */
462     CHK_NOKEY         = 4, /*!< Public key is unavailable. */
463     CHK_ERROR         = 5  /*!< File does not exist or can't be opened. */
464   };
465
466   /**
467    * Check signature of rpm file on disk.
468    *
469    * @param filename which file to check
470    *
471    * @return checkPackageResult
472   */
473   checkPackageResult checkPackage( const Pathname & path_r );
474
475   /** install rpm package
476    *
477    * @param filename file to install
478    * @param flags which rpm options to use
479    *
480    * @return success
481    *
482    * \throws RpmException
483    *
484    * */
485   void installPackage (const Pathname& filename, unsigned flags = 0 );
486
487   /** remove rpm package
488    *
489    * @param name_r Name of the rpm package to remove.
490    * @param iflags which rpm options to use
491    *
492    * @return success
493    *
494    * \throws RpmException
495    *
496    * */
497   void removePackage(const std::string & name_r, unsigned flags = 0);
498   void removePackage(Package::constPtr package, unsigned flags = 0);
499
500   /**
501    * get backup dir for rpm config files
502    *
503    * */
504   Pathname getBackupPath (void)
505   {
506     return _backuppath;
507   }
508
509   /**
510    * create tar.gz of all changed files in a Package
511    *
512    * @param packageName name of the Package to backup
513    *
514    * @see setBackupPath
515    * */
516   bool backupPackage(const std::string& packageName);
517
518   /**
519    * queries file for name and then calls above backupPackage
520    * function. For convenience.
521    *
522    * @param filename rpm file that is about to be installed
523    * */
524   bool backupPackage(const Pathname& filename);
525
526   /**
527    * set path where package backups are stored
528    *
529    * @see backupPackage
530    * */
531   void setBackupPath(const Pathname& path);
532
533   /**
534    * whether to create package backups during install or
535    * removal
536    *
537    * @param yes true or false
538    * */
539   void createPackageBackups(bool yes)
540   {
541     _packagebackups = yes;
542   }
543
544   /**
545    * determine which files of an installed package have been
546    * modified.
547    *
548    * @param fileList (output) where to store modified files
549    * @param packageName name of package to query
550    *
551    * @return false if package couln't be queried for some
552    * reason
553    * */
554   bool queryChangedFiles(FileList & fileList, const std::string& packageName);
555
556 public:
557
558   /**
559    * Dump debug info.
560    **/
561   virtual std::ostream & dumpOn( std::ostream & str ) const;
562
563 protected:
564   void doRemovePackage( const std::string & name_r, unsigned flags, callback::SendReport<RpmRemoveReport> & report );
565   void doInstallPackage( const Pathname & filename, unsigned flags, callback::SendReport<RpmInstallReport> & report );
566   const std::list<Package::Ptr> & doGetPackages(callback::SendReport<ScanDBReport> & report);
567   void doRebuildDatabase(callback::SendReport<RebuildDBReport> & report);
568
569
570 };
571
572 } // namespace rpm
573 } // namespace target
574 } // namespace zypp
575
576 #endif // ZYPP_TARGET_RPM_RPMDB_H