libzypp: zypp/repo/SUSEMediaVerifier.cc Source File

libzypp 17.38.14

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-core/base/Logger.h>
12#include <zypp-core/base/Gettext.h>
13#include <zypp/repo/SUSEMediaVerifier.h>
14#include <zypp/media/MediaHandler.h>
15
16using std::endl;
17
18namespace 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;
56 media::MediaNr _totalMedia = 0;
57 };
58
60 inline std::ostream & operator<<( std::ostream & str, const SMVData & obj )
61 { return str << "[" << obj._mediaVendor << "|" << obj._mediaIdent << "/" << obj._totalMedia << "]"; }
62
63
75 class SUSEMediaVerifier::Impl
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
91 media::MediaNr mediaNr() const
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;
113 media::MediaNr _mediaNr = 1;
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
125 SUSEMediaVerifier::SUSEMediaVerifier( const Pathname & path_r, media::MediaNr mediaNr_r )
126 : _pimpl { new Impl( path_r, mediaNr_r ) }
127 {}
128
129 SUSEMediaVerifier::SUSEMediaVerifier( const SUSEMediaVerifier & rhs, media::MediaNr mediaNr_r )
130 : _pimpl { new Impl( *rhs._pimpl, mediaNr_r ) }
131 {}
132
133 SUSEMediaVerifier::~SUSEMediaVerifier()
134 {}
135
136 bool SUSEMediaVerifier::valid() const
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
145 media::MediaNr SUSEMediaVerifier::totalMedia() const
146 { return _pimpl->smvData()._totalMedia; }
147
148 media::MediaNr SUSEMediaVerifier::mediaNr() const
149 { return _pimpl->mediaNr(); }
150
151
152 bool SUSEMediaVerifier::isDesiredMedia( const media::MediaHandler & ref ) const
153 {
154 bool ret = true; // optimistic return unless we definitely know 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 // if we have a downloading URL and can do relaxed verification we do not check the media file again
167 if ( relaxed && Url::schemeIsDownloading( ref.protocol() ) ) {
168 DBG << "Skipping verification due to downloading medium" << std::endl;
169 return ret;
170 }
171
172 Pathname mediaFile { _pimpl->mediaFilePath() };
173 try {
174 ref.provideFile( OnMediaLocation(mediaFile).setMirrorsAllowed(false) );
175 mediaFile = ref.localPath( mediaFile );
176 }
177 catch ( media::MediaFileNotFoundException & excpt_r )
178 {
179 if ( relaxed ) {
180 ZYPP_CAUGHT( excpt_r );
181 return ret;
182 }
183 excpt_r.addHistory( _pimpl->expectedAsUserString() );
184 ZYPP_RETHROW( excpt_r );
185 }
186 catch ( media::MediaNotAFileException & excpt_r )
187 {
188 if ( relaxed ) {
189 ZYPP_CAUGHT( excpt_r );
190 return ret;
191 }
192 excpt_r.addHistory( _pimpl->expectedAsUserString() );
193 ZYPP_CAUGHT( excpt_r ); return ret;
194 }
195
196 SMVData remote { mediaFile };
197 ret = smvData.matches( remote );
198 if ( ! ret ) {
199 DBG << "expect: " << smvData << " medium " << mediaNr() << endl;
200 DBG << "remote: " << remote << endl;
201 }
202 return ret;
203 }
204
205 std::ostream & operator<<( std::ostream & str, const SUSEMediaVerifier & obj )
206 { return str << *obj._pimpl; }
207
208 } // namespace repo
209} // namespace zypp
ZYPP_RETHROW
#define ZYPP_RETHROW(EXCPT)
Drops a logline and rethrows, updating the CodeLocation.
Definition Exception.h:479
ZYPP_CAUGHT
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
Definition Exception.h:475
Gettext.h
_
#define _(MSG)
Definition Gettext.h:39
Logger.h
DBG
#define DBG
Definition Logger.h:102
ERR
#define ERR
Definition Logger.h:105
SEC
#define SEC
Definition Logger.h:106
MediaHandler.h
SUSEMediaVerifier.h
bool
shared_ptr
zypp::Exception::addHistory
void addHistory(const std::string &msg_r)
Add some message text to the history.
Definition Exception.cc:189
zypp::OnMediaLocation
Describes a resource file located on a medium.
Definition onmedialocation.h:37
zypp::Url::schemeIsVolatile
bool schemeIsVolatile() const
Definition Url.h:284
zypp::Url::schemeIsDownloading
static bool schemeIsDownloading(const std::string &scheme_r)
http https ftp sftp tftp
Definition Url.cc:493
zypp::filesystem::Pathname
Pathname.
Definition Pathname.h:48
zypp::filesystem::Pathname::c_str
const char * c_str() const
String representation.
Definition Pathname.h:113
zypp::filesystem::Pathname::asString
const std::string & asString() const
String representation.
Definition Pathname.h:94
zypp::media::MediaFileNotFoundException
Definition mediaexception.h:164
zypp::media::MediaHandler
Abstract base class for 'physical' MediaHandler like MediaCD, etc.
Definition MediaHandler.h:50
zypp::media::MediaHandler::localPath
Pathname localPath(const Pathname &pathname) const
Files provided will be available at 'localPath(filename)'.
Definition MediaHandler.cc:664
zypp::media::MediaHandler::protocol
std::string protocol() const
Protocol hint for MediaAccess.
Definition MediaHandler.h:497
zypp::media::MediaHandler::provideFile
void provideFile(const OnMediaLocation &file) const
Use concrete handler to provide file denoted by path below 'localRoot'.
Definition MediaHandler.cc:969
zypp::media::MediaNotAFileException
Definition mediaexception.h:258
zypp::repo::SUSEMediaVerifier::Impl
SUSEMediaVerifier::Impl.
Definition SUSEMediaVerifier.cc:76
zypp::repo::SUSEMediaVerifier::Impl::mediaNr
media::MediaNr mediaNr() const
Definition SUSEMediaVerifier.cc:91
zypp::repo::SUSEMediaVerifier::Impl::mediaFilePath
Pathname mediaFilePath(media::MediaNr mediaNr_r=0) const
Definition SUSEMediaVerifier.cc:94
zypp::repo::SUSEMediaVerifier::Impl::expectedAsUserString
std::string expectedAsUserString() const
Definition SUSEMediaVerifier.cc:101
zypp::repo::SUSEMediaVerifier::Impl::Impl
Impl(const Impl &rhs, media::MediaNr mediaNr_r)
Definition SUSEMediaVerifier.cc:83
zypp::repo::SUSEMediaVerifier::Impl::_smvData
shared_ptr< SMVData > _smvData
Definition SUSEMediaVerifier.cc:112
zypp::repo::SUSEMediaVerifier::Impl::smvData
const SMVData & smvData() const
Definition SUSEMediaVerifier.cc:88
zypp::repo::SUSEMediaVerifier::Impl::Impl
Impl(const Pathname &path_r, media::MediaNr mediaNr_r)
Definition SUSEMediaVerifier.cc:78
zypp::repo::SUSEMediaVerifier::Impl::_mediaNr
media::MediaNr _mediaNr
Definition SUSEMediaVerifier.cc:113
zypp::repo::SUSEMediaVerifier::ident
const std::string & ident() const
Medias expected ident string.
Definition SUSEMediaVerifier.cc:142
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::repo::SUSEMediaVerifier::valid
bool valid() const
Data considered to be valid if we have vendor and ident.
Definition SUSEMediaVerifier.cc:136
zypp::repo::SUSEMediaVerifier::_pimpl
RW_pointer< Impl > _pimpl
Pointer to implementation.
Definition SUSEMediaVerifier.h:78
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::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::SUSEMediaVerifier::vendor
const std::string & vendor() const
Medias expected vendor string.
Definition SUSEMediaVerifier.cc:139
zypp::repo::SUSEMediaVerifier::~SUSEMediaVerifier
~SUSEMediaVerifier() override
Dtor.
Definition SUSEMediaVerifier.cc:133
zypp::repo::SUSEMediaVerifier::mediaNr
media::MediaNr mediaNr() const
Media number expected by this verifier (starts with 1).
Definition SUSEMediaVerifier.cc:148
str
String related utilities and Regular expression matching.
zypp::media::MediaNr
unsigned int MediaNr
Definition MediaManager.h:32
zypp::repo
Definition DeltaCandidates.cc:30
zypp::repo::operator<<
std::ostream & operator<<(std::ostream &str, const DeltaCandidates::Impl &obj)
relates: DeltaCandidates::Impl Stream output
Definition DeltaCandidates.cc:55
zypp::str::numstring
std::string numstring(char n, int w=0)
Definition String.h:290
zypp::str::strtonum
TInt strtonum(const C_Str &str)
Parsing numbers from string.
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
zypp::repo::SMVData
Data parsed from a media.1/media file.
Definition SUSEMediaVerifier.cc:26
zypp::repo::SMVData::SMVData
SMVData(const Pathname &path_r)
Definition SUSEMediaVerifier.cc:27
zypp::repo::SMVData::valid
bool valid() const
Data considered to be valid if we have vendor and ident.
Definition SUSEMediaVerifier.cc:47
zypp::repo::SMVData::_totalMedia
media::MediaNr _totalMedia
Definition SUSEMediaVerifier.cc:56
zypp::repo::SMVData::matches
bool matches(const SMVData &rhs) const
Whether rhs belongs to the same media set.
Definition SUSEMediaVerifier.cc:51
zypp::repo::SMVData::_mediaIdent
std::string _mediaIdent
Definition SUSEMediaVerifier.cc:55
zypp::repo::SMVData::_mediaVendor
std::string _mediaVendor
Definition SUSEMediaVerifier.cc:54
zypp::str::Format
Convenient building of std::string with boost::format.
Definition String.h:254
zypp::str::Format::str
std::string str() const
Definition String.h:264
zypp::str::Str
Convenient building of std::string via std::ostringstream Basically a std::ostringstream autoconverti...
Definition String.h:213