indentation
[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 <iostream>
15 //#include "zypp/base/Logger.h"
16
17 #include "zypp/zypp_detail/ZYppImpl.h"
18 #include "zypp/detail/LanguageImpl.h"
19 #include "zypp/detail/ResImplTraits.h"
20 #include "zypp/NVRAD.h"
21 #include "zypp/Language.h"
22
23 using std::endl;
24
25 ///////////////////////////////////////////////////////////////////
26 namespace zypp
27 { /////////////////////////////////////////////////////////////////
28   ///////////////////////////////////////////////////////////////////
29   namespace zypp_detail
30   { /////////////////////////////////////////////////////////////////
31
32     inline Locale defaultTextLocale()
33     {
34       Locale ret( "en" );
35       char * envlist[] = { "LC_ALL", "LC_CTYPE", "LANG", NULL };
36       for ( char ** envvar = envlist; *envvar; ++envvar )
37         {
38           char * envlang = getenv( *envvar );
39           if ( envlang )
40             {
41               std::string envstr( envlang );
42               if ( envstr != "POSIX" && envstr != "C" )
43                 {
44                   Locale lang( envlang );
45                   if ( lang != Locale::noCode )
46                     {
47                       ret = lang;
48                       break;
49                     }
50                 }
51             }
52         }
53       return ret;
54     }
55
56     ///////////////////////////////////////////////////////////////////
57     //
58     //  METHOD NAME : ZYppImpl::ZYppImpl
59     //  METHOD TYPE : Constructor
60     //
61     ZYppImpl::ZYppImpl()
62     : _textLocale( defaultTextLocale() )
63     , _pool()
64     , _sourceFeed( _pool )
65     , _resolver( new Resolver(_pool.accessor()) )
66     {
67       MIL << "defaultTextLocale: '" << _textLocale << "'" << endl;
68
69       struct utsname buf;
70       if (uname (&buf) < 0) {
71         ERR << "Can't determine system architecture" << endl;
72       }
73       else {
74         MIL << "System architecture is '" << buf.machine << "'" << endl;
75         _architecture = Arch(buf.machine);
76       }
77
78     }
79
80     ///////////////////////////////////////////////////////////////////
81     //
82     //  METHOD NAME : ZYppImpl::~ZYppImpl
83     //  METHOD TYPE : Destructor
84     //
85     ZYppImpl::~ZYppImpl()
86     {
87     }
88
89     //------------------------------------------------------------------------
90     // add/remove resolvables
91
92     void ZYppImpl::addResolvables (const ResStore& store, bool installed)
93     {
94         _pool.insert(store.begin(), store.end(), installed);
95     }
96
97     void ZYppImpl::removeResolvables (const ResStore& store)
98     {
99         for (ResStore::iterator it = store.begin(); it != store.end(); ++it)
100         {
101             _pool.erase(*it);
102         }
103     }
104
105     void ZYppImpl::removeInstalledResolvables ()
106     {
107         for (ResPool::const_iterator it = pool().begin(); it != pool().end(); ++it)
108         {
109             if (it->status().isInstalled())
110                 _pool.erase( *it );
111         }
112     }
113
114     //------------------------------------------------------------------------
115     // target
116
117     Target_Ptr ZYppImpl::target() const
118     {
119       if (! _target)
120         ZYPP_THROW(Exception("Target not initialized."));
121       return _target;
122      }
123
124     void ZYppImpl::initTarget(const Pathname & root, bool commit_only)
125     {
126       if (_target) {
127         if (_target->root() == root) {
128             MIL << "Repeated call to initTarget()" << endl;
129             return;
130         }
131         removeInstalledResolvables( );
132       }
133       _target = new Target( root );
134       if (!commit_only)
135       {
136         _target->enableStorage( root );
137         addResolvables( _target->resolvables(), true );
138       }
139     }
140
141     void ZYppImpl::finishTarget()
142     {
143       if (_target)
144         removeInstalledResolvables();
145       _target = 0;
146     }
147
148     //------------------------------------------------------------------------
149     // commit
150
151     /** \todo Remove workflow from target, lot's of it could be done here,
152     * and target used for transact. */
153     ZYpp::CommitResult ZYppImpl::commit( int medianr_r )
154     {
155       MIL << "Attempt to commit (medianr " << medianr_r << ")" << endl;
156       if (! _target)
157         ZYPP_THROW( Exception("Target not initialized.") );
158
159       ZYpp::CommitResult res;
160
161       // must redirect to Target::Impl. This kind of commit should not be
162       // in the Target interface.
163
164       res._result = _target->commit( pool(), medianr_r,
165                                      res._errors, res._remaining, res._srcremaining );
166
167       // reload new status from target
168
169       removeInstalledResolvables();
170       addResolvables( _target->resolvables(), true );
171
172       MIL << "Commit (medianr " << medianr_r << ") returned: "
173           << res._result
174           << " (errors " << res._errors.size()
175           << ", remaining " << res._remaining.size()
176           << ", srcremaining " << res._srcremaining.size()
177           << ")" << endl;
178
179       return res;
180     }
181
182
183     //------------------------------------------------------------------------
184     // locales
185
186     /** */
187     void ZYppImpl::setPossibleLocales( const LocaleSet & locales_r )
188     {
189         removeResolvables( _possible_locales );
190         _possible_locales.clear();
191
192         for (LocaleSet::const_iterator it = locales_r.begin(); it != locales_r.end(); ++it) {
193             NVRA nvra( it->code(), Edition(), Arch_noarch );
194             NVRAD ldata( nvra, Dependencies() );
195             detail::ResImplTraits<detail::LanguageImpl>::Ptr limpl = new detail::LanguageImpl();
196             Language::Ptr language = detail::makeResolvableFromImpl( ldata, limpl );
197             _possible_locales.insert( language );
198         }
199         addResolvables( _possible_locales, false );
200     }
201
202     /** */
203     ZYppImpl::LocaleSet ZYppImpl::getPossibleLocales() const
204     {
205         LocaleSet lset;
206         for (ResStore::const_iterator it = _possible_locales.begin(); it != _possible_locales.end(); ++it) {
207             lset.insert( Locale( (*it)->name() ) );
208         }
209         return lset;
210     }
211
212     //------------------------------------------------------------------------
213     // architecture
214
215     void ZYppImpl::setArchitecture( const Arch & arch )
216     {
217         _architecture = arch;
218         if (_resolver) _resolver->setArchitecture( arch );
219     }
220
221     //------------------------------------------------------------------------
222     // target store path
223
224     Pathname ZYppImpl::homePath() const
225     { return _home_path.empty() ? Pathname("/var/lib/zypp") : _home_path; }
226
227     void ZYppImpl::setHomePath( const Pathname & path )
228     { _home_path = path; }  
229     
230     /******************************************************************
231      **
232      ** FUNCTION NAME : operator<<
233      ** FUNCTION TYPE : std::ostream &
234     */
235     std::ostream & operator<<( std::ostream & str, const ZYppImpl & obj )
236     {
237       return str << "ZYppImpl";
238     }
239
240     /////////////////////////////////////////////////////////////////
241   } // namespace zypp_detail
242   ///////////////////////////////////////////////////////////////////
243   /////////////////////////////////////////////////////////////////
244 } // namespace zypp
245 ///////////////////////////////////////////////////////////////////