0b6c01552f6200bae602c3f379654e627547c28e
[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 <set>
17
18 #include "zypp/base/ReferenceCounted.h"
19 #include "zypp/base/NonCopyable.h"
20 #include "zypp/base/PtrTypes.h"
21 #include "zypp/PoolItem.h"
22 #include "zypp/ZYppCommit.h"
23
24 #include "zypp/Pathname.h"
25 #include "zypp/media/MediaAccess.h"
26 #include "zypp/Target.h"
27 #include "zypp/target/rpm/RpmDb.h"
28 #include "zypp/target/TargetException.h"
29 #include "zypp/target/RequestedLocalesFile.h"
30 #include "zypp/target/SolvIdentFile.h"
31 #include "zypp/target/HardLocksFile.h"
32 #include "zypp/ManagedFile.h"
33
34 ///////////////////////////////////////////////////////////////////
35 namespace zypp
36 { /////////////////////////////////////////////////////////////////
37   ///////////////////////////////////////////////////////////////////
38   namespace target
39   { /////////////////////////////////////////////////////////////////
40
41     DEFINE_PTR_TYPE(TargetImpl);
42     class CommitPackageCache;
43
44     ///////////////////////////////////////////////////////////////////
45     //
46     //  CLASS NAME : TargetImpl
47     //
48     /** Base class for concrete Target implementations.
49      *
50      * Constructed by \ref TargetFactory. Public access via \ref Target
51      * interface.
52     */
53     class TargetImpl : public base::ReferenceCounted, private base::NonCopyable
54     {
55       friend std::ostream & operator<<( std::ostream & str, const TargetImpl & obj );
56
57     public:
58       /** list of pool items  */
59       typedef std::list<PoolItem> PoolItemList;
60
61       /** set of pool items  */
62       typedef std::set<PoolItem> PoolItemSet;
63
64     public:
65       /** Ctor. */
66       TargetImpl(const Pathname & root_r = "/", bool doRebuild_r = false );
67       /** Dtor. */
68       virtual ~TargetImpl();
69
70       /** Null implementation */
71       static TargetImpl_Ptr nullimpl();
72
73       /**
74        * generates the unique anonymous id which is called
75        * when creating the target
76        */
77       void createAnonymousId() const;
78
79       /**
80        * generates a cache of the last product flavor
81        */
82       void createLastDistributionFlavorCache() const;
83
84       /** \name Solv file handling.
85        * If target solv file is outdated, but (non-root-)user has
86        * no permission to  create it at the default location, we
87        * use a temporary one.
88        */
89       //@{
90     private:
91       /** The systems default solv file location. */
92       Pathname defaultSolvfilesPath() const;
93
94       /** The solv file location actually in use (default or temp). */
95       Pathname solvfilesPath() const
96       { return solvfilesPathIsTemp() ? _tmpSolvfilesPath : defaultSolvfilesPath(); }
97
98       /** Whether we're using a temp. solvfile. */
99       bool solvfilesPathIsTemp() const
100       { return ! _tmpSolvfilesPath.empty(); }
101
102       Pathname _tmpSolvfilesPath;
103
104     public:
105       void load( bool force = true );
106
107       void unload();
108
109       void reload();
110
111       void clearCache();
112
113       bool buildCache();
114       //@}
115
116     public:
117
118       /** The root set for this target */
119       Pathname root() const
120       { return _root; }
121
122       /** The directory to store things. */
123       Pathname home() const
124       { return home( _root ); }
125
126       static Pathname home( const Pathname & root_r )
127       { return root_r / "/var/lib/zypp"; }
128
129       /** Commit changes in the pool */
130       ZYppCommitResult commit( ResPool pool_r, const ZYppCommitPolicy & policy_r );
131
132       /** Install a source package on the Target. */
133       void installSrcPackage( const SrcPackage_constPtr & srcPackage_r );
134
135       /** Provides a source package on the Target. */
136       ManagedFile provideSrcPackage( const SrcPackage_constPtr & srcPackage_r );
137
138       /** Overload to realize stream output. */
139       virtual std::ostream & dumpOn( std::ostream & str ) const
140       {
141         return str << "TargetImpl";
142       }
143
144       /** The RPM database */
145       rpm::RpmDb & rpm();
146
147       /** If the package is installed and provides the file
148       Needed to evaluate split provides during Resolver::Upgrade() */
149       bool providesFile (const std::string & path_str, const std::string & name_str) const;
150
151       /** Return name of package owning \a path_str
152        * or empty string if no installed package owns \a path_str. */
153       std::string whoOwnsFile (const std::string & path_str) const
154       { return _rpm.whoOwnsFile (path_str); }
155
156       /** return the last modification date of the target */
157       Date timestamp() const;
158
159       /** \copydoc Target::baseProduct() */
160       Product::constPtr baseProduct() const;
161
162       /** \copydoc Target::requestedLocales() */
163       LocaleSet requestedLocales() const
164       { return _requestedLocalesFile.locales(); }
165       /** \overload */
166       static LocaleSet requestedLocales( const Pathname & root_r );
167
168       /** \copydoc Target::targetDistribution() */
169       std::string targetDistribution() const;
170       /** \overload */
171       static std::string targetDistribution( const Pathname & root_r );
172
173       /** \copydoc Target::targetDistributionRelease()*/
174       std::string targetDistributionRelease() const;
175       /** \overload */
176       static std::string targetDistributionRelease( const Pathname & root_r );
177
178       /** \copydoc Target::targetDistributionFlavor()*/
179       std::string targetDistributionFlavor() const;
180       /** \overload */
181       static std::string targetDistributionFlavor( const Pathname & root_r );
182
183       /** \copydoc Target::distributionVersion()*/
184       Target::DistributionLabel distributionLabel() const;
185       /** \overload */
186       static Target::DistributionLabel distributionLabel( const Pathname & root_r );
187
188       /** \copydoc Target::distributionVersion()*/
189       std::string distributionVersion() const;
190       /** \overload */
191       static std::string distributionVersion( const Pathname & root_r );
192
193       /** \copydoc Target::distributionFlavor() */
194       std::string distributionFlavor() const;
195       /** \overload */
196       static std::string distributionFlavor( const Pathname & root_r );
197
198       /** \copydoc Target::anonymousUniqueId() */
199       std::string anonymousUniqueId() const;
200       /** \overload */
201       static std::string anonymousUniqueId( const Pathname & root_r );
202
203     private:
204       /** Commit ordered changes (internal helper) */
205       void commit( const ZYppCommitPolicy & policy_r,
206                    CommitPackageCache & packageCache_r,
207                    ZYppCommitResult & result_r );
208
209       /** Commit helper checking for file conflicts after download. */
210       void commitFindFileConflicts( const ZYppCommitPolicy & policy_r, ZYppCommitResult & result_r );
211     protected:
212       /** Path to the target */
213       Pathname _root;
214       /** RPM database */
215       rpm::RpmDb _rpm;
216       /** Requested Locales database */
217       RequestedLocalesFile _requestedLocalesFile;
218       /** user/auto installed database */
219       SolvIdentFile _autoInstalledFile;
220       /** Hard-Locks database */
221       HardLocksFile _hardLocksFile;
222       /** Cache distributionVersion */
223       mutable std::string _distributionVersion;
224
225     private:
226       /** Null implementation */
227       static TargetImpl_Ptr _nullimpl;
228     };
229     ///////////////////////////////////////////////////////////////////
230
231     /** \relates TargetImpl Stream output */
232     inline std::ostream & operator<<( std::ostream & str, const TargetImpl & obj )
233     {
234       return obj.dumpOn( str );
235     }
236
237     /////////////////////////////////////////////////////////////////
238   } // namespace target
239   ///////////////////////////////////////////////////////////////////
240   /////////////////////////////////////////////////////////////////
241 } // namespace zypp
242 ///////////////////////////////////////////////////////////////////
243 #endif // ZYPP_TARGET_TARGETIMPL_H