libzypp  14.29.1
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 
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  {
37  static callback::SendReport<JobReport> _report;
38  return _report;
39  }
40 
42  namespace zypp_detail
43  {
44 
46  //
47  // METHOD NAME : ZYppImpl::ZYppImpl
48  // METHOD TYPE : Constructor
49  //
51  : _target(0)
52  , _resolver( new Resolver( ResPool::instance()) )
53  {
55  MIL << "Initializing keyring..." << std::endl;
56  _keyring = new KeyRing(tmpPath());
57  }
58 
60  //
61  // METHOD NAME : ZYppImpl::~ZYppImpl
62  // METHOD TYPE : Destructor
63  //
65  {}
66 
67  //------------------------------------------------------------------------
68  // add/remove resolvables
69 
71  {
72  if ( ! _disk_usage )
73  {
75  }
76  return _disk_usage->disk_usage(pool());
77  }
78 
80  {
81  _disk_usage.reset(new DiskUsageCounter());
82  _disk_usage->setMountPoints(mp);
83  }
84 
86  {
87  if (_disk_usage)
88  return _disk_usage->getMountPoints();
89  else
91  }
92 
93  //------------------------------------------------------------------------
94  // target
95 
96  Target_Ptr ZYppImpl::target() const
97  {
98  if (! _target)
99  ZYPP_THROW(Exception("Target not initialized."));
100  return _target;
101  }
102 
103  void ZYppImpl::initializeTarget( const Pathname & root, bool doRebuild_r )
104  {
105  MIL << "initTarget( " << root << (doRebuild_r?", rebuilddb":"") << ")" << endl;
106  if (_target) {
107  if (_target->root() == root) {
108  MIL << "Repeated call to initializeTarget()" << endl;
109  return;
110  }
111 
112  _target->unload();
113 
114  }
115  _target = new Target( root, doRebuild_r );
116  _target->buildCache();
117  }
118 
120  {
121  if (_target)
122  _target->unload();
123 
124  _target = 0;
125  }
126 
127  //------------------------------------------------------------------------
128  // commit
129 
133  {
134  setenv( "ZYPP_IS_RUNNING", str::numstring(getpid()).c_str(), 1 );
135 
136  if ( getenv("ZYPP_TESTSUITE_FAKE_ARCH") )
137  {
138  ZYPP_THROW( Exception("ZYPP_TESTSUITE_FAKE_ARCH set. Commit not allowed and disabled.") );
139  }
140 
141  MIL << "Attempt to commit (" << policy_r << ")" << endl;
142  if (! _target)
143  ZYPP_THROW( Exception("Target not initialized.") );
144 
145  ZYppCommitResult res = _target->_pimpl->commit( pool(), policy_r );
146 
147  if (! policy_r.dryRun() )
148  {
149  if ( policy_r.syncPoolAfterCommit() )
150  {
151  // reload new status from target
152  DBG << "reloading " << sat::Pool::instance().systemRepoAlias() << " repo to pool" << endl;
153  _target->load();
154  }
155  else
156  {
157  DBG << "unloading " << sat::Pool::instance().systemRepoAlias() << " repo from pool" << endl;
158  _target->unload();
159  }
160  }
161 
162  MIL << "Commit (" << policy_r << ") returned: "
163  << res << endl;
164  return res;
165  }
166 
167  void ZYppImpl::installSrcPackage( const SrcPackage_constPtr & srcPackage_r )
168  {
169  if (! _target)
170  ZYPP_THROW( Exception("Target not initialized.") );
171  _target->_pimpl->installSrcPackage( srcPackage_r );
172  }
173 
174  ManagedFile ZYppImpl::provideSrcPackage( const SrcPackage_constPtr & srcPackage_r )
175  {
176  if (! _target)
177  ZYPP_THROW( Exception("Target not initialized.") );
178  return _target->_pimpl->provideSrcPackage( srcPackage_r );
179  }
180 
181  //------------------------------------------------------------------------
182  // target store path
183 
184  Pathname ZYppImpl::homePath() const
185  { return _home_path.empty() ? Pathname("/var/lib/zypp") : _home_path; }
186 
187  void ZYppImpl::setHomePath( const Pathname & path )
188  { _home_path = path; }
189 
190  Pathname ZYppImpl::tmpPath() const
191  {
192  static TmpDir zypp_tmp_dir( TmpPath::defaultLocation(), "zypp." );
193  return zypp_tmp_dir.path();
194  }
195 
196  /******************************************************************
197  **
198  ** FUNCTION NAME : operator<<
199  ** FUNCTION TYPE : std::ostream &
200  */
201  std::ostream & operator<<( std::ostream & str, const ZYppImpl & obj )
202  {
203  return str << "ZYppImpl";
204  }
205 
207  } // namespace zypp_detail
210 } // namespace zypp
#define MIL
Definition: Logger.h:47
void setHomePath(const Pathname &path)
set the home, if you need to change it
Definition: ZYppImpl.cc:187
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:320
Result returned from ZYpp::commit.
static ZConfig & instance()
Singleton ctor.
Definition: ZConfig.cc:655
Pathname path() const
Definition: TmpPath.cc:146
std::ostream & about(std::ostream &str) const
Print some detail about the current libzypp version.
Definition: ZConfig.cc:961
Gpg key handling.
Definition: KeyRing.h:135
Pathname tmpPath() const
Get the path where zypp related plugins store tmp data.
Definition: ZYppImpl.cc:190
ZYppCommitPolicy & dryRun(bool yesNo_r)
Set dry run (default: false).
Target_Ptr target() const
Definition: ZYppImpl.cc:96
String related utilities and Regular expression matching.
DiskUsageCounter::MountPointSet diskUsage()
Definition: ZYppImpl.cc:70
void initializeTarget(const Pathname &root, bool doRebuild_r)
Definition: ZYppImpl.cc:103
std::set< MountPoint > MountPointSet
shared_ptr< DiskUsageCounter > _disk_usage
defined mount points, used for disk usage counting
Definition: ZYppImpl.h:124
ZYppCommitResult commit(const ZYppCommitPolicy &policy_r)
Commit changes and transactions.
Definition: ZYppImpl.cc:132
static Pool instance()
Singleton ctor.
Definition: Pool.h:52
Dependency resolver interface.
Definition: Resolver.h:47
Provide a new empty temporary directory and recursively delete it when no longer needed.
Definition: TmpPath.h:170
static const std::string & systemRepoAlias()
Reserved system repository alias .
Definition: Pool.cc:33
ZYppImpl()
Default ctor.
Definition: ZYppImpl.cc:50
Options and policies for ZYpp::commit.
void installSrcPackage(const SrcPackage_constPtr &srcPackage_r)
Install a source package on the Target.
Definition: ZYppImpl.cc:167
ZYppCommitPolicy & syncPoolAfterCommit(bool yesNo_r)
Kepp pool in sync with the Target databases after commit (default: true)
std::string numstring(char n, int w=0)
Definition: String.h:266
std::ostream & operator<<(std::ostream &str, const ZYppImpl &obj)
Definition: ZYppImpl.cc:201
Global ResObject pool.
Definition: ResPool.h:48
Base class for Exception.
Definition: Exception.h:143
Compute disk space occupied by packages across partitions/directories.
DiskUsageCounter::MountPointSet getPartitions() const
Definition: ZYppImpl.cc:85
Reference counted access to a _Tp object calling a custom Dispose function when the last AutoDispose ...
Definition: AutoDispose.h:92
Pathname homePath() const
Get the path where zypp related plugins store persistent data and caches.
Definition: ZYppImpl.cc:184
ResPool pool() const
Definition: ZYppImpl.h:52
static callback::SendReport< JobReport > & instance()
Singleton sender instance.
Definition: ZYppImpl.cc:35
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
void setPartitions(const DiskUsageCounter::MountPointSet &mp)
Definition: ZYppImpl.cc:79
ManagedFile provideSrcPackage(const SrcPackage_constPtr &srcPackage_r)
Install a source package on the Target.
Definition: ZYppImpl.cc:174
#define DBG
Definition: Logger.h:46
static MountPointSet detectMountPoints(const std::string &rootdir="/")
Get mountpoints of system below rootdir If we happen to detect snapshotting btrfs partitions...