libzypp  17.26.0
SUSEMediaVerifier.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
9 
10 #include <fstream>
11 #include <zypp/base/Logger.h>
12 #include <zypp/base/Gettext.h>
14 
15 using std::endl;
16 
17 namespace zypp
18 {
19  namespace repo
20  {
24  struct SMVData
25  {
26  SMVData( const Pathname & path_r )
27  {
28  std::ifstream inp( path_r.c_str() );
29  if ( !inp ) {
30  ERR << "Can't setup a SUSEMediaVerifier from file: " << path_r.asString() << endl;
31  return;
32  }
33  getline( inp, _mediaVendor );
34  getline( inp, _mediaIdent );
35  std::string buffer;
36  getline( inp, buffer );
37  str::strtonum( buffer, _totalMedia );
38  //if ( !_totalMedia ) _totalMedia = 1;
39  }
40 
42  explicit operator bool() const
43  { return valid(); }
44 
46  bool valid() const
47  { return ! (_mediaVendor.empty() || _mediaIdent.empty()); }
48 
50  bool matches( const SMVData & rhs ) const
51  { return valid() && rhs._mediaVendor == _mediaVendor && rhs._mediaIdent == _mediaIdent; }
52 
53  std::string _mediaVendor;
54  std::string _mediaIdent;
56  };
57 
59  inline std::ostream & operator<<( std::ostream & str, const SMVData & obj )
60  { return str << "[" << obj._mediaVendor << "|" << obj._mediaIdent << "/" << obj._totalMedia << "]"; }
61 
62 
75  {
76  public:
77  Impl( const Pathname & path_r, media::MediaNr mediaNr_r )
78  : _smvData { new SMVData( path_r ) }
79  , _mediaNr { mediaNr_r }
80  {}
81 
82  Impl( const Impl & rhs, media::MediaNr mediaNr_r )
83  : _smvData { rhs._smvData }
84  , _mediaNr { mediaNr_r }
85  {}
86 
87  const SMVData & smvData() const
88  { return *_smvData; }
89 
91  { return _mediaNr; }
92 
93  Pathname mediaFilePath( media::MediaNr mediaNr_r = 0 ) const
94  {
95  str::Format fmt { "/media.%d/media" };
96  fmt % str::numstring( mediaNr_r ? mediaNr_r : _mediaNr );
97  return fmt.str();
98  }
99 
100  std::string expectedAsUserString() const
101  {
102  // Translator: %1% the expected medium number; %2% the total number of media in the set; %3% the ident file on the medium.
103  str::Format fmt { _("Expected medium %1%/%2% identified by file '%3%' with content:") };
104  return str::Str()
105  << ( fmt % mediaNr() % smvData()._totalMedia % mediaFilePath() ) << endl
106  << " " << smvData()._mediaVendor << endl
107  << " " << smvData()._mediaIdent;
108  }
109 
110  private:
111  shared_ptr<SMVData> _smvData;
113  };
114 
116  inline std::ostream & operator<<( std::ostream & str, const SUSEMediaVerifier::Impl & obj )
117  { return str << obj.smvData() << "(" << obj.mediaNr() << ")"; }
118 
119 
121  // SUSEMediaVerifier
123 
125  : _pimpl { new Impl( path_r, mediaNr_r ) }
126  {}
127 
129  : _pimpl { new Impl( *rhs._pimpl, mediaNr_r ) }
130  {}
131 
133  {}
134 
136  { return _pimpl->smvData().valid(); }
137 
138  const std::string & SUSEMediaVerifier::vendor() const
139  { return _pimpl->smvData()._mediaVendor; }
140 
141  const std::string & SUSEMediaVerifier::ident() const
142  { return _pimpl->smvData()._mediaIdent; }
143 
145  { return _pimpl->smvData()._totalMedia; }
146 
148  { return _pimpl->mediaNr(); }
149 
150 
152  {
153  bool ret = true; // optimistic return unless we definitely now it does not match
154 
155  const SMVData & smvData = _pimpl->smvData();
156  if ( ! smvData )
157  return ret; // we have no valid data
158 
159  // bsc#1180851: If there is just one not-volatile medium in the set
160  // tolerate a missing (vanished) media identifier and let the URL rule.
161  bool relaxed = smvData._totalMedia == 1 && ! Url::schemeIsVolatile( ref->protocol() );
162  SEC << smvData << endl;
163  SEC << ref->protocol() << " " << Url::schemeIsVolatile( ref->protocol() ) << endl;
164 
165  Pathname mediaFile { _pimpl->mediaFilePath() };
166  try {
167  ref->provideFile( mediaFile, 0 );
168  mediaFile = ref->localPath( mediaFile );
169  }
170  catch ( media::MediaFileNotFoundException & excpt_r )
171  {
172  if ( relaxed ) {
173  ZYPP_CAUGHT( excpt_r );
174  return ret;
175  }
176  excpt_r.addHistory( _pimpl->expectedAsUserString() );
177  ZYPP_RETHROW( excpt_r );
178  }
179  catch ( media::MediaNotAFileException & excpt_r )
180  {
181  if ( relaxed ) {
182  ZYPP_CAUGHT( excpt_r );
183  return ret;
184  }
185  excpt_r.addHistory( _pimpl->expectedAsUserString() );
186  ZYPP_CAUGHT( excpt_r ); return ret;
187  }
188 
189  SMVData remote { mediaFile };
190  ret = smvData.matches( remote );
191  if ( ! ret ) {
192  DBG << "expect: " << smvData << " medium " << mediaNr() << endl;
193  DBG << "remote: " << remote << endl;
194  }
195  return ret;
196  }
197 
198  std::ostream & operator<<( std::ostream & str, const SUSEMediaVerifier & obj )
199  { return str << *obj._pimpl; }
200 
201  } // namespace repo
202 } // namespace zypp
203 
Interface to gettext.
bool valid() const
Data considered to be valid if we have vendor and ident.
Implementation of the traditional SUSE media verifier.
SUSEMediaVerifier(const Pathname &path_r, media::MediaNr mediaNr_r=1)
Ctor creating a verifier by parsing media file.
Impl(const Pathname &path_r, media::MediaNr mediaNr_r)
const std::string & vendor() const
Medias expected vendor string.
const char * c_str() const
String representation.
Definition: Pathname.h:110
void addHistory(const std::string &msg_r)
Add some message text to the history.
Definition: Exception.cc:125
String related utilities and Regular expression matching.
media::MediaNr _totalMedia
Convenient building of std::string with boost::format.
Definition: String.h:249
media::MediaNr mediaNr() const
Media number expected by this verifier (starts with 1).
#define ERR
Definition: Logger.h:98
#define ZYPP_RETHROW(EXCPT)
Drops a logline and rethrows, updating the CodeLocation.
Definition: Exception.h:400
std::ostream & operator<<(std::ostream &str, const SUSEMediaVerifier::Impl &obj)
RW_pointer< Impl > _pimpl
Pointer to implementation.
std::string getline(std::istream &str)
Read one line from stream.
Definition: IOStream.cc:33
Convenient building of std::string via std::ostringstream Basically a std::ostringstream autoconverti...
Definition: String.h:208
const std::string & asString() const
String representation.
Definition: Pathname.h:91
std::ostream & operator<<(std::ostream &str, const DeltaCandidates &obj)
bool schemeIsVolatile() const
Definition: Url.h:271
#define SEC
Definition: Logger.h:99
const std::string & ident() const
Medias expected ident string.
TInt strtonum(const C_Str &str)
Parsing numbers from string.
Definition: String.h:385
#define _(MSG)
Definition: Gettext.h:37
Data parsed from a media.1/media file.
std::string numstring(char n, int w=0)
Definition: String.h:286
bool valid() const
Data considered to be valid if we have vendor and ident.
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
Definition: Exception.h:396
media::MediaNr totalMedia() const
The total number of media in this set (or 0 if not known).
Pathname mediaFilePath(media::MediaNr mediaNr_r=0) const
Impl(const Impl &rhs, media::MediaNr mediaNr_r)
bool matches(const SMVData &rhs) const
Whether rhs belongs to the same media set.
Wrapper for const correct access via Smart pointer types.
Definition: PtrTypes.h:285
bool isDesiredMedia(const media::MediaAccessRef &ref_r) const override
Check if accesses the desired media.
SMVData(const Pathname &path_r)
std::ostream & operator<<(std::ostream &str, const SMVData &obj)
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
unsigned int MediaNr
Definition: MediaManager.h:40
#define DBG
Definition: Logger.h:95