14 #include <sys/types.h> 18 #include <arpa/inet.h> 35 #undef CURLVERSION_AT_LEAST 36 #define CURLVERSION_AT_LEAST(M,N,O) LIBCURL_VERSION_NUM >= ((((M)<<8)+(N))<<8)+(O) 45 class multifetchrequest;
59 bool recheckChecksum();
60 void disableCompetition();
63 void adddnsfd(fd_set &rset,
int &maxfd);
64 void dnsevent(fd_set &rset);
88 size_t writefunction(
void *ptr,
size_t size);
89 static size_t _writefunction(
void *ptr,
size_t size,
size_t nmemb,
void *stream);
91 size_t headerfunction(
char *ptr,
size_t size);
92 static size_t _headerfunction(
void *ptr,
size_t size,
size_t nmemb,
void *stream);
105 #define WORKER_STARTING 0 106 #define WORKER_LOOKUP 1 107 #define WORKER_FETCH 2 108 #define WORKER_DISCARD 3 109 #define WORKER_DONE 4 110 #define WORKER_SLEEP 5 111 #define WORKER_BROKEN 6 120 void run(std::vector<Url> &urllist);
165 #define BLKSIZE 131072 175 if (gettimeofday(&tv, NULL))
177 return tv.tv_sec + tv.tv_usec / 1000000.;
181 multifetchworker::writefunction(
void *ptr,
size_t size)
189 len = size > _size ? _size : size;
196 if (_blkstart && _off == _blkstart)
201 (void)curl_easy_getinfo(_curl, CURLINFO_EFFECTIVE_URL, &effurl);
202 if (effurl && !strncasecmp(effurl,
"http", 4))
205 (void)curl_easy_getinfo(_curl, CURLINFO_RESPONSE_CODE, &statuscode);
206 if (statuscode != 206)
214 _request->_lastprogress = now;
220 if (_request->_blklist)
221 _dig.update((
const char *)ptr, len);
226 if (fseeko(_request->_fp, _off, SEEK_SET))
228 cnt = fwrite(ptr, 1, len, _request->_fp);
231 _request->_fetchedsize += cnt;
232 if (_request->_blklist)
233 _dig.update((
const char *)ptr, cnt);
243 multifetchworker::_writefunction(
void *ptr,
size_t size,
size_t nmemb,
void *stream)
250 multifetchworker::headerfunction(
char *p,
size_t size)
253 if (l > 9 && !strncasecmp(p,
"Location:", 9))
255 std::string line(p + 9, l - 9);
256 if (line[l - 10] ==
'\r')
257 line.erase(l - 10, 1);
258 XXX <<
"#" << _workerno <<
": redirecting to" << line << endl;
261 if (l <= 14 || l >= 128 || strncasecmp(p,
"Content-Range:", 14) != 0)
265 while (l && (*p ==
' ' || *p ==
'\t'))
267 if (l < 6 || strncasecmp(p,
"bytes", 5))
274 unsigned long long start, off, filesize;
275 if (sscanf(buf,
"%llu-%llu/%llu", &start, &off, &filesize) != 3)
277 if (_request->_filesize == (off_t)-1)
279 WAR <<
"#" << _workerno <<
": setting request filesize to " << filesize << endl;
280 _request->_filesize = filesize;
281 if (_request->_totalsize == 0 && !_request->_blklist)
282 _request->_totalsize = filesize;
284 if (_request->_filesize != (off_t)filesize)
286 XXX <<
"#" << _workerno <<
": filesize mismatch" << endl;
288 strncpy(_curlError,
"filesize mismatch", CURL_ERROR_SIZE);
294 multifetchworker::_headerfunction(
void *ptr,
size_t size,
size_t nmemb,
void *stream)
325 XXX <<
"reused worker from pool" << endl;
329 strncpy(
_curlError,
"curl_easy_init failed", CURL_ERROR_SIZE);
338 curl_easy_cleanup(
_curl);
341 strncpy(
_curlError,
"curl_easy_setopt failed", CURL_ERROR_SIZE);
344 curl_easy_setopt(
_curl, CURLOPT_PRIVATE,
this);
347 curl_easy_setopt(
_curl, CURLOPT_WRITEDATA,
this);
351 curl_easy_setopt(
_curl, CURLOPT_HEADERDATA,
this);
365 if (use_auth.empty())
366 use_auth =
"digest,basic";
368 if( auth != CURLAUTH_NONE)
370 XXX <<
"#" <<
_workerno <<
": Enabling HTTP authentication methods: " << use_auth
371 <<
" (CURLOPT_HTTPAUTH=" << auth <<
")" << std::endl;
372 curl_easy_setopt(
_curl, CURLOPT_HTTPAUTH, auth);
387 #if CURLVERSION_AT_LEAST(7,15,5) 388 curl_easy_setopt(
_curl, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t)0);
390 curl_easy_setopt(
_curl, CURLOPT_PRIVATE, (
void *)0);
391 curl_easy_setopt(
_curl, CURLOPT_WRITEFUNCTION, (
void *)0);
392 curl_easy_setopt(
_curl, CURLOPT_WRITEDATA, (
void *)0);
393 curl_easy_setopt(
_curl, CURLOPT_HEADERFUNCTION, (
void *)0);
394 curl_easy_setopt(
_curl, CURLOPT_HEADERDATA, (
void *)0);
398 curl_easy_cleanup(
_curl);
405 while (waitpid(
_pid, &status, 0) == -1)
422 const char *s = getenv(name.c_str());
423 return s && *s ? true :
false;
439 if (inet_pton(AF_INET, host.c_str(), addrbuf) == 1)
441 if (inet_pton(AF_INET6, host.c_str(), addrbuf) == 1)
452 if (schemeproxy !=
"http_proxy")
454 std::transform(schemeproxy.begin(), schemeproxy.end(), schemeproxy.begin(), ::toupper);
459 XXX <<
"checking DNS lookup of " << host << endl;
464 strncpy(
_curlError,
"DNS pipe creation failed", CURL_ERROR_SIZE);
468 if (
_pid == pid_t(-1))
474 strncpy(
_curlError,
"DNS checker fork failed", CURL_ERROR_SIZE);
481 struct addrinfo *ai, aihints;
482 memset(&aihints, 0,
sizeof(aihints));
483 aihints.ai_family = PF_UNSPEC;
484 int tstsock = socket(PF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
486 aihints.ai_family = PF_INET;
489 aihints.ai_socktype = SOCK_STREAM;
490 aihints.ai_flags = AI_CANONNAME;
493 alarm(connecttimeout);
494 signal(SIGALRM, SIG_DFL);
495 if (getaddrinfo(host.c_str(), NULL, &aihints, &ai))
521 while (waitpid(
_pid, &status, 0) == -1)
532 if (!WIFEXITED(status))
535 strncpy(
_curlError,
"DNS lookup failed", CURL_ERROR_SIZE);
539 int exitcode = WEXITSTATUS(status);
540 XXX <<
"#" <<
_workerno <<
": DNS lookup returned " << exitcode << endl;
544 strncpy(
_curlError,
"DNS lookup failed", CURL_ERROR_SIZE);
574 size_t cnt = l >
sizeof(buf) ?
sizeof(buf) : l;
589 XXX <<
"start stealing!" << endl;
593 std::list<multifetchworker *>::iterator workeriter =
_request->
_workers.begin();
600 if (worker->
_pass == -1)
659 XXX <<
"#" <<
_workerno <<
": going to sleep for " << sl * 1000 <<
" ms" << endl;
680 std::list<multifetchworker *>::iterator workeriter =
_request->
_workers.begin();
753 sprintf(rangebuf,
"%llu-", (
unsigned long long)
_blkstart);
761 strncpy(
_curlError,
"curl_easy_setopt range failed", CURL_ERROR_SIZE);
768 strncpy(
_curlError,
"curl_multi_add_handle failed", CURL_ERROR_SIZE);
818 for (
size_t blkno = 0; blkno < blklist->
numBlocks(); blkno++)
824 else if (filesize != off_t(-1))
830 for (std::list<multifetchworker *>::iterator workeriter =
_workers.begin(); workeriter !=
_workers.end(); ++workeriter)
843 std::vector<Url>::iterator urliter = urllist.begin();
846 fd_set rset, wset, xset;
851 XXX <<
"finished!" << endl;
875 WAR <<
"No more active workers!" << endl;
877 for (std::list<multifetchworker *>::iterator workeriter =
_workers.begin(); workeriter !=
_workers.end(); ++workeriter)
890 curl_multi_fdset(
_multi, &rset, &wset, &xset, &maxfd);
893 for (std::list<multifetchworker *>::iterator workeriter =
_workers.begin(); workeriter !=
_workers.end(); ++workeriter)
894 (*workeriter)->adddnsfd(rset, maxfd);
905 for (std::list<multifetchworker *>::iterator workeriter =
_workers.begin(); workeriter !=
_workers.end(); ++workeriter)
921 tv.tv_usec = sl * 1000000;
923 int r = select(maxfd + 1, &rset, &wset, &xset, &tv);
924 if (r == -1 && errno != EINTR)
927 for (std::list<multifetchworker *>::iterator workeriter =
_workers.begin(); workeriter !=
_workers.end(); ++workeriter)
943 mcode = curl_multi_perform(
_multi, &tasks);
944 if (mcode == CURLM_CALL_MULTI_PERFORM)
946 if (mcode != CURLM_OK)
967 for (std::list<multifetchworker *>::iterator workeriter =
_workers.begin(); workeriter !=
_workers.end(); ++workeriter)
976 XXX <<
"#" << worker->
_workerno <<
": sleep done, wake up" << endl;
985 while ((msg = curl_multi_info_read(
_multi, &nqueue)) != 0)
987 if (msg->msg != CURLMSG_DONE)
989 CURL *easy = msg->easy_handle;
990 CURLcode cc = msg->data.result;
992 if (curl_easy_getinfo(easy, CURLINFO_PRIVATE, &worker) != CURLE_OK)
1002 curl_multi_remove_handle(
_multi, easy);
1003 if (cc == CURLE_HTTP_RETURNED_ERROR)
1005 long statuscode = 0;
1006 (void)curl_easy_getinfo(easy, CURLINFO_RESPONSE_CODE, &statuscode);
1007 XXX <<
"HTTP status " << statuscode << endl;
1008 if (statuscode == 416 && !
_blklist)
1014 XXX <<
"#" << worker->
_workerno <<
": retrying with no end range" << endl;
1034 WAR <<
"#" << worker->
_workerno <<
": checksum error, disable worker" << endl;
1036 strncpy(worker->
_curlError,
"checksum error", CURL_ERROR_SIZE);
1050 XXX <<
"#" << worker->
_workerno <<
": recheck checksum error, refetch block" << endl;
1063 int maxworkerno = 0;
1065 for (std::list<multifetchworker *>::iterator workeriter =
_workers.begin(); workeriter !=
_workers.end(); ++workeriter)
1080 double ratio = worker->
_avgspeed / maxavg;
1083 ratio = ratio * ratio;
1086 XXX <<
"#" << worker->
_workerno <<
": too slow ("<< ratio <<
", " << worker->
_avgspeed <<
", #" << maxworkerno <<
": " << maxavg <<
"), going to sleep for " << ratio * 1000 <<
" ms" << endl;
1107 #if CURLVERSION_AT_LEAST(7,15,5) 1108 curl_easy_setopt(worker->
_curl, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t)(avg));
1150 WAR <<
"overall result" << endl;
1151 for (std::list<multifetchworker *>::iterator workeriter =
_workers.begin(); workeriter !=
_workers.end(); ++workeriter)
1165 MIL <<
"MediaMultiCurl::MediaMultiCurl(" << url_r <<
", " << attach_point_hint_r <<
")" << endl;
1179 curl_multi_cleanup(
_multi);
1182 std::map<std::string, CURL *>::iterator it;
1185 CURL *easy = it->second;
1188 curl_easy_cleanup(easy);
1204 for (; sl; sl = sl->next)
1213 while ((l = pread(fd, buf,
sizeof(buf) - 1, (off_t)0)) == -1 && errno == EINTR)
1219 while (*p ==
' ' || *p ==
'\t' || *p ==
'\r' || *p ==
'\n')
1221 if (!strncasecmp(p,
"<?xml", 5))
1223 while (*p && *p !=
'>')
1227 while (*p ==
' ' || *p ==
'\t' || *p ==
'\r' || *p ==
'\n')
1230 bool ret = !strncasecmp(p,
"<metalink", 9) ? true :
false;
1237 if ((fd = open(file.
asString().c_str(), O_RDONLY|O_CLOEXEC)) == -1)
1241 DBG <<
"looks_like_metalink(" << file <<
"): " << ret << endl;
1257 if ( curl_easy_getinfo(
_curl, CURLINFO_PRIVATE, &fp ) != CURLE_OK || !fp )
1259 if ( ftell( fp ) == 0 )
1264 long httpReturnCode = 0;
1265 if (curl_easy_getinfo(
_curl, CURLINFO_RESPONSE_CODE, &httpReturnCode ) != CURLE_OK || httpReturnCode == 0)
1269 bool ismetalink =
false;
1270 if (curl_easy_getinfo(
_curl, CURLINFO_CONTENT_TYPE, &ptr) == CURLE_OK && ptr)
1272 std::string ct = std::string(ptr);
1273 if (ct.find(
"application/metalink+xml") == 0 || ct.find(
"application/metalink4+xml") == 0)
1276 if (!ismetalink && dlnow < 256)
1285 DBG <<
"looks_like_metalink_fd: " << ismetalink << endl;
1304 DBG <<
"assert_dir " << dest.
dirname() <<
" failed" << endl;
1314 ERR <<
"out of memory for temp file name" << endl;
1318 AutoFD tmp_fd { ::mkostemp( buf, O_CLOEXEC ) };
1321 ERR <<
"mkstemp failed for file '" << destNew <<
"'" << endl;
1326 file = ::fdopen( tmp_fd,
"we" );
1329 ERR <<
"fopen failed for file '" << destNew <<
"'" << endl;
1332 tmp_fd.resetDispose();
1335 DBG <<
"dest: " << dest << endl;
1336 DBG <<
"temp: " << destNew << endl;
1341 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
1342 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, (
long)
PathInfo(target).mtime());
1346 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1347 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1353 curl_easy_setopt(
_curl, CURLOPT_PRIVATE, (*file) );
1360 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1361 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1363 curl_easy_setopt(
_curl, CURLOPT_PRIVATE, (
void *)0);
1366 curl_easy_setopt(
_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1367 curl_easy_setopt(
_curl, CURLOPT_TIMEVALUE, 0L);
1369 curl_easy_setopt(
_curl, CURLOPT_PRIVATE, (
void *)0);
1370 long httpReturnCode = 0;
1371 CURLcode infoRet = curl_easy_getinfo(
_curl, CURLINFO_RESPONSE_CODE, &httpReturnCode);
1372 if (infoRet == CURLE_OK)
1375 if ( httpReturnCode == 304
1378 DBG <<
"not modified: " <<
PathInfo(dest) << endl;
1384 WAR <<
"Could not get the reponse code." << endl;
1387 bool ismetalink =
false;
1390 if (curl_easy_getinfo(
_curl, CURLINFO_CONTENT_TYPE, &ptr) == CURLE_OK && ptr)
1392 std::string ct = std::string(ptr);
1393 if (ct.find(
"application/metalink+xml") == 0 || ct.find(
"application/metalink4+xml") == 0)
1408 bool userabort =
false;
1416 std::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";
1458 if (
PathInfo(destNew).size() >= 63336)
1467 file = fopen((*destNew).c_str(),
"w+e");
1476 ERR <<
"Failed to chmod file " << destNew << endl;
1483 ERR <<
"Fclose failed for file '" << destNew <<
"'" << endl;
1487 if (
rename( destNew, dest ) != 0 )
1489 ERR <<
"Rename failed" << endl;
1492 destNew.resetDispose();
1500 if (blklist && filesize == off_t(-1) && blklist->
haveFilesize())
1502 if (blklist && !blklist->
haveBlocks() && filesize != 0)
1504 if (blklist && (filesize == 0 || !blklist->
numBlocks()))
1513 _multi = curl_multi_init();
1527 std::vector<Url> myurllist;
1528 for (std::vector<Url>::iterator urliter = urllist->begin(); urliter != urllist->end(); ++urliter)
1532 std::string scheme = urliter->getScheme();
1533 if (scheme ==
"http" || scheme ==
"https" || scheme ==
"ftp" || scheme ==
"tftp")
1543 if (!myurllist.size())
1544 myurllist.push_back(baseurl);
1553 if (fseeko(fp, off_t(0), SEEK_SET))
1559 while ((l = fread(buf, 1,
sizeof(buf), fp)) > 0)
1567 return _dnsok.find(host) ==
_dnsok.end() ? false :
true;
1589 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'.
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
static ZConfig & instance()
Singleton ctor.
zypp::Url propagateQueryParams(zypp::Url url_r, const zypp::Url &template_r)
Compute Message Digests (MD5, SHA1 etc)
Store and operate with byte count.
Pathname extend(const std::string &r) const
Append string r to the last component of the path.
AutoDispose< const Pathname > ManagedFile
A Pathname plus associated cleanup code to be executed when path is no longer needed.
static const Unit MB
1000^2 Byte
AutoDispose<int> calling ::close
#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.
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.
void resetDispose()
Set no dispose function.
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.
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
Wrapper class for ::stat/::lstat.
AutoDispose<FILE*> calling ::fclose
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.