fa8739c120961cac6308e8f70e00fbec9d500779
[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
19 using std::endl;
20
21 ///////////////////////////////////////////////////////////////////
22 namespace zypp
23 { /////////////////////////////////////////////////////////////////
24   ///////////////////////////////////////////////////////////////////
25   namespace zypp_detail
26   { /////////////////////////////////////////////////////////////////
27
28     inline Locale defaultTextLocale()
29     {
30       Locale ret( "en" );
31       char * envlist[] = { "LC_ALL", "LC_CTYPE", "LANG", NULL };
32       for ( char ** envvar = envlist; *envvar; ++envvar )
33         {
34           char * envlang = getenv( *envvar );
35           if ( envlang )
36             {
37               std::string envstr( envlang );
38               if ( envstr != "POSIX" && envstr != "C" )
39                 {
40                   Locale lang( envlang );
41                   if ( lang != Locale::noCode )
42                     {
43                       ret = lang;
44                       break;
45                     }
46                 }
47             }
48         }
49       return ret;
50     }
51
52     ///////////////////////////////////////////////////////////////////
53     //
54     //  METHOD NAME : ZYppImpl::ZYppImpl
55     //  METHOD TYPE : Constructor
56     //
57     ZYppImpl::ZYppImpl()
58     : _textLocale( defaultTextLocale() )
59     , _pool()
60     , _sourceFeed( _pool )
61     , _resolver( new Resolver(_pool.accessor()) )
62     {
63       MIL << "defaultTextLocale: '" << _textLocale << "'" << endl;
64
65       struct utsname buf;
66       if (uname (&buf) < 0) {
67         ERR << "Can't determine system architecture" << endl;
68       }
69       else {
70         MIL << "System architecture is '" << buf.machine << "'" << endl;
71         _architecture = Arch(buf.machine);
72       }
73
74     }
75
76     ///////////////////////////////////////////////////////////////////
77     //
78     //  METHOD NAME : ZYppImpl::~ZYppImpl
79     //  METHOD TYPE : Destructor
80     //
81     ZYppImpl::~ZYppImpl()
82     {
83     }
84
85     void ZYppImpl::addResolvables (const ResStore& store, bool installed)
86     {
87         _pool.insert(store.begin(), store.end(), installed);
88     }
89
90     void ZYppImpl::removeResolvables (const ResStore& store)
91     {
92         for (ResStore::iterator it = store.begin(); it != store.end(); it++)
93         {
94             _pool.erase(*it);
95         }
96     }
97
98     Target_Ptr ZYppImpl::target() const
99     {
100       if (! _target)
101         ZYPP_THROW(Exception("Target not initialized."));
102       return _target;
103      }
104
105     void ZYppImpl::initTarget(const Pathname & root, bool commit_only)
106     {
107       if (_target) {
108         WAR << "Repeated call to initTarget()" << endl;
109         return;
110       }
111
112       _target = new Target(root);
113       if (!commit_only)
114       {
115         _target->enableStorage(root);
116         addResolvables (_target->resolvables(), true);
117       }
118     }
119
120     void ZYppImpl::finishTarget()
121     {
122 #warning not removing target resolvables for now
123 //      if (_target)
124 //      removeResolvables (_target->resolvables());
125       _target = 0;
126     }
127
128     /** \todo Remove workflow from target, lot's of it could be done here,
129     * and target used for transact. */
130     ZYpp::CommitResult ZYppImpl::commit( int medianr_r )
131     {
132       MIL << "Attempt to commit (medianr " << medianr_r << ")" << endl;
133       if (! _target)
134         ZYPP_THROW( Exception("Target not initialized.") );
135
136       ZYpp::CommitResult res;
137       // must redirect to Target::Impl. This kind of commit should not be
138       // in the Target interface.
139       res._result = _target->commit( pool(), medianr_r,
140                                      res._errors, res._remaining, res._srcremaining );
141
142       MIL << "Commit (medianr " << medianr_r << ") returned: "
143           << res._result
144           << " (errors " << res._errors.size()
145           << ", remaining " << res._remaining.size()
146           << ", srcremaining " << res._srcremaining.size()
147           << ")" << endl;
148       return res;
149     }
150
151
152     void ZYppImpl::setArchitecture( const Arch & arch )
153     {
154         _architecture = arch;
155         if (_resolver) _resolver->setArchitecture( arch );
156     }
157
158     Pathname ZYppImpl::homePath() const
159     { return _home_path.empty() ? Pathname("/var/lib/zypp") : _home_path; }
160     void ZYppImpl::setHomePath( const Pathname & path )
161     { _home_path = path; }  
162     
163     /******************************************************************
164      **
165      ** FUNCTION NAME : operator<<
166      ** FUNCTION TYPE : std::ostream &
167     */
168     std::ostream & operator<<( std::ostream & str, const ZYppImpl & obj )
169     {
170       return str << "ZYppImpl";
171     }
172
173     /////////////////////////////////////////////////////////////////
174   } // namespace zypp_detail
175   ///////////////////////////////////////////////////////////////////
176   /////////////////////////////////////////////////////////////////
177 } // namespace zypp
178 ///////////////////////////////////////////////////////////////////