17#include <zypp/base/Logger.h>
18#include <zypp/base/String.h>
19#include <zypp/base/Gettext.h>
21#include <zypp-core/base/Regex.h>
22#include <zypp-core/fs/TmpPath.h>
23#include <zypp-core/zyppng/base/EventDispatcher>
24#include <zypp-core/zyppng/base/EventLoop>
25#include <zypp-core/zyppng/base/private/threaddata_p.h>
27#include <zypp-curl/ng/network/Downloader>
28#include <zypp-curl/ng/network/NetworkRequestDispatcher>
29#include <zypp-curl/ng/network/DownloadSpec>
31#include <zypp-media/MediaConfig>
33#include <zypp-media/auth/CredentialManager>
48 using clock = std::chrono::steady_clock;
64 clock::time_point now = clock::now();
73 if ( dlnow && dlnow !=
_dnlNow ) {
102 MIL <<
"Releasing internal::SharedData for MediaNetwork." << std::endl;
106 static std::shared_ptr<SharedData> data = std::shared_ptr<SharedData>(
new SharedData() );
123 std::chrono::steady_clock::time_point
_creationTime = std::chrono::steady_clock::now();
130 auto age = std::chrono::steady_clock::now() - i->second._creationTime;
131 if ( age > std::chrono::minutes( 30 ) ) {
132 MIL <<
"Found cached media file, but it's older than 30 mins, requesting a new one" << std::endl;
146 MIL <<
"Initializing internal::SharedData for MediaNetwork" << std::endl;
147 _dispatcher = zyppng::ThreadData::current().ensureDispatcher();
148 _downloader = std::make_shared<zyppng::Downloader>();
163 const Pathname & attach_point_hint_r )
168 MIL <<
"MediaNetwork::MediaNetwork(" << url_r <<
", " << attach_point_hint_r <<
")" << endl;
177 char *atemp = ::strdup( apath.
asString().c_str());
180 atemp == NULL || (atest=::mkdtemp(atemp)) == NULL)
182 WAR <<
"attach point " << ainfo.
path()
183 <<
" is not useable for " << url_r.
getScheme() << endl;
186 else if( atest != NULL)
203 if ( !
_shared->_downloader->requestDispatcher()->supportsProtocol(
_url ) ) {
204 std::string msg(
"Unsupported protocol '");
239 unsigned internalTry = 0;
240 static constexpr unsigned maxInternalTry = 3;
244 auto ev = zyppng::EventLoop::create();
245 std::vector<zyppng::connection> signalConnections;
247 while( signalConnections.size() ) {
248 signalConnections.back().disconnect();
249 signalConnections.pop_back();
253 zyppng::DownloadRef dl =
_shared->_downloader->downloadFile( spec );
254 std::optional<internal::ProgressTracker> progTracker;
257 if ( !report)
return;
262 if ( !report || !progTracker )
264 progTracker->updateStats( 0.0, dlNow );
265 if ( !(*report)->progress( progTracker->_dnlPercent, spec.
url(), progTracker-> _drateTotal, progTracker->_drateLast ) )
269 const auto &progressSlot = [&](
zyppng::Download &req, off_t dlTotal, off_t dlNow ) {
270 if ( !report || !progTracker )
273 progTracker->updateStats( dlTotal, dlNow );
274 if ( !(*report)->progress( progTracker->_dnlPercent, spec.
url(), progTracker-> _drateTotal, progTracker->_drateLast ) )
282 bool firstTry =
true;
291 if ( cmcred && auth.lastDatabaseUpdate() < cmcred->lastDatabaseUpdate() ) {
293 DBG <<
"got stored credentials:" << endl << *credentials << endl;
306 curlcred->setUsername(cmcred->username());
315 curlcred->setAuthType( availAuth );
318 if (auth_report->prompt(
_url, prompt_msg, *curlcred))
320 DBG <<
"callback answer: retry" << endl
321 <<
"CurlAuthData: " << *curlcred << endl;
323 if (curlcred->valid())
325 credentials = curlcred;
339 DBG <<
"callback answer: cancel" << endl;
343 if ( !credentials ) {
350 credentials->setUrl(
_url);
356 signalConnections.insert( signalConnections.end(), {
357 dl->connectFunc( &zyppng::Download::sigStarted, startedSlot),
358 dl->connectFunc( &zyppng::Download::sigFinished, finishedSlot ),
359 dl->connectFunc( &zyppng::Download::sigAuthRequired, authRequiredSlot )
364 signalConnections.insert( signalConnections.end(), {
365 dl->connectFunc( &zyppng::Download::sigAlive, aliveSlot ),
366 dl->connectFunc( &zyppng::Download::sigProgress, progressSlot ),
373 std::for_each( signalConnections.begin(), signalConnections.end(), [](
auto &conn ) { conn.disconnect(); });
375 if ( dl->hasError() ) {
377 std::exception_ptr excp;
378 const auto &error = dl->lastRequestError();
379 switch ( error.type() ) {
427 DBG <<
"BUG: Download error flag is set , but Error code is NoError" << std::endl;
432 if ( internalTry < maxInternalTry ) {
445 if ( excp && !retry ) {
446 if ( report ) (*report)->finish( spec.
url(), errCode, error.toString() );
447 std::rethrow_exception( excp );
463 const auto &filename = file.
filename();
466 const bool requestedMediaFile =
_shared->mediaRegex().matches( filename.asString() );
467 auto &mediaFileCache =
_shared->_mediaCacheEntries;
470 DBG <<
"FILEURL IS: " << fileurl << std::endl;
471 DBG <<
"Downloading to: " << targetFilename << std::endl;
473 if( assert_dir( targetFilename.
dirname() ) ) {
474 DBG <<
"assert_dir " << targetFilename.
dirname() <<
" failed" << endl;
478 if ( requestedMediaFile ) {
479 MIL <<
"Requested " << filename <<
" trying media cache first" << std::endl;
481 auto i =
_shared->findInCache( mediaCacheKey );
482 if ( i != mediaFileCache.end() ) {
483 MIL <<
"Found cached media file, returning a copy to the file" << std::endl;
487 mediaFileCache.erase(i);
488 MIL <<
"Failed to copy the requested file, proceeding with download" << std::endl;
491 MIL <<
"Nothing in the file cache, requesting the file from the server." << std::endl;
505 if ( requestedMediaFile ) {
506 MIL <<
"Media file was not found, remembering in the cache" << std::endl;
509 std::rethrow_exception( std::current_exception() );
513 if ( requestedMediaFile ) {
518 MIL <<
"Saved requested media file in media cache for future use" << std::endl;
520 MIL <<
"Failed to save requested media file in cache, requesting again next time." << std::endl;
527 MIL <<
"Checking if file " << filename <<
" does exist" << std::endl;
529 const bool requestMediaFile =
_shared->mediaRegex().matches( filename.
asString() );
530 auto &mediaFileCache =
_shared->_mediaCacheEntries;
533 if ( requestMediaFile ) {
534 MIL <<
"Request for " << filename <<
" is a media file, trying the cache first" << std::endl;
535 auto i =
_shared->findInCache( mediaCacheKey );
536 if ( i != mediaFileCache.end() ) {
537 MIL <<
"Found a cache entry for requested media file, returning right away" << std::endl;
538 if ( i->second._file->empty() ) {
571 if ( !result && requestMediaFile ) {
572 MIL << filename <<
" does not exist on medium, remembering in the cache" << std::endl;
584 for ( filesystem::DirContent::const_iterator it = content.begin(); it != content.end(); ++it ) {
585 Pathname filename = dirname + it->name;
588 switch ( it->type ) {
595 getDir( filename, recurse_r );
597 res = assert_dir(
localPath( filename ) );
599 WAR <<
"Ignore error (" << res <<
") on creating local directory '" <<
localPath( filename ) <<
"'" << endl;
611 const Pathname & dirname,
bool dots )
const
617 const Pathname & dirname,
bool dots )
const
std::string asString() const
static CheckSum md5FromString(const std::string &input_r)
std::string getScheme() const
Returns the scheme name of the URL.
std::string asCompleteString() const
Returns a complete string representation of the Url object.
std::string asString() const
Returns a default string representation of the Url object.
std::string getUsername(EEncoding eflag=zypp::url::E_DECODED) const
Returns the username from the URL authority.
std::string getPathName(EEncoding eflag=zypp::url::E_DECODED) const
Returns the path name from the URL.
bool isValid() const
Verifies the Url.
static ZConfig & instance()
Singleton ctor.
Wrapper class for stat/lstat.
const Pathname & path() const
Return current Pathname.
Pathname dirname() const
Return all but the last component od this path.
const std::string & asString() const
String representation.
Pathname absolutename() const
Return this path, adding a leading '/' if relative.
Provide a new empty temporary directory and recursively delete it when no longer needed.
static const Pathname & defaultLocation()
DownloadSpec & setDeltaFile(const zypp::Pathname &file)
DownloadSpec & setCheckExistsOnly(bool set=true)
DownloadSpec & setHeaderChecksum(const zypp::CheckSum &sum)
DownloadSpec & setHeaderSize(const zypp::ByteCount &bc)
const zypp::Pathname & targetPath() const
DownloadSpec & setTransferSettings(TransferSettings &&set)
zypp::ByteCount expectedFileSize() const
constexpr std::string_view MEDIACACHE_REGEX("^\\/media\\.[1-9][0-9]*\\/media$")
int unlink(const Pathname &path)
Like 'unlink'.
int hardlinkCopy(const Pathname &oldpath, const Pathname &newpath)
Create newpath as hardlink or copy of oldpath.
std::list< DirEntry > DirContent
Returned by readdir.
Easy-to use interface to the ZYPP dependency resolver.
zypp::media::CurlAuthData NetworkAuthData
double _drateLast
Download rate in last period.
double _drateTotal
Download rate so far.
void updateStats(double dltotal=0.0, double dlnow=0.0)
double _dnlTotal
Bytes to download or 0 if unknown.
double _dnlLast
Bytes downloaded at period start.
std::chrono::steady_clock clock
double _dnlNow
Bytes downloaded now.
int _dnlPercent
Percent completed or 0 if _dnlTotal is unknown.
std::optional< clock::time_point > _timeStart
Start total stats.
std::optional< clock::time_point > _timeLast
Start last period(~1sec)
std::chrono::steady_clock::time_point _creationTime
MediaFileCacheEntry(zypp::ManagedFile &&file)
zypp::filesystem::TmpDir _mediaCacheDir
static std::shared_ptr< SharedData > instance()
static const zypp::str::regex & mediaRegex()
std::unordered_map< std::string, MediaFileCacheEntry > _mediaCacheEntries
zyppng::DownloaderRef _downloader
zyppng::EventDispatcherRef _dispatcher
auto findInCache(const std::string &mediaCacheKey)
#define ZYPP_RETHROW(EXCPT)
Drops a logline and rethrows, updating the CodeLocation.
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
#define ZYPP_EXCPT_PTR(EXCPT)
Drops a logline and returns Exception as a std::exception_ptr.
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.