1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/parser/susetags/ContentFileReader.cc
15 #include "zypp/base/LogTools.h"
16 #include "zypp/base/String.h"
17 #include "zypp/base/IOStream.h"
18 #include "zypp/base/UserRequestException.h"
19 #include "zypp/parser/ParseException.h"
21 #include "zypp/parser/susetags/ContentFileReader.h"
22 #include "zypp/parser/susetags/RepoIndex.h"
25 #undef ZYPP_BASE_LOGGER_LOGGROUP
26 #define ZYPP_BASE_LOGGER_LOGGROUP "parser::susetags"
28 ///////////////////////////////////////////////////////////////////
30 { /////////////////////////////////////////////////////////////////
31 ///////////////////////////////////////////////////////////////////
33 { /////////////////////////////////////////////////////////////////
34 ///////////////////////////////////////////////////////////////////
36 { /////////////////////////////////////////////////////////////////
38 ///////////////////////////////////////////////////////////////////
40 // CLASS NAME : ContentFileReader::Impl
42 /** ContentFileReader implementation. */
43 struct ContentFileReader::Impl
49 RepoIndex & repoindex()
52 _repoindex = new RepoIndex;
56 bool hasRepoIndex() const
57 { return _repoindex != nullptr; }
59 RepoIndex_Ptr handoutRepoIndex()
62 ret.swap( _repoindex );
68 bool setFileCheckSum( std::map<std::string, CheckSum> & map_r, const std::string & value ) const
71 std::vector<std::string> words;
72 if ( str::split( value, std::back_inserter( words ) ) == 3 )
74 map_r[words[2]] = CheckSum( words[0], words[1] );
84 std::string _inputname;
87 RepoIndex_Ptr _repoindex;
89 ///////////////////////////////////////////////////////////////////
91 ///////////////////////////////////////////////////////////////////
93 // CLASS NAME : ContentFileReader
95 ///////////////////////////////////////////////////////////////////
97 ///////////////////////////////////////////////////////////////////
99 // METHOD NAME : ContentFileReader::ContentFileReader
100 // METHOD TYPE : Ctor
102 ContentFileReader::ContentFileReader()
105 ///////////////////////////////////////////////////////////////////
107 // METHOD NAME : ContentFileReader::~ContentFileReader
108 // METHOD TYPE : Dtor
110 ContentFileReader::~ContentFileReader()
113 ///////////////////////////////////////////////////////////////////
115 // METHOD NAME : ContentFileReader::beginParse
116 // METHOD TYPE : void
118 void ContentFileReader::beginParse()
120 _pimpl.reset( new Impl() );
121 // actually mandatory, but in case they were forgotten...
122 _pimpl->repoindex().descrdir = "suse/setup/descr";
123 _pimpl->repoindex().datadir = "suse";
126 ///////////////////////////////////////////////////////////////////
128 // METHOD NAME : ContentFileReader::endParse
129 // METHOD TYPE : void
131 void ContentFileReader::endParse()
134 if ( _pimpl->hasRepoIndex() )
136 if ( _repoIndexConsumer )
137 _repoIndexConsumer( _pimpl->handoutRepoIndex() );
140 MIL << "[Content]" << endl;
144 ///////////////////////////////////////////////////////////////////
146 // METHOD NAME : ContentFileReader::userRequestedAbort
147 // METHOD TYPE : void
149 void ContentFileReader::userRequestedAbort( unsigned lineNo_r )
151 ZYPP_THROW( AbortRequestException( errPrefix( lineNo_r ) ) );
154 ///////////////////////////////////////////////////////////////////
156 // METHOD NAME : ContentFileReader::errPrefix
157 // METHOD TYPE : std::string
159 std::string ContentFileReader::errPrefix( unsigned lineNo_r,
160 const std::string & msg_r,
161 const std::string & line_r ) const
163 return str::form( "%s:%u:%s | %s",
164 _pimpl->_inputname.c_str(),
170 ///////////////////////////////////////////////////////////////////
172 // METHOD NAME : ContentFileReader::parse
173 // METHOD TYPE : void
175 void ContentFileReader::parse( const InputStream & input_r,
176 const ProgressData::ReceiverFnc & fnc_r )
178 MIL << "Start parsing content repoindex" << input_r << endl;
179 if ( ! input_r.stream() )
181 std::ostringstream s;
182 s << "Can't read bad stream: " << input_r;
183 ZYPP_THROW( ParseException( s.str() ) );
186 _pimpl->_inputname = input_r.name();
188 ProgressData ticks( makeProgressData( input_r ) );
189 ticks.sendTo( fnc_r );
190 if ( ! ticks.toMin() )
191 userRequestedAbort( 0 );
193 iostr::EachLine line( input_r );
194 for( ; line; line.next() )
196 // strip 1st word from line to separate tag and value.
197 std::string value( *line );
198 std::string key( str::stripFirstWord( value, /*ltrim_first*/true ) );
200 if ( key.empty() || *key.c_str() == '#' ) // empty or comment line
205 // strip modifier if exists
206 std::string modifier;
207 std::string::size_type pos = key.rfind( '.' );
208 if ( pos != std::string::npos )
210 modifier = key.substr( pos+1 );
215 // ReppoIndex related data:
217 else if ( key == "DESCRDIR" )
219 _pimpl->repoindex().descrdir = value;
221 else if ( key == "DATADIR" )
223 _pimpl->repoindex().datadir = value;
225 else if ( key == "KEY" )
227 if ( _pimpl->setFileCheckSum( _pimpl->repoindex().signingKeys, value ) )
229 ZYPP_THROW( ParseException( errPrefix( line.lineNo(), "Expected [KEY algorithm checksum filename]", *line ) ) );
232 else if ( key == "META" )
234 if ( _pimpl->setFileCheckSum( _pimpl->repoindex().metaFileChecksums, value ) )
236 ZYPP_THROW( ParseException( errPrefix( line.lineNo(), "Expected [algorithm checksum filename]", *line ) ) );
239 else if ( key == "HASH" )
241 if ( _pimpl->setFileCheckSum( _pimpl->repoindex().mediaFileChecksums, value ) )
243 ZYPP_THROW( ParseException( errPrefix( line.lineNo(), "Expected [algorithm checksum filename]", *line ) ) );
248 DBG << errPrefix( line.lineNo(), "ignored", *line ) << endl;
252 if ( ! ticks.set( input_r.stream().tellg() ) )
253 userRequestedAbort( line.lineNo() );
259 if ( ! ticks.toMax() )
260 userRequestedAbort( line.lineNo() );
263 MIL << "Done parsing " << input_r << endl;
266 /////////////////////////////////////////////////////////////////
267 } // namespace susetags
268 ///////////////////////////////////////////////////////////////////
269 /////////////////////////////////////////////////////////////////
270 } // namespace parser
271 ///////////////////////////////////////////////////////////////////
272 /////////////////////////////////////////////////////////////////
274 ///////////////////////////////////////////////////////////////////