Imported Upstream version 16.3.2
[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/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
30 ///////////////////////////////////////////////////////////////////
31 namespace zypp
32 { /////////////////////////////////////////////////////////////////
33
34   ///////////////////////////////////////////////////////////////////
35   namespace media
36   {
37     ScopedDisableMediaChangeReport::ScopedDisableMediaChangeReport( bool condition_r )
38     {
39       static weak_ptr<callback::TempConnect<media::MediaChangeReport> > globalguard;
40       if ( condition_r && ! (_guard = globalguard.lock()) )
41       {
42         // aquire a new one....
43         _guard.reset( new callback::TempConnect<media::MediaChangeReport>() );
44         globalguard = _guard;
45       }
46     }
47   } // namespace media
48   ///////////////////////////////////////////////////////////////////
49
50   callback::SendReport<JobReport> & JobReport::instance()
51   {
52     static callback::SendReport<JobReport> _report;
53     return _report;
54   }
55
56
57   ///////////////////////////////////////////////////////////////////
58   namespace zypp_detail
59   { /////////////////////////////////////////////////////////////////
60
61     ///////////////////////////////////////////////////////////////////
62     //
63     //  METHOD NAME : ZYppImpl::ZYppImpl
64     //  METHOD TYPE : Constructor
65     //
66     ZYppImpl::ZYppImpl()
67     : _target(0)
68     , _resolver( new Resolver( ResPool::instance()) )
69     {
70       ZConfig::instance().about( MIL );
71       MIL << "Initializing keyring..." << std::endl;
72       _keyring = new KeyRing(tmpPath());
73     }
74
75     ///////////////////////////////////////////////////////////////////
76     //
77     //  METHOD NAME : ZYppImpl::~ZYppImpl
78     //  METHOD TYPE : Destructor
79     //
80     ZYppImpl::~ZYppImpl()
81     {}
82
83     //------------------------------------------------------------------------
84     // add/remove resolvables
85
86     DiskUsageCounter::MountPointSet ZYppImpl::diskUsage()
87     {
88       if ( ! _disk_usage )
89       {
90         setPartitions( DiskUsageCounter::detectMountPoints() );
91       }
92       return _disk_usage->disk_usage(pool());
93     }
94
95     void ZYppImpl::setPartitions(const DiskUsageCounter::MountPointSet &mp)
96     {
97       _disk_usage.reset(new DiskUsageCounter());
98       _disk_usage->setMountPoints(mp);
99     }
100
101     DiskUsageCounter::MountPointSet ZYppImpl::getPartitions() const
102     {
103       if (_disk_usage)
104         return _disk_usage->getMountPoints();
105       else
106         return DiskUsageCounter::detectMountPoints();
107     }
108
109     //------------------------------------------------------------------------
110     // target
111
112     Target_Ptr ZYppImpl::target() const
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
135     void ZYppImpl::finishTarget()
136     {
137       if (_target)
138           _target->unload();
139
140       _target = 0;
141     }
142
143     //------------------------------------------------------------------------
144     // commit
145
146     /** \todo Remove workflow from target, lot's of it could be done here,
147      * and target used for transact. */
148     ZYppCommitResult ZYppImpl::commit( const ZYppCommitPolicy & policy_r )
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
200     Pathname ZYppImpl::homePath() const
201     { return _home_path.empty() ? Pathname("/var/lib/zypp") : _home_path; }
202
203     void ZYppImpl::setHomePath( const Pathname & path )
204     { _home_path = path; }
205
206     Pathname ZYppImpl::tmpPath() const
207     {
208       static TmpDir zypp_tmp_dir( TmpPath::defaultLocation(), "zypp." );
209       return zypp_tmp_dir.path();
210     }
211
212     /******************************************************************
213      **
214      ** FUNCTION NAME : operator<<
215      ** FUNCTION TYPE : std::ostream &
216     */
217     std::ostream & operator<<( std::ostream & str, const ZYppImpl & obj )
218     {
219       return str << "ZYppImpl";
220     }
221
222     /////////////////////////////////////////////////////////////////
223   } // namespace zypp_detail
224   ///////////////////////////////////////////////////////////////////
225   /////////////////////////////////////////////////////////////////
226 } // namespace zypp
227 ///////////////////////////////////////////////////////////////////