libzypp  17.28.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>
15 
16 using std::endl;
17 
18 namespace zypp
19 {
20  namespace repo
21  {
25  struct SMVData
26  {
27  SMVData( const Pathname & path_r )
28  {
29  std::ifstream inp( path_r.c_str() );
30  if ( !inp ) {
31  ERR << "Can't setup a SUSEMediaVerifier from file: " << path_r.asString() << endl;
32  return;
33  }
34  getline( inp, _mediaVendor );
35  getline( inp, _mediaIdent );
36  std::string buffer;
37  getline( inp, buffer );
38  str::strtonum( buffer, _totalMedia );
39  //if ( !_totalMedia ) _totalMedia = 1;
40  }
41 
43  explicit operator bool() const
44  { return valid(); }
45 
47  bool valid() const
48  { return ! (_mediaVendor.empty() || _mediaIdent.empty()); }
49 
51  bool matches( const SMVData & rhs ) const
52  { return valid() && rhs._mediaVendor == _mediaVendor && rhs._mediaIdent == _mediaIdent; }
53 
54  std::string _mediaVendor;
55  std::string _mediaIdent;
57  };
58 
60  inline std::ostream & operator<<( std::ostream & str, const SMVData & obj )
61  { return str << "[" << obj._mediaVendor << "|" << obj._mediaIdent << "/" << obj._totalMedia << "]"; }
62 
63 
76  {
77  public:
78  Impl( const Pathname & path_r, media::MediaNr mediaNr_r )
79  : _smvData { new SMVData( path_r ) }
80  , _mediaNr { mediaNr_r }
81  {}
82 
83  Impl( const Impl & rhs, media::MediaNr mediaNr_r )
84  : _smvData { rhs._smvData }
85  , _mediaNr { mediaNr_r }
86  {}
87 
88  const SMVData & smvData() const
89  { return *_smvData; }
90 
92  { return _mediaNr; }
93 
94  Pathname mediaFilePath( media::MediaNr mediaNr_r = 0 ) const
95  {
96  str::Format fmt { "/media.%d/media" };
97  fmt % str::numstring( mediaNr_r ? mediaNr_r : _mediaNr );
98  return fmt.str();
99  }
100 
101  std::string expectedAsUserString() const
102  {
103  // Translator: %1% the expected medium number; %2% the total number of media in the set; %3% the ident file on the medium.
104  str::Format fmt { _("Expected medium %1%/%2% identified by file '%3%' with content:") };
105  return str::Str()
106  << ( fmt % mediaNr() % smvData()._totalMedia % mediaFilePath() ) << endl
107  << " " << smvData()._mediaVendor << endl
108  << " " << smvData()._mediaIdent;
109  }
110 
111  private:
112  shared_ptr<SMVData> _smvData;
114  };
115 
117  inline std::ostream & operator<<( std::ostream & str, const SUSEMediaVerifier::Impl & obj )
118  { return str << obj.smvData() << "(" << obj.mediaNr() << ")"; }
119 
120 
122  // SUSEMediaVerifier
124 
126  : _pimpl { new Impl( path_r, mediaNr_r ) }
127  {}
128 
130  : _pimpl { new Impl( *rhs._pimpl, mediaNr_r ) }
131  {}
132 
134  {}
135 
137  { return _pimpl->smvData().valid(); }
138 
139  const std::string & SUSEMediaVerifier::vendor() const
140  { return _pimpl->smvData()._mediaVendor; }
141 
142  const std::string & SUSEMediaVerifier::ident() const
143  { return _pimpl->smvData()._mediaIdent; }
144 
146  { return _pimpl->smvData()._totalMedia; }
147 
149  { return _pimpl->mediaNr(); }
150 
151 
153  {
154  bool ret = true; // optimistic return unless we definitely now it does not match
155 
156  const SMVData & smvData = _pimpl->smvData();
157  if ( ! smvData )
158  return ret; // we have no valid data
159 
160  // bsc#1180851: If there is just one not-volatile medium in the set
161  // tolerate a missing (vanished) media identifier and let the URL rule.
162  bool relaxed = smvData._totalMedia == 1 && ! Url::schemeIsVolatile( ref.protocol() );
163  SEC << smvData << endl;
164  SEC << ref.protocol() << " " << Url::schemeIsVolatile( ref.protocol() ) << endl;
165 
166  Pathname mediaFile { _pimpl->mediaFilePath() };
167  try {
168  ref.provideFile( OnMediaLocation(mediaFile) );
169  mediaFile = ref.localPath( mediaFile );
170  }
171  catch ( media::MediaFileNotFoundException & excpt_r )
172  {
173  if ( relaxed ) {
174  ZYPP_CAUGHT( excpt_r );
175  return ret;
176  }
177  excpt_r.addHistory( _pimpl->expectedAsUserString() );
178  ZYPP_RETHROW( excpt_r );
179  }
180  catch ( media::MediaNotAFileException & excpt_r )
181  {
182  if ( relaxed ) {
183  ZYPP_CAUGHT( excpt_r );
184  return ret;
185  }
186  excpt_r.addHistory( _pimpl->expectedAsUserString() );
187  ZYPP_CAUGHT( excpt_r ); return ret;
188  }
189 
190  SMVData remote { mediaFile };
191  ret = smvData.matches( remote );
192  if ( ! ret ) {
193  DBG << "expect: " << smvData << " medium " << mediaNr() << endl;
194  DBG << "remote: " << remote << endl;
195  }
196  return ret;
197  }
198 
199  std::ostream & operator<<( std::ostream & str, const SUSEMediaVerifier & obj )
200  { return str << *obj._pimpl; }
201 
202  } // namespace repo
203 } // namespace zypp
204 
zypp::repo::SMVData::_mediaIdent
std::string _mediaIdent
Definition: SUSEMediaVerifier.cc:55
zypp::repo::SMVData::valid
bool valid() const
Data considered to be valid if we have vendor and ident.
Definition: SUSEMediaVerifier.cc:47
ERR
#define ERR
Definition: Logger.h:98
zypp::media::MediaNr
unsigned int MediaNr
Definition: MediaManager.h:30
zypp::repo::SMVData::SMVData
SMVData(const Pathname &path_r)
Definition: SUSEMediaVerifier.cc:27
zypp::repo::SUSEMediaVerifier::Impl::Impl
Impl(const Pathname &path_r, media::MediaNr mediaNr_r)
Definition: SUSEMediaVerifier.cc:78
zypp::repo::SUSEMediaVerifier
Implementation of the traditional SUSE media verifier.
Definition: SUSEMediaVerifier.h:28
zypp::repo::SUSEMediaVerifier::SUSEMediaVerifier
SUSEMediaVerifier(const Pathname &path_r, media::MediaNr mediaNr_r=1)
Ctor creating a verifier by parsing media file.
Definition: SUSEMediaVerifier.cc:125
zypp::media::MediaFileNotFoundException
Definition: MediaException.h:146
zypp::repo::SUSEMediaVerifier::isDesiredMedia
bool isDesiredMedia(const media::MediaHandler &ref_r) const override
Check if \ref_r accesses the desired media.
Definition: SUSEMediaVerifier.cc:152
zypp::repo::SMVData
Data parsed from a media.1/media file.
Definition: SUSEMediaVerifier.cc:26
MediaHandler.h
zypp::OnMediaLocation
Describes a resource file located on a medium.
Definition: OnMediaLocation.h:39
SEC
#define SEC
Definition: Logger.h:99
zypp::media::MediaHandler
Abstract base class for 'physical' MediaHandler like MediaCD, etc.
Definition: MediaHandler.h:51
zypp::repo::SUSEMediaVerifier::Impl::_mediaNr
media::MediaNr _mediaNr
Definition: SUSEMediaVerifier.cc:113
zypp::Exception::addHistory
void addHistory(const std::string &msg_r)
Add some message text to the history.
Definition: Exception.cc:125
zypp::repo::SUSEMediaVerifier::vendor
const std::string & vendor() const
Medias expected vendor string.
Definition: SUSEMediaVerifier.cc:139
ZYPP_CAUGHT
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
Definition: Exception.h:396
zypp::repo::SUSEMediaVerifier::Impl::smvData
const SMVData & smvData() const
Definition: SUSEMediaVerifier.cc:88
zypp::iostr::getline
std::string getline(std::istream &str)
Read one line from stream.
Definition: IOStream.cc:33
bool
ZYPP_RETHROW
#define ZYPP_RETHROW(EXCPT)
Drops a logline and rethrows, updating the CodeLocation.
Definition: Exception.h:400
zypp::repo::SMVData::_totalMedia
media::MediaNr _totalMedia
Definition: SUSEMediaVerifier.cc:56
zypp::repo::operator<<
std::ostream & operator<<(std::ostream &str, const DeltaCandidates &obj)
Definition: DeltaCandidates.cc:107
zypp::repo::SUSEMediaVerifier::Impl::operator<<
std::ostream & operator<<(std::ostream &str, const SUSEMediaVerifier::Impl &obj)
Stream output
Definition: SUSEMediaVerifier.cc:117
zypp::repo::SMVData::_mediaVendor
std::string _mediaVendor
Definition: SUSEMediaVerifier.cc:54
zypp::repo::SMVData::matches
bool matches(const SMVData &rhs) const
Whether rhs belongs to the same media set.
Definition: SUSEMediaVerifier.cc:51
_
#define _(MSG)
Definition: Gettext.h:37
zypp::repo::SUSEMediaVerifier::Impl
SUSEMediaVerifier::Impl.
Definition: SUSEMediaVerifier.cc:76
zypp::filesystem::Pathname::c_str
const char * c_str() const
String representation.
Definition: Pathname.h:110
zypp::repo::SUSEMediaVerifier::Impl::_smvData
shared_ptr< SMVData > _smvData
Definition: SUSEMediaVerifier.cc:112
zypp::media::MediaNotAFileException
Definition: MediaException.h:236
zypp::repo::SUSEMediaVerifier::mediaNr
media::MediaNr mediaNr() const
Media number expected by this verifier (starts with 1).
Definition: SUSEMediaVerifier.cc:148
zypp::repo::SMVData::operator<<
std::ostream & operator<<(std::ostream &str, const SMVData &obj)
Stream output
Definition: SUSEMediaVerifier.cc:60
zypp::repo::SUSEMediaVerifier::~SUSEMediaVerifier
~SUSEMediaVerifier() override
Dtor.
Definition: SUSEMediaVerifier.cc:133
zypp::repo::SUSEMediaVerifier::ident
const std::string & ident() const
Medias expected ident string.
Definition: SUSEMediaVerifier.cc:142
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
zypp::str::numstring
std::string numstring(char n, int w=0)
Definition: String.h:289
zypp::Url::schemeIsVolatile
bool schemeIsVolatile() const
Definition: Url.h:271
zypp::repo::SUSEMediaVerifier::totalMedia
media::MediaNr totalMedia() const
The total number of media in this set (or 0 if not known).
Definition: SUSEMediaVerifier.cc:145
zypp::media::MediaHandler::localPath
Pathname localPath(const Pathname &pathname) const
Files provided will be available at 'localPath(filename)'.
Definition: MediaHandler.cc:700
zypp::str::Str
Convenient building of std::string via std::ostringstream Basically a std::ostringstream autoconverti...
Definition: String.h:212
DBG
#define DBG
Definition: Logger.h:95
zypp::repo::SUSEMediaVerifier::_pimpl
RW_pointer< Impl > _pimpl
Pointer to implementation.
Definition: SUSEMediaVerifier.h:78
zypp::repo::SUSEMediaVerifier::Impl::mediaNr
media::MediaNr mediaNr() const
Definition: SUSEMediaVerifier.cc:91
zypp::media::MediaHandler::provideFile
void provideFile(const OnMediaLocation &file) const
Use concrete handler to provide file denoted by path below 'localRoot'.
Definition: MediaHandler.cc:1005
zypp::repo::SUSEMediaVerifier::Impl::mediaFilePath
Pathname mediaFilePath(media::MediaNr mediaNr_r=0) const
Definition: SUSEMediaVerifier.cc:94
zypp::repo::SUSEMediaVerifier::Impl::Impl
Impl(const Impl &rhs, media::MediaNr mediaNr_r)
Definition: SUSEMediaVerifier.cc:83
zypp::filesystem::Pathname
Pathname.
Definition: Pathname.h:45
zypp::repo::SUSEMediaVerifier::Impl::expectedAsUserString
std::string expectedAsUserString() const
Definition: SUSEMediaVerifier.cc:101
SUSEMediaVerifier.h
zypp::media::MediaHandler::protocol
std::string protocol() const
Protocol hint for MediaAccess.
Definition: MediaHandler.h:498
zypp::str::Format
Convenient building of std::string with boost::format.
Definition: String.h:253
str
String related utilities and Regular expression matching.
zypp::filesystem::Pathname::asString
const std::string & asString() const
String representation.
Definition: Pathname.h:91
zypp::repo::SUSEMediaVerifier::valid
bool valid() const
Data considered to be valid if we have vendor and ident.
Definition: SUSEMediaVerifier.cc:136
zypp::str::strtonum
TInt strtonum(const C_Str &str)
Parsing numbers from string.
Definition: String.h:388