~thopiekar/zypper/libzypp-manual-import

« back to all changes in this revision

Viewing changes to zypp/target/CommitPackageCacheReadAhead.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/target/CommitPackageCacheReadAhead.cc
 
10
 *
 
11
*/
 
12
#include <iostream>
 
13
 
 
14
#include "zypp/base/Logger.h"
 
15
#include "zypp/base/Exception.h"
 
16
#include "zypp/PathInfo.h"
 
17
#include "zypp/RepoInfo.h"
 
18
#include "zypp/Package.h"
 
19
#include "zypp/target/CommitPackageCacheReadAhead.h"
 
20
 
 
21
using std::endl;
 
22
 
 
23
///////////////////////////////////////////////////////////////////
 
24
namespace zypp
 
25
{ /////////////////////////////////////////////////////////////////
 
26
  ///////////////////////////////////////////////////////////////////
 
27
  namespace target
 
28
  { /////////////////////////////////////////////////////////////////
 
29
 
 
30
    ///////////////////////////////////////////////////////////////////
 
31
    //
 
32
    //  CLASS NAME : IMediaKey
 
33
    //
 
34
    ///////////////////////////////////////////////////////////////////
 
35
 
 
36
    std::ostream & operator<<( std::ostream & str, const IMediaKey & obj )
 
37
    {
 
38
      return str << "[S" << obj._repo.id() << ":" << obj._mediaNr << "]"
 
39
                 << " " << obj._repo.info().alias();
 
40
    }
 
41
 
 
42
    ///////////////////////////////////////////////////////////////////
 
43
    //
 
44
    //  CLASS NAME : CommitPackageCacheReadAhead
 
45
    //
 
46
    ///////////////////////////////////////////////////////////////////
 
47
 
 
48
    ///////////////////////////////////////////////////////////////////
 
49
    //
 
50
    //  METHOD NAME : CommitPackageCacheReadAhead::CommitPackageCacheReadAhead
 
51
    //  METHOD TYPE : Ctor
 
52
    //
 
53
    CommitPackageCacheReadAhead::CommitPackageCacheReadAhead( const Pathname &        /*rootDir_r*/,
 
54
                                                              const PackageProvider & packageProvider_r )
 
55
    : CommitPackageCache::Impl( packageProvider_r )
 
56
    //, _rootDir( rootDir_r )
 
57
    {}
 
58
 
 
59
    ///////////////////////////////////////////////////////////////////
 
60
    //
 
61
    //  METHOD NAME : CommitPackageCacheReadAhead::onInteractiveMedia
 
62
    //  METHOD TYPE : bool
 
63
    //
 
64
    bool CommitPackageCacheReadAhead::onInteractiveMedia( const PoolItem & pi ) const
 
65
    {
 
66
      if ( pi->mediaNr() == 0 ) // no media access at all
 
67
        return false;
 
68
      if ( pi->repoInfo().baseUrlsEmpty() )
 
69
        return false; // no Url - should actually not happen
 
70
      std::string scheme( pi->repoInfo().baseUrlsBegin()->getScheme() );
 
71
      return ( scheme == "dvd" || scheme == "cd" );
 
72
    }
 
73
 
 
74
    ///////////////////////////////////////////////////////////////////
 
75
    //
 
76
    //  METHOD NAME : CommitPackageCacheReadAhead::cacheLastInteractive
 
77
    //  METHOD TYPE : void
 
78
    //
 
79
    void CommitPackageCacheReadAhead::cacheLastInteractive( const PoolItem & citem_r )
 
80
    {
 
81
      // Fill cache errors are never proagated.
 
82
      try
 
83
        {
 
84
          doCacheLastInteractive( citem_r );
 
85
        }
 
86
      catch ( const Exception & excpt_r )
 
87
        {
 
88
          ZYPP_CAUGHT( excpt_r );
 
89
          WAR << "Failed to cache " << _lastInteractive << endl;
 
90
        }
 
91
    }
 
92
 
 
93
    ///////////////////////////////////////////////////////////////////
 
94
    //
 
95
    //  METHOD NAME : CommitPackageCacheReadAhead::doCacheLastInteractive
 
96
    //  METHOD TYPE : void
 
97
    //
 
98
    void CommitPackageCacheReadAhead::doCacheLastInteractive( const PoolItem & citem_r )
 
99
    {
 
100
      unsigned  addToCache = 0;
 
101
      bool      sawCitem = false;
 
102
 
 
103
      // Collect all remaining packages to install from
 
104
      // _lastInteractive media. (just the PoolItem data)
 
105
      for_( it, commitList().begin(), commitList().end() )
 
106
      {
 
107
        PoolItem pi( *it );
 
108
        if ( ! sawCitem )
 
109
        {
 
110
          if ( pi == citem_r )
 
111
            sawCitem = true;
 
112
          continue;
 
113
        }
 
114
        if ( IMediaKey( pi ) == _lastInteractive
 
115
          && pi.status().isToBeInstalled()
 
116
          && isKind<Package>(pi.resolvable()) )
 
117
        {
 
118
          if ( ! pi->asKind<Package>()->isCached() )
 
119
          {
 
120
            ManagedFile fromSource( sourceProvidePackage( pi ) );
 
121
            if ( fromSource->empty() )
 
122
            {
 
123
              ERR << "Copy to cache failed on " << fromSource << endl;
 
124
              ZYPP_THROW( Exception("Copy to cache failed.") );
 
125
            }
 
126
            fromSource.resetDispose(); // keep the package file in the cache
 
127
            ++addToCache;
 
128
          }
 
129
        }
 
130
      }
 
131
 
 
132
      if ( addToCache )
 
133
        MIL << "Cached " << _lastInteractive << ": " << addToCache << " items." << endl;
 
134
    }
 
135
 
 
136
    ///////////////////////////////////////////////////////////////////
 
137
    //
 
138
    //  METHOD NAME : CommitPackageCacheReadAhead::get
 
139
    //  METHOD TYPE : ManagedFile
 
140
    //
 
141
    ManagedFile CommitPackageCacheReadAhead::get( const PoolItem & citem_r )
 
142
    {
 
143
      // Non CD/DVD media provide their packages without cache.
 
144
      if ( ! onInteractiveMedia( citem_r ) )
 
145
      {
 
146
        return sourceProvidePackage( citem_r );
 
147
      }
 
148
 
 
149
      // Check whether it's cached.
 
150
      ManagedFile ret( sourceProvideCachedPackage( citem_r ) );
 
151
      if ( ! ret->empty() )
 
152
        return ret;
 
153
 
 
154
      IMediaKey current( citem_r );
 
155
      if ( current != _lastInteractive )
 
156
      {
 
157
        if ( _lastInteractive != IMediaKey() )
 
158
        {
 
159
          cacheLastInteractive( citem_r );
 
160
        }
 
161
 
 
162
        DBG << "Interactive change [" << ++_dbgChanges << "] from " << _lastInteractive << " to " << current << endl;
 
163
        _lastInteractive = current;
 
164
      }
 
165
 
 
166
      // Provide and return the file from media.
 
167
      return sourceProvidePackage( citem_r );
 
168
    }
 
169
 
 
170
 
 
171
    /////////////////////////////////////////////////////////////////
 
172
  } // namespace target
 
173
  ///////////////////////////////////////////////////////////////////
 
174
  /////////////////////////////////////////////////////////////////
 
175
} // namespace zypp
 
176
///////////////////////////////////////////////////////////////////