libzypp 17.37.10

ServiceFileReader.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12#include <iostream>
13#include <zypp/base/Logger.h>
14#include <zypp/base/String.h>
15#include <zypp/base/Regex.h>
16#include <zypp-core/base/InputStream>
17#include <zypp-core/base/UserRequestException>
18
19#include <zypp-core/parser/IniDict>
20#include <zypp/parser/ServiceFileReader.h>
21#include <zypp/ServiceInfo.h>
22
23 using std::endl;
24 using zypp::parser::IniDict;
25
27 namespace zypp
28{
30 namespace parser
31 {
32
33 class ServiceFileReader::Impl
34 {
35 public:
36 static void parseServices( const Pathname & file,
37 const ServiceFileReader::ProcessService & callback );
38 };
39
40 void ServiceFileReader::Impl::parseServices( const Pathname & file,
41 const ServiceFileReader::ProcessService & callback/*,
42 const ProgressData::ReceiverFnc &progress*/ )
43 try {
44 InputStream is(file);
45 if( is.stream().fail() )
46 {
47 ZYPP_THROW(Exception("Failed to open service file"));
48 }
49
50 parser::IniDict dict(is);
51 for ( parser::IniDict::section_const_iterator its = dict.sectionsBegin();
52 its != dict.sectionsEnd();
53 ++its )
54 {
55 MIL << (*its) << endl;
56
57 ServiceInfo service(*its);
58 std::map<std::string,std::pair<std::string,ServiceInfo::RepoState>> repoStates; // <repo_NUM,< alias,RepoState >>
59
60 for ( IniDict::entry_const_iterator it = dict.entriesBegin(*its);
61 it != dict.entriesEnd(*its);
62 ++it )
63 {
64 // MIL << (*it).first << endl;
65 if ( it->first == "name" )
66 service.setName( it->second );
67 else if ( it->first == "url" && ! it->second.empty() )
68 service.setUrl( Url (it->second) );
69 else if ( it->first == "enabled" )
70 service.setEnabled( str::strToTrue( it->second ) );
71 else if ( it->first == "autorefresh" )
72 service.setAutorefresh( str::strToTrue( it->second ) );
73 else if ( it->first == "type" )
74 service.setType( repo::ServiceType(it->second) );
75 else if ( it->first == "ttl_sec" )
76 service.setTtl( str::strtonum<Date::Duration>(it->second) );
77 else if ( it->first == "lrf_dat" )
78 service.setLrf( Date( it->second ) );
79 else if ( it->first == "repostoenable" )
80 {
81 std::vector<std::string> aliases;
82 str::splitEscaped( it->second, std::back_inserter(aliases) );
83 for_( ait, aliases.begin(), aliases.end() )
84 {
85 service.addRepoToEnable( *ait );
86 }
87 }
88 else if ( it->first == "repostodisable" )
89 {
90 std::vector<std::string> aliases;
91 str::splitEscaped( it->second, std::back_inserter(aliases) );
92 for_( ait, aliases.begin(), aliases.end() )
93 {
94 service.addRepoToDisable( *ait );
95 }
96 }
97 else if ( str::startsWith( it->first, "repo_" ) )
98 {
99 static str::regex rxexpr( "([0-9]+)(_(.*))?" );
100 str::smatch what;
101 if ( str::regex_match( it->first.c_str()+5/*repo_*/, what, rxexpr ) )
102 {
103 std::string tag( what[1] );
104 if ( what.size() > 3 )
105 {
106 // attribute
107 if ( what[3] == "enabled" )
108 repoStates[tag].second.enabled = str::strToBool( it->second, repoStates[tag].second.enabled );
109 else if ( what[3] == "autorefresh" )
110 repoStates[tag].second.autorefresh = str::strToBool( it->second, repoStates[tag].second.autorefresh );
111 else if ( what[3] == "priority" )
112 str::strtonum( it->second, repoStates[tag].second.priority );
113 else
114 ERR << "Unknown attribute " << it->first << " ignored" << endl;
115 }
116 else
117 {
118 // alias
119 repoStates[tag].first = it->second;
120 }
121 }
122 else
123 ERR << "Unknown attribute " << it->first << " ignored" << endl;
124 }
125 else
126 ERR << "Unknown attribute " << it->first << " ignored" << endl;
127 }
128
129 if ( ! repoStates.empty() )
130 {
131 ServiceInfo::RepoStates data;
132 for ( const auto & el : repoStates )
133 {
134 if ( el.second.first.empty() )
135 ERR << "Missing alias for repo_" << el.first << "; ignore entry" << endl;
136 else
137 data[el.second.first] = el.second.second;
138 }
139 if ( ! data.empty() )
140 service.setRepoStates( std::move(data) );
141 }
142
143 MIL << "Linking ServiceInfo with file " << file << endl;
144 service.setFilepath(file);
145
146 // add it to the list.
147 if ( !callback(service) )
148 ZYPP_THROW(AbortRequestException());
149 }
150 }
151 catch ( Exception & ex ) {
152 ex.addHistory( "Parsing .service file "+file.asString() );
153 ZYPP_RETHROW( ex );
154 }
155
157 //
158 // CLASS NAME : RepoFileReader
159 //
161
162 ServiceFileReader::ServiceFileReader( const Pathname & repo_file,
163 const ProcessService & callback/*,
164 const ProgressData::ReceiverFnc &progress */)
165 {
166 Impl::parseServices(repo_file, callback/*, progress*/);
167 //MIL << "Done" << endl;
168 }
169
170 ServiceFileReader::~ServiceFileReader()
171 {}
172
173 std::ostream & operator<<( std::ostream & str, const ServiceFileReader & obj )
174 {
175 return str;
176 }
177
179 } // namespace parser
182} // namespace zypp
ServiceFileReader.h
ServiceInfo.h
zypp::Date
Store and operate on date (time_t).
Definition Date.h:33
zypp::Exception
Base class for Exception.
Definition Exception.h:153
zypp::Exception::addHistory
void addHistory(const std::string &msg_r)
Add some message text to the history.
Definition Exception.cc:189
zypp::InputStream
Helper to create and pass std::istream.
Definition inputstream.h:57
zypp::InputStream::stream
std::istream & stream() const
The std::istream.
Definition inputstream.h:93
zypp::ServiceInfo
Service data.
Definition ServiceInfo.h:37
zypp::ServiceInfo::setLrf
void setLrf(Date lrf_r)
Set date of last refresh.
Definition ServiceInfo.cc:120
zypp::ServiceInfo::setType
void setType(const repo::ServiceType &type)
Set service type.
Definition ServiceInfo.cc:112
zypp::ServiceInfo::RepoStates
std::map< std::string, RepoState > RepoStates
Definition ServiceInfo.h:185
zypp::ServiceInfo::addRepoToDisable
void addRepoToDisable(const std::string &alias_r)
Add alias_r to the set of ReposToDisable.
Definition ServiceInfo.cc:151
zypp::ServiceInfo::setTtl
void setTtl(Date::Duration ttl_r)
Set sugested TTL.
Definition ServiceInfo.cc:116
zypp::ServiceInfo::addRepoToEnable
void addRepoToEnable(const std::string &alias_r)
Add alias_r to the set of ReposToEnable.
Definition ServiceInfo.cc:130
zypp::ServiceInfo::setUrl
void setUrl(const Url &url)
Set the service url (raw value)
Definition ServiceInfo.cc:108
zypp::ServiceInfo::setRepoStates
void setRepoStates(RepoStates newStates_r)
Remember a new set of repository states.
Definition ServiceInfo.cc:165
zypp::Url
Url manipulation class.
Definition Url.h:93
zypp::filesystem::Pathname
Pathname.
Definition Pathname.h:47
zypp::parser::IniDict
Parses a INI file and offers its structure as a dictionary.
Definition inidict.h:42
zypp::parser::IniDict::sectionsEnd
section_const_iterator sectionsEnd() const
Definition inidict.cc:113
zypp::parser::IniDict::entriesBegin
entry_const_iterator entriesBegin(const std::string &section) const
Definition inidict.cc:75
zypp::parser::IniDict::sectionsBegin
section_const_iterator sectionsBegin() const
Definition inidict.cc:108
zypp::parser::IniDict::section_const_iterator
MapKVIteratorTraits< SectionSet >::Key_const_iterator section_const_iterator
Definition inidict.h:47
zypp::parser::IniDict::entry_const_iterator
EntrySet::const_iterator entry_const_iterator
Definition inidict.h:48
zypp::parser::IniDict::entriesEnd
entry_const_iterator entriesEnd(const std::string &section) const
Definition inidict.cc:86
zypp::parser::ServiceFileReader::Impl
Definition ServiceFileReader.cc:34
zypp::parser::ServiceFileReader::Impl::parseServices
static void parseServices(const Pathname &file, const ServiceFileReader::ProcessService &callback)
Definition ServiceFileReader.cc:40
zypp::parser::ServiceFileReader::operator<<
friend std::ostream & operator<<(std::ostream &str, const ServiceFileReader &obj)
Definition ServiceFileReader.cc:173
zypp::parser::ServiceFileReader::ProcessService
function< bool(const ServiceInfo &)> ProcessService
Callback definition.
Definition ServiceFileReader.h:55
zypp::parser::ServiceFileReader::ServiceFileReader
ServiceFileReader(const Pathname &serviceFile, const ProcessService &callback)
Constructor.
Definition ServiceFileReader.cc:162
zypp::parser::ServiceFileReader::~ServiceFileReader
~ServiceFileReader()
Dtor.
Definition ServiceFileReader.cc:170
zypp::repo::RepoInfoBase::setAutorefresh
void setAutorefresh(bool autorefresh)
enable or disable autorefresh
Definition RepoInfoBase.cc:86
zypp::repo::RepoInfoBase::setFilepath
void setFilepath(const Pathname &filename)
set the path to the .repo file
Definition RepoInfoBase.cc:95
zypp::repo::RepoInfoBase::setName
void setName(const std::string &name)
set the repository name
Definition RepoInfoBase.cc:92
zypp::repo::RepoInfoBase::setEnabled
void setEnabled(bool enabled)
enable or disable the repository
Definition RepoInfoBase.cc:83
zypp::str::regex
Regular expression.
Definition Regex.h:95
zypp::str::smatch
Regular expression match result.
Definition Regex.h:168
zypp::str::smatch::size
unsigned size() const
Definition Regex.cc:106
str
String related utilities and Regular expression matching.
zypp::callback
Callbacks light.
Definition Callback.h:146
zypp::parser
Definition HistoryLogReader.cc:30
zypp::str::strToTrue
bool strToTrue(const C_Str &str)
Parsing boolean from string.
Definition String.cc:66
zypp::str::startsWith
bool startsWith(const C_Str &str_r, const C_Str &prefix_r)
alias for hasPrefix
Definition String.h:1155
zypp::str::regex_match
bool regex_match(const std::string &s, smatch &matches, const regex &regex)
\relates regex \ingroup ZYPP_STR_REGEX \relates regex \ingroup ZYPP_STR_REGEX
Definition Regex.h:70
zypp::str::strToBool
bool strToBool(const C_Str &str, bool default_r)
Parse str into a bool depending on the default value.
Definition String.h:500
zypp::str::strtonum
TInt strtonum(const C_Str &str)
Parsing numbers from string.
zypp::str::splitEscaped
unsigned splitEscaped(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=" \t", bool withEmpty=false)
Split line_r into words with respect to escape delimeters.
Definition String.h:665
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition Application.cc:20
zypp::repo::ServiceType
Service type enumeration.
Definition ServiceType.h:27
for_
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition Easy.h:27
ZYPP_RETHROW
#define ZYPP_RETHROW(EXCPT)
Drops a logline and rethrows, updating the CodeLocation.
Definition Exception.h:479
ZYPP_THROW
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition Exception.h:459
MIL
#define MIL
Definition Logger.h:100
ERR
#define ERR
Definition Logger.h:102
Logger.h
Regex.h
String.h