14 #include <sys/types.h> 18 #include <arpa/inet.h> 33 #undef CURLVERSION_AT_LEAST 34 #define CURLVERSION_AT_LEAST(M,N,O) LIBCURL_VERSION_NUM >= ((((M)<<8)+(N))<<8)+(O) 43 class multifetchrequest;
57 bool recheckChecksum();
58 void disableCompetition();
61 void adddnsfd(fd_set &rset,
int &maxfd);
62 void dnsevent(fd_set &rset);
86 size_t writefunction(
void *ptr,
size_t size);
87 static size_t _writefunction(
void *ptr,
size_t size,
size_t nmemb,
void *stream);
89 size_t headerfunction(
char *ptr,
size_t size);
90 static size_t _headerfunction(
void *ptr,
size_t size,
size_t nmemb,
void *stream);
103 #define WORKER_STARTING 0 104 #define WORKER_LOOKUP 1 105 #define WORKER_FETCH 2 106 #define WORKER_DISCARD 3 107 #define WORKER_DONE 4 108 #define WORKER_SLEEP 5 109 #define WORKER_BROKEN 6 118 void run(std::vector<Url> &urllist);
163 #define BLKSIZE 131072 173 if (gettimeofday(&tv, NULL))
175 return tv.tv_sec + tv.tv_usec / 1000000.;
179 multifetchworker::writefunction(
void *ptr,
size_t size)
187 len = size > _size ? _size : size;
194 if (_blkstart && _off == _blkstart)
199 (void)curl_easy_getinfo(_curl, CURLINFO_EFFECTIVE_URL, &effurl);
200 if (effurl && !strncasecmp(effurl,
"http", 4))
203 (void)curl_easy_getinfo(_curl, CURLINFO_RESPONSE_CODE, &statuscode);
204 if (statuscode != 206)
212 _request->_lastprogress = now;
218 if (_request->_blklist)
219 _dig.update((
const char *)ptr, len);
224 if (fseeko(_request->_fp, _off, SEEK_SET))
226 cnt = fwrite(ptr, 1, len, _request->_fp);
229 _request->_fetchedsize += cnt;
230 if (_request->_blklist)
231 _dig.update((
const char *)ptr, cnt);
241 multifetchworker::_writefunction(
void *ptr,
size_t size,
size_t nmemb,
void *stream)
248 multifetchworker::headerfunction(
char *p,
size_t size)
251 if (l > 9 && !strncasecmp(p,
"Location:", 9))
253 string line(p + 9, l - 9);
254 if (line[l - 10] ==
'\r')
255 line.erase(l - 10, 1);
256 XXX <<
"#" << _workerno <<
": redirecting to" << line << endl;
259 if (l <= 14 || l >= 128 || strncasecmp(p,
"Content-Range:", 14) != 0)
263 while (l && (*p ==
' ' || *p ==
'\t'))
265 if (l < 6 || strncasecmp(p,
"bytes", 5))
272 unsigned long long start, off, filesize;
273 if (sscanf(buf,
"%llu-%llu/%llu", &start, &off, &filesize) != 3)
275 if (_request->_filesize == (off_t)-1)
277 WAR <<
"#" << _workerno <<
": setting request filesize to " << filesize << endl;
278 _request->_filesize = filesize;
279 if (_request->_totalsize == 0 && !_request->_blklist)
280 _request->_totalsize = filesize;
282 if (_request->_filesize != (off_t)filesize)
284 XXX <<
"#" << _workerno <<
": filesize mismatch" << endl;
286 strncpy(_curlError,
"filesize mismatch", CURL_ERROR_SIZE);
292 multifetchworker::_headerfunction(
void *ptr,
size_t size,
size_t nmemb,
void *stream)
323 XXX <<
"reused worker from pool" << endl;
327 strncpy(
_curlError,
"curl_easy_init failed", CURL_ERROR_SIZE);
336 curl_easy_cleanup(
_curl);
339 strncpy(
_curlError,
"curl_easy_setopt failed", CURL_ERROR_SIZE);
342 curl_easy_setopt(
_curl, CURLOPT_PRIVATE,
this);
345 curl_easy_setopt(
_curl, CURLOPT_WRITEDATA,
this);
349 curl_easy_setopt(
_curl, CURLOPT_HEADERDATA,
this);
363 if (use_auth.empty())
364 use_auth =
"digest,basic";
366 if( auth != CURLAUTH_NONE)
368 XXX <<
"#" <<
_workerno <<
": Enabling HTTP authentication methods: " << use_auth
369 <<
" (CURLOPT_HTTPAUTH=" << auth <<
")" << std::endl;
370 curl_easy_setopt(
_curl, CURLOPT_HTTPAUTH, auth);
385 #if CURLVERSION_AT_LEAST(7,15,5) 386 curl_easy_setopt(
_curl, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t)0);
388 curl_easy_setopt(
_curl, CURLOPT_PRIVATE, (
void *)0);
389 curl_easy_setopt(
_curl, CURLOPT_WRITEFUNCTION, (
void *)0);
390 curl_easy_setopt(
_curl, CURLOPT_WRITEDATA, (
void *)0);
391 curl_easy_setopt(
_curl, CURLOPT_HEADERFUNCTION, (
void *)0);
392 curl_easy_setopt(
_curl, CURLOPT_HEADERDATA, (
void *)0);
396 curl_easy_cleanup(
_curl);
403 while (waitpid(
_pid, &status, 0) == -1)
420 const char *s = getenv(name.c_str());
421 return s && *s ? true :
false;
437 if (inet_pton(AF_INET, host.c_str(), addrbuf) == 1)
439 if (inet_pton(AF_INET6, host.c_str(), addrbuf) == 1)
450 if (schemeproxy !=
"http_proxy")
452 std::transform(schemeproxy.begin(), schemeproxy.end(), schemeproxy.begin(), ::toupper);
457 XXX <<
"checking DNS lookup of " << host << endl;
462 strncpy(
_curlError,
"DNS pipe creation failed", CURL_ERROR_SIZE);
466 if (
_pid == pid_t(-1))
472 strncpy(
_curlError,
"DNS checker fork failed", CURL_ERROR_SIZE);
479 struct addrinfo *ai, aihints;
480 memset(&aihints, 0,
sizeof(aihints));
481 aihints.ai_family = PF_UNSPEC;
482 int tstsock = socket(PF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
484 aihints.ai_family = PF_INET;
487 aihints.ai_socktype = SOCK_STREAM;
488 aihints.ai_flags = AI_CANONNAME;
491 alarm(connecttimeout);
492 signal(SIGALRM, SIG_DFL);
493 if (getaddrinfo(host.c_str(), NULL, &aihints, &ai))
519 while (waitpid(
_pid, &status, 0) == -1)
530 if (!WIFEXITED(status))
533 strncpy(
_curlError,
"DNS lookup failed", CURL_ERROR_SIZE);
537 int exitcode = WEXITSTATUS(status);
538 XXX <<
"#" <<
_workerno <<
": DNS lookup returned " << exitcode << endl;
542 strncpy(
_curlError,
"DNS lookup failed", CURL_ERROR_SIZE);
572 size_t cnt = l >
sizeof(buf) ?
sizeof(buf) : l;
587 XXX <<
"start stealing!" << endl;
591 std::list<multifetchworker *>::iterator workeriter =
_request->
_workers.begin();
598 if (worker->
_pass == -1)
657 XXX <<
"#" <<
_workerno <<
": going to sleep for " << sl * 1000 <<
" ms" << endl;
678 std::list<multifetchworker *>::iterator workeriter =
_request->
_workers.begin();
751 sprintf(rangebuf,
"%llu-", (
unsigned long long)
_blkstart);
759 strncpy(
_curlError,
"curl_easy_setopt range failed", CURL_ERROR_SIZE);
766 strncpy(
_curlError,
"curl_multi_add_handle failed", CURL_ERROR_SIZE);
816 for (
size_t blkno = 0; blkno < blklist->
numBlocks(); blkno++)
822 else if (filesize != off_t(-1))
828 for (std::list<multifetchworker *>::iterator workeriter =
_workers.begin(); workeriter !=
_workers.end(); ++workeriter)
841 std::vector<Url>::iterator urliter = urllist.begin();
844 fd_set rset, wset, xset;
849 XXX <<
"finished!" << endl;
873 WAR <<
"No more active workers!" << endl;
875 for (std::list<multifetchworker *>::iterator workeriter =
_workers.begin(); workeriter !=
_workers.end(); ++workeriter)
888 curl_multi_fdset(
_multi, &rset, &wset, &xset, &maxfd);
891 for (std::list<multifetchworker *>::iterator workeriter =
_workers.begin(); workeriter !=
_workers.end(); ++workeriter)
892 (*workeriter)->adddnsfd(rset, maxfd);
903 for (std::list<multifetchworker *>::iterator workeriter =
_workers.begin(); workeriter !=
_workers.end(); ++workeriter)
919 tv.tv_usec = sl * 1000000;
921 int r = select(maxfd + 1, &rset, &wset, &xset, &tv);
922 if (r == -1 && errno != EINTR)
925 for (std::list<multifetchworker *>::iterator workeriter =
_workers.begin(); workeriter !=
_workers.end(); ++workeriter)
941 mcode = curl_multi_perform(
_multi, &tasks);
942 if (mcode == CURLM_CALL_MULTI_PERFORM)
944 if (mcode != CURLM_OK)
965 for (std::list<multifetchworker *>::iterator workeriter =
_workers.begin(); workeriter !=
_workers.end(); ++workeriter)
974 XXX <<
"#" << worker->
_workerno <<
": sleep done, wake up" << endl;
983 while ((msg = curl_multi_info_read(
_multi, &nqueue)) != 0)
985 if (msg->msg != CURLMSG_DONE)
987 CURL *easy = msg->easy_handle;
988 CURLcode cc = msg->data.result;
990 if (curl_easy_getinfo(easy, CURLINFO_PRIVATE, &worker) != CURLE_OK)
1000 curl_multi_remove_handle(
_multi, easy);
1001 if (cc == CURLE_HTTP_RETURNED_ERROR)
1003 long statuscode = 0;
1004 (void)curl_easy_getinfo(easy, CURLINFO_RESPONSE_CODE, &statuscode);
1005 XXX <<
"HTTP status " << statuscode << endl;
1006 if (statuscode == 416 && !
_blklist)
1012 XXX <<
"#" << worker->
_workerno <<
": retrying with no end range" << endl;
1032 WAR <<
"#" << worker->
_workerno <<
": checksum error, disable worker" << endl;
1034 strncpy(worker->
_curlError,
"checksum error", CURL_ERROR_SIZE);
1048 XXX <<
"#" << worker->
_workerno <<
": recheck checksum error, refetch block" << endl;
1061 int maxworkerno = 0;
1063 for (std::list<multifetchworker *>::iterator workeriter =
_workers.begin(); workeriter !=
_workers.end(); ++workeriter)
1078 double ratio = worker->
_avgspeed / maxavg;
1081 ratio = ratio * ratio;
1084 XXX <<
"#" << worker->
_workerno <<
": too slow ("<< ratio <<
", " << worker->
_avgspeed <<
", #" << maxworkerno <<
": " << maxavg <<
"), going to sleep for " << ratio * 1000 <<
" ms" << endl;
1105 #if CURLVERSION_AT_LEAST(7,15,5) 1106 curl_easy_setopt(worker->
_curl, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t)(avg));
1148 WAR <<
"overall result" << endl;
1149 for (std::list<multifetchworker *>::iterator workeriter =
_workers.begin(); workeriter !=
_workers.end(); ++workeriter)
1163 MIL <<
"MediaMultiCurl::MediaMultiCurl(" << url_r <<
", " << attach_point_hint_r <<
")" << endl;
1177 curl_multi_cleanup(
_multi);
1180 std::map<std::string, CURL *>::iterator it;
1183 CURL *easy = it->second;
1186 curl_easy_cleanup(easy);
1202 for (; sl; sl = sl->next)
1211 while ((l = pread(fd, buf,
sizeof(buf) - 1, (off_t)0)) == -1 && errno == EINTR)
1217 while (*p ==
' ' || *p ==
'\t' || *p ==
'\r' || *p ==
'\n')
1219 if (!strncasecmp(p,
"<?xml", 5))
1221 while (*p && *p !=
'>')
1225 while (*p ==
' ' || *p ==
'\t' || *p ==
'\r' || *p ==
'\n')
1228 bool ret = !strncasecmp(p,
"<metalink", 9) ? true :
false;
1235 if ((fd = open(file.
asString().c_str(), O_RDONLY|O_CLOEXEC)) == -1)
1239 DBG <<
"looks_like_metalink(" << file <<
"): " << ret << endl;
1255 if ( curl_easy_getinfo(
_curl, CURLINFO_PRIVATE, &fp ) != CURLE_OK || !fp )
1257 if ( ftell( fp ) == 0 )
1262 long httpReturnCode = 0;
1263 if (curl_easy_getinfo(
_curl, CURLINFO_RESPONSE_CODE, &httpReturnCode ) != CURLE_OK || httpReturnCode == 0)
1267 bool ismetalink =
false;
1268 if (curl_easy_getinfo(
_curl, CURLINFO_CONTENT_TYPE, &ptr) == CURLE_OK && ptr)
1270 string ct = string(ptr);
1271 if (ct.find(
"application/metalink+xml") == 0 || ct.find(
"application/metalink4+xml") == 0)
1274 if (!ismetalink && dlnow < 256)
1283 DBG <<
"looks_like_metalink_fd: " << ismetalink << endl;
1302 DBG <<
"assert_dir " << dest.
dirname() <<
" failed" << endl;
1306 string destNew = target.
asString() +
".new.zypp.XXXXXX";
1307 char *buf = ::strdup( destNew.c_str());
1310 ERR <<
"out of memory for temp file name" << endl;
1315 int tmp_fd = ::mkostemp( buf, O_CLOEXEC );
1319 ERR <<
"mkstemp failed for file '" << destNew <<
"'" << endl;
1325 FILE *file = ::fdopen( tmp_fd,
"we" );
1329 ERR <<
"fopen failed for file '" << destNew <<
"'" << endl;
1332 DBG <<
"dest: " << dest << endl;
1333 DBG <<
"temp: " << destNew << endl;
1338 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
1339 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, (
long)
PathInfo(target).mtime());
1343 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1344 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1350 curl_easy_setopt(
_curl, CURLOPT_PRIVATE, file);
1359 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1360 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1362 curl_easy_setopt(
_curl, CURLOPT_PRIVATE, (
void *)0);
1365 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1366 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1368 curl_easy_setopt(
_curl, CURLOPT_PRIVATE, (
void *)0);
1369 long httpReturnCode = 0;
1370 CURLcode infoRet = curl_easy_getinfo(
_curl, CURLINFO_RESPONSE_CODE, &httpReturnCode);
1371 if (infoRet == CURLE_OK)
1374 if ( httpReturnCode == 304
1377 DBG <<
"not modified: " <<
PathInfo(dest) << endl;
1383 WAR <<
"Could not get the reponse code." << endl;
1386 bool ismetalink =
false;
1389 if (curl_easy_getinfo(
_curl, CURLINFO_CONTENT_TYPE, &ptr) == CURLE_OK && ptr)
1391 string ct = string(ptr);
1392 if (ct.find(
"application/metalink+xml") == 0 || ct.find(
"application/metalink4+xml") == 0)
1407 bool userabort =
false;
1416 vector<Url> urls = mlp.
getUrls();
1418 file = fopen(destNew.c_str(),
"w+e");
1423 XXX <<
"reusing blocks from file " << target << endl;
1429 XXX <<
"reusing blocks from file " << failedFile << endl;
1437 XXX <<
"reusing blocks from file " <<
df << endl;
1447 userabort = ex.
errstr() ==
"User abort";
1460 if (
PathInfo(destNew).size() >= 63336)
1470 file = fopen(destNew.c_str(),
"w+e");
1479 ERR <<
"Failed to chmod file " << destNew << endl;
1484 ERR <<
"Fclose failed for file '" << destNew <<
"'" << endl;
1487 if (
rename( destNew, dest ) != 0 )
1489 ERR <<
"Rename failed" << endl;
1498 inline Url propagateQueryParams(
Url url_r,
const Url & template_r )
1500 for ( std::string param : {
"proxy",
"proxyport",
"proxyuser",
"proxypass"} )
1502 const std::string & value( template_r.
getQueryParam( param ) );
1503 if ( ! value.empty() )
1514 if (blklist && filesize == off_t(-1) && blklist->
haveFilesize())
1516 if (blklist && !blklist->
haveBlocks() && filesize != 0)
1518 if (blklist && (filesize == 0 || !blklist->
numBlocks()))
1527 _multi = curl_multi_init();
1541 std::vector<Url> myurllist;
1542 for (std::vector<Url>::iterator urliter = urllist->begin(); urliter != urllist->end(); ++urliter)
1546 string scheme = urliter->getScheme();
1547 if (scheme ==
"http" || scheme ==
"https" || scheme ==
"ftp" || scheme ==
"tftp")
1550 myurllist.push_back(propagateQueryParams(*urliter,
_url));
1557 if (!myurllist.size())
1558 myurllist.push_back(baseurl);
1567 if (fseeko(fp, off_t(0), SEEK_SET))
1573 while ((l = fread(buf, 1,
sizeof(buf), fp)) > 0)
1581 return _dnsok.find(host) ==
_dnsok.end() ? false :
true;
1603 curl_easy_cleanup(oldeasy);
std::string getScheme() const
Returns the scheme name of the URL.
int assert_dir(const Pathname &path, unsigned mode)
Like 'mkdir -p'.
void setQueryParam(const std::string ¶m, const std::string &value)
Set or add value for the specified query parameter.
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
static ZConfig & instance()
Singleton ctor.
Compute Message Digests (MD5, SHA1 etc)
Store and operate with byte count.
static const Unit MB
1000^2 Byte
#define ZYPP_RETHROW(EXCPT)
Drops a logline and rethrows, updating the CodeLocation.
std::string asString() const
Returns a default string representation of the Url object.
std::string getQueryParam(const std::string ¶m, EEncoding eflag=zypp::url::E_DECODED) const
Return the value for the specified query parameter.
int unlink(const Pathname &path)
Like 'unlink'.
const std::string & asString() const
String representation.
int rename(const Pathname &oldpath, const Pathname &newpath)
Like 'rename'.
bool isExist() const
Return whether valid stat info exists.
Pathname repoCachePath() const
Path where the caches are kept (/var/cache/zypp)
Pathname dirname() const
Return all but the last component od this path.
int hardlinkCopy(const Pathname &oldpath, const Pathname &newpath)
Create newpath as hardlink or copy of oldpath.
std::string numstring(char n, int w=0)
std::string asString(unsigned field_width_r=0, unsigned unit_width_r=1) const
Auto selected Unit and precision.
Pathname absolutename() const
Return this path, adding a leading '/' if relative.
Base class for Exception.
std::string getHost(EEncoding eflag=zypp::url::E_DECODED) const
Returns the hostname or IP from the URL authority.
Wrapper class for ::stat/::lstat.
AutoDispose< void * > _state
mode_t applyUmaskTo(mode_t mode_r)
Modify mode_r according to the current umask ( mode_r & ~getUmask() ).
Easy-to use interface to the ZYPP dependency resolver.
bool update(const char *bytes, size_t len)
feed data into digest computation algorithm
ByteCount df(const Pathname &path_r)
Report free disk space on a mounted file system.