merge REFACTORING-10_3 back to trunk
[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 <sys/utsname.h>
14 #include <unistd.h>
15 #include <iostream>
16 #include <fstream>
17 #include "zypp/base/Logger.h"
18 #include "zypp/base/String.h"
19
20 #include "zypp/zypp_detail/ZYppImpl.h"
21 #include "zypp/detail/ResImplTraits.h"
22 #include "zypp/solver/detail/Helper.h"
23 #include "zypp/target/TargetImpl.h"
24 #include "zypp/ZYpp.h"
25 #include "zypp/NVRAD.h"
26 #include "zypp/Language.h"
27 #include "zypp/DiskUsageCounter.h"
28 #include "zypp/NameKindProxy.h"
29
30 using std::endl;
31
32 ///////////////////////////////////////////////////////////////////
33 namespace zypp
34 { /////////////////////////////////////////////////////////////////
35   ///////////////////////////////////////////////////////////////////
36   namespace zypp_detail
37   { /////////////////////////////////////////////////////////////////
38
39     inline Locale defaultTextLocale()
40     {
41       Locale ret( "en" );
42       const char * envlist[] = { "LC_ALL", "LC_CTYPE", "LANG", NULL };
43       for ( const char ** envvar = envlist; *envvar; ++envvar )
44         {
45           const char * envlang = getenv( *envvar );
46           if ( envlang )
47             {
48               std::string envstr( envlang );
49               if ( envstr != "POSIX" && envstr != "C" )
50                 {
51                   Locale lang( envlang );
52                   if ( lang != Locale::noCode )
53                     {
54                       ret = lang;
55                       break;
56                     }
57                 }
58             }
59         }
60       return ret;
61     }
62
63     Arch defaultArchitecture()
64     {
65       Arch architecture;
66
67       // detect the true architecture
68       struct utsname buf;
69       if ( uname( &buf ) < 0 )
70         {
71           ERR << "Can't determine system architecture" << endl;
72         }
73       else
74         {
75           architecture = Arch( buf.machine );
76           DBG << "uname architecture is '" << buf.machine << "'" << endl;
77
78           // some CPUs report i686 but dont implement cx8 and cmov
79           // check for both flags in /proc/cpuinfo and downgrade
80           // to i586 if either is missing (cf bug #18885)
81
82           if ( architecture == Arch_i686 )
83             {
84               std::ifstream cpuinfo( "/proc/cpuinfo" );
85               if ( !cpuinfo )
86                 {
87                   ERR << "Cant open /proc/cpuinfo" << endl;
88                 }
89               else
90                 {
91                   char infoline[1024];
92                   while ( cpuinfo.good() )
93                     {
94                       if ( !cpuinfo.getline( infoline, 1024, '\n' ) )
95                         {
96                           if ( cpuinfo.eof() )
97                             break;
98                         }
99                       if ( strncmp( infoline, "flags", 5 ) == 0 )
100                         {
101                           std::string flagsline( infoline );
102                           if ( flagsline.find( "cx8" ) == std::string::npos
103                                || flagsline.find( "cmov" ) == std::string::npos )
104                             {
105                               architecture = Arch_i586;
106                               DBG << "CPU lacks 'cx8' or 'cmov': architecture downgraded to '" << architecture << "'" << endl;
107                             }
108                           break;
109                         } // flags found
110                     } // read proc/cpuinfo
111                 } // proc/cpuinfo opened
112             } // i686 extra flags check
113         }
114
115       if ( getenv( "ZYPP_TESTSUITE_FAKE_ARCH" ) )
116       {
117         architecture = Arch( getenv( "ZYPP_TESTSUITE_FAKE_ARCH" ) );
118         WAR << "ZYPP_TESTSUITE_FAKE_ARCH: Setting fake system architecture for test purpuses to: '" << architecture << "'" << endl;
119       }
120
121       return architecture;
122     }
123     ///////////////////////////////////////////////////////////////////
124     //
125     //  METHOD NAME : ZYppImpl::ZYppImpl
126     //  METHOD TYPE : Constructor
127     //
128     ZYppImpl::ZYppImpl()
129     : _textLocale( defaultTextLocale() )
130     , _pool()
131     , _target(0)
132     , _resolver( new Resolver(_pool.accessor()) )
133     , _architecture( defaultArchitecture() )
134     , _disk_usage()
135     {
136       MIL << "libzypp: " << VERSION << " built " << __DATE__ << " " <<  __TIME__ << endl;
137       MIL << "defaultTextLocale: '" << _textLocale << "'" << endl;
138       MIL << "System architecture is '" << _architecture << "'" << endl;
139
140       MIL << "initializing keyring..." << std::endl;
141       //_keyring = new KeyRing(homePath() + Pathname("/keyring/all"), homePath() + Pathname("/keyring/trusted"));
142       _keyring = new KeyRing(tmpPath());
143     }
144
145     ///////////////////////////////////////////////////////////////////
146     //
147     //  METHOD NAME : ZYppImpl::~ZYppImpl
148     //  METHOD TYPE : Destructor
149     //
150     ZYppImpl::~ZYppImpl()
151     {}
152
153     //------------------------------------------------------------------------
154     // add/remove resolvables
155
156     void ZYppImpl::addResolvables (const ResStore& store, bool installed)
157     {
158         _pool.insert(store.begin(), store.end(), installed);
159     }
160
161     void ZYppImpl::removeResolvables (const ResStore& store)
162     {
163         for (ResStore::iterator it = store.begin(); it != store.end(); ++it)
164         {
165             _pool.erase(*it);
166         }
167     }
168
169     void ZYppImpl::removeInstalledResolvables ()
170     {
171         for (ResPool::const_iterator it = pool().begin(); it != pool().end();)
172         {
173             ResPool::const_iterator next = it; ++next;
174             if (it->status().isInstalled())
175                 _pool.erase( *it );
176             it = next;
177         }
178     }
179
180     DiskUsageCounter::MountPointSet ZYppImpl::diskUsage()
181     { return _disk_usage.disk_usage(pool()); }
182
183     void ZYppImpl::setPartitions(const DiskUsageCounter::MountPointSet &mp)
184     { _disk_usage.setMountPoints(mp); }
185
186     DiskUsageCounter::MountPointSet ZYppImpl::getPartitions() const
187     { return _disk_usage.getMountPoints(); }
188
189     //------------------------------------------------------------------------
190     // target
191
192     Target_Ptr ZYppImpl::target() const
193     {
194       if (! _target)
195         ZYPP_THROW(Exception("Target not initialized."));
196       return _target;
197      }
198
199     void ZYppImpl::initializeTarget(const Pathname & root)
200     {
201       MIL << "initTarget( " << root << endl;
202       if (_target) {
203         if (_target->root() == root) {
204             MIL << "Repeated call to initializeTarget()" << endl;
205             return;
206         }
207         removeInstalledResolvables( );
208       }
209       _target = new Target( root );
210       _target->enableStorage( root );
211     }
212
213     void ZYppImpl::initTarget(const Pathname & root, bool commit_only)
214     {
215       MIL << "initTarget( " << root << ", " << commit_only << ")" << endl;
216       if (_target) {
217         if (_target->root() == root) {
218           MIL << "Repeated call to initTarget()" << endl;
219           return;
220         }
221         removeInstalledResolvables( );
222       }
223       _target = new Target( root );
224       _target->enableStorage( root );
225       if (!commit_only)
226       {
227         addResolvables( _target->resolvables(), true );
228       }
229     }
230
231
232     void ZYppImpl::finishTarget()
233     {
234       if (_target)
235         removeInstalledResolvables();
236       _target = 0;
237     }
238
239     //------------------------------------------------------------------------
240     // commit
241
242     /** \todo Remove workflow from target, lot's of it could be done here,
243      * and target used for transact. */
244     ZYppCommitResult ZYppImpl::commit( const ZYppCommitPolicy & policy_r )
245     {
246       if ( getenv("ZYPP_TESTSUITE_FAKE_ARCH") )
247       {
248         ZYPP_THROW( Exception("ZYPP_TESTSUITE_FAKE_ARCH set. Commit not allowed and disabled.") );
249       }
250
251       MIL << "Attempt to commit (" << policy_r << ")" << endl;
252       if (! _target)
253         ZYPP_THROW( Exception("Target not initialized.") );
254
255       ZYppCommitResult res = _target->_pimpl->commit( pool(), policy_r );
256
257       if (! policy_r.dryRun() ) {
258         // Tag target data invalid, so they are reloaded on the next call to
259         // target->resolvables(). Actually the target should do this without
260         // foreign help.
261         _target->reset();
262         removeInstalledResolvables();
263         if ( policy_r.syncPoolAfterCommit() )
264           {
265             // reload new status from target
266             addResolvables( _target->resolvables(), true );
267           }
268       }
269
270       MIL << "Commit (" << policy_r << ") returned: "
271           << res << endl;
272       return res;
273     }
274
275
276     //------------------------------------------------------------------------
277     // locales
278
279     /** */
280     void ZYppImpl::setRequestedLocales( const LocaleSet & locales_r )
281     {
282       ResPool mpool( pool() );
283       // assert all requested are available
284       for ( LocaleSet::const_iterator it = locales_r.begin();
285             it != locales_r.end(); ++it )
286         {
287           NameKindProxy select( nameKindProxy<Language>( mpool, it->code() ) );
288           if ( select.installedEmpty() && select.availableEmpty() )
289             _pool.insert( Language::availableInstance( *it ) );
290         }
291
292       // now adjust status
293       for ( ResPool::byKind_iterator it = mpool.byKindBegin<Language>();
294             it != mpool.byKindEnd<Language>(); ++it )
295         {
296           NameKindProxy select( nameKindProxy<Language>( mpool, (*it)->name() ) );
297           if ( locales_r.find( Locale( (*it)->name() ) ) != locales_r.end() )
298             {
299               // Language is requested
300               if ( select.installedEmpty() )
301                 {
302                   if ( select.availableEmpty() )
303                     {
304                       // no item ==> provide available to install
305                       _pool.insert( Language::availableInstance( Locale((*it)->name()) ) );
306                       select = nameKindProxy<Language>( mpool, (*it)->name() );
307                     }
308                   // available only ==> to install
309                   select.availableBegin()->status().setTransactValue( ResStatus::TRANSACT, ResStatus::USER );
310                 }
311               else
312                 {
313                   // installed ==> keep it
314                   select.installedBegin()->status().setTransactValue( ResStatus::KEEP_STATE, ResStatus::USER );
315                   if ( ! select.availableEmpty() )
316                     {
317                       // both items ==> keep
318                       select.availableBegin()->status().resetTransact( ResStatus::USER );
319                     }
320                 }
321             }
322           else
323             {
324               // Language is NOT requested
325               if ( ! select.installedEmpty() )
326                 select.installedBegin()->status().setTransactValue( ResStatus::TRANSACT, ResStatus::USER );
327               if ( ! select.availableEmpty() )
328                 select.availableBegin()->status().resetTransact( ResStatus::USER );
329             }
330         }
331     }
332
333     /** */
334     ZYppImpl::LocaleSet ZYppImpl::getAvailableLocales() const
335     {
336       ZYpp::LocaleSet ret;
337       ResPool mpool( pool() );
338       for ( ResPool::byKind_iterator it = mpool.byKindBegin<Language>();
339             it != mpool.byKindEnd<Language>(); ++it )
340         {
341           if ( (*it).status().isUninstalled() ) // available!
342             ret.insert( Locale( (*it)->name() ) );
343         }
344       return ret;
345     }
346
347     /** */
348     ZYppImpl::LocaleSet ZYppImpl::getRequestedLocales() const
349     {
350       ZYpp::LocaleSet ret;
351       ResPool mpool( pool() );
352       for ( ResPool::byKind_iterator it = mpool.byKindBegin<Language>();
353             it != mpool.byKindEnd<Language>(); ++it )
354         {
355           NameKindProxy select( nameKindProxy<Language>( mpool, (*it)->name() ) );
356           if ( ! select.installedEmpty()
357                && select.installedBegin()->status().getTransactValue() != ResStatus::TRANSACT )
358             ret.insert( Locale( (*it)->name() ) );
359           else if ( ! select.availableEmpty()
360                     && select.availableBegin()->status().getTransactValue() == ResStatus::TRANSACT )
361             ret.insert( Locale( (*it)->name() ) );
362         }
363       return ret;
364     }
365
366     void ZYppImpl::availableLocale( const Locale & locale_r )
367     {
368       _pool.insert( Language::availableInstance( locale_r ) );
369     }
370
371     //------------------------------------------------------------------------
372     // architecture
373
374     void ZYppImpl::setArchitecture( const Arch & arch )
375     {
376         _architecture = arch;
377         if (_resolver) _resolver->setArchitecture( arch );
378     }
379
380     //------------------------------------------------------------------------
381     // target store path
382
383     Pathname ZYppImpl::homePath() const
384     { return _home_path.empty() ? Pathname("/var/lib/zypp") : _home_path; }
385
386     void ZYppImpl::setHomePath( const Pathname & path )
387     { _home_path = path; }
388
389     Pathname ZYppImpl::tmpPath() const
390     {
391       static TmpDir zypp_tmp_dir("/var/tmp", "zypp.");
392       return zypp_tmp_dir.path();
393     }
394
395     /******************************************************************
396      **
397      ** FUNCTION NAME : operator<<
398      ** FUNCTION TYPE : std::ostream &
399     */
400     std::ostream & operator<<( std::ostream & str, const ZYppImpl & obj )
401     {
402       return str << "ZYppImpl";
403     }
404
405     /////////////////////////////////////////////////////////////////
406   } // namespace zypp_detail
407   ///////////////////////////////////////////////////////////////////
408   /////////////////////////////////////////////////////////////////
409 } // namespace zypp
410 ///////////////////////////////////////////////////////////////////