libzypp  17.31.31
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 
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 
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 
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 
65  {
66  return _totalMedia;
67  }
68 
69  std::ostream &SuseMediaDataVerifier::toStream( std::ostream &str ) const
70  {
71  return str << "[" << _mediaVendor << "|" << _mediaIdent << "/" << _totalMedia << "]";
72  }
73 
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 );
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 
117  { }
118 
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_FWD_DECL_TYPE_WITH_REFS(SuseMediaDataVerifier)
const std::string & mediaIdent() const override
#define _(MSG)
Definition: Gettext.h:37
uint totalMedia() const override
std::ostream & toStream(std::ostream &str) const override
const char * c_str() const
String representation.
Definition: Pathname.h:110
String related utilities and Regular expression matching.
bool loadFromMedium(const zypp::filesystem::Pathname &data, uint expectedMediaNr) override
Convenient building of std::string with boost::format.
Definition: String.h:252
std::string asString() const
Definition: String.h:262
#define ERR
Definition: Logger.h:98
std::ostream & operator<<(std::ostream &str, const MediaDataVerifierRef &obj)
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:211
const std::string & asString() const
String representation.
Definition: Pathname.h:91
bool valid() const override
Data considered to be valid if we have vendor and ident.
bool matches(const MediaDataVerifierRef &rhs) const override
Whether rhs belongs to the same media set.
TInt strtonum(const C_Str &str)
Parsing numbers from string.
Definition: String.h:388
zypp::filesystem::Pathname mediaFilePath(uint mediaNr) const override
std::string numstring(char n, int w=0)
Definition: String.h:289
static MediaDataVerifierRef createVerifier(const std::string &verifierType)
MediaDataVerifierRef clone() const override
std::string expectedAsUserString(uint mediaNr) const override
bool load(const zypp::Pathname &data) override
const std::string & mediaVendor() const override