14 #include <zypp-core/base/Logger.h> 16 #include <sys/types.h> 33 static bool val = [](){
34 const char * env = getenv(
"ZYPP_METALINK_DEBUG");
44 ZsyncParser::ZsyncParser()
55 for (i = 0; i < buflen; i++)
57 #define c2h(c) (((c)>='0' && (c)<='9') ? ((c)-'0') \ 58 : ((c)>='a' && (c)<='f') ? ((c)-('a'-10)) \ 59 : ((c)>='A' && (c)<='F') ? ((c)-('A'-10)) \ 70 buf[i] = (buf[i] << 4) | v;
81 std::ifstream is(filename.
c_str());
84 is.exceptions(std::ifstream::eofbit | std::ifstream::failbit | std::ifstream::badbit);
85 off_t filesize = off_t(-1);
88 is.getline(buf,
sizeof(buf));
91 if (!strncmp(buf,
"Length: ", 8))
92 filesize = (off_t)strtoull(buf + 8, 0, 10);
93 else if (!strncmp(buf,
"Hash-Lengths: ", 14))
94 (
void)sscanf(buf + 14,
"%d,%d,%d", &sql, &rsl, &csl);
95 else if (!strncmp(buf,
"Blocksize: ", 11))
96 blksize = atoi(buf + 11);
97 else if (!strncmp(buf,
"URL: http://", 12) || !strncmp(buf,
"URL: https://", 13) || !strncmp(buf,
"URL: ftp://", 11) || !strncmp(buf,
"URL: tftp://", 12) )
98 urls.push_back(buf + 5);
99 else if (!strncmp(buf,
"SHA-1: ", 7))
101 unsigned char sha1[20];
103 bl.setFileChecksum(
"SHA1", 20, sha1);
106 if (filesize == off_t(-1))
108 if (blksize <= 0 || (blksize & (blksize - 1)) != 0)
110 bl.setFilesize(filesize);
114 if (csl < 3 || csl > 16 || rsl < 1 || rsl > 4 || sql < 1 || sql > 2)
117 bl.setRsumSequence( sql );
119 size_t nblks = (filesize + blksize - 1) / blksize;
122 size_t size = blksize;
123 for (i = 0; i < nblks; i++)
127 size = filesize % blksize;
131 size_t blkno = bl.addBlock(off, size);
132 unsigned char rp[16];
133 rp[0] = rp[1] = rp[2] = rp[3] = 0;
135 is.read((
char *)rp + 4 - rsl, rsl);
136 }
catch (
const std::exception &e ) {
149 bl.setRsum(blkno, rsl, rp[0] << 24 | rp[1] << 16 | rp[2] << 8 | rp[3], blksize);
151 is.read((
char *)rp, csl);
152 }
catch (
const std::exception &e ) {
174 bl.setChecksum(blkno,
"MD4", csl, rp, blksize);
179 MIL <<
"Parsed " << urls.size() <<
" mirrors from " << filename << std::endl;
181 for (
const auto &url : urls )
182 DBG <<
"- " << url << std::endl;
187 ZsyncParser::getUrls()
189 std::vector<Url> ret;
191 for (i = 0; i < urls.size(); i++)
192 ret.push_back(
Url(urls[i]));
197 ZsyncParser::getBlockList()
bool ZYPP_METALINK_DEBUG()
Hack to circumvent the currently poor –root support.
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
const char * c_str() const
String representation.
String related utilities and Regular expression matching.
Base class for Exception.
bool strToBool(const C_Str &str, bool default_r)
Parse str into a bool depending on the default value.
Easy-to use interface to the ZYPP dependency resolver.