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