Use a signleton JobReport callback sender
[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   callback::SendReport<JobReport> & JobReport::instance()
36   {
37     static callback::SendReport<JobReport> _report;
38     return _report;
39   }
40
41   ///////////////////////////////////////////////////////////////////
42   namespace zypp_detail
43   { /////////////////////////////////////////////////////////////////
44
45     ///////////////////////////////////////////////////////////////////
46     //
47     //  METHOD NAME : ZYppImpl::ZYppImpl
48     //  METHOD TYPE : Constructor
49     //
50     ZYppImpl::ZYppImpl()
51     : _target(0)
52     , _resolver( new Resolver( ResPool::instance()) )
53     {
54       ZConfig::instance().about( MIL );
55       MIL << "Initializing keyring..." << std::endl;
56       _keyring = new KeyRing(tmpPath());
57     }
58
59     ///////////////////////////////////////////////////////////////////
60     //
61     //  METHOD NAME : ZYppImpl::~ZYppImpl
62     //  METHOD TYPE : Destructor
63     //
64     ZYppImpl::~ZYppImpl()
65     {}
66
67     //------------------------------------------------------------------------
68     // add/remove resolvables
69
70     DiskUsageCounter::MountPointSet ZYppImpl::diskUsage()
71     {
72       if ( ! _disk_usage )
73       {
74         setPartitions( DiskUsageCounter::detectMountPoints() );
75       }
76       return _disk_usage->disk_usage(pool());
77     }
78
79     void ZYppImpl::setPartitions(const DiskUsageCounter::MountPointSet &mp)
80     {
81       _disk_usage.reset(new DiskUsageCounter());
82       _disk_usage->setMountPoints(mp);
83     }
84
85     DiskUsageCounter::MountPointSet ZYppImpl::getPartitions() const
86     {
87       if (_disk_usage)
88         return _disk_usage->getMountPoints();
89       else
90         return DiskUsageCounter::detectMountPoints();
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
119     void ZYppImpl::finishTarget()
120     {
121       if (_target)
122           _target->unload();
123
124       _target = 0;
125     }
126
127     //------------------------------------------------------------------------
128     // commit
129
130     /** \todo Remove workflow from target, lot's of it could be done here,
131      * and target used for transact. */
132     ZYppCommitResult ZYppImpl::commit( const ZYppCommitPolicy & policy_r )
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
206     /////////////////////////////////////////////////////////////////
207   } // namespace zypp_detail
208   ///////////////////////////////////////////////////////////////////
209   /////////////////////////////////////////////////////////////////
210 } // namespace zypp
211 ///////////////////////////////////////////////////////////////////