non-root use temporary @System solvfile if global one needed refresh.
[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/PoolItem.h"
24 #include "zypp/ZYppCommit.h"
25
26 #include "zypp/Pathname.h"
27 #include "zypp/media/MediaAccess.h"
28 #include "zypp/Target.h"
29 #include "zypp/target/rpm/RpmDb.h"
30 #include "zypp/target/TargetException.h"
31 #include "zypp/target/RequestedLocalesFile.h"
32 #include "zypp/target/SoftLocksFile.h"
33 #include "zypp/target/HardLocksFile.h"
34
35 ///////////////////////////////////////////////////////////////////
36 namespace zypp
37 { /////////////////////////////////////////////////////////////////
38   ///////////////////////////////////////////////////////////////////
39   namespace target
40   { /////////////////////////////////////////////////////////////////
41
42     DEFINE_PTR_TYPE(TargetImpl);
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();
106
107       void unload();
108
109       void clearCache();
110
111       void buildCache();
112       //@}
113
114       std::string anonymousUniqueId() const;
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 _root / "/var/lib/zypp"; }
125
126       /** Commit changes in the pool */
127       ZYppCommitResult commit( ResPool pool_r, const ZYppCommitPolicy & policy_r );
128
129       ZYPP_DEPRECATED int commit( ResPool pool_r, unsigned int medianr,
130                                   PoolItemList & errors_r,
131                                   PoolItemList & remaining_r,
132                                   PoolItemList & srcremaining_r,
133                                   bool dry_run = false )
134       {
135         ZYppCommitPolicy policy;
136         policy.restrictToMedia( medianr ).dryRun( dry_run );
137         ZYppCommitResult res = commit( pool_r, policy );
138         errors_r.swap( res._errors );
139         remaining_r.swap( res._remaining );
140         srcremaining_r.swap( res._srcremaining );
141         return res._result;
142       }
143
144       /** Commit ordered changes
145        *  @param pool_r only needed for #160792
146        *  @return uncommitted ones (due to error)
147        */
148       PoolItemList commit( const PoolItemList & items_r, const ZYppCommitPolicy & policy_r, const ResPool & pool_r );
149
150       /** Install a source package on the Target. */
151       void installSrcPackage( const SrcPackage_constPtr & srcPackage_r );
152
153       /** Overload to realize stream output. */
154       virtual std::ostream & dumpOn( std::ostream & str ) const
155       {
156         return str << "TargetImpl";
157       }
158
159       /** The RPM database */
160       rpm::RpmDb & rpm();
161
162       /** If the package is installed and provides the file
163       Needed to evaluate split provides during Resolver::Upgrade() */
164       bool providesFile (const std::string & path_str, const std::string & name_str) const;
165
166       /** Return name of package owning \a path_str
167        * or empty string if no installed package owns \a path_str. */
168       std::string whoOwnsFile (const std::string & path_str) const
169       { return _rpm.whoOwnsFile (path_str); }
170
171       /** return the last modification date of the target */
172       Date timestamp() const;
173
174       /** \copydoc Target::baseProduct() */
175       Product::constPtr baseProduct() const;
176
177       /** \copydoc Target::release() */
178       std::string release() const;
179
180       /** \copydoc Target::targetDistribution() */
181       std::string targetDistribution() const;
182
183       /** \copydoc Target::targetDistributionRelease()*/
184       std::string targetDistributionRelease() const;
185
186       /** \copydoc Target::distributionVersion()*/
187       std::string distributionVersion() const;
188
189       /** \copydoc Target::distributionFlavor() */
190       std::string distributionFlavor() const;
191
192     protected:
193       /** Path to the target */
194       Pathname _root;
195       /** RPM database */
196       rpm::RpmDb _rpm;
197       /** Requested Locales database */
198       RequestedLocalesFile _requestedLocalesFile;
199       /** Soft-locks database */
200       SoftLocksFile _softLocksFile;
201       /** Hard-Locks database */
202       HardLocksFile _hardLocksFile;
203       /** Cache distributionVersion */
204       mutable std::string _distributionVersion;
205     private:
206       /** Null implementation */
207       static TargetImpl_Ptr _nullimpl;
208     };
209     ///////////////////////////////////////////////////////////////////
210
211     /** \relates TargetImpl Stream output */
212     inline std::ostream & operator<<( std::ostream & str, const TargetImpl & obj )
213     {
214       return obj.dumpOn( str );
215     }
216
217     /////////////////////////////////////////////////////////////////
218   } // namespace target
219   ///////////////////////////////////////////////////////////////////
220   /////////////////////////////////////////////////////////////////
221 } // namespace zypp
222 ///////////////////////////////////////////////////////////////////
223 #endif // ZYPP_TARGET_TARGETIMPL_H