~thopiekar/zypper/libzypp-manual-import

« back to all changes in this revision

Viewing changes to zypp/Repository.cc

  • Committer: Thomas-Karl Pietrowski
  • Date: 2014-01-29 22:44:28 UTC
  • Revision ID: thopiekar@googlemail.com-20140129224428-gpcqnsdakby362n8
firstĀ import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*---------------------------------------------------------------------\
 
2
|                          ____ _   __ __ ___                          |
 
3
|                         |__  / \ / / . \ . \                         |
 
4
|                           / / \ V /|  _/  _/                         |
 
5
|                          / /__ | | | | | |                           |
 
6
|                         /_____||_| |_| |_|                           |
 
7
|                                                                      |
 
8
\---------------------------------------------------------------------*/
 
9
/** \file       zypp/sat/Repository.cc
 
10
 *
 
11
*/
 
12
#include <climits>
 
13
#include <iostream>
 
14
 
 
15
#include "zypp/base/Logger.h"
 
16
#include "zypp/base/Gettext.h"
 
17
#include "zypp/base/Exception.h"
 
18
#include "zypp/base/Xml.h"
 
19
 
 
20
#include "zypp/AutoDispose.h"
 
21
#include "zypp/Pathname.h"
 
22
 
 
23
#include "zypp/sat/detail/PoolImpl.h"
 
24
#include "zypp/Repository.h"
 
25
#include "zypp/sat/Pool.h"
 
26
 
 
27
using std::endl;
 
28
 
 
29
///////////////////////////////////////////////////////////////////
 
30
namespace zypp
 
