130603294603d5875e1a2be2b2a387cb2e8e2700
[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     Repository Repository::nextInPool() const
128     {
129       NO_REPOSITORY_RETURN( noRepository );
130       for_( it, sat::Pool::instance().reposBegin(), sat::Pool::instance().reposEnd() )
131       {
132         if ( *it == *this )
133         {
134           if ( ++it != _for_end )
135             return *it;
136           break;
137         }
138       }
139       return noRepository;
140     }
141
142 #warning NEED POOL MANIP EXEPTIONS
143     void Repository::addSolv( const Pathname & file_r )
144     {
145         NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo." ) );
146
147         AutoDispose<FILE*> file( ::fopen( file_r.c_str(), "r" ), ::fclose );
148         if ( file == NULL )
149         {
150             file.resetDispose();
151             ZYPP_THROW( Exception( "Can't open solv-file: "+file_r.asString() ));
152         }
153
154         if ( myPool()._addSolv( _repo, file, isSystemRepo() ) != 0 )
155         {
156             ZYPP_THROW( Exception( "Error reading solv-file: "+file_r.asString() ));
157         }
158     }
159
160     sat::detail::SolvableIdType Repository::addSolvables( unsigned count_r )
161     {
162         NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo.") );
163         return myPool()._addSolvables( _repo, count_r );
164     }
165
166     /******************************************************************
167      **
168      ** FUNCTION NAME : operator<<
169      ** FUNCTION TYPE : std::ostream &
170      */
171     std::ostream & operator<<( std::ostream & str, const Repository & obj )
172     {
173         if ( ! obj )
174             return str << "noRepository";
175
176         return str << "sat::repo(" << obj.name() << ")"
177                    << "{"
178                    << "prio " << obj.get()->priority
179                    << ", size " << obj.solvablesSize()
180                    <<"}";
181     }
182
183
184     /////////////////////////////////////////////////////////////////
185 } // namespace zypp
186 ///////////////////////////////////////////////////////////////////