14#include <zypp-core/base/Logger.h>
15#include <zypp-core/ByteArray.h>
16#include <zypp-core/AutoDispose.h>
22#include <libxml2/libxml/SAX2.h>
31 static bool val = [](){
32 const char * env = getenv(
"ZYPP_METALINK_DEBUG");
72 const std::unordered_map<ParserState, std::vector<transition> > &
transitions () {
73 static std::unordered_map<ParserState, std::vector<transition> > map {
122static void XMLCALL
startElement(
void *userData,
const xmlChar *name,
const xmlChar **atts);
123static void XMLCALL
endElement(
void *userData,
const xmlChar *name);
124static void XMLCALL
characterData(
void *userData,
const xmlChar *s,
int len);
142 memset(&sax, 0,
sizeof(sax));
185 std::vector<MetalinkMirror>
urls;
209 for (; *atts; atts += 2)
211 if (!strcmp(
reinterpret_cast<const char*
>(*atts), txt))
212 return reinterpret_cast<const char*
>(atts[1]);
231 const auto currStateTrs = trMap.find( pd->
state );
232 if ( currStateTrs == trMap.end() )
236 auto foundTr = std::find_if( currStateTrs->second.begin(), currStateTrs->second.end(), [name](
const auto &tr ){
237 return tr.elementName == reinterpret_cast<const char *>(name);
240 if ( foundTr == currStateTrs->second.end() ) {
256 const char *priority =
find_attr(
"priority", atts);
257 const char *preference =
find_attr(
"preference", atts);
258 const char *maxconnections =
find_attr(
"maxconnections", atts);
260 auto &mirr = pd->
urls.emplace_back();
262 prio = str::strtonum<int>(priority);
264 prio = 101 - str::strtonum<int>(preference);
267 mirr.priority = prio;
269 if ( maxconnections )
270 mirr.maxConnections = str::strtonum<int>( maxconnections );
277 const char *type =
find_attr(
"type", atts);
278 const char *length =
find_attr(
"length", atts);
281 if (!type || !length)
286 blksize = str::strtonum<unsigned long>(length);
294 if (!strcmp(type,
"sha1") || !strcmp(type,
"sha-1"))
296 else if (!strcmp(type,
"zsync"))
308 const char *type =
find_attr(
"type", atts);
311 if ((!strcmp(type,
"sha1") || !strcmp(type,
"sha-1")) && pd->
chksuml < 20)
313 else if (!strcmp(type,
"sha256") || !strcmp(type,
"sha-256"))
325 const char *piece =
find_attr(
"piece", atts);
339 return Digest::hexStringToUByteArray(
str );
356 pd->
size = (off_t)str::strtonum<off_t>(pd->
content);
373 if ( !pieceHash.size() )
374 pieceHash.resize( pd->
piecel, 0 );
375 pd->
piece.push_back( pieceHash );
414 pd->
content.append( s, s+len );
439 while (is.
stream().good())
441 is.
stream().read(buf,
sizeof(buf));
445 MIL <<
"Parsed " <<
pd->
urls.size() <<
" mirrors from " << is.
path() << std::endl;
447 for (
const auto &mirr :
pd->
urls )
448 DBG <<
"- " << mirr.priority <<
" " << mirr.url << std::endl;
458 if (xmlParseChunk(
pd->
parser, buf, len, 0)) {
466 if (xmlParseChunk(
pd->
parser, NULL, 0, 1)) {
470 stable_sort(
pd->
urls.begin(),
pd->
urls.end(), [](
const auto &
a,
const auto &
b ){
471 return a.priority < b.priority;
479 std::vector<Url> urls;
480 for (
const auto &mirr :
pd->
urls )
481 urls.push_back( mirr.url );
502 for (
size_t i = 0; i < nb; i++ )
510 size_t blkno = bl.
addBlock(off, size);
511 if ( i < pd->sha1.size())
514 if ( i < pd->zsync.size())
516 unsigned char *p =
pd->
zsync[i].data();
517 bl.
setRsum(blkno, 4, p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24,
pd->
blksize);
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
Base class for Exception.
String related utilities and Regular expression matching.
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
bool ZYPP_METALINK_DEBUG()
Hack to circumvent the currently poor –root support.
bool strToBool(const C_Str &str, bool default_r)
Parse str into a bool depending on the default value.
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.