985d42957b5055cc5e691b50b52238c3f6a62a96
[platform/upstream/libzypp.git] / zypp / zypp_detail / ZYppImpl.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/zypp_detail/ZYppImpl.cc
10  *
11 */
12
13 #include <iostream>
14 #include "zypp/TmpPath.h"
15 #include "zypp/base/Logger.h"
16 #include "zypp/base/String.h"
17
18 #include "zypp/zypp_detail/ZYppImpl.h"
19 #include "zypp/solver/detail/Helper.h"
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
31 ///////////////////////////////////////////////////////////////////
32 namespace zypp
33 { /////////////////////////////////////////////////////////////////
34
35   ///////////////////////////////////////////////////////////////////
36   namespace media
37   {
38     ScopedDisableMediaChangeReport::ScopedDisableMediaChangeReport( bool condition_r )
39     {
40       static weak_ptr<callback::TempConnect<media::MediaChangeReport> > globalguard;
41       if ( condition_r && ! (_guard = globalguard.lock()) )
42       {
43         // aquire a new one....
44         _guard.reset( new callback::TempConnect<media::MediaChangeReport>() );
45         globalguard = _guard;
46       }
47     }
48   } // namespace media
49   ///////////////////////////////////////////////////////////////////
50
51   callback::SendReport<JobReport> & JobReport::instance()
52   {
53     static callback::SendReport<JobReport> _report;
54     return _report;
55   }
56
57
58   ///////////////////////////////////////////////////////////////////
59   namespace zypp_detail
60   { /////////////////////////////////////////////////////////////////
61
62     ///////////////////////////////////////////////////////////////////
63     //
64     //  METHOD NAME : ZYppImpl::ZYppImpl
65     //  METHOD TYPE : Constructor
66     //
67     ZYppImpl::ZYppImpl()
68     : _target(0)
69     , _resolver( new Resolver( ResPool::instance()) )
70     {
71       ZConfig::instance().about( MIL );
72       MIL << "Initializing keyring..." << std::endl;
73       _keyring = new KeyRing(tmpPath());
74     }
75
76     ///////////////////////////////////////////////////////////////////
77     //
78     //  METHOD NAME : ZYppImpl::~ZYppImpl
79     //  METHOD TYPE : Destructor
80     //
81     ZYppImpl::~ZYppImpl()
82     {}
83
84     //------------------------------------------------------------------------
85     // add/remove resolvables
86
87     DiskUsageCounter::MountPointSet ZYppImpl::diskUsage()
88     {
89       if ( ! _disk_usage )
90       {
91         setPartitions( DiskUsageCounter::detectMountPoints() );
92       }
93       return _disk_usage->disk_usage(pool());
94     }
95
96     void ZYppImpl::setPartitions(const DiskUsageCounter::MountPointSet &mp)
97     {
98       _disk_usage.reset(new DiskUsageCounter());
99       _disk_usage->setMountPoints(mp);
100     }
101
102     DiskUsageCounter::MountPointSet ZYppImpl::getPartitions() const
103     {
104       if (_disk_usage)
105         return _disk_usage->getMountPoints();
106       else
107         return DiskUsageCounter::detectMountPoints();
108     }
109
110     //------------------------------------------------------------------------
111     // target
112
113     Target_Ptr ZYppImpl::target() const
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
136     void ZYppImpl::finishTarget()
137     {
138       if (_target)
139           _target->unload();
140
141       _target = 0;
142     }
143
144     //------------------------------------------------------------------------
145     // commit
146
147     /** \todo Remove workflow from target, lot's of it could be done here,
148      * and target used for transact. */
149     ZYppCommitResult ZYppImpl::commit( const ZYppCommitPolicy & policy_r )
150     {
151       setenv( "ZYPP_IS_RUNNING", str::numstring(getpid()).c_str(), 1 );
152
153       if ( getenv("ZYPP_TESTSUITE_FAKE_ARCH") )
154       {
155         ZYPP_THROW( Exception("ZYPP_TESTSUITE_FAKE_ARCH set. Commit not allowed and disabled.") );
156       }
157
158       MIL << "Attempt to commit (" << policy_r << ")" << endl;
159       if (! _target)
160         ZYPP_THROW( Exception("Target not initialized.") );
161
162       ZYppCommitResult res = _target->_pimpl->commit( pool(), policy_r );
163
164       if (! policy_r.dryRun() )
165       {
166         if ( policy_r.syncPoolAfterCommit() )
167           {
168             // reload new status from target
169             DBG << "reloading " << sat::Pool::instance().systemRepoAlias() << " repo to pool" << endl;
170             _target->load();
171           }
172         else
173           {
174             DBG << "unloading " << sat::Pool::instance().systemRepoAlias() << " repo from pool" << endl;
175             _target->unload();
176           }
177       }
178
179       MIL << "Commit (" << policy_r << ") returned: "
180           << res << endl;
181       return res;
182     }
183
184     void ZYppImpl::installSrcPackage( const SrcPackage_constPtr & srcPackage_r )
185     {
186       if (! _target)
187         ZYPP_THROW( Exception("Target not initialized.") );
188       _target->_pimpl->installSrcPackage( srcPackage_r );
189     }
190
191     ManagedFile ZYppImpl::provideSrcPackage( const SrcPackage_constPtr & srcPackage_r )
192     {
193       if (! _target)
194         ZYPP_THROW( Exception("Target not initialized.") );
195       return _target->_pimpl->provideSrcPackage( srcPackage_r );
196     }
197
198     //------------------------------------------------------------------------
199     // target store path
200
201     Pathname ZYppImpl::homePath() const
202     { return _home_path.empty() ? Pathname("/var/lib/zypp") : _home_path; }
203
204     void ZYppImpl::setHomePath( const Pathname & path )
205     { _home_path = path; }
206
207     Pathname ZYppImpl::tmpPath() const
208     {
209       static TmpDir zypp_tmp_dir( TmpPath::defaultLocation(), "zypp." );
210       return zypp_tmp_dir.path();
211     }
212
213     /******************************************************************
214      **
215      ** FUNCTION NAME : operator<<
216      ** FUNCTION TYPE : std::ostream &
217     */
218     std::ostream & operator<<( std::ostream & str, const ZYppImpl & obj )
219     {
220       return str << "ZYppImpl";
221     }
222
223     /////////////////////////////////////////////////////////////////
224   } // namespace zypp_detail
225   ///////////////////////////////////////////////////////////////////
226   /////////////////////////////////////////////////////////////////
227 } // namespace zypp
228 ///////////////////////////////////////////////////////////////////