21#include <zypp-core/base/DefaultIntegral>
23#include <zypp-media/MediaException>
27#include <zypp-core/base/UserRequestException>
31#undef ZYPP_BASE_LOGGER_LOGGROUP
32#define ZYPP_BASE_LOGGER_LOGGROUP "zypp::fetcher"
68 if ( lhs->location.medianr() == rhs->location.medianr() )
69 return lhs->location.filename() < rhs->location.filename();
71 return lhs->location.medianr() < rhs->location.medianr();
118 return str << obj->location;
141 Fetcher::Options
options()
const;
162 static shared_ptr<Impl> _nullimpl(
new Impl );
204 void validate(
const Pathname & localfile_r,
const std::list<FileChecker> & checkers_r );
210 const Pathname &dest_dir, FetcherJob::Flags flags );
228 {
return new Impl( *
this ); }
231 std::set<FetcherIndex_Ptr,SameFetcherIndex>
_indexes;
273 job->checkers.push_back(checker);
278 _resources.push_back(job);
288 job->checkers.push_back(checker);
294 _resources.push_back(job);
300 if ( _mediaSetAccess )
301 _mediaSetAccess->precacheFiles( {resource} );
306 job->checkers.push_back(checker);
307 _resources.push_back(job);
312 MIL <<
"adding index " << resource << endl;
327 _mediaSetAccess = &media;
337 DBG <<
"Adding fetcher cache: '" << cache_dir <<
"'." << endl;
338 _caches.insert(cache_dir);
343 ERR <<
"Not adding cache: '" << cache_dir <<
"'. Not a directory." << endl;
348 ERR <<
"Not adding cache '" << cache_dir <<
"'. Path does not exists." << endl;
363 if ( !suffix_in_cache.empty() )
364 cacheLocation = cacheLocation.
extend( suffix_in_cache.c_str() );
366 if (
PathInfo(cacheLocation).isExist() && is_checksum( cacheLocation, resource_r.
checksum() ) )
368 swap( ret, cacheLocation );
372 MIL <<
"start fetcher with " << _caches.size() <<
" cache directories." << endl;
373 for(
const Pathname & cacheDir : _caches )
375 cacheLocation = cacheDir / resource_r.
filename();
376 std::string detail = cacheDir.
asString();
377 if ( !suffix_in_cache.empty() )
378 detail +=
"with suffix " + suffix_in_cache;
380 if (
PathInfo(cacheLocation).isExist() )
382 MIL <<
"file " << resource_r.
filename() <<
" found in cache " << detail << endl;
383 if (!is_checksum( cacheLocation, resource_r.
checksum() ) )
384 MIL <<
"file " << resource_r.
filename() <<
" failed checksum verification in cache " << detail << endl;
386 swap( ret, cacheLocation );
399 MIL <<
"Checking job [" << localfile_r <<
"] (" << checkers_r.size() <<
" checkers )" << endl;
404 chkfnc( localfile_r );
406 ERR <<
"Invalid checker for '" << localfile_r <<
"'" << endl;
429 auto fnc_addIfInContent( [&](
const std::string & index_r ) ->
bool
438 downloadAndReadIndexList( media, dest_dir );
444 fnc_addIfInContent(
"CHECKSUMS" ) || fnc_addIfInContent(
"SHA1SUMS" );
448 fnc_addIfInContent(
"content" );
457 != _dircontent.end() )
461 std::copy(filled.begin(), filled.end(), std::back_inserter(content));
470 std::copy(tofill.begin(), tofill.end(), std::back_inserter(content));
477 const Pathname &dest_dir, FetcherJob::Flags flags )
481 MIL <<
"Adding directory " << resource.
filename() << endl;
484 getDirectoryContent(media, resource, content);
489 WAR <<
"Skipping subtree hidden at " << resource.
filename() << endl;
495 autoaddIndexes(content, media, resource, dest_dir);
497 for ( filesystem::DirContent::const_iterator it = content.begin();
513 if ( _checksums.find(filename.
asString()) != _checksums.end() )
516 chksm = _checksums[filename.
asString()];
520 WAR <<
"Resource " << filename <<
" has no checksum in the index either." << endl;
530 addDirJobs(media, filename, dest_dir, flags);
545 scoped_ptr<MediaSetAccess::ReleaseFileGuard> releaseFileGuard;
548 Pathname tmpFile = locateInCache( resource, destDir_r );
549 if ( tmpFile.
empty() ) {
550 const std::string suffix(
".unverified");
551 Pathname tmpFileUnverified = locateInCache( resource, destDir_r, suffix );
552 if (!tmpFileUnverified.
empty()) {
554 validate( tmpFileUnverified, jobp_r->checkers );
555 if (tmpFileUnverified.
extension() == suffix ) {
556 std::string_view dest = tmpFileUnverified.
asString().c_str();
557 dest.remove_suffix( suffix.length() );
559 rename( tmpFileUnverified.
asString().c_str(), std::string(dest).c_str() );
560 tmpFile = locateInCache( resource, destDir_r );
568 if ( !tmpFile.
empty() ) {
569 MIL <<
"Found in cache, verifying..." << endl;
571 MIL <<
"Not found in cache, retrieving..." << endl;
580 if ( tmpFile == destFullPath )
584 validate( tmpFile, jobp_r->checkers );
587 if ( tmpFile == destFullPath )
591 if ( assert_dir( destFullPath->
dirname() ) != 0 )
603 WAR <<
"optional resource " << resource <<
" could not be transferred." << endl;
619 setRepoIndexConsumer( bind( &ContentReaderHelper::consumeIndex,
this, _1 ) );
632 readChecksumsIndex(index, basedir);
633 else if ( index.
basename() ==
"content" )
634 readContentFileIndex(index, basedir);
636 WAR << index <<
": index file format not known" << endl;
644 MIL << index <<
" contains " << reader.
_repoindex->mediaFileChecksums.size() <<
" checksums." << endl;
648 _checksums[(basedir + it->first).
asString()] = it->second;
655 std::ifstream in( index.
c_str() );
659 while ( getline( in, buffer ) )
662 if ( buffer[0] ==
'#' )
666 if ( checksum.empty() )
669 if ( buffer.empty() )
671 WAR <<
"Missing filename in CHECKSUMS file: " << index.
asString() <<
" (" << checksum <<
")" << endl;
675 _checksums[(basedir/buffer).
asString()] = checksum;
684 MIL <<
"downloading index " << resource << endl;
710 fetcher.
start( dest_dir, media );
719 fetcher.
start( dest_dir, media );
726 WAR <<
"No public key specified by user for index '" << keyloc.
filename() <<
"'"<< endl;
730 fetcher.
start( dest_dir, media );
741 if ( _indexes.empty() )
743 MIL <<
"No indexes to read." << endl;
747 for_( it_idx, _indexes.begin(), _indexes.end() )
749 if ( (*it_idx)->read )
751 DBG <<
"Already read index " <<
PathInfo(dest_dir + (*it_idx)->location.filename()) << endl;
756 downloadIndex( media, (*it_idx)->location, dest_dir );
758 readIndex( dest_dir + (*it_idx)->location.filename(), (*it_idx)->location.filename().
dirname() );
760 MIL <<
"Remember read index " <<
PathInfo(dest_dir + (*it_idx)->location.filename()) << endl;
761 (*it_idx)->read =
true;
764 MIL <<
"done reading indexes" << endl;
770 if ( !_mediaSetAccess )
772 start( dest_dir, *_mediaSetAccess, progress );
781 _mediaSetAccess =
nullptr;
784 progress.
sendTo(progress_receiver);
786 downloadAndReadIndexList(media, dest_dir);
793 addDirJobs(media, location, dest_dir, jobp->flags);
807 getDirectoryContent(media, jobp->location.filename().dirname(), content);
810 MIL <<
"Autodiscovering signed indexes on '"
811 << jobp->location.filename().dirname() <<
"' for '"
812 << jobp->location.filename() <<
"'" << endl;
814 autoaddIndexes(content, media, jobp->location.filename().dirname(), dest_dir);
818 getDirectoryContent(media,
Pathname(
"/"), content);
821 MIL <<
"Autodiscovering signed indexes on '"
823 << jobp->location.filename() <<
"'" << endl;
825 autoaddIndexes(content, media,
Pathname(
"/"), dest_dir);
830 if ( jobp->location.checksum().empty() )
832 if ( _checksums.find(jobp->location.filename().asString())
833 != _checksums.end() )
835 CheckSum chksm = _checksums[jobp->location.filename().asString()];
837 jobp->checkers.push_back(digest_check);
847 jobp->checkers.push_back(digest_check);
855 jobp->checkers.push_back(digest_check);
860 provideToDest( media, dest_dir, jobp );
862 if ( ! progress.
incr() )
870 for ( std::list<FetcherJob_Ptr>::const_iterator it_res = obj.
_resources.begin(); it_res != obj.
_resources.end(); ++it_res )
void resetDispose()
Set no dispose function.
void setDispose(const Dispose &dispose_r)
Set a new dispose function.
Integral type with defined initial value when default constructed.
Base class for Exception.
void remember(const Exception &old_r)
Store an other Exception as history.
void enqueueDigested(const OnMediaLocation &resource, const FileChecker &checker=FileChecker())
void autoaddIndexes(const filesystem::DirContent &content, MediaSetAccess &media, const OnMediaLocation &resource, const Pathname &dest_dir)
auto discovery and reading of indexes
void downloadIndex(MediaSetAccess &media, const OnMediaLocation &resource, const Pathname &dest_dir)
download the indexes and reads them
Impl & operator=(const Impl &)=delete
void readContentFileIndex(const Pathname &index, const Pathname &basedir)
specific version of readIndex for content file
void readIndex(const Pathname &index, const Pathname &basedir)
reads a downloaded index file and updates internal attributes table
std::set< FetcherIndex_Ptr, SameFetcherIndex > _indexes
friend std::ostream & operator<<(std::ostream &str, const Fetcher::Impl &obj)
std::map< std::string, CheckSum > _checksums
void downloadAndReadIndexList(MediaSetAccess &media, const Pathname &dest_dir)
download the indexes and reads them
void addDirJobs(MediaSetAccess &media, const OnMediaLocation &resource, const Pathname &dest_dir, FetcherJob::Flags flags)
scan the directory and adds the individual jobs
Impl * clone() const
clone for RWCOW_pointer
void addCachePath(const Pathname &cache_dir)
std::list< FetcherJob_Ptr > _resources
friend Impl * rwcowClone(const Impl *rhs)
void readChecksumsIndex(const Pathname &index, const Pathname &basedir)
specific version of readIndex for CHECKSUMS file
void enqueueDir(const OnMediaLocation &resource, bool recursive, const FileChecker &checker=FileChecker())
Impl & operator=(Impl &&)=delete
void addIndex(const OnMediaLocation &resource)
std::map< std::string, filesystem::DirContent > _dircontent
void provideToDest(MediaSetAccess &media_r, const Pathname &destDir_r, const FetcherJob_Ptr &jobp_r)
Provide the resource to dest_dir.
void enqueue(const OnMediaLocation &resource, const FileChecker &checker=FileChecker())
MediaSetAccess * _mediaSetAccess
std::set< Pathname > _caches
Impl(const Impl &)=default
void enqueueDigestedDir(const OnMediaLocation &resource, bool recursive, const FileChecker &checker=FileChecker())
void validate(const Pathname &localfile_r, const std::list< FileChecker > &checkers_r)
Validates the provided file against its checkers.
Fetcher::Options options() const
void getDirectoryContent(MediaSetAccess &media, const OnMediaLocation &resource, filesystem::DirContent &content)
reads the content of a directory but keeps a cache
static shared_ptr< Impl > nullimpl()
Offer default Impl.
Pathname locateInCache(const OnMediaLocation &resource_r, const Pathname &destDir_r, const std::string &suffix_in_cache="")
Tries to locate the file represented by job by looking at the cache (matching checksum is mandatory).
void start(const Pathname &dest_dir, const ProgressData::ReceiverFnc &progress)
void setOptions(Fetcher::Options options)
Fetcher::Options _options
void setMediaSetAccess(MediaSetAccess &media)
This class allows to retrieve a group of files in a confortable way, providing some smartness that do...
void start(const Pathname &dest_dir, const ProgressData::ReceiverFnc &progress=ProgressData::ReceiverFnc())
start the transfer to a destination directory dest_dir The media has to be provides with setMediaSetA...
RWCOW_pointer< Impl > _pimpl
Pointer to implementation.
void addIndex(const OnMediaLocation &resource)
Adds an index containing metadata (for example checksums ) that will be retrieved and read before the...
Options options() const
Get current options.
void reset()
Reset the transfer (jobs) list.
void enqueue(const OnMediaLocation &resource, const FileChecker &checker=FileChecker())
Enqueue a object for transferal, they will not be transferred until start() is called.
void enqueueDir(const OnMediaLocation &resource, bool recursive=false, const FileChecker &checker=FileChecker())
Enqueue a directory.
void setOptions(Options options)
Set the Fetcher options.
@ AutoAddChecksumsIndexes
If a CHECKSUMS file is found, it is downloaded and read.
@ AutoAddContentFileIndexes
If a content file is found, it is downloaded and read.
void enqueueDigestedDir(const OnMediaLocation &resource, bool recursive=false, const FileChecker &checker=FileChecker())
Enqueue a directory and always check for checksums.
void enqueueDigested(const OnMediaLocation &resource, const FileChecker &checker=FileChecker())
Enqueue a object for transferal, they will not be transferred until start() is called.
void addCachePath(const Pathname &cache_dir)
adds a directory to the list of directories where to look for cached files
void setMediaSetAccess(MediaSetAccess &media)
Sets the media set access that will be used to precache and to download the files when start is calle...
Maintain [min,max] and counter (value) for progress counting.
void sendTo(const ReceiverFnc &fnc_r)
Set ReceiverFnc.
function< bool(const ProgressData &)> ReceiverFnc
Most simple version of progress reporting The percentage in most cases.
bool incr(value_type val_r=1)
Increment counter value (default by 1).
Class representing one GPG Public Key (PublicKeyData + ASCII armored in a tempfile).
Checks for the validity of a signature.
ZYpp::Ptr getZYpp()
Convenience to get the Pointer to the ZYpp instance.
Wrapper class for stat/lstat.
bool isExist() const
Return whether valid stat info exists.
Pathname extend(const std::string &r) const
Append string r to the last component of the path.
Pathname dirname() const
Return all but the last component od this path.
const char * c_str() const
String representation.
const std::string & asString() const
String representation.
std::string basename() const
Return the last component of this path.
bool empty() const
Test for an empty path.
std::string extension() const
Return all of the characters in name after and including the last dot in the last element of name.
Parse repoindex part from a content file.
virtual void parse(const InputStream &imput_r, const ProgressData::ReceiverFnc &fnc_r=ProgressData::ReceiverFnc())
Parse the stream.
String related utilities and Regular expression matching.
std::list< DirEntry > DirContent
Returned by readdir.
int hardlinkCopy(const Pathname &oldpath, const Pathname &newpath)
Create newpath as hardlink or copy of oldpath.
int unlink(const Pathname &path)
Like 'unlink'.
std::string stripFirstWord(std::string &line, const bool ltrim_first)
bool hasPrefix(const C_Str &str_r, const C_Str &prefix_r)
Return whether str_r has prefix prefix_r.
Easy-to use interface to the ZYPP dependency resolver.
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
shared_ptr< FetcherIndex > FetcherIndex_Ptr
std::string asString(const Patch::Category &obj)
function< void(const Pathname &file)> FileChecker
Functor signature used to check files.
shared_ptr< FetcherJob > FetcherJob_Ptr
zypp::RepoManager::RefreshServiceOptions _options
parser::susetags::RepoIndex_Ptr _repoindex
void consumeIndex(const parser::susetags::RepoIndex_Ptr &data_r)
class that represents indexes which add metadata to fetcher jobs and therefore need to be retrieved i...
OnMediaLocation location
Index localtion.
FetcherIndex(const OnMediaLocation &loc)
DefaultIntegral< bool, false > read
Whether we read this index.
Class to encapsulate the OnMediaLocation object and the FileChecker together.
std::list< FileChecker > checkers
FetcherJob(const FetcherJob &)=default
FetcherJob(FetcherJob &&)=default
FetcherJob & operator=(const FetcherJob &)=default
ZYPP_DECLARE_FLAGS(Flags, Flag)
FetcherJob & operator=(FetcherJob &&)=default
FetcherJob(const OnMediaLocation &loc)
std::set ordering (less semantic)
bool operator()(const FetcherIndex_Ptr &lhs, const FetcherIndex_Ptr &rhs) const
Listentry returned by readdir.
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
#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_THROW(EXCPT)
Drops a logline and throws the Exception.
#define ZYPP_DECLARE_OPERATORS_FOR_FLAGS(Name)