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();
107 MIL <<
"Releasing internal::SharedData for MediaNetwork." << std::endl;
111 static std::shared_ptr<SharedData> data = std::shared_ptr<SharedData>(
new SharedData() );
128 std::chrono::steady_clock::time_point
_creationTime = std::chrono::steady_clock::now();
135 auto age = std::chrono::steady_clock::now() -
i->second._creationTime;
136 if (
age > std::chrono::minutes( 30 ) ) {
137 MIL <<
"Found cached media file, but it's older than 30 mins, requesting a new one" << std::endl;
151 MIL <<
"Initializing internal::SharedData for MediaNetwork" << std::endl;
152 _dispatcher = zyppng::ThreadData::current().ensureDispatcher();
153 _downloader = std::make_shared<zyppng::Downloader>();
187 WAR <<
"attach point " <<
ainfo.path()
188 <<
" is not useable for " <<
url_r.getScheme() << endl;
208 if ( !
_shared->_downloader->requestDispatcher()->supportsProtocol(
_url ) ) {
209 std::string msg(
"Unsupported protocol '");
249 auto ev = zyppng::EventLoop::create();
253 signalConnections.back().disconnect();
254 signalConnections.pop_back();
258 zyppng::DownloadRef
dl =
_shared->_downloader->downloadFile( spec );
259 std::optional<internal::ProgressTracker>
progTracker;
262 if ( !report)
return;
325 DBG <<
"callback answer: retry" << endl
326 <<
"CurlAuthData: " << *
curlcred << endl;
344 DBG <<
"callback answer: cancel" << endl;
362 dl->connectFunc( &zyppng::Download::sigStarted, startedSlot),
363 dl->connectFunc( &zyppng::Download::sigFinished, finishedSlot ),
364 dl->connectFunc( &zyppng::Download::sigAuthRequired, authRequiredSlot )
370 dl->connectFunc( &zyppng::Download::sigAlive, aliveSlot ),
371 dl->connectFunc( &zyppng::Download::sigProgress, progressSlot ),
380 if (
dl->hasError() ) {
382 std::exception_ptr
excp;
383 const auto &error =
dl->lastRequestError();
384 switch ( error.type() ) {
432 DBG <<
"BUG: Download error flag is set , but Error code is NoError" << std::endl;
451 if ( report ) (*report)->finish( spec.
url(),
errCode, error.toString() );
452 std::rethrow_exception(
excp );
468 const auto &filename = file.
filename();
475 DBG <<
"FILEURL IS: " <<
fileurl << std::endl;
484 MIL <<
"Requested " << filename <<
" trying media cache first" << std::endl;
488 MIL <<
"Found cached media file, returning a copy to the file" << std::endl;
493 MIL <<
"Failed to copy the requested file, proceeding with download" << std::endl;
496 MIL <<
"Nothing in the file cache, requesting the file from the server." << std::endl;
511 MIL <<
"Media file was not found, remembering in the cache" << std::endl;
514 std::rethrow_exception( std::current_exception() );
523 MIL <<
"Saved requested media file in media cache for future use" << std::endl;
525 MIL <<
"Failed to save requested media file in cache, requesting again next time." << std::endl;
532 MIL <<
"Checking if file " << filename <<
" does exist" << std::endl;
539 MIL <<
"Request for " << filename <<
" is a media file, trying the cache first" << std::endl;
542 MIL <<
"Found a cache entry for requested media file, returning right away" << std::endl;
543 if (
i->second._file->empty() ) {
577 MIL << filename <<
" does not exist on medium, remembering in the cache" << std::endl;
589 for ( filesystem::DirContent::const_iterator
it = content.begin();
it != content.end(); ++
it ) {
593 switch (
it->type ) {
604 WAR <<
"Ignore error (" <<
res <<
") on creating local directory '" <<
localPath( filename ) <<
"'" << endl;
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
void reset()
Reset to default Ctor values.
static CheckSum md5FromString(const std::string &input_r)
std::string getScheme() const
Returns the scheme name of the URL.
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 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'.
std::list< DirEntry > DirContent
Returned by readdir.
int hardlinkCopy(const Pathname &oldpath, const Pathname &newpath)
Create newpath as hardlink or copy of oldpath.
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)
SharedData(const SharedData &)=delete
zypp::filesystem::TmpDir _mediaCacheDir
static std::shared_ptr< SharedData > instance()
SharedData & operator=(SharedData &&)=delete
SharedData(SharedData &&)=delete
static const zypp::str::regex & mediaRegex()
std::unordered_map< std::string, MediaFileCacheEntry > _mediaCacheEntries
zyppng::DownloaderRef _downloader
zyppng::EventDispatcherRef _dispatcher
SharedData & operator=(const SharedData &)=delete
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.