Imported Upstream version 16.3.2
[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       /**
71        * generates the unique anonymous id which is called
72        * when creating the target
73        */
74       void createAnonymousId() const;
75
76       /**
77        * generates a cache of the last product flavor
78        */
79       void createLastDistributionFlavorCache() const;
80
81       /** \name Solv file handling.
82        * If target solv file is outdated, but (non-root-)user has
83        * no permission to  create it at the default location, we
84        * use a temporary one.
85        */
86       //@{
87     private:
88       /** The systems default solv file location. */
89       Pathname defaultSolvfilesPath() const;
90
91       /** The solv file location actually in use (default or temp). */
92       Pathname solvfilesPath() const
93       { return solvfilesPathIsTemp() ? _tmpSolvfilesPath : defaultSolvfilesPath(); }
94
95       /** Whether we're using a temp. solvfile. */
96       bool solvfilesPathIsTemp() const
97       { return ! _tmpSolvfilesPath.empty(); }
98
99       Pathname _tmpSolvfilesPath;
100
101     public:
102       void load( bool force = true );
103
104       void unload();
105
106       void reload();
107
108       void clearCache();
109
110       bool buildCache();
111       //@}
112
113     public:
114
115       /** The root set for this target */
116       Pathname root() const
117       { return _root; }
118
119       /** The directory to store things. */
120       Pathname home() const
121       { return home( _root ); }
122
123       static Pathname home( const Pathname & root_r )
124       { return root_r / "/var/lib/zypp"; }
125
126       /** Commit changes in the pool */
127       ZYppCommitResult commit( ResPool pool_r, const ZYppCommitPolicy & policy_r );
128
129       /** Install a source package on the Target. */
130       void installSrcPackage( const SrcPackage_constPtr & srcPackage_r );
131
132       /** Provides a source package on the Target. */
133       ManagedFile provideSrcPackage( const SrcPackage_constPtr & srcPackage_r );
134
135       /** Overload to realize stream output. */
136       virtual std::ostream & dumpOn( std::ostream & str ) const
137       {
138         return str << "TargetImpl";
139       }
140
141       /** The RPM database */
142       rpm::RpmDb & rpm();
143
144       /** If the package is installed and provides the file
145       Needed to evaluate split provides during Resolver::Upgrade() */
146       bool providesFile (const std::string & path_str, const std::string & name_str) const;
147
148       /** Return name of package owning \a path_str
149        * or empty string if no installed package owns \a path_str. */
150       std::string whoOwnsFile (const std::string & path_str) const
151       { return _rpm.whoOwnsFile (path_str); }
152
153       /** return the last modification date of the target */
154       Date timestamp() const;
155
156       /** \copydoc Target::baseProduct() */
157       Product::constPtr baseProduct() const;
158
159       /** \copydoc Target::requestedLocales() */
160       LocaleSet requestedLocales() const
161       { return _requestedLocalesFile.locales(); }
162       /** \overload */
163       static LocaleSet requestedLocales( const Pathname & root_r );
164
165       /** \copydoc Target::targetDistribution() */
166       std::string targetDistribution() const;
167       /** \overload */
168       static std::string targetDistribution( const Pathname & root_r );
169
170       /** \copydoc Target::targetDistributionRelease()*/
171       std::string targetDistributionRelease() const;
172       /** \overload */
173       static std::string targetDistributionRelease( const Pathname & root_r );
174
175       /** \copydoc Target::targetDistributionFlavor()*/
176       std::string targetDistributionFlavor() const;
177       /** \overload */
178       static std::string targetDistributionFlavor( const Pathname & root_r );
179
180       /** \copydoc Target::distributionLabel()*/
181       Target::DistributionLabel distributionLabel() const;
182       /** \overload */
183       static Target::DistributionLabel distributionLabel( const Pathname & root_r );
184
185       /** \copydoc Target::distributionVersion()*/
186       std::string distributionVersion() const;
187       /** \overload */
188       static std::string distributionVersion( const Pathname & root_r );
189
190       /** \copydoc Target::distributionFlavor() */
191       std::string distributionFlavor() const;
192       /** \overload */
193       static std::string distributionFlavor( const Pathname & root_r );
194
195       /** \copydoc Target::anonymousUniqueId() */
196       std::string anonymousUniqueId() const;
197       /** \overload */
198       static std::string anonymousUniqueId( const Pathname & root_r );
199
200     private:
201       /** Commit ordered changes (internal helper) */
202       void commit( const ZYppCommitPolicy & policy_r,
203                    CommitPackageCache & packageCache_r,
204                    ZYppCommitResult & result_r );
205
206       /** Commit helper checking for file conflicts after download. */
207       void commitFindFileConflicts( const ZYppCommitPolicy & policy_r, ZYppCommitResult & result_r );
208     protected:
209       /** Path to the target */
210       Pathname _root;
211       /** RPM database */
212       rpm::RpmDb _rpm;
213       /** Requested Locales database */
214       RequestedLocalesFile _requestedLocalesFile;
215       /** user/auto installed database */
216       SolvIdentFile _autoInstalledFile;
217       /** Hard-Locks database */
218       HardLocksFile _hardLocksFile;
219       /** Cache distributionVersion */
220       mutable std::string _distributionVersion;
221     };
222     ///////////////////////////////////////////////////////////////////
223
224     /** \relates TargetImpl Stream output */
225     inline std::ostream & operator<<( std::ostream & str, const TargetImpl & obj )
226     {
227       return obj.dumpOn( str );
228     }
229
230     /////////////////////////////////////////////////////////////////
231   } // namespace target
232   ///////////////////////////////////////////////////////////////////
233   /////////////////////////////////////////////////////////////////
234 } // namespace zypp
235 ///////////////////////////////////////////////////////////////////
236 #endif // ZYPP_TARGET_TARGETIMPL_H