libzypp  17.28.4
ServiceRepos.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <sstream>
3 #include <zypp/base/Logger.h>
10 #include <zypp/ExternalProgram.h>
11 
12 using std::stringstream;
13 using std::endl;
14 
16 namespace zypp
17 {
19  namespace repo
20  {
22  { virtual ~Impl() {} };
23 
25 
27  {
28  RIMServiceRepos( const Pathname & /*root_r*/,
29  const ServiceInfo & service,
30  const ServiceRepos::ProcessRepo & callback,
32  {
33  // repoindex.xml must be fetched always without using cookies (bnc #573897)
34  Url serviceUrl( service.url() );
35  serviceUrl.setQueryParam( "cookies", "0" );
36 
37  // download the repo index file
38  media::MediaManager mediamanager;
39  media::MediaAccessId mid = mediamanager.open( serviceUrl );
40  mediamanager.attach( mid );
41  mediamanager.provideFile( mid, OnMediaLocation("repo/repoindex.xml") );
42  Pathname path = mediamanager.localPath(mid, "repo/repoindex.xml" );
43  try {
44  parser::RepoindexFileReader reader(path, callback);
45  service.setProbedTtl( reader.ttl() ); // hack! Modifying the const Service to set parsed TTL
46  mediamanager.release( mid );
47  mediamanager.close( mid );
48  } catch ( const Exception &e ) {
49  //Reader throws a bare exception, we need to translate it into something our calling
50  //code expects and handles (bnc#1116840)
51  ZYPP_CAUGHT ( e );
53  ex.remember( e );
54  ZYPP_THROW( ex );
55  }
56  }
57  };
58 
60 
62  {
63  PluginServiceRepos( const Pathname & root_r,
64  const ServiceInfo & service,
65  const ServiceRepos::ProcessRepo & callback,
67  {
68  // bsc#1080693: Service script needs to be executed chrooted to the RepoManagers rootDir.
69  // The service is not aware of the rootDir, so it's explicitly passed and needs to be
70  // stripped from the URLs path.
71  stringstream buffer;
72 
74  args.reserve( 3 );
75  args.push_back( "/bin/sh" );
76  args.push_back( "-c" );
77  args.push_back( Pathname::stripprefix( root_r, service.url().getPathName() ).asString() );
78  ExternalProgramWithStderr prog( args, root_r );
79  prog >> buffer;
80 
81  if ( prog.close() != 0 )
82  {
83  // ServicePluginInformalException:
84  // Ignore this error but we'd like to report it somehow...
85  std::string errbuffer;
86  prog.stderrGetUpTo( errbuffer, '\0' );
87  ERR << "Capture plugin error:[" << endl << errbuffer << endl << ']' << endl;
88  ZYPP_THROW( repo::ServicePluginInformalException( service, errbuffer ) );
89  }
90  parser::RepoFileReader parser( buffer, callback );
91  }
92  };
93 
95 
97  const ServiceInfo & service,
98  const ServiceRepos::ProcessRepo & callback,
99  const ProgressData::ReceiverFnc &progress )
100  : _impl( ( service.type() == ServiceType::PLUGIN )
101  ? static_cast<ServiceRepos::Impl*>( new PluginServiceRepos( root_r, service, callback, progress ) )
102  : static_cast<ServiceRepos::Impl*>( new RIMServiceRepos( root_r, service, callback, progress ) ) )
103  {}
104 
106  {}
107 
108  } // namespace repo
110 } //namespace zypp
Interface of repoindex.xml file reader.
Base class for Exception.
Definition: Exception.h:146
const std::string & msg() const
Return the message string provided to the ctor.
Definition: Exception.h:195
void remember(const Exception &old_r)
Store an other Exception as history.
Definition: Exception.cc:105
ExternalProgram extended to offer reading programs stderr.
bool stderrGetUpTo(std::string &retval_r, const char delim_r, bool returnDelim_r=false)
Read data up to delim_r from stderr (nonblocking).
std::vector< std::string > Arguments
int close()
Wait for the progamm to complete.
Describes a resource file located on a medium.
function< bool(const ProgressData &)> ReceiverFnc
Most simple version of progress reporting The percentage in most cases.
Definition: ProgressData.h:139
Service data.
Definition: ServiceInfo.h:37
void setProbedTtl(Date::Duration ttl_r) const
Lazy init sugested TTL.
Definition: ServiceInfo.cc:114
Url url() const
The service url.
Definition: ServiceInfo.cc:99
Url manipulation class.
Definition: Url.h:92
std::string getPathName(EEncoding eflag=zypp::url::E_DECODED) const
Returns the path name from the URL.
Definition: Url.cc:599
void setQueryParam(const std::string &param, const std::string &value)
Set or add value for the specified query parameter.
Definition: Url.cc:833
static Pathname stripprefix(const Pathname &root_r, const Pathname &path_r)
Return path_r with any root_r dir prefix striped.
Definition: Pathname.cc:244
const std::string & asString() const
String representation.
Definition: Pathname.h:91
Manages access to the 'physical' media, e.g CDROM drives, Disk volumes, directory trees,...
Definition: MediaManager.h:454
MediaAccessId open(const Url &url, const Pathname &preferred_attach_point="")
Opens the media access for specified with the url.
void attach(MediaAccessId accessId)
Attach the media using the concrete handler (checks all devices).
void close(MediaAccessId accessId)
Close the media access with specified id.
ZYPP_DEPRECATED void provideFile(MediaAccessId accessId, const Pathname &filename, const ByteCount &expectedFileSize) const
void release(MediaAccessId accessId, const std::string &ejectDev="")
Release the attached media and optionally eject.
Pathname localPath(MediaAccessId accessId, const Pathname &pathname) const
Shortcut for 'localRoot() + pathname', but returns an empty pathname if media is not attached.
Read repository data from a .repo file.
Reads through a repoindex.xml file and collects repositories.
Date::Duration ttl() const
Metadata TTL (repoindex.xml:xpath:/repoindex@ttl or 0).
Service plugin has trouble providing the metadata but this should not be treated as error.
Retrieval of repository list for a service.
Definition: ServiceRepos.h:26
function< bool(const RepoInfo &)> ProcessRepo
Return false from the callback to get a AbortRequestException to be thrown and the processing to be c...
Definition: ServiceRepos.h:32
ServiceRepos(const Pathname &root_r, const ServiceInfo &service, const ProcessRepo &callback, const ProgressData::ReceiverFnc &progress=ProgressData::ReceiverFnc())
bsc#1080693: Explicitly pass the RemoManagers rootDir until it can be queried from the ServiceInfo.
Definition: ServiceRepos.cc:96
unsigned int MediaAccessId
Media manager access Id type.
Definition: MediaSource.h:29
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
PluginServiceRepos(const Pathname &root_r, const ServiceInfo &service, const ServiceRepos::ProcessRepo &callback, const ProgressData::ReceiverFnc &progress=ProgressData::ReceiverFnc())
Definition: ServiceRepos.cc:63
RIMServiceRepos(const Pathname &, const ServiceInfo &service, const ServiceRepos::ProcessRepo &callback, const ProgressData::ReceiverFnc &progress=ProgressData::ReceiverFnc())
Definition: ServiceRepos.cc:28
Service type enumeration.
Definition: ServiceType.h:27
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
Definition: Exception.h:396
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:392
#define ERR
Definition: Logger.h:98