libzypp 17.31.23
ZYppImpl.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
13#include <iostream>
14#include <zypp/TmpPath.h>
15#include <zypp/base/Logger.h>
16#include <zypp/base/String.h>
17#include <zypp/base/Env.h>
18
21#include <zypp/ZYpp.h>
23#include <zypp/ZConfig.h>
24#include <zypp/sat/Pool.h>
25#include <zypp/PoolItem.h>
26
27#include <zypp/ZYppCallbacks.h> // JobReport::instance
28
29using std::endl;
30
32namespace zypp
33{
34
36 namespace media
37 {
39 {
40 static weak_ptr<callback::TempConnect<media::MediaChangeReport> > globalguard;
41 if ( condition_r && ! (_guard = globalguard.lock()) )
42 {
43 // aquire a new one....
45 globalguard = _guard;
46 }
47 }
48 } // namespace media
50
52 {
54 return _report;
55 }
56
57
59 namespace zypp_detail
60 {
61
63 //
64 // METHOD NAME : ZYppImpl::ZYppImpl
65 // METHOD TYPE : Constructor
66 //
68 : _target( nullptr )
69 , _resolver( new Resolver( ResPool::instance()) )
70 {
72 MIL << "Initializing keyring..." << std::endl;
73 _keyring = new KeyRing(tmpPath());
74 _keyring->allowPreload( true );
75 }
76
78 //
79 // METHOD NAME : ZYppImpl::~ZYppImpl
80 // METHOD TYPE : Destructor
81 //
83 {}
84
85 //------------------------------------------------------------------------
86 // add/remove resolvables
87
89 {
90 if ( ! _disk_usage )
91 {
93 }
94 return _disk_usage->disk_usage(pool());
95 }
96
98 {
99 _disk_usage.reset(new DiskUsageCounter());
100 _disk_usage->setMountPoints(mp);
101 }
102
104 {
105 if (_disk_usage)
106 return _disk_usage->getMountPoints();
107 else
109 }
110
111 //------------------------------------------------------------------------
112 // target
113
115 {
116 if (! _target)
117 ZYPP_THROW(Exception("Target not initialized."));
118 return _target;
119 }
120
122 {
123 if ( _target && newtarget_r ) // bsc#1203760: Make sure the old target is deleted before a new one is created!
124 INT << "2 active targets at the same time must not happen!" << endl;
125 _target = newtarget_r;
127 resolver()->setDefaultSolverFlags( /*all_r*/false ); // just changed defaults
128 }
129
130 void ZYppImpl::initializeTarget( const Pathname & root, bool doRebuild_r )
131 {
132 MIL << "initTarget( " << root << (doRebuild_r?", rebuilddb":"") << ")" << endl;
133 if (_target) {
134 if (_target->root() == root) {
135 MIL << "Repeated call to initializeTarget()" << endl;
136 return;
137 }
138 _target->unload();
139 _target = nullptr; // bsc#1203760: Make sure the old target is deleted before a new one is created!
140 }
141 changeTargetTo( new Target( root, doRebuild_r ) );
142 _target->buildCache();
143 }
144
146 {
147 if (_target)
148 _target->unload();
149
150 changeTargetTo( nullptr );
151 }
152
153 //------------------------------------------------------------------------
154 // commit
155
159 {
160 if ( getenv("ZYPP_TESTSUITE_FAKE_ARCH") )
161 {
162 ZYPP_THROW( Exception("ZYPP_TESTSUITE_FAKE_ARCH set. Commit not allowed and disabled.") );
163 }
164
165 MIL << "Attempt to commit (" << policy_r << ")" << endl;
166 if (! _target)
167 ZYPP_THROW( Exception("Target not initialized.") );
168
169
170 env::ScopedSet ea { "ZYPP_IS_RUNNING", str::numstring(getpid()).c_str() };
172 if ( _target->chrooted() )
173 eb = env::ScopedSet( "SYSTEMD_OFFLINE", "1" ); // bsc#1118758 - indicate no systemd if chrooted install
174
175 ZYppCommitResult res = _target->_pimpl->commit( pool(), policy_r );
176
177 if (! policy_r.dryRun() )
178 {
179 if ( policy_r.syncPoolAfterCommit() )
180 {
181 // reload new status from target
182 DBG << "reloading " << sat::Pool::instance().systemRepoAlias() << " repo to pool" << endl;
183 _target->load();
184 }
185 else
186 {
187 DBG << "unloading " << sat::Pool::instance().systemRepoAlias() << " repo from pool" << endl;
188 _target->unload();
189 }
190 }
191
192 MIL << "Commit (" << policy_r << ") returned: "
193 << res << endl;
194 return res;
195 }
196
197 void ZYppImpl::installSrcPackage( const SrcPackage_constPtr & srcPackage_r )
198 {
199 if (! _target)
200 ZYPP_THROW( Exception("Target not initialized.") );
201 _target->_pimpl->installSrcPackage( srcPackage_r );
202 }
203
204 ManagedFile ZYppImpl::provideSrcPackage( const SrcPackage_constPtr & srcPackage_r )
205 {
206 if (! _target)
207 ZYPP_THROW( Exception("Target not initialized.") );
208 return _target->_pimpl->provideSrcPackage( srcPackage_r );
209 }
210
211 //------------------------------------------------------------------------
212 // target store path
213
215 { return _home_path.empty() ? Pathname("/var/lib/zypp") : _home_path; }
216
217 void ZYppImpl::setHomePath( const Pathname & path )
218 { _home_path = path; }
219
221 { return zypp::myTmpDir(); }
222
223 /******************************************************************
224 **
225 ** FUNCTION NAME : operator<<
226 ** FUNCTION TYPE : std::ostream &
227 */
228 std::ostream & operator<<( std::ostream & str, const ZYppImpl & obj )
229 {
230 return str << "ZYppImpl";
231 }
232
234 } // namespace zypp_detail
236
237 Pathname myTmpDir() // from TmpPath.h
238 {
239 static filesystem::TmpDir _tmpdir( filesystem::TmpPath::defaultLocation(), "zypp." );
240 return _tmpdir.path();
241 }
242
244} // namespace zypp
Compute disk space occupied by packages across partitions/directories.
std::set< MountPoint > MountPointSet
static MountPointSet detectMountPoints(const std::string &rootdir="/")
Get mountpoints of system below rootdir If we happen to detect snapshotting btrfs partitions,...
Base class for Exception.
Definition: Exception.h:146
Gpg key handling.
Definition: KeyRing.h:187
Global ResObject pool.
Definition: ResPool.h:61
Dependency resolver interface.
Definition: Resolver.h:45
void notifyTargetChanged()
internal
Definition: ZConfig.cc:947
static ZConfig & instance()
Singleton ctor.
Definition: ZConfig.cc:922
std::ostream & about(std::ostream &str) const
Print some detail about the current libzypp version.
Definition: ZConfig.cc:1331
Options and policies for ZYpp::commit.
ZYppCommitPolicy & syncPoolAfterCommit(bool yesNo_r)
Kepp pool in sync with the Target databases after commit (default: true)
ZYppCommitPolicy & dryRun(bool yesNo_r)
Set dry run (default: false).
Result returned from ZYpp::commit.
bool empty() const
Test for an empty path.
Definition: Pathname.h:114
Provide a new empty temporary directory and recursively delete it when no longer needed.
Definition: TmpPath.h:178
static const Pathname & defaultLocation()
Definition: TmpPath.cc:157
Pathname path() const
Definition: TmpPath.cc:146
static Pool instance()
Singleton ctor.
Definition: Pool.h:55
static const std::string & systemRepoAlias()
Reserved system repository alias @System .
Definition: Pool.cc:46
DiskUsageCounter::MountPointSet getPartitions() const
Definition: ZYppImpl.cc:103
ResPool pool() const
Definition: ZYppImpl.h:50
ZYppImpl()
Default ctor.
Definition: ZYppImpl.cc:67
void setPartitions(const DiskUsageCounter::MountPointSet &mp)
Definition: ZYppImpl.cc:97
Target_Ptr target() const
Definition: ZYppImpl.cc:114
void setHomePath(const Pathname &path)
set the home, if you need to change it
Definition: ZYppImpl.cc:217
Resolver_Ptr resolver() const
Definition: ZYppImpl.h:61
Pathname tmpPath() const
Get the path where zypp related plugins store tmp data
Definition: ZYppImpl.cc:220
ZYppCommitResult commit(const ZYppCommitPolicy &policy_r)
Commit changes and transactions.
Definition: ZYppImpl.cc:158
void changeTargetTo(Target_Ptr newtarget_r)
Hook for actions to trigger if the Target changes (initialize/finish)
Definition: ZYppImpl.cc:121
void installSrcPackage(const SrcPackage_constPtr &srcPackage_r)
Install a source package on the Target.
Definition: ZYppImpl.cc:197
shared_ptr< DiskUsageCounter > _disk_usage
defined mount points, used for disk usage counting
Definition: ZYppImpl.h:126
ManagedFile provideSrcPackage(const SrcPackage_constPtr &srcPackage_r)
Install a source package on the Target.
Definition: ZYppImpl.cc:204
Pathname homePath() const
Get the path where zypp related plugins store persistent data and caches
Definition: ZYppImpl.cc:214
DiskUsageCounter::MountPointSet diskUsage()
Definition: ZYppImpl.cc:88
void initializeTarget(const Pathname &root, bool doRebuild_r)
Definition: ZYppImpl.cc:130
String related utilities and Regular expression matching.
std::string numstring(char n, int w=0)
Definition: String.h:289
std::ostream & operator<<(std::ostream &str, const ZYppImpl &obj)
Definition: ZYppImpl.cc:228
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
Pathname myTmpDir()
Global access to the zypp.TMPDIR (created on demand, deleted when libzypp is unloaded)
Definition: ZYppImpl.cc:237
static callback::SendReport< JobReport > & instance()
Singleton sender instance.
Definition: ZYppImpl.cc:51
Temporarily connect a ReceiveReport then restore the previous one.
Definition: Callback.h:285
Temporarily set/unset an environment variable.
Definition: Env.h:29
ScopedDisableMediaChangeReport(bool condition_r=true)
Disbale MediaChangeReport if condition_r is true.
Definition: ZYppImpl.cc:38
shared_ptr< callback::TempConnect< media::MediaChangeReport > > _guard
#define nullptr
Definition: Easy.h:55
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:428
#define DBG
Definition: Logger.h:95
#define MIL
Definition: Logger.h:96
#define INT
Definition: Logger.h:100