libzypp 17.34.0
MediaMultiCurl.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
13#include <ctype.h>
14#include <sys/types.h>
15#include <signal.h>
16#include <sys/wait.h>
17#include <netdb.h>
18#include <arpa/inet.h>
19#include <glib.h>
20
21#include <utility>
22#include <vector>
23#include <iostream>
24#include <algorithm>
25
26
27#include <zypp/ManagedFile.h>
28#include <zypp/ZConfig.h>
29#include <zypp/base/Logger.h>
33#include <zypp-curl/parser/MetaLinkParser>
36#include <zypp-curl/auth/CurlAuthData>
39
40using std::endl;
41using namespace zypp::base;
42
43#undef CURLVERSION_AT_LEAST
44#define CURLVERSION_AT_LEAST(M,N,O) LIBCURL_VERSION_NUM >= ((((M)<<8)+(N))<<8)+(O)
45
46namespace zypp {
47 namespace media {
48
50
51
115
116struct Stripe {
117
118 enum RState {
119 PENDING, //< Pending Range
120 FETCH, //< Fetch is running!
121 COMPETING, //< Competing workers, needs checksum recheck
122 FINALIZED, //< Done, don't write to it anymore
123 REFETCH //< This block needs a refetch
124 };
125
126 std::vector<off_t> blocks; //< required block numbers from blocklist
127 std::vector<RState> blockStates; //< current state of each block in blocks
128};
129
139
140// Hack: we derive from MediaCurl just to get the storage space for
141// settings, url, curlerrors and the like
143 friend class multifetchrequest;
144
145public:
151 ~multifetchworker() override;
152
157 void nextjob();
158
163 void runjob();
164
170 bool continueJob();
171
176 bool recheckChecksum( off_t blockIdx );
177
181 void disableCompetition();
182
186 void checkdns();
187 void adddnsfd( std::vector<GPollFD> &waitFds );
188 void dnsevent(const std::vector<GPollFD> &waitFds );
189
190 const int _workerno;
191
193 bool _competing = false;
194
195 std::vector<MultiByteHandler::Range> _blocks;
196 std::vector<off_t> _rangeToStripeBlock;
197
199 std::unique_ptr<MultiByteHandler> _multiByteHandler;
200
201 off_t _stripe = 0; //< The current stripe we are downloading
202 size_t _datasize = 0; //< The nr of bytes we need to download overall
203
204 double _starttime = 0; //< When was the current job started
205 size_t _datareceived = 0; //< Data downloaded in the current job only
206 off_t _received = 0; //< Overall data"MultiByteHandler::prepare failed" fetched by this worker
207
208 double _avgspeed = 0;
209 double _maxspeed = 0;
210
211 double _sleepuntil = 0;
212
213private:
214 void run();
215 void stealjob();
216 bool setupHandle();
218
219 size_t writefunction ( char *ptr, std::optional<off_t> offset, size_t bytes ) override;
220 size_t headerfunction ( char *ptr, size_t bytes ) override;
221 bool beginRange ( off_t range, std::string &cancelReason ) override;
222 bool finishedRange ( off_t range, bool validated, std::string &cancelReason ) override;
223
225 int _pass = 0;
226 std::string _urlbuf;
227
228 pid_t _pid = 0;
229 int _dnspipe = -1;
230};
231
233public:
235 Url baseurl, CURLM *multi, FILE *fp,
237 MediaBlockList &&blklist, off_t filesize);
243
244 void run(std::vector<Url> &urllist);
245 static ByteCount makeBlksize( uint maxConns, size_t filesize );
246
248 return _blklist;
249 }
250
251protected:
252 friend class multifetchworker;
253
257
258 FILE *_fp = nullptr;
261
262 std::vector<Stripe> _requiredStripes; // all the data we need
263
264 off_t _filesize = 0; //< size of the file we want to download
265
266 std::list< std::unique_ptr<multifetchworker> > _workers;
267 bool _stealing = false;
268 bool _havenewjob = false;
269
270 zypp::ByteCount _defaultBlksize = 0; //< The blocksize to use if the metalink file does not specify one
271 off_t _stripeNo = 0; //< next stripe to download
272
273 size_t _activeworkers = 0;
274 size_t _lookupworkers = 0;
275 size_t _sleepworkers = 0;
276 double _minsleepuntil = 0;
277 bool _finished = false;
278
279 off_t _totalsize = 0; //< nr of bytes we need to download ( e.g. filesize - ( bytes reused from deltafile ) )
280 off_t _fetchedsize = 0;
282
283 double _starttime = 0;
284 double _lastprogress = 0;
285
288 double _periodavg = 0;
289
290public:
291 double _timeout = 0;
293 double _maxspeed = 0;
294 int _maxworkers = 0;
295};
296
297constexpr auto MIN_REQ_MIRRS = 4;
298constexpr auto MAXURLS = 10;
299
300// TCP communication scales up as a connection proceeds. This is due to TCP slowstart where
301// the congestion window scales up. The stripe calculation assumes that every package can be fairly
302// downloaded from multiple mirrors omits that attempting to download say 1MB from 4 mirrors
303// means 4 requests of 256k, where then you have four congestion windows that need to increase
304// meaning the overall download speed is significantly lower. Counter intuitively this leads to
305// cases where *more* mirrors being available to zypper significantly lowers performance.
306//
307// Instead, there should be a minimum stripe size cap. This way any item smaller than the value
308// is downloaded in a single request, where as larger items are downloaded from many mirrors
309// but each range has enough time to increase it's congestion window to something reasonable.
310//
311// Initial value 4 MB;
312constexpr auto MIN_STRIPE_SIZE_KB = 4096;
313
315
316static double
318{
319#if _POSIX_C_SOURCE >= 199309L
320 struct timespec ts;
322 return 0;
323 return ts.tv_sec + ts.tv_nsec / 1000000000.;
324#else
325 struct timeval tv;
326 if (gettimeofday(&tv, NULL))
327 return 0;
328 return tv.tv_sec + tv.tv_usec / 1000000.;
329#endif
330}
331
332size_t
333multifetchworker::writefunction(char *ptr, std::optional<off_t> offset, size_t bytes)
334{
336 return bytes ? 0 : 1;
337
338 double now = currentTime();
339
340 // update stats of overall data
342 _received += bytes;
343 _request->_lastprogress = now;
344
345 const auto &currRange = _multiByteHandler->currentRange();
346 if (!currRange)
347 return 0; // we always write to a range
348
350 if ( !_request->_fp || stripeDesc.blockStates[ _rangeToStripeBlock[*currRange] ] == Stripe::FINALIZED ) {
351 // someone else finished our block first!
352 // we stop here and fetch new jobs if there are still some
354 _competing = false;
355 return 0;
356 }
357
358 const auto &blk = _blocks[*currRange];
359 off_t seekTo = blk.start + blk.bytesWritten;
360
361 if ( ftell( _request->_fp ) != seekTo ) {
362 // if we can't seek the file there is no purpose in trying again
363 if (fseeko(_request->_fp, seekTo, SEEK_SET))
364 return bytes ? 0 : 1;
365 }
366
367 size_t cnt = fwrite(ptr, 1, bytes, _request->_fp);
368 _request->_fetchedsize += cnt;
369 return cnt;
370}
371
373{
376 const auto &currRangeState = stripeDesc.blockStates[stripeRangeOff];
377
379 cancelReason = "Cancelled because stripe block is already finalized";
381 WAR << "#" << _workerno << ": trying to start a range ("<<stripeRangeOff<<"["<< _blocks[workerRangeOff].start <<" : "<<_blocks[workerRangeOff].len<<"]) that was already finalized, cancelling. Stealing was: " << _request->_stealing << endl;
382 return false;
383 }
385 return true;
386}
387
389{
392 const auto &currRangeState = stripeDesc.blockStates[stripeRangeOff];
393
394 if ( !validated ) {
395 // fail, worker will go into WORKER_BROKEN
396 cancelReason = "Block failed to validate";
397 return false;
398 }
399
400 if ( currRangeState == Stripe::FETCH ) {
401 // only us who wrote here, block is finalized
404 } else {
405 // others wrote here, we need to check the full checksum
409 } else {
410 // someone messed that block up, set it to refetch but continue since our
411 // data is valid
412 WAR << "#" << _workerno << ": Broken data in COMPETING block, requesting refetch. Stealing is: " << _request->_stealing << endl;
414 }
415 }
416 return true;
417}
418
419size_t
421{
422 size_t l = bytes;
423 if (l > 9 && !strncasecmp(p, "Location:", 9)) {
424 std::string line(p + 9, l - 9);
425 if (line[l - 10] == '\r')
426 line.erase(l - 10, 1);
427 XXX << "#" << _workerno << ": redirecting to" << line << endl;
428 return bytes;
429 }
430
431 const auto &repSize = _multiByteHandler->reportedFileSize ();
432 if ( repSize && *repSize != _request->_filesize ) {
433 XXX << "#" << _workerno << ": filesize mismatch" << endl;
435 setCurlError("filesize mismatch");
436 return 0;
437 }
438
439 return bytes;
440}
441
443: MediaCurl(url, Pathname())
444, _workerno( no )
445, _maxspeed( request._maxspeed )
446, _request ( &request )
447{
449 _urlbuf = curlUrl.asString();
451 if (_curl)
452 XXX << "reused worker from pool" << endl;
453 if (!_curl && !(_curl = curl_easy_init()))
454 {
456 setCurlError("curl_easy_init failed");
457 return;
458 }
459
460 if ( url.getScheme() == "http" || url.getScheme() == "https" )
462
463 setupHandle();
464 checkdns();
465}
466
468{
469 try {
470 setupEasy();
471 } catch (Exception &ex) {
473 _curl = 0;
475 setCurlError("curl_easy_setopt failed");
476 return false;
477 }
480
481 // if this is the same host copy authorization
482 // (the host check is also what curl does when doing a redirect)
483 // (note also that unauthorized exceptions are thrown with the request host)
484 if ( _url.getHost() == _request->_context->_url.getHost()) {
488 if ( _settings.userPassword().size() ) {
490 std::string use_auth = _settings.authType();
491 if (use_auth.empty())
492 use_auth = "digest,basic"; // our default
494 if( auth != CURLAUTH_NONE)
495 {
496 XXX << "#" << _workerno << ": Enabling HTTP authentication methods: " << use_auth
497 << " (CURLOPT_HTTPAUTH=" << auth << ")" << std::endl;
499 }
500 }
501 }
502 return true;
503}
504
506{
507 if (_curl)
508 {
512 {
513#if CURLVERSION_AT_LEAST(7,15,5)
515#endif
522 }
523 else
525 _curl = 0;
526 }
527 if (_pid)
528 {
529 kill(_pid, SIGKILL);
530 int status = 0;
531 while (waitpid(_pid, &status, 0) == -1)
532 if (errno != EINTR)
533 break;
534 _pid = 0;
535 }
536 if (_dnspipe != -1)
537 {
538 close(_dnspipe);
539 _dnspipe = -1;
540 }
541 // the destructor in MediaCurl doesn't call disconnect() if
542 // the media is not attached, so we do it here manually
544}
545
546static inline bool env_isset(const std::string& name)
547{
548 const char *s = getenv(name.c_str());
549 return s && *s ? true : false;
550}
551
552void
554{
555 std::string host = _url.getHost();
556
557 if (host.empty())
558 return;
559
560 if (_request->_context->isDNSok(host))
561 return;
562
563 // no need to do dns checking for numeric hosts
564 char addrbuf[128];
565 if (inet_pton(AF_INET, host.c_str(), addrbuf) == 1)
566 return;
567 if (inet_pton(AF_INET6, host.c_str(), addrbuf) == 1)
568 return;
569
570 // no need to do dns checking if we use a proxy
571 if (!_settings.proxy().empty())
572 return;
573 if (env_isset("all_proxy") || env_isset("ALL_PROXY"))
574 return;
575 std::string schemeproxy = _url.getScheme() + "_proxy";
577 return;
578 if (schemeproxy != "http_proxy")
579 {
580 std::transform(schemeproxy.begin(), schemeproxy.end(), schemeproxy.begin(), ::toupper);
582 return;
583 }
584
585 XXX << "checking DNS lookup of " << host << endl;
586 int pipefds[2];
587 if (pipe(pipefds))
588 {
590 setCurlError("DNS pipe creation failed");
591 return;
592 }
593 _pid = fork();
594 if (_pid == pid_t(-1))
595 {
596 close(pipefds[0]);
597 close(pipefds[1]);
598 _pid = 0;
600 setCurlError("DNS checker fork failed");
601 return;
602 }
603 else if (_pid == 0)
604 {
605 close(pipefds[0]);
606 // XXX: close all other file descriptors
607 struct addrinfo *ai = nullptr, aihints;
608 memset(&aihints, 0, sizeof(aihints));
609 aihints.ai_family = PF_UNSPEC;
610 int tstsock = socket(PF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
611 if (tstsock == -1)
612 aihints.ai_family = PF_INET;
613 else
614 close(tstsock);
615 aihints.ai_socktype = SOCK_STREAM;
616 aihints.ai_flags = AI_CANONNAME;
618 if (connecttimeout)
620 signal(SIGALRM, SIG_DFL);
621 if (getaddrinfo(host.c_str(), NULL, &aihints, &ai))
622 _exit(1);
623 _exit(0);
624 }
625 close(pipefds[1]);
626 _dnspipe = pipefds[0];
628}
629
630void
632{
633 if (_state != WORKER_LOOKUP)
634 return;
635
636 waitFds.push_back (
637 GPollFD {
638 .fd = _dnspipe,
639 .events = G_IO_IN | G_IO_HUP | G_IO_ERR,
640 .revents = 0
641 });
642}
643
644void
645multifetchworker::dnsevent( const std::vector<GPollFD> &waitFds )
646{
647 bool hasEvent = std::any_of( waitFds.begin (), waitFds.end(),[this]( const GPollFD &waitfd ){
648 return ( waitfd.fd == _dnspipe && waitfd.revents != 0 );
649 });
650
651 if (_state != WORKER_LOOKUP || !hasEvent)
652 return;
653 int status = 0;
654 while (waitpid(_pid, &status, 0) == -1)
655 {
656 if (errno != EINTR)
657 return;
658 }
659 _pid = 0;
660 if (_dnspipe != -1)
661 {
662 close(_dnspipe);
663 _dnspipe = -1;
664 }
665 if (!WIFEXITED(status))
666 {
668 setCurlError("DNS lookup failed");
670 return;
671 }
672 int exitcode = WEXITSTATUS(status);
673 XXX << "#" << _workerno << ": DNS lookup returned " << exitcode << endl;
674 if (exitcode != 0)
675 {
677 setCurlError("DNS lookup failed");
679 return;
680 }
682 nextjob();
683}
684
686{
687 // XXX << "recheckChecksum block " << _blkno << endl;
688 if (!_request->_fp || !_datasize || !_blocks.size() )
689 return true;
690
691 auto &blk = _blocks[workerRangeIdx];
692 if ( !blk._digest )
693 return true;
694
695 const auto currOf = ftell( _request->_fp );
696 if ( currOf == -1 )
697 return false;
698
699 if (fseeko(_request->_fp, blk.start, SEEK_SET))
700 return false;
701
702 zypp::Digest newDig = blk._digest->clone();
703
704 char buf[4096];
705 size_t l = blk.len;
706 while (l) {
707 size_t cnt = l > sizeof(buf) ? sizeof(buf) : l;
708 if (fread(buf, cnt, 1, _request->_fp) != 1)
709 return false;
710 newDig.update(buf, cnt);
711 l -= cnt;
712 }
713
715 return false;
716
717 blk._digest = std::move(newDig);
718 if (!_multiByteHandler->validateRange(blk)) {
719 WAR << "#" << _workerno << " Stripe: " << _stripe << ": Stripe-Block: " << _rangeToStripeBlock[workerRangeIdx] << " failed to validate" << endl;
720 return false;
721 }
722
723 return true;
724}
725
730{
732 std::optional<zypp::Digest> digest;
733 std::optional<size_t> relDigLen;
734 std::optional<size_t> blkSumPad;
735
736 const auto &blk = _request->_blklist.getBlock( blkNo );
737 if ( _request->_blklist.haveChecksum ( blkNo ) ) {
739 relDigLen = sum.size( );
741 digest = zypp::Digest();
742 digest->create( _request->_blklist.getChecksumType() );
743 }
744
745 return MultiByteHandler::Range::make(
746 blk.off,
747 blk.size,
748 std::move(digest),
749 std::move(sum),
750 {}, // empty user data
751 std::move(relDigLen),
752 std::move(blkSumPad) );
753}
754
756{
757 if (!_request->_stealing)
758 {
759 XXX << "start stealing!" << endl;
760 _request->_stealing = true;
761 }
762
763 multifetchworker *best = 0; // best choice for the worker we want to compete with
764 double now = 0;
765
766 // look through all currently running workers to find the best candidate we
767 // could steal from
768 for (auto workeriter = _request->_workers.begin(); workeriter != _request->_workers.end(); ++workeriter)
769 {
770 multifetchworker *worker = workeriter->get();
771 if (worker == this)
772 continue;
773 if (worker->_pass == -1)
774 continue; // do not steal!
775 if (worker->_state == WORKER_DISCARD || worker->_state == WORKER_DONE || worker->_state == WORKER_SLEEP || !worker->_datasize)
776 continue; // do not steal finished jobs
777 if (!worker->_avgspeed && worker->_datareceived)
778 {
779 // calculate avg speed for the worker if that was not done yet
780 if (!now)
781 now = currentTime();
782 if (now > worker->_starttime)
783 worker->_avgspeed = worker->_datareceived / (now - worker->_starttime);
784 }
785 // only consider worker who still have work
786 if ( worker->_datasize - worker->_datareceived <= 0 )
787 continue;
788 if (!best || best->_pass > worker->_pass)
789 {
790 best = worker;
791 continue;
792 }
793 if (best->_pass < worker->_pass)
794 continue;
795 // if it is the same stripe, our current best choice is competing with the worker we are looking at
796 // we need to check if we are faster than the fastest one competing for this stripe, so we want the best.
797 // Otherwise the worst.
798 if (worker->_stripe == best->_stripe)
799 {
800 if ((worker->_datasize - worker->_datareceived) * best->_avgspeed < (best->_datasize - best->_datareceived) * worker->_avgspeed)
801 best = worker;
802 }
803 else
804 {
805 if ((worker->_datasize - worker->_datareceived) * best->_avgspeed > (best->_datasize - best->_datareceived) * worker->_avgspeed)
806 best = worker;
807 }
808 }
809 if (!best)
810 {
813 _request->_finished = true;
814 return;
815 }
816 // do not sleep twice
817 if (_state != WORKER_SLEEP)
818 {
819 if (!_avgspeed && _datareceived)
820 {
821 if (!now)
822 now = currentTime();
823 if (now > _starttime)
825 }
826
827 // lets see if we should sleep a bit
828 XXX << "me #" << _workerno << ": " << _avgspeed << ", size " << best->_datasize << endl;
829 XXX << "best #" << best->_workerno << ": " << best->_avgspeed << ", size " << (best->_datasize - best->_datareceived) << endl;
830
831 // check if we could download the full data from best faster than best could download its remaining data
832 if ( _avgspeed && best->_avgspeed // we and best have average speed information
833 && _avgspeed <= best->_avgspeed ) // and we are not faster than best
834 {
835 if (!now)
836 now = currentTime();
837 double sl = (best->_datasize - best->_datareceived) / best->_avgspeed * 2;
838 if (sl > 1)
839 sl = 1;
840 XXX << "#" << _workerno << ": going to sleep for " << sl * 1000 << " ms" << endl;
841 _sleepuntil = now + sl;
844 return;
845 }
846 }
847
848 _competing = true;
849 best->_competing = true;
850 _stripe = best->_stripe;
851
852 best->_pass++;
853 _pass = best->_pass;
854
855 runjob();
856}
857
858void
860{
861 for ( auto workeriter = _request->_workers.begin(); workeriter != _request->_workers.end(); ++workeriter)
862 {
863 multifetchworker *worker = workeriter->get();
864 if (worker == this)
865 continue;
866 if (worker->_stripe == _stripe)
867 {
868 if (worker->_state == WORKER_FETCH)
869 worker->_state = WORKER_DISCARD;
870 worker->_pass = -1; /* do not steal this one, we already have it */
871 }
872 }
873}
874
876{
877 _datasize = 0;
878 _blocks.clear();
879
880 // claim next stripe for us, or steal if there nothing left to claim
882 stealjob();
883 return;
884 }
885
887 runjob();
888}
889
891{
892 _datasize = 0;
893 _blocks.clear ();
894 _rangeToStripeBlock.clear ();
895
897 for ( uint i = 0; i < stripeDesc.blocks.size(); i++ ) {
898 // ignore verified and finalized ranges
899 if( stripeDesc.blockStates[i] == Stripe::FINALIZED ) {
900 continue;
901 } else {
902 _blocks.push_back( rangeFromBlock(stripeDesc.blocks[i]) );
903 _rangeToStripeBlock.push_back( i );
904 _datasize += _blocks.back().len;
905 }
906 }
907
908 if ( _datasize == 0 ) {
909 // no blocks left in this stripe
912 if ( !_request->_activeworkers )
913 _request->_finished = true;
914 return;
915 }
916
917 DBG << "#" << _workerno << "Done adding blocks to download, going to download: " << _blocks.size() << " nr of block with " << _datasize << " nr of bytes" << std::endl;
918
919 _multiByteHandler.reset( nullptr );
920 _multiByteHandler = std::make_unique<MultiByteHandler>(_protocolMode, _curl, _blocks, *this );
922 _datareceived = 0;
923 run();
924}
925
927{
928 bool hadRangeFail = _multiByteHandler->lastError() == MultiByteHandler::Code::RangeFail;
929 if ( !_multiByteHandler->prepareToContinue() ) {
930 setCurlError(_multiByteHandler->lastErrorMessage().c_str());
931 return false;
932 }
933
934 if ( hadRangeFail ) {
935 // we reset the handle to default values. We do this to not run into
936 // "transfer closed with outstanding read data remaining" error CURL sometimes returns when
937 // we cancel a connection because of a range error to request a smaller batch.
938 // The error will still happen but much less frequently than without resetting the handle.
939 //
940 // Note: Even creating a new handle will NOT fix the issue
942 if ( !setupHandle())
943 return false;
944 }
945
946 run();
947 return true;
948}
949
950void
952{
954 return; // just in case...
955
956 if ( !_multiByteHandler->prepare() ) {
959 setCurlError(_multiByteHandler->lastErrorMessage ().c_str());
960 return;
961 }
962
966 setCurlError("curl_multi_add_handle failed");
967 return;
968 }
969
970 _request->_havenewjob = true;
972}
973
974
976
977
979 : internal::CurlPollHelper::CurlPoll{ multi }
980 , _context(context)
981 , _filename(std::move(filename))
982 , _baseurl(std::move(baseurl))
983 , _fp(fp)
984 , _report(report)
985 , _blklist(std::move(blklist))
986 , _filesize(filesize)
987 , _starttime(currentTime())
988 , _timeout(context->_settings.timeout())
989 , _connect_timeout(context->_settings.connectTimeout())
990 , _maxspeed(context->_settings.maxDownloadSpeed())
991 , _maxworkers(context->_settings.maxConcurrentConnections())
992 {
994 if (_maxworkers > MAXURLS)
996 if (_maxworkers <= 0)
997 _maxworkers = 1;
998
999 // calculate the total size of our download
1000 for (size_t blkno = 0; blkno < _blklist.numBlocks(); blkno++)
1002
1003 // equally distribute the data we want to download over all workers
1005
1006 // lets build stripe informations
1008 for (size_t blkno = 0; blkno < _blklist.numBlocks(); blkno++) {
1009
1011 if ( _requiredStripes.empty() || currStripeSize >= _defaultBlksize ) {
1012 _requiredStripes.push_back( Stripe{} );
1013 currStripeSize = 0;
1014 }
1015
1016 _requiredStripes.back().blocks.push_back(blkno);
1017 _requiredStripes.back().blockStates.push_back(Stripe::PENDING);
1018 currStripeSize += blk.size;
1019 }
1020
1021 MIL << "Downloading " << _blklist.numBlocks() << " blocks via " << _requiredStripes.size() << " stripes on " << _maxworkers << " connections." << endl;
1022}
1023
1028
1029void
1031{
1032 int workerno = 0;
1033 std::vector<Url>::iterator urliter = urllist.begin();
1034
1036
1037 // kickstart curl
1038 CURLMcode mcode = _curlHelper.handleTimout();
1039 if (mcode != CURLM_OK)
1040 ZYPP_THROW(MediaCurlException(_baseurl, "curl_multi_socket_action", "unknown error"));
1041
1042 for (;;)
1043 {
1044 // list of all fds we want to poll
1045 std::vector<GPollFD> waitFds;
1046 int dnsFdCount = 0;
1047
1048 if (_finished)
1049 {
1050 XXX << "finished!" << endl;
1051 break;
1052 }
1053
1054 if ((int)_activeworkers < _maxworkers && urliter != urllist.end() && _workers.size() < MAXURLS)
1055 {
1056 // spawn another worker!
1057 _workers.push_back(std::make_unique<multifetchworker>(workerno++, *this, *urliter));
1058 auto &worker = _workers.back();
1059 if (worker->_state != WORKER_BROKEN)
1060 {
1062 if (worker->_state != WORKER_LOOKUP)
1063 {
1064 worker->nextjob();
1065 }
1066 else
1068 }
1069 ++urliter;
1070 continue;
1071 }
1072 if (!_activeworkers)
1073 {
1074 WAR << "No more active workers!" << endl;
1075 // show the first worker error we find
1076 for (auto workeriter = _workers.begin(); workeriter != _workers.end(); ++workeriter)
1077 {
1078 if ((*workeriter)->_state != WORKER_BROKEN)
1079 continue;
1080 ZYPP_THROW(MediaCurlException(_baseurl, "Server error", (*workeriter)->curlError()));
1081 }
1082 break;
1083 }
1084
1085 if (_lookupworkers)
1086 for (auto workeriter = _workers.begin(); workeriter != _workers.end(); ++workeriter)
1087 (*workeriter)->adddnsfd( waitFds );
1088
1089 // if we added a new job we have to call multi_perform once
1090 // to make it show up in the fd set. do not sleep in this case.
1091 int timeoutMs = _havenewjob ? 0 : 200;
1092 if ( _sleepworkers && !_havenewjob ) {
1093 if (_minsleepuntil == 0) {
1094 for (auto workeriter = _workers.begin(); workeriter != _workers.end(); ++workeriter) {
1095 multifetchworker *worker = workeriter->get();
1096 if (worker->_state != WORKER_SLEEP)
1097 continue;
1098 if (!_minsleepuntil || _minsleepuntil > worker->_sleepuntil)
1099 _minsleepuntil = worker->_sleepuntil;
1100 }
1101 }
1102 double sl = _minsleepuntil - currentTime();
1103 if (sl < 0) {
1104 sl = 0;
1105 _minsleepuntil = 0;
1106 }
1107 if (sl < .2)
1108 timeoutMs = sl * 1000;
1109 }
1110
1111 if ( _curlHelper.timeout_ms.has_value() )
1112 timeoutMs = std::min<long>( timeoutMs, _curlHelper.timeout_ms.value() );
1113
1114 dnsFdCount = waitFds.size(); // remember how many dns fd's we have
1115 waitFds.insert( waitFds.end(), _curlHelper.socks.begin(), _curlHelper.socks.end() ); // add the curl fd's to the poll data
1116
1118 if ( r == -1 )
1119 ZYPP_THROW(MediaCurlException(_baseurl, "zypp_poll() failed", "unknown error"));
1120 if ( r != 0 && _lookupworkers ) {
1121 for (auto workeriter = _workers.begin(); workeriter != _workers.end(); ++workeriter)
1122 {
1123 multifetchworker *worker = workeriter->get();
1124 if (worker->_state != WORKER_LOOKUP)
1125 continue;
1126 (*workeriter)->dnsevent( waitFds );
1127 if (worker->_state != WORKER_LOOKUP)
1129 }
1130 }
1131 _havenewjob = false;
1132
1133 // run curl
1134 if ( r == 0 ) {
1135 CURLMcode mcode = _curlHelper.handleTimout();
1136 if (mcode != CURLM_OK)
1137 ZYPP_THROW(MediaCurlException(_baseurl, "curl_multi_socket_action", "unknown error"));
1138 } else {
1139 CURLMcode mcode = _curlHelper.handleSocketActions( waitFds, dnsFdCount );
1140 if (mcode != CURLM_OK)
1141 ZYPP_THROW(MediaCurlException(_baseurl, "curl_multi_socket_action", "unknown error"));
1142 }
1143
1144 double now = currentTime();
1145
1146 // update periodavg
1147 if (now > _lastperiodstart + .5)
1148 {
1149 if (!_periodavg)
1151 else
1154 _lastperiodstart = now;
1155 }
1156
1157 // wake up sleepers
1158 if (_sleepworkers)
1159 {
1160 for (auto workeriter = _workers.begin(); workeriter != _workers.end(); ++workeriter)
1161 {
1162 multifetchworker *worker = workeriter->get();
1163 if (worker->_state != WORKER_SLEEP)
1164 continue;
1165 if (worker->_sleepuntil > now)
1166 continue;
1167 if (_minsleepuntil == worker->_sleepuntil)
1168 _minsleepuntil = 0;
1169 XXX << "#" << worker->_workerno << ": sleep done, wake up" << endl;
1170 _sleepworkers--;
1171 // nextjob changes the state
1172 worker->nextjob();
1173 }
1174 }
1175
1176 // collect all curl results, (re)schedule jobs
1177 CURLMsg *msg = nullptr;
1178 int nqueue = 0;
1179 while ((msg = curl_multi_info_read(_multi, &nqueue)) != 0)
1180 {
1181 if (msg->msg != CURLMSG_DONE)
1182 continue;
1183 CURL *easy = msg->easy_handle;
1184 CURLcode cc = msg->data.result;
1185 multifetchworker *worker = nullptr;
1186
1188 ZYPP_THROW(MediaCurlException(_baseurl, "curl_easy_getinfo", "unknown error"));
1189
1190 if (worker->_datareceived && now > worker->_starttime) {
1191 if (worker->_avgspeed)
1192 worker->_avgspeed = (worker->_avgspeed + worker->_datareceived / (now - worker->_starttime)) / 2;
1193 else
1194 worker->_avgspeed = worker->_datareceived / (now - worker->_starttime);
1195 }
1196
1197 XXX << "#" << worker->_workerno << " done code " << cc << " speed " << worker->_avgspeed << endl;
1199
1200 const auto &setWorkerBroken = [&]( const std::string &str = {} ){
1201 worker->_state = WORKER_BROKEN;
1202 if ( !str.empty () )
1203 worker->setCurlError(str.c_str());
1205
1206 if (!_activeworkers && !(urliter != urllist.end() && _workers.size() < MAXURLS)) {
1207 // end of workers reached! goodbye!
1208 worker->evaluateCurlCode(Pathname(), cc, false);
1209 }
1210 };
1211
1212 if ( !worker->_multiByteHandler ) {
1213 WAR << "#" << worker->_workerno << ": has no multibyte handler, this is a bug" << endl;
1214 setWorkerBroken("Multibyte handler error");
1215 continue;
1216 }
1217
1218 // tell the worker to finalize the current block
1219 worker->_multiByteHandler->finalize();
1220
1221 if ( worker->_multiByteHandler->hasMoreWork() && ( cc == CURLE_OK || worker->_multiByteHandler->canRecover() ) ) {
1222
1223 WAR << "#" << worker->_workerno << ": still has work to do or can recover from a error, continuing the job!" << endl;
1224 // the current job is not done, or we failed and need to try more, enqueue and start again
1225 if ( !worker->continueJob() ) {
1226 WAR << "#" << worker->_workerno << ": failed to continue (" << worker->_multiByteHandler->lastErrorMessage() << ")" << endl;
1227 setWorkerBroken( worker->_multiByteHandler->lastErrorMessage() );
1228 }
1229 continue;
1230 }
1231
1232 // --- from here on worker has no more ranges in its current job, or had a error it can't recover from ---
1233
1234 if ( cc != CURLE_OK ) {
1235 if ( worker->_state != WORKER_DISCARD ) {
1236 // something went wrong and we can not recover, broken worker!
1238 continue;
1239 } else {
1240 WAR << "#" << worker->_workerno << ": failed, but was set to discard, reusing for new requests" << endl;
1241 }
1242 } else {
1243
1244 // we got what we asked for, maybe. Lets see if all ranges have been marked as finalized
1245 if( !worker->_multiByteHandler->verifyData() ) {
1246 WAR << "#" << worker->_workerno << ": error: " << worker->_multiByteHandler->lastErrorMessage() << ", disable worker" << endl;
1248 continue;
1249 }
1250
1251 // from here on we know THIS worker only got data that verified
1252 // now lets see if the stripe was finished too
1253 // stripe blocks can now be only in FINALIZED or ERROR states
1254 if (worker->_state == WORKER_FETCH ) {
1255 if ( worker->_competing ) {
1256 worker->disableCompetition ();
1257 }
1258 auto &wrkerStripe = _requiredStripes[worker->_stripe];
1259 bool done = std::all_of( wrkerStripe.blockStates.begin(), wrkerStripe.blockStates.begin(), []( const Stripe::RState s ) { return s == Stripe::FINALIZED; } );
1260 if ( !done ) {
1261 // all ranges that are not finalized are in a bogus state, refetch them
1262 std::for_each( wrkerStripe.blockStates.begin(), wrkerStripe.blockStates.begin(), []( Stripe::RState &s ) {
1263 if ( s != Stripe::FINALIZED)
1264 s = Stripe::PENDING;
1265 });
1266
1267 _finished = false; //reset finished flag
1268 worker->runjob();
1269 continue;
1270 }
1271 }
1272
1273 // make bad workers ( bad as in really slow ) sleep a little
1274 double maxavg = 0;
1275 int maxworkerno = 0;
1276 int numbetter = 0;
1277 for (auto workeriter = _workers.begin(); workeriter != _workers.end(); ++workeriter)
1278 {
1280 if (oworker->_state == WORKER_BROKEN)
1281 continue;
1282 if (oworker->_avgspeed > maxavg)
1283 {
1284 maxavg = oworker->_avgspeed;
1285 maxworkerno = oworker->_workerno;
1286 }
1287 if (oworker->_avgspeed > worker->_avgspeed)
1288 numbetter++;
1289 }
1290 if (maxavg && !_stealing)
1291 {
1292 double ratio = worker->_avgspeed / maxavg;
1293 ratio = 1 - ratio;
1294 if (numbetter < 3) // don't sleep that much if we're in the top two
1295 ratio = ratio * ratio;
1296 if (ratio > .01)
1297 {
1298 XXX << "#" << worker->_workerno << ": too slow ("<< ratio << ", " << worker->_avgspeed << ", #" << maxworkerno << ": " << maxavg << "), going to sleep for " << ratio * 1000 << " ms" << endl;
1299 worker->_sleepuntil = now + ratio;
1300 worker->_state = WORKER_SLEEP;
1301 _sleepworkers++;
1302 continue;
1303 }
1304 }
1305
1306 // do rate control (if requested)
1307 // should use periodavg, but that's not what libcurl does
1308 if (_maxspeed && now > _starttime)
1309 {
1310 double avg = _fetchedsize / (now - _starttime);
1311 avg = worker->_maxspeed * _maxspeed / avg;
1312 if (avg < _maxspeed / _maxworkers)
1314 if (avg > _maxspeed)
1315 avg = _maxspeed;
1316 if (avg < 1024)
1317 avg = 1024;
1318 worker->_maxspeed = avg;
1319#if CURLVERSION_AT_LEAST(7,15,5)
1320 curl_easy_setopt(worker->_curl, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t)(avg));
1321#endif
1322 }
1323
1324 worker->nextjob();
1325 }
1326
1327 if ( _filesize > 0 && _fetchedgoodsize > _filesize ) {
1329 }
1330 }
1331
1332 // send report
1333 if (_report)
1334 {
1336
1337 double avg = 0;
1338 if (now > _starttime)
1339 avg = _fetchedsize / (now - _starttime);
1340 if (!(*(_report))->progress(percent, _baseurl, avg, _lastperiodstart == _starttime ? avg : _periodavg))
1341 ZYPP_THROW(MediaCurlException(_baseurl, "User abort", "cancelled"));
1342 }
1343
1344 if (_timeout && now - _lastprogress > _timeout)
1345 break;
1346 }
1347
1348 if (!_finished)
1350
1351 // print some download stats
1352 WAR << "overall result" << endl;
1353 for (auto workeriter = _workers.begin(); workeriter != _workers.end(); ++workeriter)
1354 {
1355 multifetchworker *worker = workeriter->get();
1356 WAR << "#" << worker->_workerno << ": state: " << worker->_state << " received: " << worker->_received << " url: " << worker->_url << endl;
1357 }
1358}
1359
1361{
1362 // If the calculated strip size is too small and can cause a loss in TCP throughput. Raise
1363 // it to a reasonable value.
1364 return std::max<zypp::ByteCount>( filesize / std::min( std::max<int>( 1, maxConns ) , MAXURLS ), zypp::ByteCount(MIN_STRIPE_SIZE_KB, zypp::ByteCount::K) );
1365}
1366
1368
1369
1372{
1373 MIL << "MediaMultiCurl::MediaMultiCurl(" << url_r << ", " << attach_point_hint_r << ")" << endl;
1374 _multi = 0;
1376}
1377
1379{
1381 {
1384 }
1385 if (_multi)
1386 {
1388 _multi = 0;
1389 }
1390 std::map<std::string, CURL *>::iterator it;
1391 for (it = _easypool.begin(); it != _easypool.end(); it++)
1392 {
1393 CURL *easy = it->second;
1394 if (easy)
1395 {
1397 it->second = NULL;
1398 }
1399 }
1400}
1401
1403{
1405
1407 {
1410 }
1411 struct curl_slist *sl = _customHeaders;
1412 for (; sl; sl = sl->next)
1414 //, application/x-zsync
1415 _customHeadersMetalink = curl_slist_append(_customHeadersMetalink, "Accept: */*, application/x-zsync, application/metalink+xml, application/metalink4+xml");
1416}
1417
1418// here we try to suppress all progress coming from a metalink download
1419// bsc#1021291: Nevertheless send alive trigger (without stats), so UIs
1420// are able to abort a hanging metalink download via callback response.
1421int MediaMultiCurl::progressCallback( void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
1422{
1424 if (!_curl)
1426
1427 // bsc#408814: Don't report any sizes before we don't have data on disk. Data reported
1428 // due to redirection etc. are not interesting, but may disturb filesize checks.
1429 FILE *fp = 0;
1432 if ( ftell( fp ) == 0 )
1434
1435 // (no longer needed due to the filesize check above?)
1436 // work around curl bug that gives us old data
1437 long httpReturnCode = 0;
1440
1441 char *ptr = NULL;
1442 bool ismetalink = false;
1444 {
1445 std::string ct = std::string(ptr);
1446 if (ct.find("application/x-zsync") == 0 || ct.find("application/metalink+xml") == 0 || ct.find("application/metalink4+xml") == 0)
1447 ismetalink = true;
1448 }
1449 if (!ismetalink && dlnow < 256)
1450 {
1451 // can't tell yet, ...
1453 }
1454 if (!ismetalink)
1455 {
1456 fflush(fp);
1458 DBG << "looks_like_meta_file: " << ismetalink << endl;
1459 }
1460 if (ismetalink)
1461 {
1462 // this is a metalink file change the expected filesize
1464 // we're downloading the metalink file. Just trigger aliveCallbacks
1467 }
1470}
1471
1473{
1474 Pathname dest = target.absolutename();
1475 if( assert_dir( dest.dirname() ) )
1476 {
1477 DBG << "assert_dir " << dest.dirname() << " failed" << endl;
1478 ZYPP_THROW( MediaSystemException(getFileUrl(srcFile.filename()), "System error on " + dest.dirname().asString()) );
1479 }
1480
1481 ManagedFile destNew { target.extend( ".new.zypp.XXXXXX" ) };
1482 AutoFILE file;
1483 {
1484 AutoFREE<char> buf { ::strdup( (*destNew).c_str() ) };
1485 if( ! buf )
1486 {
1487 ERR << "out of memory for temp file name" << endl;
1488 ZYPP_THROW(MediaSystemException(getFileUrl(srcFile.filename()), "out of memory for temp file name"));
1489 }
1490
1491 AutoFD tmp_fd { ::mkostemp( buf, O_CLOEXEC ) };
1492 if( tmp_fd == -1 )
1493 {
1494 ERR << "mkstemp failed for file '" << destNew << "'" << endl;
1496 }
1498
1499 file = ::fdopen( tmp_fd, "we" );
1500 if ( ! file )
1501 {
1502 ERR << "fopen failed for file '" << destNew << "'" << endl;
1504 }
1505 tmp_fd.resetDispose(); // don't close it here! ::fdopen moved ownership to file
1506 }
1507
1508 DBG << "dest: " << dest << endl;
1509 DBG << "temp: " << destNew << endl;
1510
1511 // set IFMODSINCE time condition (no download if not modified)
1512 if( PathInfo(target).isExist() && !(options & OPTION_NO_IFMODSINCE) )
1513 {
1515 curl_easy_setopt(_curl, CURLOPT_TIMEVALUE, (long)PathInfo(target).mtime());
1516 }
1517 else
1518 {
1521 }
1522 // change header to include Accept: metalink
1524 // change to our own progress funcion
1526 curl_easy_setopt(_curl, CURLOPT_PRIVATE, (*file) ); // important to pass the FILE* explicitly (passing through varargs)
1527 try
1528 {
1529 MediaCurl::doGetFileCopyFile( srcFile, dest, file, report, options );
1530 }
1531 catch (Exception &ex)
1532 {
1538 }
1543 long httpReturnCode = 0;
1545 if (infoRet == CURLE_OK)
1546 {
1547 DBG << "HTTP response: " + str::numstring(httpReturnCode) << endl;
1548 if ( httpReturnCode == 304
1549 || ( httpReturnCode == 213 && _url.getScheme() == "ftp" ) ) // not modified
1550 {
1551 DBG << "not modified: " << PathInfo(dest) << endl;
1552 return;
1553 }
1554 }
1555 else
1556 {
1557 WAR << "Could not get the response code." << endl;
1558 }
1559
1561
1562 char *ptr = NULL;
1564 {
1565 std::string ct = std::string(ptr);
1566 if (ct.find("application/x-zsync") == 0 )
1568 else if (ct.find("application/metalink+xml") == 0 || ct.find("application/metalink4+xml") == 0)
1570 }
1571
1573 {
1574 // some proxies do not store the content type, so also look at the file to find
1575 // out if we received a metalink (bnc#649925)
1576 fflush(file);
1578 }
1579
1581 {
1582 bool userabort = false;
1583 Pathname failedFile = ZConfig::instance().repoCachePath() / "MultiCurl.failed";
1584 file = nullptr; // explicitly close destNew before the parser reads it.
1585 try
1586 {
1587 MediaBlockList bl;
1588 std::vector<Url> urls;
1590 ZsyncParser parser;
1591 parser.parse( destNew );
1592 bl = parser.getBlockList();
1593 urls = parser.getUrls();
1594
1595 XXX << getFileUrl(srcFile.filename()) << " returned zsync meta data." << std::endl;
1596 } else {
1598 mlp.parse(destNew);
1599 bl = mlp.getBlockList();
1600 urls = mlp.getUrls();
1601
1602 XXX << getFileUrl(srcFile.filename()) << " returned metalink meta data." << std::endl;
1603 }
1604
1605 if ( bl.numBlocks() )
1606 XXX << "With " << bl.numBlocks() << " nr of blocks and a blocksize of " << bl.getBlock(0).size << std::endl;
1607 else
1608 XXX << "With no blocks" << std::endl;
1609
1610 /*
1611 * gihub issue libzipp:#277 Multicurl backend breaks with MirrorCache and Metalink with unknown filesize.
1612 * Fall back to a normal download if we have no knowledge about the filesize we want to download.
1613 */
1614 if ( !bl.haveFilesize() && ! srcFile.downloadSize() ) {
1615 XXX << "No filesize in metalink file and no expected filesize, aborting multicurl." << std::endl;
1616 ZYPP_THROW( MediaException("Multicurl requires filesize but none was provided.") );
1617 }
1618
1619#if 0
1621 /*
1622 * bsc#1191609 In certain locations we do not receive a suitable number of metalink mirrors, and might even
1623 * download chunks serially from one and the same server. In those cases we need to fall back to a normal download.
1624 */
1625 if ( urls.size() < MIN_REQ_MIRRS ) {
1626 ZYPP_THROW( MediaException("Multicurl enabled but not enough mirrors provided") );
1627 }
1628#endif
1629
1630 // XXX << bl << endl;
1631 file = fopen((*destNew).c_str(), "w+e");
1632 if (!file)
1634 if (PathInfo(target).isExist())
1635 {
1636 XXX << "reusing blocks from file " << target << endl;
1637 bl.reuseBlocks(file, target.asString());
1638 XXX << bl << endl;
1639 }
1640 if (bl.haveChecksum(1) && PathInfo(failedFile).isExist())
1641 {
1642 XXX << "reusing blocks from file " << failedFile << endl;
1643 bl.reuseBlocks(file, failedFile.asString());
1644 XXX << bl << endl;
1646 }
1647 const Pathname& df = srcFile.deltafile();
1648 if (!df.empty())
1649 {
1650 XXX << "reusing blocks from file " << df << endl;
1651 bl.reuseBlocks(file, df.asString());
1652 XXX << bl << endl;
1653 }
1654 try
1655 {
1656 multifetch(srcFile.filename(), file, &urls, &report, std::move(bl), srcFile.downloadSize());
1657 }
1658 catch (MediaCurlException &ex)
1659 {
1660 userabort = ex.errstr() == "User abort";
1662 }
1663 }
1666 }
1667 catch (Exception &ex)
1668 {
1669 // something went wrong. fall back to normal download
1670 file = nullptr; // explicitly close destNew before moving it
1671 WAR<< "Failed to multifetch file " << ex << " falling back to single Curl download!" << std::endl;
1672 if (PathInfo(destNew).size() >= 63336)
1673 {
1674 ::unlink(failedFile.asString().c_str());
1676 }
1677 if (userabort)
1678 {
1680 }
1681 file = fopen((*destNew).c_str(), "w+e");
1682 if (!file)
1684
1685 // use the default progressCallback
1688 }
1689 }
1690
1691 if (::fchmod( ::fileno(file), filesystem::applyUmaskTo( 0644 )))
1692 {
1693 ERR << "Failed to chmod file " << destNew << endl;
1694 }
1695
1696 file.resetDispose(); // we're going to close it manually here
1697 if (::fclose(file))
1698 {
1700 ERR << "Fclose failed for file '" << destNew << "'" << endl;
1702 }
1703
1704 if ( rename( destNew, dest ) != 0 )
1705 {
1706 ERR << "Rename failed" << endl;
1708 }
1709 destNew.resetDispose(); // no more need to unlink it
1710
1711 DBG << "done: " << PathInfo(dest) << endl;
1712}
1713
1714void MediaMultiCurl::multifetch(const Pathname & filename, FILE *fp, std::vector<Url> *urllist, MediaBlockList &&blklist, callback::SendReport<DownloadProgressReport> *report, off_t filesize) const
1715{
1716 Url baseurl(getFileUrl(filename));
1717 if (filesize == off_t(-1) && blklist.haveFilesize())
1718 filesize = blklist.getFilesize();
1719 if (!blklist.haveBlocks() && filesize != 0) {
1720 if ( filesize == -1 ) {
1721 ZYPP_THROW(MediaException("No filesize and no blocklist, falling back to normal download."));
1722 }
1723
1724 // build a blocklist on demand just so that we have ranges
1725 MIL << "Generate blocklist, since there was none in the metalink file." << std::endl;
1726
1727 off_t currOff = 0;
1729
1730 while ( currOff < filesize ) {
1731
1732 auto blksize = filesize - currOff ;
1733 if ( blksize > prefSize )
1734 blksize = prefSize;
1735
1736 blklist.addBlock( currOff, blksize );
1737 currOff += blksize;
1738 }
1739
1740 XXX << "Generated blocklist: " << std::endl << blklist << std::endl << " End blocklist " << std::endl;
1741
1742 }
1743 if (filesize == 0 || !blklist.numBlocks()) {
1745 return;
1746 }
1747 if (filesize == 0)
1748 return;
1749
1750 if (!_multi)
1751 {
1753 if (!_multi)
1755 }
1756
1757 multifetchrequest req(this, filename, baseurl, _multi, fp, report, std::move(blklist), filesize);
1758 std::vector<Url> myurllist;
1759 for (std::vector<Url>::iterator urliter = urllist->begin(); urliter != urllist->end(); ++urliter)
1760 {
1761 try
1762 {
1763 std::string scheme = urliter->getScheme();
1764 if (scheme == "http" || scheme == "https" || scheme == "ftp" || scheme == "tftp")
1765 {
1768 }
1769 }
1770 catch (...)
1771 {
1772 }
1773 }
1774 if (!myurllist.size())
1775 myurllist.push_back(baseurl);
1776 req.run(myurllist);
1778}
1779
1781{
1782 if ( !blklist.haveFileChecksum() )
1783 return;
1784 if (fseeko(fp, off_t(0), SEEK_SET))
1785 ZYPP_THROW(MediaCurlException(url, "fseeko", "seek error"));
1786 Digest dig;
1787 blklist.createFileDigest(dig);
1788 char buf[4096];
1789 size_t l = 0;
1790 while ((l = fread(buf, 1, sizeof(buf), fp)) > 0)
1791 dig.update(buf, l);
1792 if (!blklist.verifyFileDigest(dig))
1793 ZYPP_THROW(MediaCurlException(url, "file verification failed", "checksum error"));
1794}
1795
1796bool MediaMultiCurl::isDNSok(const std::string &host) const
1797{
1798 return _dnsok.find(host) == _dnsok.end() ? false : true;
1799}
1800
1801void MediaMultiCurl::setDNSok(const std::string &host) const
1802{
1803 _dnsok.insert(host);
1804}
1805
1806CURL *MediaMultiCurl::fromEasyPool(const std::string &host) const
1807{
1808 if (_easypool.find(host) == _easypool.end())
1809 return 0;
1810 CURL *ret = _easypool[host];
1811 _easypool.erase(host);
1812 return ret;
1813}
1814
1815void MediaMultiCurl::toEasyPool(const std::string &host, CURL *easy) const
1816{
1817 CURL *oldeasy = _easypool[host];
1818 _easypool[host] = easy;
1819 if (oldeasy)
1821}
1822
1823 } // namespace media
1824} // namespace zypp
struct _GPollFD GPollFD
Definition ZYppImpl.h:26
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
Definition AutoDispose.h:95
reference value() const
Reference to the Tp object.
void resetDispose()
Set no dispose function.
Store and operate with byte count.
Definition ByteCount.h:32
static const Unit MB
1000^2 Byte
Definition ByteCount.h:61
static const Unit K
1024 Byte
Definition ByteCount.h:46
std::string asString(unsigned field_width_r=0, unsigned unit_width_r=1) const
Auto selected Unit and precision.
Definition ByteCount.h:134
Compute Message Digests (MD5, SHA1 etc)
Definition Digest.h:38
Base class for Exception.
Definition Exception.h:147
Describes a resource file located on a medium.
Url manipulation class.
Definition Url.h:92
std::string getScheme() const
Returns the scheme name of the URL.
Definition Url.cc:537
std::string getHost(EEncoding eflag=zypp::url::E_DECODED) const
Returns the hostname or IP from the URL authority.
Definition Url.cc:592
static ZConfig & instance()
Singleton ctor.
Definition ZConfig.cc:925
Wrapper class for stat/lstat.
Definition PathInfo.h:222
Pathname extend(const std::string &r) const
Append string r to the last component of the path.
Definition Pathname.h:175
const std::string & asString() const
String representation.
Definition Pathname.h:93
Pathname absolutename() const
Return this path, adding a leading '/' if relative.
Definition Pathname.h:141
static long auth_type_str2long(std::string &auth_type_str)
Converts a string of comma separated list of authetication type names into a long of ORed CURLAUTH_* ...
bool haveChecksum(size_t blkno) const
const MediaBlock & getBlock(size_t blkno) const
return the offset/size of a block with number blkno
UByteArray getChecksum(size_t blkno) const
void reuseBlocks(FILE *wfp, const std::string &filename)
std::string getChecksumType() const
size_t numBlocks() const
return the number of blocks in the blocklist
Implementation class for FTP, HTTP and HTTPS MediaHandler.
Definition MediaCurl.h:32
virtual void setupEasy()
initializes the curl easy handle with the data from the url
Definition MediaCurl.cc:426
@ OPTION_NO_IFMODSINCE
to not add a IFMODSINCE header if target exists
Definition MediaCurl.h:43
@ OPTION_NO_REPORT_START
do not send a start ProgressReport
Definition MediaCurl.h:45
static void resetExpectedFileSize(void *clientp, const ByteCount &expectedFileSize)
MediaMultiCurl needs to reset the expected filesize in case a metalink file is downloaded otherwise t...
static int progressCallback(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
Callback reporting download progress.
Url clearQueryString(const Url &url) const
Definition MediaCurl.cc:382
void doGetFileCopyFile(const OnMediaLocation &srcFile, const Pathname &dest, FILE *file, callback::SendReport< DownloadProgressReport > &report, RequestOptions options=OPTION_NONE) const
void checkProtocol(const Url &url) const
check the url is supported by the curl library
Definition MediaCurl.cc:401
void evaluateCurlCode(const zypp::Pathname &filename, CURLcode code, bool timeout) const
Evaluates a curl return code and throws the right MediaException filename Filename being downloaded c...
Definition MediaCurl.cc:842
void setCurlError(const char *error)
Definition MediaCurl.cc:392
static CURL * progressCallback_getcurl(void *clientp)
static int aliveCallback(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
Callback sending just an alive trigger to the UI, without stats (e.g.
void disconnectFrom() override
Definition MediaCurl.cc:714
curl_slist * _customHeaders
Definition MediaCurl.h:171
Just inherits Exception to separate media exceptions.
Url url() const
Url used.
const Url _url
Url to handle.
void setupEasy() override
initializes the curl easy handle with the data from the url
std::map< std::string, CURL * > _easypool
void checkFileDigest(Url &url, FILE *fp, MediaBlockList &blklist) const
void setDNSok(const std::string &host) const
MediaMultiCurl(const Url &url_r, const Pathname &attach_point_hint_r)
std::set< std::string > _dnsok
bool isDNSok(const std::string &host) const
void multifetch(const Pathname &filename, FILE *fp, std::vector< Url > *urllist, MediaBlockList &&blklist, callback::SendReport< DownloadProgressReport > *report=0, off_t filesize=off_t(-1)) const
static int progressCallback(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
CURL * fromEasyPool(const std::string &host) const
void doGetFileCopy(const OnMediaLocation &srcFile, const Pathname &targetFilename, callback::SendReport< DownloadProgressReport > &_report, RequestOptions options=OPTION_NONE) const override
void toEasyPool(const std::string &host, CURL *easy) const
Url getFileUrl(const Pathname &filename) const
concatenate the attach url and the filename to a complete download url
const std::string & password() const
auth password
const std::string & authType() const
get the allowed authentication types
void setUsername(const std::string &val_r)
sets the auth username
std::string userPassword() const
returns the user and password as a user:pass string
const std::string & proxy() const
proxy host
long maxConcurrentConnections() const
Maximum number of concurrent connections for a single transfer.
void setPassword(const std::string &val_r)
sets the auth password
const std::string & username() const
auth username
void setAuthType(const std::string &val_r)
set the allowed authentication types
void parse(const Pathname &filename)
parse a file consisting of zlink data
MediaBlockList getBlockList()
return the block list from the parsed metalink data
std::vector< Url > getUrls()
return the download urls from the parsed metalink data
multifetchrequest(multifetchrequest &&)=delete
multifetchrequest(const MediaMultiCurl *context, Pathname filename, Url baseurl, CURLM *multi, FILE *fp, callback::SendReport< DownloadProgressReport > *report, MediaBlockList &&blklist, off_t filesize)
multifetchrequest & operator=(multifetchrequest &&)=delete
std::vector< Stripe > _requiredStripes
callback::SendReport< DownloadProgressReport > * _report
void run(std::vector< Url > &urllist)
multifetchrequest(const multifetchrequest &)=delete
std::list< std::unique_ptr< multifetchworker > > _workers
static ByteCount makeBlksize(uint maxConns, size_t filesize)
multifetchrequest & operator=(const multifetchrequest &)=delete
const MediaMultiCurl * _context
bool beginRange(off_t range, std::string &cancelReason) override
multifetchworker(int no, multifetchrequest &request, const Url &url)
size_t writefunction(char *ptr, std::optional< off_t > offset, size_t bytes) override
size_t headerfunction(char *ptr, size_t bytes) override
MultiByteHandler::Range rangeFromBlock(off_t blockNo) const
multifetchworker(multifetchworker &&)=delete
std::vector< MultiByteHandler::Range > _blocks
void adddnsfd(std::vector< GPollFD > &waitFds)
MultiByteHandler::ProtocolMode _protocolMode
std::unique_ptr< MultiByteHandler > _multiByteHandler
bool recheckChecksum(off_t blockIdx)
void dnsevent(const std::vector< GPollFD > &waitFds)
std::vector< off_t > _rangeToStripeBlock
bool finishedRange(off_t range, bool validated, std::string &cancelReason) override
multifetchworker & operator=(const multifetchworker &)=delete
MultiFetchWorkerState _state
multifetchworker & operator=(multifetchworker &&)=delete
multifetchworker(const multifetchworker &)=delete
The CurlMultiPartHandler class.
zypp::callback::SendReport< zypp::KeyRingReport > _report
Definition keyringwf.cc:457
zypp::Url propagateQueryParams(zypp::Url url_r, const zypp::Url &template_r)
Definition Arch.h:364
String related utilities and Regular expression matching.
mode_t applyUmaskTo(mode_t mode_r)
Modify mode_r according to the current umask ( mode_r & ~getUmask() ).
Definition PathInfo.h:798
int hardlinkCopy(const Pathname &oldpath, const Pathname &newpath)
Create newpath as hardlink or copy of oldpath.
Definition PathInfo.cc:888
int unlink(const Pathname &path)
Like 'unlink'.
Definition PathInfo.cc:705
static bool env_isset(const std::string &name)
constexpr auto MAXURLS
static double currentTime()
constexpr auto MIN_REQ_MIRRS
MetaDataType looks_like_meta_file(const Pathname &file)
constexpr auto MIN_STRIPE_SIZE_KB
std::string numstring(char n, int w=0)
Definition String.h:289
int zypp_poll(std::vector< GPollFD > &fds, int timeout)
Small wrapper around g_poll that additionally listens to the shutdown FD returned by ZYpp::shutdownSi...
Definition ZYppImpl.cc:313
Easy-to use interface to the ZYPP dependency resolver.
AutoDispose< const Pathname > ManagedFile
A Pathname plus associated cleanup code to be executed when path is no longer needed.
Definition ManagedFile.h:27
AutoDispose<int> calling ::close
AutoDispose<FILE*> calling ::fclose
a single block from the blocklist, consisting of an offset and a size
std::vector< off_t > blocks
std::vector< RState > blockStates
#define ZYPP_RETHROW(EXCPT)
Drops a logline and rethrows, updating the CodeLocation.
Definition Exception.h:441
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition Exception.h:429
#define DBG
Definition Logger.h:97
#define MIL
Definition Logger.h:98
#define ERR
Definition Logger.h:100
#define WAR
Definition Logger.h:99
#define XXX
Definition Logger.h:96