libzypp  17.23.0
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 
20 #include "zypp/target/TargetImpl.h"
21 #include "zypp/ZYpp.h"
22 #include "zypp/DiskUsageCounter.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 
29 using std::endl;
30 
32 namespace 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  {
53  static callback::SendReport<JobReport> _report;
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  }
75 
77  //
78  // METHOD NAME : ZYppImpl::~ZYppImpl
79  // METHOD TYPE : Destructor
80  //
82  {}
83 
84  //------------------------------------------------------------------------
85  // add/remove resolvables
86 
88  {
89  if ( ! _disk_usage )
90  {
92  }
93  return _disk_usage->disk_usage(pool());
94  }
95 
97  {
98  _disk_usage.reset(new DiskUsageCounter());
99  _disk_usage->setMountPoints(mp);
100  }
101 
103  {
104  if (_disk_usage)
105  return _disk_usage->getMountPoints();
106  else
108  }
109 
110  //------------------------------------------------------------------------
111  // target
112 
114  {
115  if (! _target)
116  ZYPP_THROW(Exception("Target not initialized."));
117  return _target;
118  }
119 
120  void ZYppImpl::initializeTarget( const Pathname & root, bool doRebuild_r )
121  {
122  MIL << "initTarget( " << root << (doRebuild_r?", rebuilddb":"") << ")" << endl;
123  if (_target) {
124  if (_target->root() == root) {
125  MIL << "Repeated call to initializeTarget()" << endl;
126  return;
127  }
128 
129  _target->unload();
130 
131  }
132  _target = new Target( root, doRebuild_r );
133  _target->buildCache();
134  }
135 
137  {
138  if (_target)
139  _target->unload();
140 
141  _target = nullptr;
142  }
143 
144  //------------------------------------------------------------------------
145  // commit
146 
150  {
151  if ( getenv("ZYPP_TESTSUITE_FAKE_ARCH") )
152  {
153  ZYPP_THROW( Exception("ZYPP_TESTSUITE_FAKE_ARCH set. Commit not allowed and disabled.") );
154  }
155 
156  MIL << "Attempt to commit (" << policy_r << ")" << endl;
157  if (! _target)
158  ZYPP_THROW( Exception("Target not initialized.") );
159 
160 
161  env::ScopedSet ea { "ZYPP_IS_RUNNING", str::numstring(getpid()).c_str() };
162  env::ScopedSet eb;
163  if ( _target->chrooted() )
164  eb = env::ScopedSet( "SYSTEMD_OFFLINE", "1" ); // bsc#1118758 - indicate no systemd if chrooted install
165 
166  ZYppCommitResult res = _target->_pimpl->commit( pool(), policy_r );
167 
168  if (! policy_r.dryRun() )
169  {
170  if ( policy_r.syncPoolAfterCommit() )
171  {
172  // reload new status from target
173  DBG << "reloading " << sat::Pool::instance().systemRepoAlias() << " repo to pool" << endl;
174  _target->load();
175  }
176  else
177  {
178  DBG << "unloading " << sat::Pool::instance().systemRepoAlias() << " repo from pool" << endl;
179  _target->unload();
180  }
181  }
182 
183  MIL << "Commit (" << policy_r << ") returned: "
184  << res << endl;
185  return res;
186  }
187 
188  void ZYppImpl::installSrcPackage( const SrcPackage_constPtr & srcPackage_r )
189  {
190  if (! _target)
191  ZYPP_THROW( Exception("Target not initialized.") );
192  _target->_pimpl->installSrcPackage( srcPackage_r );
193  }
194 
195  ManagedFile ZYppImpl::provideSrcPackage( const SrcPackage_constPtr & srcPackage_r )
196  {
197  if (! _target)
198  ZYPP_THROW( Exception("Target not initialized.") );
199  return _target->_pimpl->provideSrcPackage( srcPackage_r );
200  }
201 
202  //------------------------------------------------------------------------
203  // target store path
204 
206  { return _home_path.empty() ? Pathname("/var/lib/zypp") : _home_path; }
207 
208  void ZYppImpl::setHomePath( const Pathname & path )
209  { _home_path = path; }
210 
212  { return zypp::myTmpDir(); }
213 
214  /******************************************************************
215  **
216  ** FUNCTION NAME : operator<<
217  ** FUNCTION TYPE : std::ostream &
218  */
219  std::ostream & operator<<( std::ostream & str, const ZYppImpl & obj )
220  {
221  return str << "ZYppImpl";
222  }
223 
225  } // namespace zypp_detail
227 
228  Pathname myTmpDir() // from TmpPath.h
229  {
230  static filesystem::TmpDir _tmpdir( filesystem::TmpPath::defaultLocation(), "zypp." );
231  return _tmpdir.path();
232  }
233 
235 } // namespace zypp
zypp::ZYppCommitPolicy::dryRun
ZYppCommitPolicy & dryRun(bool yesNo_r)
Set dry run (default: false).
Definition: ZYppCommitPolicy.cc:72
zypp::zypp_detail::ZYppImpl::installSrcPackage
void installSrcPackage(const SrcPackage_constPtr &srcPackage_r)
Install a source package on the Target.
Definition: ZYppImpl.cc:188
zypp::sat::Pool::instance
static Pool instance()
Singleton ctor.
Definition: Pool.h:55
zypp::env::ScopedSet
Temporarily set/unset an environment variable.
Definition: Env.h:28
TmpPath.h
zypp::Target
Definition: Target.h:49
zypp::Exception
Base class for Exception.
Definition: Exception.h:145
zypp::filesystem::TmpPath::defaultLocation
static const Pathname & defaultLocation()
Definition: TmpPath.cc:157
zypp::filesystem::TmpPath::path
Pathname path() const
Definition: TmpPath.cc:146
zypp::callback::SendReport
Definition: Callback.h:236
zypp::KeyRing
Gpg key handling.
Definition: KeyRing.h:168
ZConfig.h
zypp::zypp_detail::ZYppImpl::~ZYppImpl
~ZYppImpl()
Dtor.
Definition: ZYppImpl.cc:81
MIL
#define MIL
Definition: Logger.h:79
zypp::sat::Pool::systemRepoAlias
static const std::string & systemRepoAlias()
Reserved system repository alias @System .
Definition: Pool.cc:46
ZYPP_THROW
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:392
zypp::JobReport::instance
static callback::SendReport< JobReport > & instance()
Singleton sender instance.
Definition: ZYppImpl.cc:51
zypp::zypp_detail::ZYppImpl::setPartitions
void setPartitions(const DiskUsageCounter::MountPointSet &mp)
Definition: ZYppImpl.cc:96
ZYpp.h
zypp::zypp_detail::ZYppImpl::homePath
Pathname homePath() const
Get the path where zypp related plugins store persistent data and caches
Definition: ZYppImpl.cc:205
nullptr
#define nullptr
Definition: Easy.h:55
zypp::zypp_detail::ZYppImpl::_keyring
KeyRing_Ptr _keyring
Definition: ZYppImpl.h:118
zypp::zypp_detail::ZYppImpl::provideSrcPackage
ManagedFile provideSrcPackage(const SrcPackage_constPtr &srcPackage_r)
Install a source package on the Target.
Definition: ZYppImpl.cc:195
zypp::zypp_detail::ZYppImpl::target
Target_Ptr target() const
Definition: ZYppImpl.cc:113
zypp::zypp_detail::ZYppImpl::_home_path
Pathname _home_path
Definition: ZYppImpl.h:120
DiskUsageCounter.h
zypp::filesystem::TmpDir
Provide a new empty temporary directory and recursively delete it when no longer needed.
Definition: TmpPath.h:177
zypp::Resolver
Dependency resolver interface.
Definition: Resolver.h:44
Logger.h
zypp::ZConfig::about
std::ostream & about(std::ostream &str) const
Print some detail about the current libzypp version.
Definition: ZConfig.cc:1197
zypp::DiskUsageCounter
Compute disk space occupied by packages across partitions/directories.
Definition: DiskUsageCounter.h:31
ZYppCallbacks.h
zypp::media::ScopedDisableMediaChangeReport::ScopedDisableMediaChangeReport
ScopedDisableMediaChangeReport(bool condition_r=true)
Disbale MediaChangeReport if condition_r is true.
Definition: ZYppImpl.cc:38
zypp::ZConfig::instance
static ZConfig & instance()
Singleton ctor.
Definition: Resolver.cc:124
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
zypp::str::numstring
std::string numstring(char n, int w=0)
Definition: String.h:286
ZYppImpl.h
zypp::zypp_detail::operator<<
std::ostream & operator<<(std::ostream &str, const ZYppImpl &obj)
Definition: ZYppImpl.cc:218
TargetImpl.h
zypp::ZYppCommitPolicy::syncPoolAfterCommit
ZYppCommitPolicy & syncPoolAfterCommit(bool yesNo_r)
Kepp pool in sync with the Target databases after commit (default: true)
Definition: ZYppCommitPolicy.cc:105
zypp::ZYppCommitResult
Result returned from ZYpp::commit.
Definition: ZYppCommitResult.h:62
Pool.h
zypp::zypp_detail::ZYppImpl::_target
Target_Ptr _target
Definition: ZYppImpl.h:114
zypp::DiskUsageCounter::detectMountPoints
static MountPointSet detectMountPoints(const std::string &rootdir="/")
Get mountpoints of system below rootdir If we happen to detect snapshotting btrfs partitions,...
Definition: DiskUsageCounter.cc:129
zypp::myTmpDir
Pathname myTmpDir()
Global access to the zypp.TMPDIR (created on demand, deleted when libzypp is unloaded)
Definition: ZYppImpl.cc:228
zypp::zypp_detail::ZYppImpl::pool
ResPool pool() const
Definition: ZYppImpl.h:50
Env.h
zypp::zypp_detail::ZYppImpl
Definition: ZYppImpl.h:38
zypp::zypp_detail::ZYppImpl::diskUsage
DiskUsageCounter::MountPointSet diskUsage()
Definition: ZYppImpl.cc:87
zypp::ResPool
Global ResObject pool.
Definition: ResPool.h:60
zypp::AutoDispose
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
Definition: AutoDispose.h:92
zypp::DiskUsageCounter::MountPointSet
std::set< MountPoint > MountPointSet
Definition: DiskUsageCounter.h:153
zypp::media::ScopedDisableMediaChangeReport::_guard
shared_ptr< callback::TempConnect< media::MediaChangeReport > > _guard
Definition: ZYppCallbacks.h:350
Target_Ptr
zypp::filesystem::Pathname::empty
bool empty() const
Test for an empty path.
Definition: Pathname.h:114
String.h
zypp::filesystem::Pathname
Pathname.
Definition: Pathname.h:44
zypp::zypp_detail::ZYppImpl::commit
ZYppCommitResult commit(const ZYppCommitPolicy &policy_r)
Commit changes and transactions.
Definition: ZYppImpl.cc:149
zypp::callback::TempConnect
Temporarily connect a ReceiveReport then restore the previous one.
Definition: Callback.h:284
PoolItem.h
DBG
#define DBG
Definition: Logger.h:78
zypp::zypp_detail::ZYppImpl::initializeTarget
void initializeTarget(const Pathname &root, bool doRebuild_r)
Definition: ZYppImpl.cc:120
zypp::zypp_detail::ZYppImpl::_disk_usage
shared_ptr< DiskUsageCounter > _disk_usage
defined mount points, used for disk usage counting
Definition: ZYppImpl.h:122
zypp::zypp_detail::ZYppImpl::tmpPath
Pathname tmpPath() const
Get the path where zypp related plugins store tmp data
Definition: ZYppImpl.cc:211
str
String related utilities and Regular expression matching.
zypp::ZYppCommitPolicy
Options and policies for ZYpp::commit.
Definition: ZYppCommitPolicy.h:33
zypp::zypp_detail::ZYppImpl::finishTarget
void finishTarget()
Definition: ZYppImpl.cc:136
zypp::zypp_detail::ZYppImpl::getPartitions
DiskUsageCounter::MountPointSet getPartitions() const
Definition: ZYppImpl.cc:102
zypp::zypp_detail::ZYppImpl::ZYppImpl
ZYppImpl()
Default ctor.
Definition: ZYppImpl.cc:67
zypp::zypp_detail::ZYppImpl::setHomePath
void setHomePath(const Pathname &path)
set the home, if you need to change it
Definition: ZYppImpl.cc:208