18#include <zypp-core/ng/pipelines/Algorithm>
21#include <zypp-media/ng/ProvideSpec>
26 : _zyppContext( std::move(zyppContext) )
27 , _destDir( std::move(destDir) )
66 struct ProvideFromCacheOrMediumLogic {
69 using ContextType = Context;
71 using MediaHandle =
typename ProvideType::MediaHandle;
72 using ProvideRes =
typename ProvideType::Res;
75 ProvideFromCacheOrMediumLogic( CacheProviderContextRef cacheContext, MediaHandle &&medium, zypp::Pathname &&file, ProvideFileSpec &&filespec )
76 : _ctx( std::move(cacheContext) )
77 , _medium( std::move(medium) )
78 , _file(std::move( file ))
79 , _filespec( std::move(filespec) ) {}
81 MaybeAwaitable<expected<zypp::ManagedFile>> execute() {
83 return findFileInCache( )
84 | [
this]( expected<zypp::ManagedFile> cached ) -> MaybeAwaitable<expected<zypp::ManagedFile>> {
86 MIL <<
"Didn't find " << _file <<
" in the caches, providing from medium" << std::endl;
89 std::shared_ptr<ProvideType> provider = _ctx->zyppContext()->provider();
90 return provider->provide( _medium, _file, _filespec )
91 |
and_then( [
this]( ProvideRes res ) {
92 return verifyFile( res.
file() )
94 return expected<ProvideRes>::success( std::move(res) );
97 |
and_then( ProvideType::copyResultToDest( _ctx->zyppContext()->provider(), _ctx->destDir() / _file ) )
100 return make_expected_success (std::move(file));
105 return verifyFile ( cached.get() )
106 |
and_then([
this, cachedFile = cached.get() ]()
mutable {
107 if ( cachedFile == _ctx->destDir() / _file ) {
108 cachedFile.resetDispose();
109 return makeReadyTask( expected<zypp::ManagedFile>::success(std::move(cachedFile) ));
112 const auto &targetPath = _ctx->destDir() / _file;
115 return _ctx->zyppContext()->provider()->copyFile( cachedFile, _ctx->destDir() / _file )
126 MaybeAwaitable<expected<zypp::ManagedFile>> findFileInCache( ) {
129 if ( _filespec.checksum().empty() )
132 const auto &confDirs = _ctx->cacheDirs();
133 const auto targetFile = _ctx->destDir() /
_file ;
134 std::vector<zypp::Pathname> caches;
135 caches.push_back( _ctx->destDir() );
136 caches.insert( caches.end(), confDirs.begin(), confDirs.end() );
141 if ( !cacheFileInfo.isExist () ) {
144 auto provider = _ctx->zyppContext()->provider();
148 return provider->checksumForFile( cacheFilePath, _filespec.checksum().type() )
154 if ( cacheFilePath == targetFile )
157 if ( sum == _filespec.checksum () ) {
171 MaybeAwaitable<expected<void>> verifyFile (
const zypp::Pathname &dlFilePath ) {
175 if ( !_filespec.checksum().empty () ) {
183 CacheProviderContextRef _ctx;
194 ProvideFromCacheOrMediumLogic impl(std::move(cacheContext), std::move(medium), std::move(file), std::move(filespec));
195 zypp_co_return zypp_co_await( impl.execute() );
Base class for Exception.
Wrapper class for stat/lstat.
const zypp::Pathname & destDir() const
const ContextRef & zyppContext() const
void addCacheDir(const zypp::Pathname &p)
std::vector< zypp::Pathname > _cacheDirs
const std::vector< zypp::Pathname > & cacheDirs() const
const zypp::Pathname file() const
static expected success(ConsParams &&...params)
static expected error(ConsParams &&...params)
String related utilities and Regular expression matching.
int unlink(const Pathname &path)
Like 'unlink'.
int assert_dir(const Pathname &path, unsigned mode)
Like 'mkdir -p'.
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.
MaybeAwaitable< expected< void > > verifyChecksum(ContextRef zyppCtx, zypp::CheckSum checksum, zypp::filesystem::Pathname file)
MaybeAwaitable< expected< zypp::ManagedFile > > provideToCacheDir(CacheProviderContextRef cacheContext, ProvideMediaHandle medium, zypp::Pathname file, ProvideFileSpec filespec)
auto and_then(Fun &&function)
ResultType and_then(const expected< T, E > &exp, Function &&f)
auto firstOf(Transformation &&transformFunc, DefaultType &&def, Predicate &&predicate=detail::ContinueUntilValidPredicate())
#define ZYPP_PRIVATE_CONSTR_ARG