17#include <zypp/base/Easy.h>
18#include <zypp/base/LogControl.h>
19#include <zypp/base/LogTools.h>
20#include <zypp/base/PtrTypes.h>
21#include <zypp-core/base/DefaultIntegral>
22#include <zypp/base/String.h>
23#include <zypp-media/MediaException>
26#include <zypp/CheckSum.h>
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();
117 return str << obj->location;
135 Fetcher::Options
options()
const;
156 static shared_ptr<Impl> _nullimpl(
new Impl );
198 void validate(
const Pathname & localfile_r,
const std::list<FileChecker> & checkers_r );
204 const Pathname &dest_dir, FetcherJob::Flags flags );
219 friend Impl * rwcowClone<Impl>(
const Impl * rhs );
222 {
return new Impl( *
this ); }
225 std::set<FetcherIndex_Ptr,SameFetcherIndex>
_indexes;
267 job->checkers.push_back(checker);
272 _resources.push_back(job);
282 job->checkers.push_back(checker);
288 _resources.push_back(job);
294 if ( _mediaSetAccess )
295 _mediaSetAccess->precacheFiles( {resource} );
300 job->checkers.push_back(checker);
301 _resources.push_back(job);
306 MIL <<
"adding index " << resource << endl;
321 _mediaSetAccess = &media;
331 DBG <<
"Adding fetcher cache: '" << cache_dir <<
"'." << endl;
332 _caches.insert(cache_dir);
337 ERR <<
"Not adding cache: '" << cache_dir <<
"'. Not a directory." << endl;
342 ERR <<
"Not adding cache '" << cache_dir <<
"'. Path does not exists." << endl;
356 if (
PathInfo(cacheLocation).isExist() && is_checksum( cacheLocation, resource_r.
checksum() ) )
358 swap( ret, cacheLocation );
362 MIL <<
"start fetcher with " << _caches.size() <<
" cache directories." << endl;
363 for(
const Pathname & cacheDir : _caches )
365 cacheLocation = cacheDir / resource_r.
filename();
366 if (
PathInfo(cacheLocation).isExist() && is_checksum( cacheLocation, resource_r.
checksum() ) )
368 MIL <<
"file " << resource_r.
filename() <<
" found in cache " << cacheDir << endl;
369 swap( ret, cacheLocation );
381 MIL <<
"Checking job [" << localfile_r <<
"] (" << checkers_r.size() <<
" checkers )" << endl;
386 chkfnc( localfile_r );
388 ERR <<
"Invalid checker for '" << localfile_r <<
"'" << endl;
411 auto fnc_addIfInContent( [&](
const std::string & index_r ) ->
bool
420 downloadAndReadIndexList( media, dest_dir );
426 fnc_addIfInContent(
"CHECKSUMS" ) || fnc_addIfInContent(
"SHA1SUMS" );
430 fnc_addIfInContent(
"content" );
439 != _dircontent.end() )
443 std::copy(filled.begin(), filled.end(), std::back_inserter(content));
452 std::copy(tofill.begin(), tofill.end(), std::back_inserter(content));
459 const Pathname &dest_dir, FetcherJob::Flags flags )
463 MIL <<
"Adding directory " << resource.
filename() << endl;
466 getDirectoryContent(media, resource, content);
471 WAR <<
"Skipping subtree hidden at " << resource.
filename() << endl;
477 autoaddIndexes(content, media, resource, dest_dir);
479 for ( filesystem::DirContent::const_iterator it = content.begin();
495 if ( _checksums.find(filename.
asString()) != _checksums.end() )
498 chksm = _checksums[filename.
asString()];
502 WAR <<
"Resource " << filename <<
" has no checksum in the index either." << endl;
512 addDirJobs(media, filename, dest_dir, flags);
527 scoped_ptr<MediaSetAccess::ReleaseFileGuard> releaseFileGuard;
530 Pathname tmpFile = locateInCache( resource, destDir_r );
531 if ( tmpFile.
empty() )
533 MIL <<
"Not found in cache, retrieving..." << endl;
542 if ( tmpFile == destFullPath )
546 validate( tmpFile, jobp_r->checkers );
549 if ( tmpFile == destFullPath )
553 if ( assert_dir( destFullPath->
dirname() ) != 0 )
565 WAR <<
"optional resource " << resource <<
" could not be transferred." << endl;
581 setRepoIndexConsumer( bind( &ContentReaderHelper::consumeIndex,
this, _1 ) );
585 { _repoindex = data_r; }
594 readChecksumsIndex(index, basedir);
595 else if ( index.
basename() ==
"content" )
596 readContentFileIndex(index, basedir);
598 WAR << index <<
": index file format not known" << endl;
606 MIL << index <<
" contains " << reader.
_repoindex->mediaFileChecksums.size() <<
" checksums." << endl;
610 _checksums[(basedir + it->first).asString()] = it->second;
617 std::ifstream in( index.
c_str() );
621 while ( getline( in, buffer ) )
624 if ( buffer[0] ==
'#' )
628 if ( checksum.empty() )
631 if ( buffer.empty() )
633 WAR <<
"Missing filename in CHECKSUMS file: " << index.
asString() <<
" (" << checksum <<
")" << endl;
637 _checksums[(basedir/buffer).asString()] = checksum;
646 MIL <<
"downloading index " << resource << endl;
672 fetcher.
start( dest_dir, media );
681 fetcher.
start( dest_dir, media );
688 WAR <<
"No public key specified by user for index '" << keyloc.
filename() <<
"'"<< endl;
692 fetcher.
start( dest_dir, media );
703 if ( _indexes.empty() )
705 MIL <<
"No indexes to read." << endl;
709 for_( it_idx, _indexes.begin(), _indexes.end() )
711 if ( (*it_idx)->read )
713 DBG <<
"Already read index " <<
PathInfo(dest_dir + (*it_idx)->location.filename()) << endl;
718 downloadIndex( media, (*it_idx)->location, dest_dir );
720 readIndex( dest_dir + (*it_idx)->location.filename(), (*it_idx)->location.filename().
dirname() );
722 MIL <<
"Remember read index " <<
PathInfo(dest_dir + (*it_idx)->location.filename()) << endl;
723 (*it_idx)->read =
true;
726 MIL <<
"done reading indexes" << endl;
732 if ( !_mediaSetAccess )
734 start( dest_dir, *_mediaSetAccess, progress );
743 _mediaSetAccess =
nullptr;
746 progress.
sendTo(progress_receiver);
748 downloadAndReadIndexList(media, dest_dir);
755 addDirJobs(media, location, dest_dir, jobp->flags);
769 getDirectoryContent(media, jobp->location.filename().dirname(), content);
772 MIL <<
"Autodiscovering signed indexes on '"
773 << jobp->location.filename().dirname() <<
"' for '"
774 << jobp->location.filename() <<
"'" << endl;
776 autoaddIndexes(content, media, jobp->location.filename().dirname(), dest_dir);
780 getDirectoryContent(media,
Pathname(
"/"), content);
783 MIL <<
"Autodiscovering signed indexes on '"
785 << jobp->location.filename() <<
"'" << endl;
787 autoaddIndexes(content, media,
Pathname(
"/"), dest_dir);
792 if ( jobp->location.checksum().empty() )
794 if ( _checksums.find(jobp->location.filename().asString())
795 != _checksums.end() )
797 CheckSum chksm = _checksums[jobp->location.filename().asString()];
799 jobp->checkers.push_back(digest_check);
809 jobp->checkers.push_back(digest_check);
817 jobp->checkers.push_back(digest_check);
822 provideToDest( media, dest_dir, jobp );
824 if ( ! progress.
incr() )
832 for ( std::list<FetcherJob_Ptr>::const_iterator it_res = obj.
_resources.begin(); it_res != obj.
_resources.end(); ++it_res )
863 _pimpl->enqueueDigested( resource, checker );
870 _pimpl->enqueueDir(resource, recursive, checker);
877 _pimpl->enqueueDigestedDir(resource, recursive, checker);
883 _pimpl->addIndex(resource);
889 _pimpl->enqueue(resource, checker);
895 _pimpl->addCachePath(cache_dir);
905 _pimpl->setMediaSetAccess( media );
910 _pimpl->start( dest_dir, progress );
917 _pimpl->start(dest_dir, media, progress_receiver);
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
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
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())
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
Pathname locateInCache(const OnMediaLocation &resource_r, const Pathname &destDir_r)
Tries to locate the file represented by job by looking at the cache (matching checksum is mandatory).
std::set< Pathname > _caches
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.
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.
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.
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.
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.
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.
shared_ptr< FetcherIndex > FetcherIndex_Ptr
shared_ptr< FetcherJob > FetcherJob_Ptr
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
function< void(const Pathname &file)> FileChecker
Functor signature used to check files.
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
ZYPP_DECLARE_FLAGS(Flags, Flag)
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)