log libzypp version in ZYppImpl ctor.
[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/NameKindProxy.h"
24 #include "zypp/ZConfig.h"
25 #include "zypp/sat/Pool.h"
26 #include "zypp/PoolItem.h"
27
28 using std::endl;
29
30 ///////////////////////////////////////////////////////////////////
31 namespace zypp
32 { /////////////////////////////////////////////////////////////////
33   ///////////////////////////////////////////////////////////////////
34   namespace zypp_detail
35   { /////////////////////////////////////////////////////////////////
36
37     ///////////////////////////////////////////////////////////////////
38     //
39     //  METHOD NAME : ZYppImpl::ZYppImpl
40     //  METHOD TYPE : Constructor
41     //
42     ZYppImpl::ZYppImpl()
43     : _target(0)
44     , _resolver( new Resolver( ResPool::instance()) )
45     {
46       MIL << "libzypp: " << VERSION << " built " << __DATE__ << " " <<  __TIME__ << endl;
47       MIL << "defaultTextLocale: '" << ZConfig::instance().textLocale() << "'" << endl;
48       MIL << "System architecture is '" << ZConfig::instance().systemArchitecture() << "'" << endl;
49       MIL << "Initializing keyring..." << std::endl;
50       _keyring = new KeyRing(tmpPath());
51     }
52
53     ///////////////////////////////////////////////////////////////////
54     //
55     //  METHOD NAME : ZYppImpl::~ZYppImpl
56     //  METHOD TYPE : Destructor
57     //
58     ZYppImpl::~ZYppImpl()
59     {}
60
61     //------------------------------------------------------------------------
62     // add/remove resolvables
63
64     DiskUsageCounter::MountPointSet ZYppImpl::diskUsage()
65     {
66       if ( ! _disk_usage )
67       {
68         setPartitions( DiskUsageCounter::detectMountPoints() );
69       }
70       return _disk_usage->disk_usage(pool());
71     }
72
73     void ZYppImpl::setPartitions(const DiskUsageCounter::MountPointSet &mp)
74     {
75       _disk_usage.reset(new DiskUsageCounter());
76       _disk_usage->setMountPoints(mp);
77     }
78
79     DiskUsageCounter::MountPointSet ZYppImpl::getPartitions() const
80     {
81       if (_disk_usage)
82         return _disk_usage->getMountPoints();
83       else
84         return DiskUsageCounter::detectMountPoints();
85     }
86
87     //------------------------------------------------------------------------
88     // target
89
90     Target_Ptr ZYppImpl::target() const
91     {
92       if (! _target)
93         ZYPP_THROW(Exception("Target not initialized."));
94       return _target;
95      }
96
97     void ZYppImpl::initializeTarget( const Pathname & root, bool doRebuild_r )
98     {
99       MIL << "initTarget( " << root << (doRebuild_r?", rebuilddb":"") << ")" << endl;
100       if (_target) {
101           if (_target->root() == root) {
102               MIL << "Repeated call to initializeTarget()" << endl;
103               return;
104           }
105
106           _target->unload();
107
108       }
109       _target = new Target( root, doRebuild_r );
110       _target->buildCache();
111     }
112
113     void ZYppImpl::finishTarget()
114     {
115       if (_target)
116           _target->unload();
117
118       _target = 0;
119     }
120
121     //------------------------------------------------------------------------
122     // commit
123
124     /** \todo Remove workflow from target, lot's of it could be done here,
125      * and target used for transact. */
126     ZYppCommitResult ZYppImpl::commit( const ZYppCommitPolicy & policy_r )
127     {
128       if ( getenv("ZYPP_TESTSUITE_FAKE_ARCH") )
129       {
130         ZYPP_THROW( Exception("ZYPP_TESTSUITE_FAKE_ARCH set. Commit not allowed and disabled.") );
131       }
132
133       MIL << "Attempt to commit (" << policy_r << ")" << endl;
134       if (! _target)
135         ZYPP_THROW( Exception("Target not initialized.") );
136
137       ZYppCommitResult res = _target->_pimpl->commit( pool(), policy_r );
138
139       if (! policy_r.dryRun() ) {
140
141           DBG << "unloading " << sat::Pool::instance().systemRepoAlias() << " repo from pool" << endl;
142
143         _target->unload();
144
145         if ( policy_r.syncPoolAfterCommit() )
146           {
147             // reload new status from target
148             DBG << "reloading " << sat::Pool::instance().systemRepoAlias() << " repo to pool" << endl;
149             _target->load();
150           }
151       }
152
153       MIL << "Commit (" << policy_r << ") returned: "
154           << res << endl;
155       return res;
156     }
157
158     void ZYppImpl::installSrcPackage( const SrcPackage_constPtr & srcPackage_r )
159     {
160       if (! _target)
161         ZYPP_THROW( Exception("Target not initialized.") );
162       _target->_pimpl->installSrcPackage( srcPackage_r );
163     }
164
165     //------------------------------------------------------------------------
166     // target store path
167
168     Pathname ZYppImpl::homePath() const
169     { return _home_path.empty() ? Pathname("/var/lib/zypp") : _home_path; }
170
171     void ZYppImpl::setHomePath( const Pathname & path )
172     { _home_path = path; }
173
174     Pathname ZYppImpl::tmpPath() const
175     {
176       static TmpDir zypp_tmp_dir( TmpPath::defaultLocation(), "zypp." );
177       return zypp_tmp_dir.path();
178     }
179
180     /******************************************************************
181      **
182      ** FUNCTION NAME : operator<<
183      ** FUNCTION TYPE : std::ostream &
184     */
185     std::ostream & operator<<( std::ostream & str, const ZYppImpl & obj )
186     {
187       return str << "ZYppImpl";
188     }
189
190     /////////////////////////////////////////////////////////////////
191   } // namespace zypp_detail
192   ///////////////////////////////////////////////////////////////////
193   /////////////////////////////////////////////////////////////////
194 } // namespace zypp
195 ///////////////////////////////////////////////////////////////////