31
{ /////////////////////////////////////////////////////////////////
 
32
 
 
33
    const Repository Repository::noRepository;
 
34
 
 
35
    const std::string & Repository::systemRepoAlias()
 
36
    { return sat::detail::PoolImpl::systemRepoAlias(); }
 
37
 
 
38
    /////////////////////////////////////////////////////////////////
 
39
 
 
40
    ::_Repo * Repository::get() const
 
41
    { return myPool().getRepo( _id ); }
 
42
 
 
43
#define NO_REPOSITORY_RETURN( VAL ) \
 
44
    ::_Repo * _repo( get() ); \
 
45
    if ( ! _repo ) return VAL
 
46
 
 
47
#define NO_REPOSITORY_THROW( VAL ) \
 
48
    ::_Repo * _repo( get() ); \
 
49
    if ( ! _repo ) ZYPP_THROW( VAL )
 
50
 
 
51
    bool Repository::isSystemRepo() const
 
52
    {
 
53
        NO_REPOSITORY_RETURN( false );
 
54
        return myPool().isSystemRepo( _repo );
 
55
    }
 
56
 
 
57
    std::string Repository::alias() const
 
58
    {
 
59
      NO_REPOSITORY_RETURN( std::string() );
 
60
      if ( ! _repo->name )
 
61
        return std::string();
 
62
      return _repo->name;
 
63
    }
 
64
 
 
65
    std::string Repository::name() const
 
66
    { return info().name(); }
 
67
 
 
68
    int Repository::satInternalPriority() const
 
69
    {
 
70
      NO_REPOSITORY_RETURN( INT_MIN );
 
71
      return _repo->priority;
 
72
    }
 
73
 
 
74
    int Repository::satInternalSubPriority() const
 
75
    {
 
76
      NO_REPOSITORY_RETURN( INT_MIN );
 
77
      return _repo->subpriority;
 
78
    }
 
79
 
 
80
 
 
81
    zypp::Date Repository::generatedTimestamp() const
 
82
    {
 
83
      NO_REPOSITORY_RETURN( 0 );
 
84
      sat::LookupRepoAttr q( sat::SolvAttr::repositoryTimestamp, *this );
 
85
      return( q.empty() ? 0 : q.begin().asUnsigned() );
 
86
    }
 
87
 
 
88
    zypp::Date Repository::suggestedExpirationTimestamp() const
 
89
    {
 
90
      NO_REPOSITORY_RETURN( 0 );
 
91
      Date generated = generatedTimestamp();
 
92
      if ( ! generated )
 
93
        return 0; // do not calculate over a missing generated timestamp
 
94
 
 
95
      sat::LookupRepoAttr q( sat::SolvAttr::repositoryExpire, *this );
 
96
      if ( q.empty() )
 
97
        return 0;
 
98
 
 
99
      return generated + q.begin().asUnsigned();
 
100
    }
 
101
 
 
102
    Repository::Keywords Repository::keywords() const
 
103
    {
 
104
      NO_REPOSITORY_RETURN( Keywords() );
 
105
      return Keywords( sat::SolvAttr::repositoryKeywords, *this, sat::LookupAttr::REPO_ATTR );
 
106
    }
 
107
 
 
108
    bool Repository::maybeOutdated() const
 
109
    {
 
110
      NO_REPOSITORY_RETURN( false );
 
111
      // system repo is not mirrored
 
112
      if ( isSystemRepo() )
 
113
        return false;
 
114
 
 
115
      Date suggested = suggestedExpirationTimestamp();
 
116
 
 
117
      // if no data, don't suggest
 
118
      if ( ! suggested )
 
119
        return false;
 
120
 
 
121
      return suggestedExpirationTimestamp() < Date::now();
 
122
    }
 
123
 
 
124
    bool Repository::providesUpdatesFor( const std::string &key ) const
 
125
    {
 
126
      NO_REPOSITORY_RETURN( false );
 
127
 
 
128
      for_( it,
 
129
            updatesProductBegin(),
 
130
            updatesProductEnd() )
 
131
      {
 
132
        // FIXME implement real CPE matching here
 
133
        // someday
 
134
        if ( key == it.cpeId() )
 
135
          return true;
 
136
      }
 
137
 
 
138
      return false;
 
139
    }
 
140
 
 
141
    bool Repository::isUpdateRepo() const
 
142
    {
 
143
      NO_REPOSITORY_RETURN( false );
 
144
      return ( updatesProductBegin() != updatesProductEnd() );
 
145
    }
 
146
 
 
147
    bool Repository::solvablesEmpty() const
 
148
    {
 
149
      NO_REPOSITORY_RETURN( true );
 
150
      return !_repo->nsolvables;
 
151
    }
 
152
 
 
153
    Repository::size_type Repository::solvablesSize() const
 
154
    {
 
155
      NO_REPOSITORY_RETURN( 0 );
 
156
      return _repo->nsolvables;
 
157
    }
 
158
 
 
159
    Repository::SolvableIterator Repository::solvablesBegin() const
 
160
    {
 
161
      NO_REPOSITORY_RETURN( make_filter_iterator( detail::ByRepository( *this ),
 
162
                            sat::detail::SolvableIterator(),
 
163
                            sat::detail::SolvableIterator() ) );
 
164
      return make_filter_iterator( detail::ByRepository( *this ),
 
165
                                   sat::detail::SolvableIterator(_repo->start),
 
166
                                   sat::detail::SolvableIterator(_repo->end) );
 
167
    }
 
168
 
 
169
    Repository::SolvableIterator Repository::solvablesEnd() const
 
170
    {
 
171
      NO_REPOSITORY_RETURN( make_filter_iterator( detail::ByRepository( *this ),
 
172
                            sat::detail::SolvableIterator(),
 
173
                            sat::detail::SolvableIterator() ) );
 
174
      return make_filter_iterator(detail::ByRepository( *this ),
 
175
                                  sat::detail::SolvableIterator(_repo->end),
 
176
                                  sat::detail::SolvableIterator(_repo->end) );
 
177
    }
 
178
 
 
179
    Repository::ProductInfoIterator Repository::compatibleWithProductBegin() const
 
180
    {
 
181
      NO_REPOSITORY_RETURN( ProductInfoIterator() );
 
182
      return ProductInfoIterator( sat::SolvAttr::repositoryDistros, *this );
 
183
    }
 
184
 
 
185
    Repository::ProductInfoIterator Repository::compatibleWithProductEnd() const
 
186
    {
 
187
      return ProductInfoIterator();
 
188
    }
 
189
 
 
190
    Repository::ProductInfoIterator Repository::updatesProductBegin() const
 
191
    {
 
192
      NO_REPOSITORY_RETURN( ProductInfoIterator() );
 
193
      return ProductInfoIterator( sat::SolvAttr::repositoryUpdates, *this );
 
194
    }
 
195
 
 
196
    Repository::ProductInfoIterator Repository::updatesProductEnd() const
 
197
    {
 
198
      return ProductInfoIterator();
 
199
    }
 
200
 
 
201
    RepoInfo Repository::info() const
 
202
    {
 
203
      NO_REPOSITORY_RETURN( RepoInfo() );
 
204
      return myPool().repoInfo( _repo );
 
205
    }
 
206
 
 
207
    void Repository::setInfo( const RepoInfo & info_r )
 
208
    {
 
209
        NO_REPOSITORY_THROW( Exception( "Can't set RepoInfo for norepo." ) );
 
210
        if ( info_r.alias() != alias() )
 
211
        {
 
212
            ZYPP_THROW( Exception( str::form( "RepoInfo alias (%s) does not match repository alias (%s)",
 
213
                                              info_r.alias().c_str(), alias().c_str() ) ) );
 
214
        }
 
215
        myPool().setRepoInfo( _repo, info_r );
 
216
        MIL << *this << endl;
 
217
    }
 
218
 
 
219
    void Repository::clearInfo()
 
220
    {
 
221
        NO_REPOSITORY_RETURN();
 
222
        myPool().setRepoInfo( _repo, RepoInfo() );
 
223
    }
 
224
 
 
225
    void Repository::eraseFromPool()
 
226
    {
 
227
        NO_REPOSITORY_RETURN();
 
228
        MIL << *this << " removed from pool" << endl;
 
229
        myPool()._deleteRepo( _repo );
 
230
        _id = sat::detail::noRepoId;
 
231
    }
 
232
 
 
233
    Repository Repository::nextInPool() const
 
234
    {
 
235
      NO_REPOSITORY_RETURN( noRepository );
 
236
      for_( it, sat::Pool::instance().reposBegin(), sat::Pool::instance().reposEnd() )
 
237
      {
 
238
        if ( *it == *this )
 
239
        {
 
240
          if ( ++it != _for_end )
 
241
            return *it;
 
242
          break;
 
243
        }
 
244
      }
 
245
      return noRepository;
 
246
    }
 
247
 
 
248
    void Repository::addSolv( const Pathname & file_r )
 
249
    {
 
250
      NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo." ) );
 
251
 
 
252
      AutoDispose<FILE*> file( ::fopen( file_r.c_str(), "re" ), ::fclose );
 
253
      if ( file == NULL )
 
254
      {
 
255
        file.resetDispose();
 
256
        ZYPP_THROW( Exception( "Can't open solv-file: "+file_r.asString() ) );
 
257
      }
 
258
 
 
259
      if ( myPool()._addSolv( _repo, file ) != 0 )
 
260
      {
 
261
        ZYPP_THROW( Exception( "Error reading solv-file: "+file_r.asString() ) );
 
262
      }
 
263
 
 
264
      MIL << *this << " after adding " << file_r << endl;
 
265
    }
 
266
 
 
267
    void Repository::addHelix( const Pathname & file_r )
 
268
    {
 
269
      NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo." ) );
 
270
 
 
271
      std::string command( file_r.extension() == ".gz" ? "zcat " : "cat " );
 
272
      command += file_r.asString();
 
273
 
 
274
      AutoDispose<FILE*> file( ::popen( command.c_str(), "re" ), ::pclose );
 
275
      if ( file == NULL )
 
276
      {
 
277
        file.resetDispose();
 
278
        ZYPP_THROW( Exception( "Can't open helix-file: "+file_r.asString() ) );
 
279
      }
 
280
 
 
281
      if ( myPool()._addHelix( _repo, file ) != 0 )
 
282
      {
 
283
        ZYPP_THROW( Exception( "Error reading helix-file: "+file_r.asString() ) );
 
284
      }
 
285
 
 
286
      MIL << *this << " after adding " << file_r << endl;
 
287
    }
 
288
 
 
289
    sat::detail::SolvableIdType Repository::addSolvables( unsigned count_r )
 
290
    {
 
291
        NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo.") );
 
292
        return myPool()._addSolvables( _repo, count_r );
 
293
    }
 
294
 
 
295
    /******************************************************************
 
296
     **
 
297
     ** FUNCTION NAME : operator<<
 
298
     ** FUNCTION TYPE : std::ostream &
 
299
     */
 
300
    std::ostream & operator<<( std::ostream & str, const Repository & obj )
 
301
    {
 
302
        if ( ! obj )
 
303
            return str << "noRepository";
 
304
 
 
305
        return str << "sat::repo(" << obj.alias() << ")"
 
306
                   << "{"
 
307
                   << "prio " << obj.get()->priority << '.' << obj.get()->subpriority
 
308
                   << ", size " << obj.solvablesSize()
 
309
                   << "}";
 
310
    }
 
311
 
 
312
    std::ostream & dumpAsXmlOn( std::ostream & str, const Repository & obj )
 
313
    {
 
314
      return xmlout::node( str, "repository", {
 
315
        { "alias", obj.name() },
 
316
        { "name", obj.alias() }
 
317
      } );
 
318
    }
 
319
 
 
320
    //////////////////////////////////////////////////////////////////
 
321
    namespace detail
 
322
    {
 
323
      void RepositoryIterator::increment()
 
324
      {
 
325
        if ( base() )
 
326
        {
 
327
          ::_Pool * satpool = sat::Pool::instance().get();
 
328
          do {
 
329
            ++base_reference();
 
330
          } while ( base() < satpool->repos+satpool->nrepos && !*base() );
 
331
        }
 
332
      }
 
333
    } // namespace detail
 
334
    //////////////////////////////////////////////////////////////////
 
335
 
 
336
    ///////////////////////////////////////////////////////////////////
 
337
    //
 
338
    // Repository::ProductInfoIterator
 
339
    //
 
340
    ///////////////////////////////////////////////////////////////////
 
341
 
 
342
    Repository::ProductInfoIterator::ProductInfoIterator( sat::SolvAttr attr_r, Repository repo_r )
 
343
    { base_reference() = sat::LookupRepoAttr( attr_r, repo_r ).begin(); }
 
344
 
 
345
    std::string Repository::ProductInfoIterator::label() const
 
346
    { return base_reference().subFind( sat::SolvAttr::repositoryProductLabel ).asString(); }
 
347
 
 
348
    std::string Repository::ProductInfoIterator::cpeId() const
 
349
    { return base_reference().subFind( sat::SolvAttr::repositoryProductCpeid ).asString(); }
 
350
 
 
351
    /////////////////////////////////////////////////////////////////
 
352
} // namespace zypp
 
353
///////////////////////////////////////////////////////////////////