libzypp  17.34.1
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 
40 using std::endl;
41 using 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 
46 namespace zypp {
47  namespace media {
48 
50 
51 
114 class multifetchrequest;
115 
116 struct 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 
138 };
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 
145 public:
146  multifetchworker(int no, multifetchrequest &request, const Url &url);
147  multifetchworker(const multifetchworker &) = delete;
148  multifetchworker(multifetchworker &&) = delete;
149  multifetchworker &operator=(const multifetchworker &) = delete;
150  multifetchworker &operator=(multifetchworker &&) = delete;
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 
198  MultiByteHandler::ProtocolMode _protocolMode = MultiByteHandler::ProtocolMode::Basic;
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 
213 private:
214  void run();
215  void stealjob();
216  bool setupHandle();
217  MultiByteHandler::Range rangeFromBlock( off_t blockNo ) const;
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 
224  multifetchrequest *_request = nullptr;
225  int _pass = 0;
226  std::string _urlbuf;
227 
228  pid_t _pid = 0;
229  int _dnspipe = -1;
230 };
231 
233 public:
234  multifetchrequest(const MediaMultiCurl *context, Pathname filename,
235  Url baseurl, CURLM *multi, FILE *fp,
237  MediaBlockList &&blklist, off_t filesize);
238  multifetchrequest(const multifetchrequest &) = delete;
240  multifetchrequest &operator=(const multifetchrequest &) = delete;
241  multifetchrequest &operator=(multifetchrequest &&) = delete;
243 
244  void run(std::vector<Url> &urllist);
245  static ByteCount makeBlksize( uint maxConns, size_t filesize );
246 
248  return _blklist;
249  }
250 
251 protected:
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;
281  off_t _fetchedgoodsize = 0;
282 
283  double _starttime = 0;
284  double _lastprogress = 0;
285 
286  double _lastperiodstart = 0;
287  double _lastperiodfetched = 0;
288  double _periodavg = 0;
289 
290 public:
291  double _timeout = 0;
292  double _connect_timeout = 0;
293  double _maxspeed = 0;
294  int _maxworkers = 0;
295 };
296 
297 constexpr auto MIN_REQ_MIRRS = 4;
298 constexpr 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;
312 constexpr auto MIN_STRIPE_SIZE_KB = 4096;
313 
315 
316 static double
318 {
319 #if _POSIX_C_SOURCE >= 199309L
320  struct timespec ts;
321  if ( clock_gettime( CLOCK_MONOTONIC, &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 
332 size_t
333 multifetchworker::writefunction(char *ptr, std::optional<off_t> offset, size_t bytes)
334 {
335  if ( _state == WORKER_BROKEN || _state == WORKER_DISCARD )
336  return bytes ? 0 : 1;
337 
338  double now = currentTime();
339 
340  // update stats of overall data
341  _datareceived += bytes;
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 
349  auto &stripeDesc = _request->_requiredStripes[_stripe];
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 
372 bool multifetchworker::beginRange ( off_t workerRangeOff, std::string &cancelReason )
373 {
374  auto &stripeDesc = _request->_requiredStripes[_stripe];
375  auto stripeRangeOff = _rangeToStripeBlock[workerRangeOff];
376  const auto &currRangeState = stripeDesc.blockStates[stripeRangeOff];
377 
378  if ( currRangeState == Stripe::FINALIZED ){
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  }
384  stripeDesc.blockStates[stripeRangeOff] = currRangeState == Stripe::PENDING ? Stripe::FETCH : Stripe::COMPETING;
385  return true;
386 }
387 
388 bool multifetchworker::finishedRange ( off_t workerRangeOff, bool validated, std::string &cancelReason )
389 {
390  auto &stripeDesc = _request->_requiredStripes[_stripe];
391  auto stripeRangeOff = _rangeToStripeBlock[workerRangeOff];
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
402  stripeDesc.blockStates[stripeRangeOff] = Stripe::FINALIZED;
403  _request->_fetchedgoodsize += _blocks[workerRangeOff].len;
404  } else {
405  // others wrote here, we need to check the full checksum
406  if ( recheckChecksum ( workerRangeOff ) ) {
407  stripeDesc.blockStates[stripeRangeOff] = Stripe::FINALIZED;
408  _request->_fetchedgoodsize += _blocks[workerRangeOff].len;
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;
413  stripeDesc.blockStates[stripeRangeOff] = Stripe::REFETCH;
414  }
415  }
416  return true;
417 }
418 
419 size_t
420 multifetchworker::headerfunction( char *p, size_t bytes )
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 
442 multifetchworker::multifetchworker(int no, multifetchrequest &request, const Url &url)
443 : MediaCurl(url, Pathname())
444 , _workerno( no )
445 , _maxspeed( request._maxspeed )
446 , _request ( &request )
447 {
448  Url curlUrl( clearQueryString(url) );
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) {
472  curl_easy_cleanup(_curl);
473  _curl = 0;
475  setCurlError("curl_easy_setopt failed");
476  return false;
477  }
478  curl_easy_setopt(_curl, CURLOPT_PRIVATE, this);
479  curl_easy_setopt(_curl, CURLOPT_URL, _urlbuf.c_str());
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() ) {
489  curl_easy_setopt(_curl, CURLOPT_USERPWD, _settings.userPassword().c_str());
490  std::string use_auth = _settings.authType();
491  if (use_auth.empty())
492  use_auth = "digest,basic"; // our default
493  long auth = CurlAuthData::auth_type_str2long(use_auth);
494  if( auth != CURLAUTH_NONE)
495  {
496  XXX << "#" << _workerno << ": Enabling HTTP authentication methods: " << use_auth
497  << " (CURLOPT_HTTPAUTH=" << auth << ")" << std::endl;
498  curl_easy_setopt(_curl, CURLOPT_HTTPAUTH, auth);
499  }
500  }
501  }
502  return true;
503 }
504 
506 {
507  if (_curl)
508  {
510  curl_multi_remove_handle(_request->_multi, _curl);
511  if (_state == WORKER_DONE || _state == WORKER_SLEEP)
512  {
513 #if CURLVERSION_AT_LEAST(7,15,5)
514  curl_easy_setopt(_curl, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t)0);
515 #endif
516  curl_easy_setopt(_curl, CURLOPT_PRIVATE, (void *)0);
517  curl_easy_setopt(_curl, CURLOPT_WRITEFUNCTION, (void *)0);
518  curl_easy_setopt(_curl, CURLOPT_WRITEDATA, (void *)0);
519  curl_easy_setopt(_curl, CURLOPT_HEADERFUNCTION, (void *)0);
520  curl_easy_setopt(_curl, CURLOPT_HEADERDATA, (void *)0);
522  }
523  else
524  curl_easy_cleanup(_curl);
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
543  disconnectFrom();
544 }
545 
546 static 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 
552 void
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";
576  if (env_isset(schemeproxy))
577  return;
578  if (schemeproxy != "http_proxy")
579  {
580  std::transform(schemeproxy.begin(), schemeproxy.end(), schemeproxy.begin(), ::toupper);
581  if (env_isset(schemeproxy))
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;
617  unsigned int connecttimeout = _request->_connect_timeout;
618  if (connecttimeout)
619  alarm(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 
630 void
631 multifetchworker::adddnsfd(std::vector<GPollFD> &waitFds)
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 
644 void
645 multifetchworker::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 
685 bool multifetchworker::recheckChecksum( off_t workerRangeIdx )
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 
714  if (fseeko(_request->_fp, currOf, SEEK_SET))
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 {
731  UByteArray sum;
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 ) ) {
738  sum = _request->_blklist.getChecksum( blkNo );
739  relDigLen = sum.size( );
740  blkSumPad = _request->_blklist.checksumPad( );
741  digest = zypp::Digest();
742  digest->create( _request->_blklist.getChecksumType() );
743  }
744 
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)
824  _avgspeed = _datareceived / (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 
858 void
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 
896  auto &stripeDesc = _request->_requiredStripes[_stripe];
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
941  curl_easy_reset( _curl );
942  if ( !setupHandle())
943  return false;
944  }
945 
946  run();
947  return true;
948 }
949 
950 void
952 {
953  if (_state == WORKER_BROKEN || _state == WORKER_DONE)
954  return; // just in case...
955 
956  if ( !_multiByteHandler->prepare() ) {
959  setCurlError(_multiByteHandler->lastErrorMessage ().c_str());
960  return;
961  }
962 
963  if (curl_multi_add_handle(_request->_multi, _curl) != CURLM_OK) {
966  setCurlError("curl_multi_add_handle failed");
967  return;
968  }
969 
970  _request->_havenewjob = true;
972 }
973 
974 
976 
977 
978 multifetchrequest::multifetchrequest(const MediaMultiCurl *context, Pathname filename, Url baseurl, CURLM *multi, FILE *fp, callback::SendReport<DownloadProgressReport> *report, MediaBlockList &&blklist, off_t filesize)
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  {
993  _lastperiodstart = _lastprogress = _starttime;
994  if (_maxworkers > MAXURLS)
995  _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++)
1001  _totalsize += _blklist.getBlock(blkno).size;
1002 
1003  // equally distribute the data we want to download over all workers
1004  _defaultBlksize = makeBlksize( _maxworkers, _totalsize );
1005 
1006  // lets build stripe informations
1007  zypp::ByteCount currStripeSize = 0;
1008  for (size_t blkno = 0; blkno < _blklist.numBlocks(); blkno++) {
1009 
1010  const MediaBlock &blk = _blklist.getBlock(blkno);
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 
1025 {
1026  _workers.clear();
1027 }
1028 
1029 void
1030 multifetchrequest::run(std::vector<Url> &urllist)
1031 {
1032  int workerno = 0;
1033  std::vector<Url>::iterator urliter = urllist.begin();
1034 
1035  internal::CurlPollHelper _curlHelper(*this);
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  {
1061  _activeworkers++;
1062  if (worker->_state != WORKER_LOOKUP)
1063  {
1064  worker->nextjob();
1065  }
1066  else
1067  _lookupworkers++;
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 
1117  int r = zypp_detail::zypp_poll( waitFds, timeoutMs );
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)
1128  _lookupworkers--;
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 
1187  if (curl_easy_getinfo(easy, CURLINFO_PRIVATE, &worker) != CURLE_OK)
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;
1198  curl_multi_remove_handle(_multi, easy);
1199 
1200  const auto &setWorkerBroken = [&]( const std::string &str = {} ){
1201  worker->_state = WORKER_BROKEN;
1202  if ( !str.empty () )
1203  worker->setCurlError(str.c_str());
1204  _activeworkers--;
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!
1237  setWorkerBroken();
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;
1247  setWorkerBroken();
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  {
1279  multifetchworker *oworker = workeriter->get();
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)
1313  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  {
1335  int percent = _totalsize ? (100 * (_fetchedgoodsize + _fetchedsize)) / (_totalsize + _fetchedsize) : 0;
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 
1360 inline zypp::ByteCount multifetchrequest::makeBlksize ( uint maxConns, size_t filesize )
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 
1370 MediaMultiCurl::MediaMultiCurl(const Url &url_r, const Pathname & attach_point_hint_r)
1371  : MediaCurl(url_r, attach_point_hint_r)
1372 {
1373  MIL << "MediaMultiCurl::MediaMultiCurl(" << url_r << ", " << attach_point_hint_r << ")" << endl;
1374  _multi = 0;
1376 }
1377 
1379 {
1381  {
1382  curl_slist_free_all(_customHeadersMetalink);
1384  }
1385  if (_multi)
1386  {
1387  curl_multi_cleanup(_multi);
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  {
1396  curl_easy_cleanup(easy);
1397  it->second = NULL;
1398  }
1399  }
1400 }
1401 
1403 {
1405 
1407  {
1408  curl_slist_free_all(_customHeadersMetalink);
1410  }
1411  struct curl_slist *sl = _customHeaders;
1412  for (; sl; sl = sl->next)
1413  _customHeadersMetalink = curl_slist_append(_customHeadersMetalink, sl->data);
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.
1421 int MediaMultiCurl::progressCallback( void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
1422 {
1423  CURL *_curl = MediaCurl::progressCallback_getcurl(clientp);
1424  if (!_curl)
1425  return MediaCurl::aliveCallback(clientp, dltotal, dlnow, ultotal, ulnow);
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;
1430  if ( curl_easy_getinfo( _curl, CURLINFO_PRIVATE, &fp ) != CURLE_OK || !fp )
1431  return MediaCurl::aliveCallback( clientp, dltotal, dlnow, ultotal, ulnow );
1432  if ( ftell( fp ) == 0 )
1433  return MediaCurl::aliveCallback( clientp, dltotal, 0.0, ultotal, ulnow );
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;
1438  if (curl_easy_getinfo(_curl, CURLINFO_RESPONSE_CODE, &httpReturnCode ) != CURLE_OK || httpReturnCode == 0)
1439  return MediaCurl::aliveCallback(clientp, dltotal, dlnow, ultotal, ulnow);
1440 
1441  char *ptr = NULL;
1442  bool ismetalink = false;
1443  if (curl_easy_getinfo(_curl, CURLINFO_CONTENT_TYPE, &ptr) == CURLE_OK && ptr)
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, ...
1452  return MediaCurl::aliveCallback(clientp, dltotal, dlnow, ultotal, ulnow);
1453  }
1454  if (!ismetalink)
1455  {
1456  fflush(fp);
1457  ismetalink = looks_like_meta_file(fp) != MetaDataType::None;
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
1465  curl_easy_setopt(_curl, CURLOPT_XFERINFOFUNCTION, &MediaCurl::aliveCallback);
1466  return MediaCurl::aliveCallback(clientp, dltotal, dlnow, ultotal, ulnow);
1467  }
1468  curl_easy_setopt(_curl, CURLOPT_XFERINFOFUNCTION, &MediaCurl::progressCallback);
1469  return MediaCurl::progressCallback(clientp, dltotal, dlnow, ultotal, ulnow);
1470 }
1471 
1472 void MediaMultiCurl::doGetFileCopy( const OnMediaLocation &srcFile , const Pathname & target, callback::SendReport<DownloadProgressReport> & report, RequestOptions options ) const
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;
1495  ZYPP_THROW(MediaWriteException(destNew));
1496  }
1497  destNew = ManagedFile( (*buf), filesystem::unlink );
1498 
1499  file = ::fdopen( tmp_fd, "we" );
1500  if ( ! file )
1501  {
1502  ERR << "fopen failed for file '" << destNew << "'" << endl;
1503  ZYPP_THROW(MediaWriteException(destNew));
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  {
1514  curl_easy_setopt(_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
1515  curl_easy_setopt(_curl, CURLOPT_TIMEVALUE, (long)PathInfo(target).mtime());
1516  }
1517  else
1518  {
1519  curl_easy_setopt(_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1520  curl_easy_setopt(_curl, CURLOPT_TIMEVALUE, 0L);
1521  }
1522  // change header to include Accept: metalink
1523  curl_easy_setopt(_curl, CURLOPT_HTTPHEADER, _customHeadersMetalink);
1524  // change to our own progress funcion
1525  curl_easy_setopt(_curl, CURLOPT_XFERINFOFUNCTION, &progressCallback);
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  {
1533  curl_easy_setopt(_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1534  curl_easy_setopt(_curl, CURLOPT_TIMEVALUE, 0L);
1535  curl_easy_setopt(_curl, CURLOPT_HTTPHEADER, _customHeaders);
1536  curl_easy_setopt(_curl, CURLOPT_PRIVATE, (void *)0);
1537  ZYPP_RETHROW(ex);
1538  }
1539  curl_easy_setopt(_curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
1540  curl_easy_setopt(_curl, CURLOPT_TIMEVALUE, 0L);
1541  curl_easy_setopt(_curl, CURLOPT_HTTPHEADER, _customHeaders);
1542  curl_easy_setopt(_curl, CURLOPT_PRIVATE, (void *)0);
1543  long httpReturnCode = 0;
1544  CURLcode infoRet = curl_easy_getinfo(_curl, CURLINFO_RESPONSE_CODE, &httpReturnCode);
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 
1560  MetaDataType ismetalink = MetaDataType::None;
1561 
1562  char *ptr = NULL;
1563  if (curl_easy_getinfo(_curl, CURLINFO_CONTENT_TYPE, &ptr) == CURLE_OK && ptr)
1564  {
1565  std::string ct = std::string(ptr);
1566  if (ct.find("application/x-zsync") == 0 )
1567  ismetalink = MetaDataType::Zsync;
1568  else if (ct.find("application/metalink+xml") == 0 || ct.find("application/metalink4+xml") == 0)
1569  ismetalink = MetaDataType::MetaLink;
1570  }
1571 
1572  if ( ismetalink == MetaDataType::None )
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);
1577  ismetalink = looks_like_meta_file(destNew);
1578  }
1579 
1580  if ( ismetalink != MetaDataType::None )
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;
1589  if ( ismetalink == MetaDataType::Zsync ) {
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 {
1597  MetaLinkParser mlp;
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
1620  Disabling this workaround for now, since we now do zip ranges into bigger requests
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)
1633  ZYPP_THROW(MediaWriteException(destNew));
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;
1645  filesystem::unlink(failedFile);
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";
1661  ZYPP_RETHROW(ex);
1662  }
1663  }
1664  catch (MediaFileSizeExceededException &ex) {
1665  ZYPP_RETHROW(ex);
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());
1675  filesystem::hardlinkCopy(destNew, failedFile);
1676  }
1677  if (userabort)
1678  {
1679  ZYPP_RETHROW(ex);
1680  }
1681  file = fopen((*destNew).c_str(), "w+e");
1682  if (!file)
1683  ZYPP_THROW(MediaWriteException(destNew));
1684 
1685  // use the default progressCallback
1686  curl_easy_setopt(_curl, CURLOPT_XFERINFOFUNCTION, &MediaCurl::progressCallback);
1687  MediaCurl::doGetFileCopyFile(srcFile, dest, file, report, options | OPTION_NO_REPORT_START);
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  {
1699  filesystem::unlink(destNew);
1700  ERR << "Fclose failed for file '" << destNew << "'" << endl;
1701  ZYPP_THROW(MediaWriteException(destNew));
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 
1714 void 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;
1728  const auto prefSize = multifetchrequest::makeBlksize( _settings.maxConcurrentConnections(), filesize );
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()) {
1744  checkFileDigest(baseurl, fp, blklist);
1745  return;
1746  }
1747  if (filesize == 0)
1748  return;
1749 
1750  if (!_multi)
1751  {
1752  _multi = curl_multi_init();
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  {
1766  checkProtocol(*urliter);
1767  myurllist.push_back(internal::propagateQueryParams(*urliter, _url));
1768  }
1769  }
1770  catch (...)
1771  {
1772  }
1773  }
1774  if (!myurllist.size())
1775  myurllist.push_back(baseurl);
1776  req.run(myurllist);
1777  checkFileDigest(baseurl, fp, req.blockList() );
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 
1796 bool MediaMultiCurl::isDNSok(const std::string &host) const
1797 {
1798  return _dnsok.find(host) == _dnsok.end() ? false : true;
1799 }
1800 
1801 void MediaMultiCurl::setDNSok(const std::string &host) const
1802 {
1803  _dnsok.insert(host);
1804 }
1805 
1806 CURL *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 
1815 void MediaMultiCurl::toEasyPool(const std::string &host, CURL *easy) const
1816 {
1817  CURL *oldeasy = _easypool[host];
1818  _easypool[host] = easy;
1819  if (oldeasy)
1820  curl_easy_cleanup(oldeasy);
1821 }
1822 
1823  } // namespace media
1824 } // namespace zypp
std::string getScheme() const
Returns the scheme name of the URL.
Definition: Url.cc:537
Url getFileUrl(const Pathname &filename) const
concatenate the attach url and the filename to a complete download url
MediaBlockList getBlockList() const
return the block list from the parsed metalink data
#define MIL
Definition: Logger.h:98
constexpr auto MAXURLS
void doGetFileCopy(const OnMediaLocation &srcFile, const Pathname &targetFilename, callback::SendReport< DownloadProgressReport > &_report, RequestOptions options=OPTION_NONE) const override
bool recheckChecksum(off_t blockIdx)
std::vector< Url > getUrls()
return the download urls from the parsed metalink data
Definition: zsyncparser.cc:187
int assert_dir(const Pathname &path, unsigned mode)
Like &#39;mkdir -p&#39;.
Definition: PathInfo.cc:324
The CurlMultiPartHandler class.
std::set< std::string > _dnsok
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:429
Describes a resource file located on a medium.
void dnsevent(const std::vector< GPollFD > &waitFds)
static ZConfig & instance()
Singleton ctor.
Definition: ZConfig.cc:925
void checkProtocol(const Url &url) const
check the url is supported by the curl library
Definition: MediaCurl.cc:401
Implementation class for FTP, HTTP and HTTPS MediaHandler.
Definition: MediaCurl.h:31
zypp::Url propagateQueryParams(zypp::Url url_r, const zypp::Url &template_r)
Definition: curlhelper.cc:400
void setPassword(const std::string &val_r)
sets the auth password
Compute Message Digests (MD5, SHA1 etc)
Definition: Digest.h:37
Store and operate with byte count.
Definition: ByteCount.h:31
static Range make(size_t start, size_t len=0, std::optional< zypp::Digest > &&digest={}, CheckSumBytes &&expectedChkSum=CheckSumBytes(), std::any &&userData=std::any(), std::optional< size_t > digestCompareLen={}, std::optional< size_t > _dataBlockPadding={})
to not add a IFMODSINCE header if target exists
Definition: MediaCurl.h:43
void run(std::vector< Url > &urllist)
const std::string & authType() const
get the allowed authentication types
Pathname extend(const std::string &r) const
Append string r to the last component of the path.
Definition: Pathname.h:175
std::map< std::string, CURL * > _easypool
callback::SendReport< DownloadProgressReport > * _report
String related utilities and Regular expression matching.
void setUsername(const std::string &val_r)
sets the auth username
static double currentTime()
#define XXX
Definition: Logger.h:96
static const Unit MB
1000^2 Byte
Definition: ByteCount.h:61
virtual void setupEasy()
initializes the curl easy handle with the data from the url
Definition: MediaCurl.cc:426
std::vector< MultiByteHandler::Range > _blocks
void parse(const Pathname &filename)
parse a file consisting of zlink data
Definition: zsyncparser.cc:77
void toEasyPool(const std::string &host, CURL *easy) const
AutoDispose<int> calling ::close
Definition: AutoDispose.h:309
const std::string & password() const
auth password
void parse(const Pathname &filename)
parse a file consisting of metalink xml data
CURLMcode handleSocketActions(const std::vector< GPollFD > &actionsFds, int first=0)
Definition: curlhelper.cc:469
int hardlinkCopy(const Pathname &oldpath, const Pathname &newpath)
Create newpath as hardlink or copy of oldpath.
Definition: PathInfo.cc:888
#define ERR
Definition: Logger.h:100
MediaBlockList getBlockList()
return the block list from the parsed metalink data
Definition: zsyncparser.cc:197
const std::string & username() const
auth username
static ByteCount makeBlksize(uint maxConns, size_t filesize)
AutoDispose< const Pathname > ManagedFile
A Pathname plus associated cleanup code to be executed when path is no longer needed.
Definition: ManagedFile.h:27
struct _GPollFD GPollFD
Definition: ZYppImpl.h:26
Container< Ret > transform(Container< Msg, CArgs... > &&val, Transformation &&transformation)
Definition: transform.h:31
static void resetExpectedFileSize(void *clientp, const ByteCount &expectedFileSize)
MediaMultiCurl needs to reset the expected filesize in case a metalink file is downloaded otherwise t...
Definition: MediaCurl.cc:1408
#define ZYPP_RETHROW(EXCPT)
Drops a logline and rethrows, updating the CodeLocation.
Definition: Exception.h:441
const MediaMultiCurl * _context
void setupEasy() override
initializes the curl easy handle with the data from the url
std::string asString() const
Returns a default string representation of the Url object.
Definition: Url.cc:501
bool verifyFileDigest(Digest &digest) const
Url clearQueryString(const Url &url) const
Definition: MediaCurl.cc:382
void setAuthType(const std::string &val_r)
set the allowed authentication types
CURL * fromEasyPool(const std::string &host) const
multifetchrequest * _request
const Url _url
Url to handle.
Definition: MediaHandler.h:113
bool isDNSok(const std::string &host) const
std::vector< GPollFD > socks
Definition: curlhelper_p.h:103
const std::string & asString() const
String representation.
Definition: Pathname.h:93
bool isExist() const
Return whether valid stat info exists.
Definition: PathInfo.h:282
std::vector< Stripe > _requiredStripes
Just inherits Exception to separate media exceptions.
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
static int progressCallback(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
Pathname repoCachePath() const
Path where the caches are kept (/var/cache/zypp)
Definition: ZConfig.cc:1042
std::string getChecksumType() const
const ByteCount & downloadSize() const
The size of the resource on the server.
Pathname dirname() const
Return all but the last component od this path.
Definition: Pathname.h:126
do not send a start ProgressReport
Definition: MediaCurl.h:45
#define WAR
Definition: Logger.h:99
MetaDataType looks_like_meta_file(const Pathname &file)
a single block from the blocklist, consisting of an offset and a size
bool createFileDigest(Digest &digest) const
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.
Definition: MediaCurl.cc:1365
void setDNSok(const std::string &host) const
size_t numBlocks() const
return the number of blocks in the blocklist
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
const Pathname & filename() const
The path to the resource on the medium.
std::string numstring(char n, int w=0)
Definition: String.h:289
int unlink(const Pathname &path)
Like &#39;unlink&#39;.
Definition: PathInfo.cc:705
std::string asString(unsigned field_width_r=0, unsigned unit_width_r=1) const
Auto selected Unit and precision.
Definition: ByteCount.h:134
void resetDispose()
Set no dispose function.
Definition: AutoDispose.h:171
void doGetFileCopyFile(const OnMediaLocation &srcFile, const Pathname &dest, FILE *file, callback::SendReport< DownloadProgressReport > &report, RequestOptions options=OPTION_NONE) const
Definition: MediaCurl.cc:1202
curl_slist * _customHeaders
Definition: MediaCurl.h:171
SizeType blocks(ByteCount blocksize_r=K) const
Return number of blocks of size blocksize_r (default 1K).
Definition: ByteCount.h:115
MultiByteHandler::Range rangeFromBlock(off_t blockNo) const
const Pathname & deltafile() const
The existing deltafile that can be used to reduce download size ( zchunk or metalink ) ...
static bool env_isset(const std::string &name)
MediaMultiCurl(const Url &url_r, const Pathname &attach_point_hint_r)
constexpr std::string_view FILE("file")
Pathname absolutename() const
Return this path, adding a leading &#39;/&#39; if relative.
Definition: Pathname.h:141
UByteArray getChecksum(size_t blkno) const
Base class for Exception.
Definition: Exception.h:146
Url url() const
Url used.
Definition: MediaHandler.h:503
std::unique_ptr< MultiByteHandler > _multiByteHandler
multifetchrequest(const MediaMultiCurl *context, Pathname filename, Url baseurl, CURLM *multi, FILE *fp, callback::SendReport< DownloadProgressReport > *report, MediaBlockList &&blklist, off_t filesize)
bool any_of(const Container &c, Fnc &&cb)
Definition: Algorithm.h:76
const MediaBlock & getBlock(size_t blkno) const
return the offset/size of a block with number blkno
long maxConcurrentConnections() const
Maximum number of concurrent connections for a single transfer.
std::string getHost(EEncoding eflag=zypp::url::E_DECODED) const
Returns the hostname or IP from the URL authority.
Definition: Url.cc:592
curl_slist * _customHeadersMetalink
void disconnectFrom() override
Definition: MediaCurl.cc:714
static CURL * progressCallback_getcurl(void *clientp)
Definition: MediaCurl.cc:1381
MultiFetchWorkerState _state
void setCurlError(const char *error)
Definition: MediaCurl.cc:392
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_* ...
Definition: curlauthdata.cc:50
bool haveChecksum(size_t blkno) const
void reuseBlocks(FILE *wfp, const std::string &filename)
std::list< std::unique_ptr< multifetchworker > > _workers
static const Unit K
1024 Byte
Definition: ByteCount.h:46
Wrapper class for ::stat/::lstat.
Definition: PathInfo.h:221
constexpr auto MIN_REQ_MIRRS
std::vector< off_t > _rangeToStripeBlock
AutoDispose<FILE*> calling ::fclose
Definition: AutoDispose.h:320
AutoDispose< void * > _state
mode_t applyUmaskTo(mode_t mode_r)
Modify mode_r according to the current umask ( mode_r & ~getUmask() ).
Definition: PathInfo.h:799
std::vector< Url > getUrls() const
return the download urls from the parsed metalink data
std::optional< long > timeout_ms
Definition: curlhelper_p.h:104
MultiByteHandler::ProtocolMode _protocolMode
std::string userPassword() const
returns the user and password as a user:pass string
Digest clone() const
Returns a clone of the current Digest and returns it.
Definition: Digest.cc:230
int rename(const Pathname &oldpath, const Pathname &newpath)
Like &#39;rename&#39;.
Definition: PathInfo.cc:747
zypp::callback::SendReport< zypp::KeyRingReport > _report
Definition: keyringwf.cc:457
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.
Definition: Application.cc:19
const std::string & proxy() const
proxy host
bool update(const char *bytes, size_t len)
feed data into digest computation algorithm
Definition: Digest.cc:311
constexpr auto MIN_STRIPE_SIZE_KB
std::vector< off_t > blocks
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.
Definition: MediaCurl.cc:1351
void adddnsfd(std::vector< GPollFD > &waitFds)
Url manipulation class.
Definition: Url.h:91
#define DBG
Definition: Logger.h:97
ByteCount df(const Pathname &path_r)
Report free disk space on a mounted file system.
Definition: PathInfo.cc:1163
void checkFileDigest(Url &url, FILE *fp, MediaBlockList &blklist) const
std::vector< RState > blockStates