libzypp 17.37.10

mediaverifier.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
9
10#include "mediaverifier.h"
11#include <ostream>
12#include <fstream>
13#include <zypp-core/Pathname.h>
14#include <zypp-core/base/String.h>
15#include <zypp-core/base/Gettext.h>
16#include "private/providedbg_p.h"
17
18 namespace zyppng {
19
20 ZYPP_FWD_DECL_TYPE_WITH_REFS (SuseMediaDataVerifier);
21
22 class SuseMediaDataVerifier : public MediaDataVerifier
23 {
24 public:
25 // MediaDataVerifier interface
26 bool valid() const override;
27 bool matches(const MediaDataVerifierRef &rhs) const override;
28 const std::string &mediaVendor() const override;
29 const std::string &mediaIdent() const override;
30 uint totalMedia() const override;
31 std::ostream &toStream(std::ostream &str) const override;
32 bool load( const zypp::Pathname &data ) override;
33 bool loadFromMedium(const zypp::filesystem::Pathname &data, uint expectedMediaNr ) override;
34 zypp::filesystem::Pathname mediaFilePath(uint mediaNr) const override;
35 MediaDataVerifierRef clone () const override;
36 std::string expectedAsUserString( uint mediaNr ) const override;
37
38 private:
39 std::string _mediaVendor;
40 std::string _mediaIdent;
41 uint _totalMedia = 0;
42 };
43
44 zypp::Pathname SuseMediaDataVerifier::mediaFilePath(uint mediaNr) const
45 {
46 zypp::str::Format fmt { "/media.%d/media" };
47 return (fmt % zypp::str::numstring( mediaNr )).asString();
48 }
49
50 bool SuseMediaDataVerifier::loadFromMedium( const zypp::filesystem::Pathname &data, uint expectedMediaNr )
51 {
52 return load ( data / mediaFilePath(expectedMediaNr) );
53 }
54
55 bool SuseMediaDataVerifier::valid() const
56 { return ! (_mediaVendor.empty() || _mediaIdent.empty()); }
57
58 bool SuseMediaDataVerifier::matches(const MediaDataVerifierRef &rhs) const
59 {
60 auto conv = std::dynamic_pointer_cast<SuseMediaDataVerifier>(rhs);
61 return conv && valid() && conv->_mediaVendor == _mediaVendor && conv->_mediaIdent == _mediaIdent;
62 }
63
64 uint SuseMediaDataVerifier::totalMedia() const
65 {
66 return _totalMedia;
67 }
68
69 std::ostream &SuseMediaDataVerifier::toStream( std::ostream &str ) const
70 {
71 return str << "[" << _mediaVendor << "|" << _mediaIdent << "/" << _totalMedia << "]";
72 }
73
74 bool SuseMediaDataVerifier::load( const zypp::Pathname &path_r )
75 {
76 std::ifstream inp( path_r.c_str() );
77 if ( !inp ) {
78 ERR << "Can't setup a SUSEMediaVerifier from file: " << path_r.asString() << std::endl;
79 return false;
80 }
81 getline( inp, _mediaVendor );
82 getline( inp, _mediaIdent );
83 std::string buffer;
84 getline( inp, buffer );
85 zypp::str::strtonum( buffer, _totalMedia );
86 //if ( !_totalMedia ) _totalMedia = 1;
87 // loaded but maybe not valid
88 return true;
89 }
90
91 const std::string &SuseMediaDataVerifier::mediaIdent() const
92 {
93 return _mediaIdent;
94 }
95
96 const std::string &SuseMediaDataVerifier::mediaVendor() const
97 {
98 return _mediaVendor;
99 }
100
101 MediaDataVerifierRef SuseMediaDataVerifier::clone () const
102 {
103 return SuseMediaDataVerifierRef( new SuseMediaDataVerifier( *this ) );
104 }
105
106 std::string SuseMediaDataVerifier::expectedAsUserString( uint mediaNr ) const
107 {
108 // Translator: %1% the expected medium number; %2% the total number of media in the set; %3% the ident file on the medium.
109 zypp::str::Format fmt { _("Expected medium %1%/%2% identified by file '%3%' with content:") };
110 return zypp::str::Str()
111 << ( fmt % mediaNr % _totalMedia % mediaFilePath( mediaNr ) ) << "\n"
112 << " " << _mediaVendor << "\n"
113 << " " << _mediaIdent;
114 }
115
116 MediaDataVerifier::MediaDataVerifier() noexcept
117 { }
118
119 MediaDataVerifier::~MediaDataVerifier()
120 { }
121
122 MediaDataVerifierRef MediaDataVerifier::createVerifier( const std::string &verifierType )
123 {
124 if ( verifierType == "SuseMediaV1" ) {
125 return SuseMediaDataVerifierRef( new SuseMediaDataVerifier() );
126 }
127 return nullptr;
128 }
129
130 std::ostream &operator<<(std::ostream &str, const MediaDataVerifierRef &obj)
131 {
132 if ( obj )
133 return obj->toStream(str);
134 return str << "[MediaVerifier: null]";
135 }
136
137}
zypp::filesystem::Pathname
Pathname.
Definition Pathname.h:47
zypp::filesystem::Pathname::c_str
const char * c_str() const
String representation.
Definition Pathname.h:112
zypp::filesystem::Pathname::asString
const std::string & asString() const
String representation.
Definition Pathname.h:93
zyppng::MediaDataVerifier::MediaDataVerifier
MediaDataVerifier() noexcept
Definition mediaverifier.cc:116
zyppng::MediaDataVerifier::createVerifier
static MediaDataVerifierRef createVerifier(const std::string &verifierType)
Definition mediaverifier.cc:122
zyppng::MediaDataVerifier::~MediaDataVerifier
virtual ~MediaDataVerifier()
Definition mediaverifier.cc:119
zyppng::SuseMediaDataVerifier
Definition mediaverifier.cc:23
zyppng::SuseMediaDataVerifier::valid
bool valid() const override
Data considered to be valid if we have vendor and ident.
Definition mediaverifier.cc:55
zyppng::SuseMediaDataVerifier::_mediaIdent
std::string _mediaIdent
Definition mediaverifier.cc:40
zyppng::SuseMediaDataVerifier::clone
MediaDataVerifierRef clone() const override
Definition mediaverifier.cc:101
zyppng::SuseMediaDataVerifier::mediaVendor
const std::string & mediaVendor() const override
Definition mediaverifier.cc:96
zyppng::SuseMediaDataVerifier::mediaIdent
const std::string & mediaIdent() const override
Definition mediaverifier.cc:91
zyppng::SuseMediaDataVerifier::totalMedia
uint totalMedia() const override
Definition mediaverifier.cc:64
zyppng::SuseMediaDataVerifier::mediaFilePath
zypp::filesystem::Pathname mediaFilePath(uint mediaNr) const override
Definition mediaverifier.cc:44
zyppng::SuseMediaDataVerifier::expectedAsUserString
std::string expectedAsUserString(uint mediaNr) const override
Definition mediaverifier.cc:106
zyppng::SuseMediaDataVerifier::_totalMedia
uint _totalMedia
Definition mediaverifier.cc:41
zyppng::SuseMediaDataVerifier::loadFromMedium
bool loadFromMedium(const zypp::filesystem::Pathname &data, uint expectedMediaNr) override
Definition mediaverifier.cc:50
zyppng::SuseMediaDataVerifier::load
bool load(const zypp::Pathname &data) override
Definition mediaverifier.cc:74
zyppng::SuseMediaDataVerifier::_mediaVendor
std::string _mediaVendor
Definition mediaverifier.cc:39
zyppng::SuseMediaDataVerifier::matches
bool matches(const MediaDataVerifierRef &rhs) const override
Whether rhs belongs to the same media set.
Definition mediaverifier.cc:58
zyppng::SuseMediaDataVerifier::toStream
std::ostream & toStream(std::ostream &str) const override
Definition mediaverifier.cc:69
mediaverifier.h
str
String related utilities and Regular expression matching.
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::operator<<
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
Definition SerialNumber.cc:52
zyppng
Definition MediaNetworkRequestExecutor.h:16
providedbg_p.h
zypp::str::Format
Convenient building of std::string with boost::format.
Definition String.h:254
zypp::str::Format::asString
std::string asString() const
Definition String.h:263
zypp::str::Str
Convenient building of std::string via std::ostringstream Basically a std::ostringstream autoconverti...
Definition String.h:213
Pathname.h
Gettext.h
_
#define _(MSG)
Definition Gettext.h:39
ERR
#define ERR
Definition Logger.h:102
String.h
ZYPP_FWD_DECL_TYPE_WITH_REFS
#define ZYPP_FWD_DECL_TYPE_WITH_REFS(T)
Definition zyppglobal.h:126