14 #include <sys/types.h>
18 #include <arpa/inet.h>
31 using namespace zypp::base;
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);
753 sprintf(rangebuf,
"%llu-%llu", (
unsigned long long)_blkstart, (
unsigned long long)_blkstart +
_blksize - 1);
755 if (curl_easy_setopt(
_curl, CURLOPT_RANGE, !
_noendrange || _blkstart != 0 ? rangebuf : (
char *)0) != CURLE_OK)
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));
1143 WAR <<
"overall result" << endl;
1144 for (std::list<multifetchworker *>::iterator workeriter =
_workers.begin(); workeriter !=
_workers.end(); ++workeriter)
1158 MIL <<
"MediaMultiCurl::MediaMultiCurl(" << url_r <<
", " << attach_point_hint_r <<
")" << endl;
1172 curl_multi_cleanup(
_multi);
1175 std::map<std::string, CURL *>::iterator it;
1178 CURL *easy = it->second;
1181 curl_easy_cleanup(easy);
1197 for (; sl; sl = sl->next)
1206 while ((l = pread(fd, buf,
sizeof(buf) - 1, (off_t)0)) == -1 && errno == EINTR)
1212 while (*p ==
' ' || *p ==
'\t' || *p ==
'\r' || *p ==
'\n')
1214 if (!strncasecmp(p,
"<?xml", 5))
1216 while (*p && *p !=
'>')
1220 while (*p ==
' ' || *p ==
'\t' || *p ==
'\r' || *p ==
'\n')
1223 bool ret = !strncasecmp(p,
"<metalink", 9) ?
true :
false;
1230 if ((fd = open(file.asString().c_str(), O_RDONLY|O_CLOEXEC)) == -1)
1234 DBG <<
"looks_like_metalink(" << file <<
"): " << ret << endl;
1246 long httpReturnCode = 0;
1247 if (curl_easy_getinfo(_curl, CURLINFO_RESPONSE_CODE, &httpReturnCode ) != CURLE_OK || httpReturnCode == 0)
1251 bool ismetalink =
false;
1252 if (curl_easy_getinfo(_curl, CURLINFO_CONTENT_TYPE, &ptr) == CURLE_OK && ptr)
1254 string ct = string(ptr);
1255 if (ct.find(
"application/metalink+xml") == 0 || ct.find(
"application/metalink4+xml") == 0)
1258 if (!ismetalink && dlnow < 256)
1266 if (curl_easy_getinfo(_curl, CURLINFO_PRIVATE, &fp) != CURLE_OK)
1272 DBG <<
"looks_like_metalink_fd: " << ismetalink << endl;
1277 curl_easy_setopt(_curl, CURLOPT_NOPROGRESS, 1L);
1286 Pathname dest = target.absolutename();
1289 DBG <<
"assert_dir " << dest.dirname() <<
" failed" << endl;
1293 string destNew = target.asString() +
".new.zypp.XXXXXX";
1294 char *buf = ::strdup( destNew.c_str());
1297 ERR <<
"out of memory for temp file name" << endl;
1302 int tmp_fd = ::mkostemp( buf, O_CLOEXEC );
1306 ERR <<
"mkstemp failed for file '" << destNew <<
"'" << endl;
1312 FILE *file = ::fdopen( tmp_fd,
"we" );
1316 ERR <<
"fopen failed for file '" << destNew <<
"'" << endl;
1319 DBG <<
"dest: " << dest << endl;
1320 DBG <<
"temp: " << destNew << endl;
1325 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
1326 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, (
long)PathInfo(target).mtime());
1330 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1331 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1337 curl_easy_setopt(
_curl, CURLOPT_PRIVATE, file);
1346 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1347 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1349 curl_easy_setopt(
_curl, CURLOPT_PRIVATE, (
void *)0);
1352 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1353 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1355 curl_easy_setopt(
_curl, CURLOPT_PRIVATE, (
void *)0);
1356 long httpReturnCode = 0;
1357 CURLcode infoRet = curl_easy_getinfo(
_curl, CURLINFO_RESPONSE_CODE, &httpReturnCode);
1358 if (infoRet == CURLE_OK)
1361 if ( httpReturnCode == 304
1364 DBG <<
"not modified: " << PathInfo(dest) << endl;
1370 WAR <<
"Could not get the reponse code." << endl;
1373 bool ismetalink =
false;
1376 if (curl_easy_getinfo(
_curl, CURLINFO_CONTENT_TYPE, &ptr) == CURLE_OK && ptr)
1378 string ct = string(ptr);
1379 if (ct.find(
"application/metalink+xml") == 0 || ct.find(
"application/metalink4+xml") == 0)
1394 bool userabort =
false;
1401 mlp.
parse(Pathname(destNew));
1403 vector<Url> urls = mlp.
getUrls();
1405 file = fopen(destNew.c_str(),
"w+e");
1408 if (PathInfo(target).isExist())
1410 XXX <<
"reusing blocks from file " << target << endl;
1414 if (bl.
haveChecksum(1) && PathInfo(failedFile).isExist())
1416 XXX <<
"reusing blocks from file " << failedFile << endl;
1424 XXX <<
"reusing blocks from file " << df << endl;
1430 multifetch(filename, file, &urls, &report, &bl);
1434 userabort = ex.
errstr() ==
"User abort";
1444 if (PathInfo(destNew).size() >= 63336)
1446 ::unlink(failedFile.asString().c_str());
1454 file = fopen(destNew.c_str(),
"w+e");
1463 ERR <<
"Failed to chmod file " << destNew << endl;
1468 ERR <<
"Fclose failed for file '" << destNew <<
"'" << endl;
1471 if (
rename( destNew, dest ) != 0 )
1473 ERR <<
"Rename failed" << endl;
1476 DBG <<
"done: " << PathInfo(dest) << endl;
1482 if (blklist && filesize == off_t(-1) && blklist->
haveFilesize())
1484 if (blklist && !blklist->
haveBlocks() && filesize != 0)
1486 if (blklist && (filesize == 0 || !blklist->
numBlocks()))
1495 _multi = curl_multi_init();
1508 std::vector<Url> myurllist;
1509 for (std::vector<Url>::iterator urliter = urllist->begin(); urliter != urllist->end(); ++urliter)
1513 string scheme = urliter->getScheme();
1514 if (scheme ==
"http" || scheme ==
"https" || scheme ==
"ftp" || scheme ==
"tftp")
1517 myurllist.push_back(*urliter);
1524 if (!myurllist.size())
1525 myurllist.push_back(baseurl);
1534 if (fseeko(fp, off_t(0), SEEK_SET))
1540 while ((l = fread(buf, 1,
sizeof(buf), fp)) > 0)
1548 return _dnsok.find(host) ==
_dnsok.end() ?
false :
true;
1570 curl_easy_cleanup(oldeasy);
int assert_dir(const Pathname &path, unsigned mode)
Like 'mkdir -p'.
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
static ZConfig & instance()
Singleton ctor.
Compute Message Digests (MD5, SHA1 etc)
std::string getHost(EEncoding eflag=zypp::url::E_DECODED) const
Returns the hostname or IP from the URL authority.
Pathname repoCachePath() const
Path where the caches are kept (/var/cache/zypp)
std::string asString() const
Returns a default string representation of the Url object.
#define ZYPP_RETHROW(EXCPT)
Drops a logline and rethrows, updating the CodeLocation.
int unlink(const Pathname &path)
Like 'unlink'.
int rename(const Pathname &oldpath, const Pathname &newpath)
Like 'rename'.
int hardlinkCopy(const Pathname &oldpath, const Pathname &newpath)
Create newpath as hardlink or copy of oldpath.
std::string numstring(char n, int w=0)
Base class for Exception.
mode_t applyUmaskTo(mode_t mode_r)
Modify mode_r according to the current umask ( mode_r & ~getUmask() ).
std::string getScheme() const
Returns the scheme name of the URL.
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.