- Separate target init from adding resolvables, getting rid of the
[platform/upstream/libzypp.git] / zypp / target / TargetImpl.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/target/TargetImpl.h
10  *
11 */
12 #ifndef ZYPP_TARGET_TARGETIMPL_H
13 #define ZYPP_TARGET_TARGETIMPL_H
14
15 #include <iosfwd>
16 #include <list>
17 #include <set>
18
19 #include "zypp/base/ReferenceCounted.h"
20 #include "zypp/base/NonCopyable.h"
21 #include "zypp/base/DefaultFalseBool.h"
22 #include "zypp/base/PtrTypes.h"
23 #include "zypp/ResStore.h"
24 #include "zypp/PoolItem.h"
25 #include "zypp/ZYppCommit.h"
26
27 #include "zypp/Pathname.h"
28 #include "zypp/media/MediaAccess.h"
29 #include "zypp/Target.h"
30 #include "zypp/target/rpm/RpmDb.h"
31 #include "zypp/target/store/PersistentStorage.h"
32 #include "zypp/target/TargetException.h"
33
34 ///////////////////////////////////////////////////////////////////
35 namespace zypp
36 { /////////////////////////////////////////////////////////////////
37   ///////////////////////////////////////////////////////////////////
38   namespace target
39   { /////////////////////////////////////////////////////////////////
40
41     DEFINE_PTR_TYPE(TargetImpl);
42
43     ///////////////////////////////////////////////////////////////////
44     //
45     //  CLASS NAME : TargetImpl
46     //
47     /** Base class for concrete Target implementations.
48      *
49      * Constructed by \ref TargetFactory. Public access via \ref Target
50      * interface.
51     */
52     class TargetImpl : public base::ReferenceCounted, private base::NonCopyable
53     {
54       friend std::ostream & operator<<( std::ostream & str, const TargetImpl & obj );
55
56     public:
57       /** list of pool items  */
58       typedef std::list<PoolItem_Ref> PoolItemList;
59
60       /** set of pool items  */
61       typedef std::set<PoolItem_Ref> PoolItemSet;
62
63     public:
64       /** JUST FOR TESTSUITE */
65       /** Sort according to prereqs and media numbers */
66       void getResolvablesToInsDel ( const ResPool pool_r,
67                                     PoolItemList & dellist_r,
68                                     PoolItemList & instlist_r,
69                                     PoolItemList & srclist_r ) const;
70
71
72     public:
73       /** Ctor. */
74       TargetImpl(const Pathname & root_r = "/");
75       /** Dtor. */
76       virtual ~TargetImpl();
77
78       /** Null implementation */
79       static TargetImpl_Ptr nullimpl();
80
81     public:
82
83       /** All resolvables in the target. */
84       const ResStore & resolvables();
85
86       /**
87         * resolvables of certain kind
88         */
89       ResStore resolvablesByKind( const Resolvable::Kind kind );
90
91       /** The root set for this target */
92       Pathname root() const;
93
94       /** Commit changes in the pool */
95       ZYppCommitResult commit( ResPool pool_r, const ZYppCommitPolicy & policy_r );
96
97       ZYPP_DEPRECATED int commit( ResPool pool_r, unsigned int medianr,
98                                   PoolItemList & errors_r,
99                                   PoolItemList & remaining_r,
100                                   PoolItemList & srcremaining_r,
101                                   bool dry_run = false )
102       {
103         ZYppCommitPolicy policy;
104         policy.restrictToMedia( medianr ).dryRun( dry_run );
105         ZYppCommitResult res = commit( pool_r, policy );
106         errors_r.swap( res._errors );
107         remaining_r.swap( res._remaining );
108         srcremaining_r.swap( res._srcremaining );
109         return res._result;
110       }
111
112       /** enables the storage target */
113       bool isStorageEnabled() const;
114       void enableStorage(const Pathname &root_r);
115
116       /** Commit ordered changes
117        *  @param pool_r only needed for #160792
118        *  @return uncommitted ones (due to error)
119        */
120       PoolItemList commit( const PoolItemList & items_r, const ZYppCommitPolicy & policy_r, const ResPool & pool_r );
121
122       /** Overload to realize stream output. */
123       virtual std::ostream & dumpOn( std::ostream & str ) const
124       { return str << "TargetImpl"; }
125
126       /** The RPM database */
127       rpm::RpmDb & rpm();
128
129       /** If the package is installed and provides the file
130           Needed to evaluate split provides during Resolver::Upgrade() */
131       bool providesFile (const std::string & path_str, const std::string & name_str) const;
132
133       /** Return the resolvable which provides path_str (rpm -qf)
134           return NULL if no resolvable provides this file  */
135       ResObject::constPtr whoOwnsFile (const std::string & path_str) const;
136
137       /** Set the log file for target */
138       bool setInstallationLogfile(const Pathname & path_r);
139
140       /** return the last modification date of the target */
141       Date timestamp() const;
142       
143     protected:
144       void loadKindResolvables( const Resolvable::Kind kind );
145       /** All resolvables provided by the target. */
146       ResStore _store;
147       /** Path to the target */
148       Pathname _root;
149       /** RPM database */
150       rpm::RpmDb _rpm;
151 #ifndef STORAGE_DISABLED
152       zypp::storage::PersistentStorage _storage;
153       bool _storage_enabled;
154       std::map< const Resolvable::Kind, DefaultFalseBool> _resstore_loaded;
155 #endif
156     private:
157       /** Null implementation */
158       static TargetImpl_Ptr _nullimpl;
159     };
160     ///////////////////////////////////////////////////////////////////
161
162     /** \relates TargetImpl Stream output */
163     inline std::ostream & operator<<( std::ostream & str, const TargetImpl & obj )
164     { return obj.dumpOn( str ); }
165
166     /////////////////////////////////////////////////////////////////
167   } // namespace target
168   ///////////////////////////////////////////////////////////////////
169   /////////////////////////////////////////////////////////////////
170 } // namespace zypp
171 ///////////////////////////////////////////////////////////////////
172 #endif // ZYPP_TARGET_TARGETIMPL_H