libzypp  17.31.31
RepoFileReader.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #include <iostream>
13 #include <zypp/base/LogTools.h>
14 #include <zypp/base/String.h>
15 #include <zypp/base/StringV.h>
16 #include <zypp-core/base/InputStream>
17 #include <zypp-core/base/UserRequestException>
18 
19 #include <zypp-core/parser/IniDict>
21 
22 using std::endl;
23 
25 namespace zypp
26 {
28  namespace parser
29  {
31  namespace {
32 
37  class RepoFileParser : public IniDict
38  {
39  public:
40  RepoFileParser( const InputStream & is_r )
41  { read( is_r ); }
42 
43  using IniDict::consume; // don't hide overloads we don't redefine here
44 
45  virtual void consume( const std::string & section_r, const std::string & key_r, const std::string & value_r )
46  {
47  if ( key_r == "baseurl" )
48  {
49  _inMultiline = MultiLine::baseurl;
50  storeUrl( _baseurls[section_r], value_r );
51  }
52  else if ( key_r == "gpgkey" )
53  {
54  _inMultiline = MultiLine::gpgkey;
55  storeUrl( _gpgkeys[section_r], value_r );
56  }
57  else if ( key_r == "mirrorlist" )
58  {
59  _inMultiline = MultiLine::mirrorlist;
60  storeUrl( _mirrorlist[section_r], value_r );
61  }
62  else if ( key_r == "metalink" )
63  {
64  _inMultiline = MultiLine::metalink;
65  storeUrl( _metalink[section_r], value_r );
66  }
67  else
68  {
69  _inMultiline = MultiLine::none;
70  IniDict::consume( section_r, key_r, value_r );
71  }
72  }
73 
74  virtual void garbageLine( const std::string & section_r, const std::string & line_r )
75  {
76  switch ( _inMultiline )
77  {
78  case MultiLine::baseurl:
79  storeUrl( _baseurls[section_r], line_r );
80  break;
81 
82  case MultiLine::gpgkey:
83  storeUrl( _gpgkeys[section_r], line_r );
84  break;
85 
86  case MultiLine::mirrorlist:
87  storeUrl( _mirrorlist[section_r], line_r );
88  break;
89 
90  case MultiLine::metalink:
91  storeUrl( _metalink[section_r], line_r );
92  break;
93 
94  case MultiLine::none:
95  IniDict::garbageLine( section_r, line_r ); // throw
96  break;
97  }
98  }
99 
100  std::list<Url> & baseurls( const std::string & section_r )
101  { return _baseurls[section_r]; }
102 
103  std::list<Url> & gpgkeys( const std::string & section_r )
104  { return _gpgkeys[section_r]; }
105 
106  std::list<Url> & mirrorlist( const std::string & section_r )
107  { return _mirrorlist[section_r]; }
108 
109  std::list<Url> & metalink( const std::string & section_r )
110  { return _metalink[section_r]; }
111 
112  private:
113  void storeUrl( std::list<Url> & store_r, const std::string & line_r )
114  {
115  // #285: Fedora/dnf allows WS separated urls (and an optional comma)
116  strv::splitRx( line_r, "[,[:blank:]]*[[:blank:]][,[:blank:]]*", [&store_r]( std::string_view w ) {
117  if ( ! w.empty() )
118  store_r.push_back( Url(std::string(w)) );
119  });
120  }
121 
122  enum class MultiLine { none, baseurl, gpgkey, mirrorlist, metalink };
123  MultiLine _inMultiline = MultiLine::none;
124 
125  std::map<std::string,std::list<Url>> _baseurls;
126  std::map<std::string,std::list<Url>> _gpgkeys;
127  std::map<std::string,std::list<Url>> _mirrorlist;
128  std::map<std::string,std::list<Url>> _metalink;
129  };
130 
131  } //namespace
133 
138  static void repositories_in_stream( const InputStream &is,
139  const RepoFileReader::ProcessRepo &callback,
140  const ProgressData::ReceiverFnc &progress )
141  try {
142  RepoFileParser dict(is);
143  for_( its, dict.sectionsBegin(), dict.sectionsEnd() )
144  {
145  RepoInfo info;
146  info.setAlias(*its);
147  std::string proxy;
148  std::string proxyport;
149 
150  for_( it, dict.entriesBegin(*its), dict.entriesEnd(*its) )
151  {
152  //MIL << (*it).first << endl;
153  if (it->first == "name" )
154  info.setName(it-> second);
155  else if ( it->first == "enabled" )
156  info.setEnabled( str::strToTrue( it->second ) );
157  else if ( it->first == "priority" )
158  info.setPriority( str::strtonum<unsigned>( it->second ) );
159  else if ( it->first == "path" )
160  info.setPath( Pathname(it->second) );
161  else if ( it->first == "type" )
162  ; // bsc#1177427 et.al.: type in a .repo file is legacy - ignore it and let RepoManager probe
163  else if ( it->first == "autorefresh" )
164  info.setAutorefresh( str::strToTrue( it->second ) );
165  else if ( it->first == "gpgcheck" )
166  info.setGpgCheck( str::strToTriBool( it->second ) );
167  else if ( it->first == "repo_gpgcheck" )
168  info.setRepoGpgCheck( str::strToTrue( it->second ) );
169  else if ( it->first == "pkg_gpgcheck" )
170  info.setPkgGpgCheck( str::strToTrue( it->second ) );
171  else if ( it->first == "keeppackages" )
172  info.setKeepPackages( str::strToTrue( it->second ) );
173  else if ( it->first == "service" )
174  info.setService( it->second );
175  else if ( it->first == "proxy" )
176  {
177  // Translate it into baseurl queryparams
178  // NOTE: The hack here does not add proxy to mirrorlist urls but the
179  // original code worked without complains, so keep it for now.
180  static const str::regex ex( ":[0-9]+$" ); // portspec
181  str::smatch what;
182  if ( str::regex_match( it->second, what, ex ) )
183  {
184  proxy = it->second.substr( 0, it->second.size() - what[0].size() );
185  proxyport = what[0].substr( 1 );
186  }
187  else
188  {
189  proxy = it->second;
190  }
191  }
192  else
193  ERR << "Unknown attribute in [" << *its << "]: " << it->first << "=" << it->second << " ignored" << endl;
194  }
195 
196  for ( auto & url : dict.baseurls( *its ) )
197  {
198  if ( ! proxy.empty() && url.getQueryParam( "proxy" ).empty() )
199  {
200  url.setQueryParam( "proxy", proxy );
201  url.setQueryParam( "proxyport", proxyport );
202  }
203  info.addBaseUrl( url );
204  }
205 
206  if ( ! dict.gpgkeys( *its ).empty() )
207  info.setGpgKeyUrls( std::move(dict.gpgkeys( *its )) );
208 
209  if ( ! dict.mirrorlist( *its ).empty() )
210  info.setMirrorListUrls( std::move(dict.mirrorlist( *its )) );
211 
212  if ( ! dict.metalink( *its ).empty() )
213  info.setMetalinkUrls( std::move(dict.metalink( *its )) );
214 
215 
216  info.setFilepath(is.path());
217  MIL << info << endl;
218  // add it to the list.
219  callback(info);
220  //if (!progress.tick())
221  // ZYPP_THROW(AbortRequestException());
222  }
223  }
224  catch ( Exception & ex ) {
225  ex.addHistory( "Parsing .repo file "+is.name() );
226  ZYPP_RETHROW( ex );
227  }
228 
230  //
231  // CLASS NAME : RepoFileReader
232  //
234 
236  const ProcessRepo & callback,
237  const ProgressData::ReceiverFnc &progress )
238  : _callback(callback)
239  {
240  repositories_in_stream(InputStream(repo_file), _callback, progress);
241  }
242 
244  const ProcessRepo & callback,
245  const ProgressData::ReceiverFnc &progress )
246  : _callback(callback)
247  {
248  repositories_in_stream(is, _callback, progress);
249  }
250 
252  {}
253 
254 
255  std::ostream & operator<<( std::ostream & str, const RepoFileReader & obj )
256  {
257  return str;
258  }
259 
260  } // namespace parser
262 } // namespace zypp
MultiLine _inMultiline
TriBool strToTriBool(const C_Str &str)
Parse str into a bool if it&#39;s a legal true or false string; else indeterminate.
Definition: String.cc:93
#define MIL
Definition: Logger.h:96
unsigned size() const
Definition: Regex.cc:106
constexpr std::string_view Url("url")
function< bool(const RepoInfo &)> ProcessRepo
Callback definition.
void setAutorefresh(bool autorefresh)
enable or disable autorefresh
Definition: RepoInfoBase.cc:91
Regular expression.
Definition: Regex.h:94
void setPriority(unsigned newval_r)
Set repository priority for solver.
Definition: RepoInfo.cc:400
virtual void consume(const std::string &section)
Called when a section is found.
Definition: inidict.cc:64
std::map< std::string, std::list< Url > > _gpgkeys
std::map< std::string, std::list< Url > > _mirrorlist
void setEnabled(bool enabled)
enable or disable the repository
Definition: RepoInfoBase.cc:88
std::map< std::string, std::list< Url > > _baseurls
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition: Easy.h:28
void addHistory(const std::string &msg_r)
Add some message text to the history.
Definition: Exception.cc:140
void setAlias(const std::string &alias)
set the repository alias
Definition: RepoInfoBase.cc:94
String related utilities and Regular expression matching.
void setFilepath(const Pathname &filename)
set the path to the .repo file
What is known about a repository.
Definition: RepoInfo.h:71
void setGpgCheck(TriBool value_r)
Set the value for gpgCheck (or indeterminate to use the default).
Definition: RepoInfo.cc:407
Helper to create and pass std::istream.
Definition: inputstream.h:56
RepoFileReader(const Pathname &repo_file, const ProcessRepo &callback, const ProgressData::ReceiverFnc &progress=ProgressData::ReceiverFnc())
Constructor.
std::map< std::string, std::string > read(const Pathname &_path)
Read sysconfig file path_r and return (key,valye) pairs.
Definition: sysconfig.cc:34
function< bool(const ProgressData &)> ReceiverFnc
Most simple version of progress reporting The percentage in most cases.
Definition: progressdata.h:140
#define ERR
Definition: Logger.h:98
void setMirrorListUrls(url_set urls)
Like setMirrorListUrl but take an url_set.
Definition: RepoInfo.cc:518
static void repositories_in_stream(const InputStream &is, const RepoFileReader::ProcessRepo &callback, const ProgressData::ReceiverFnc &progress)
List of RepoInfo&#39;s from a file.
#define ZYPP_RETHROW(EXCPT)
Drops a logline and rethrows, updating the CodeLocation.
Definition: Exception.h:440
std::map< std::string, std::list< Url > > _metalink
std::ostream & operator<<(std::ostream &str, const ProductFileData &obj)
void setRepoGpgCheck(TriBool value_r)
Set the value for repoGpgCheck (or indeterminate to use the default).
Definition: RepoInfo.cc:425
void setPath(const Pathname &path)
set the product path.
Definition: RepoInfo.cc:652
void setService(const std::string &name)
sets service which added this repository
Definition: RepoInfo.cc:671
void setKeepPackages(bool keep)
Set if packaqes downloaded from this repository will be kept in local cache.
Definition: RepoInfo.cc:668
Read repository data from a .repo file.
void addBaseUrl(const Url &url)
Add a base url.
Definition: RepoInfo.cc:635
const Pathname & path() const
Path to the input file or empty if no file.
Definition: inputstream.h:111
virtual void garbageLine(const std::string &section, const std::string &line)
Called whenever a garbage line is found.
Definition: iniparser.cc:72
const ProcessCredentials & _callback
void setGpgKeyUrls(url_set urls)
Set a list of gpgkey URLs defined for this repo.
Definition: RepoInfo.cc:527
bool strToTrue(const C_Str &str)
Parsing boolean from string.
Definition: String.cc:63
Regular expression match result.
Definition: Regex.h:167
Base class for Exception.
Definition: Exception.h:145
bool regex_match(const std::string &s, smatch &matches, const regex &regex)
regex ZYPP_STR_REGEX regex ZYPP_STR_REGEX
Definition: Regex.h:70
void setMetalinkUrls(url_set urls)
Like setMirrorListUrls but expect metalink format.
Definition: RepoInfo.cc:524
void setName(const std::string &name)
set the repository name
Definition: RepoInfoBase.cc:97
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
void setPkgGpgCheck(TriBool value_r)
Set the value for pkgGpgCheck (or indeterminate to use the default).
Definition: RepoInfo.cc:435