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