- Allow prioritizing repos by adding a line 'priority=N' to the
[platform/upstream/libzypp.git] / zypp / Repository.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/sat/Repository.cc
10  *
11 */
12 #include <iostream>
13
14 #include "zypp/base/Logger.h"
15 #include "zypp/base/Gettext.h"
16 #include "zypp/base/Exception.h"
17
18 #include "zypp/AutoDispose.h"
19 #include "zypp/Pathname.h"
20
21 #include "zypp/sat/detail/PoolImpl.h"
22 #include "zypp/Repository.h"
23 #include "zypp/sat/Pool.h"
24
25 using std::endl;
26
27 ///////////////////////////////////////////////////////////////////
28 namespace zypp
29 { /////////////////////////////////////////////////////////////////
30   ///////////////////////////////////////////////////////////////////
31
32     const Repository Repository::noRepository;
33
34     /////////////////////////////////////////////////////////////////
35
36     ::_Repo * Repository::get() const
37     { return myPool().getRepo( _id ); }
38
39 #define NO_REPOSITORY_RETURN( VAL ) \
40     ::_Repo * _repo( get() ); \
41     if ( ! _repo ) return VAL
42
43 #define NO_REPOSITORY_THROW( VAL ) \
44     ::_Repo * _repo( get() ); \
45     if ( ! _repo ) ZYPP_THROW( VAL )
46
47     bool Repository::isSystemRepo() const
48     {
49         NO_REPOSITORY_RETURN( false );
50         return( sat::Pool::systemRepoName() == _repo->name );
51     }
52
53     std::string Repository::name() const
54     {
55         NO_REPOSITORY_RETURN( std::string() );
56         if ( ! _repo->name )
57             return std::string();
58         return _repo->name;
59     }
60
61     bool Repository::solvablesEmpty() const
62     {
63         NO_REPOSITORY_RETURN( true );
64         return _repo->nsolvables;
65     }
66
67     Repository::size_type Repository::solvablesSize() const
68     {
69         NO_REPOSITORY_RETURN( 0 );
70         return _repo->nsolvables;
71     }
72
73     Repository::SolvableIterator Repository::solvablesBegin() const
74     {
75         NO_REPOSITORY_RETURN( make_filter_iterator( detail::ByRepository( *this ),
76                                               sat::detail::SolvableIterator(),
77                                               sat::detail::SolvableIterator() ) );
78         return make_filter_iterator( detail::ByRepository( *this ),
79                                      sat::detail::SolvableIterator(_repo->start),
80                                      sat::detail::SolvableIterator(_repo->end) );
81     }
82
83     Repository::SolvableIterator Repository::solvablesEnd() const
84     {
85         NO_REPOSITORY_RETURN( make_filter_iterator( detail::ByRepository( *this ),
86                                               sat::detail::SolvableIterator(),
87                                               sat::detail::SolvableIterator() ) );
88         return make_filter_iterator(detail::ByRepository( *this ),
89                                     sat::detail::SolvableIterator(_repo->end),
90                                     sat::detail::SolvableIterator(_repo->end) );
91     }
92
93     RepoInfo Repository::info() const
94     {
95         NO_REPOSITORY_RETURN( RepoInfo() );
96         return myPool().repoInfo( _repo );
97     }
98
99     void Repository::setInfo( const RepoInfo & info_r )
100     {
101         NO_REPOSITORY_THROW( Exception( _("Can't set RepoInfo for norepo.") ) );
102         if ( info_r.alias() != name() )
103         {
104             ZYPP_THROW( Exception( str::form( _("RepoInfo alias (%s) does not match repository name (%s)"),
105                                               info_r.alias().c_str(), name().c_str() ) ) );
106         }
107         myPool().setRepoInfo( _repo, info_r );
108
109         // satsolver priority is based on '<', while yum's repoinfo
110         // uses 1(highest)->99(lowest). Thus we use -info_r.priority.
111         _repo->priority = -info_r.priority();
112     }
113
114     void Repository::clearInfo()
115     {
116         NO_REPOSITORY_RETURN();
117         myPool().setRepoInfo( _repo, RepoInfo() );
118     }
119
120     void Repository::eraseFromPool()
121     {
122         NO_REPOSITORY_RETURN();
123         myPool()._deleteRepo( _repo );
124         _id = sat::detail::noRepoId;
125     }
126
127 #warning NEED POOL MANIP EXEPTIONS
128     void Repository::addSolv( const Pathname & file_r )
129     {
130         NO_REPOSITORY_THROW( Exception( _("Can't add solvables to norepo.") ) );
131
132         AutoDispose<FILE*> file( ::fopen( file_r.c_str(), "r" ), ::fclose );
133         if ( file == NULL )
134         {
135             file.resetDispose();
136             ZYPP_THROW( Exception( _("Can't open solv-file: ")+file_r.asString() ) );
137         }
138
139         if ( myPool()._addSolv( _repo, file, isSystemRepo() ) != 0 )
140         {
141             ZYPP_THROW( Exception( _("Error reading solv-file: ")+file_r.asString() ) );
142         }
143     }
144
145     sat::detail::SolvableIdType Repository::addSolvables( unsigned count_r )
146     {
147         NO_REPOSITORY_THROW( Exception( _("Can't add solvables to norepo.") ) );
148         return myPool()._addSolvables( _repo, count_r );
149     }
150
151     /******************************************************************
152      **
153      ** FUNCTION NAME : operator<<
154      ** FUNCTION TYPE : std::ostream &
155      */
156     std::ostream & operator<<( std::ostream & str, const Repository & obj )
157     {
158         if ( ! obj )
159             return str << "sat::repo()";
160
161         return str << "sat::repo(" << obj.name() << ")"
162                    << "{"
163                    << "prio " << obj.get()->priority
164                    << ", size " << obj.solvablesSize()
165                    <<"}";
166     }
167
168
169     /////////////////////////////////////////////////////////////////
170 } // namespace zypp
171 ///////////////////////////////////////////////////////////////////