libzypp  17.31.0
ServiceRepos.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <sstream>
3 #include <zypp/base/Logger.h>
6 #include <zypp-media/MediaException>
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
std::string asString(const Patch::Category &obj)
Definition: Patch.cc:122
Service data.
Definition: ServiceInfo.h:36
void setProbedTtl(Date::Duration ttl_r) const
Lazy init sugested TTL.
Definition: ServiceInfo.cc:114
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
Retrieval of repository list for a service.
Definition: ServiceRepos.h:25
void setQueryParam(const std::string &param, const std::string &value)
Set or add value for the specified query parameter.
Definition: Url.cc:838
Reads through a repoindex.xml file and collects repositories.
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:428
Describes a resource file located on a medium.
ExternalProgram extended to offer reading programs stderr.
PluginServiceRepos(const Pathname &root_r, const ServiceInfo &service, const ServiceRepos::ProcessRepo &callback, const ProgressData::ReceiverFnc &progress=ProgressData::ReceiverFnc())
Definition: ServiceRepos.cc:63
Service plugin has trouble providing the metadata but this should not be treated as error...
ZYPP_DEPRECATED void provideFile(MediaAccessId accessId, const Pathname &filename, const ByteCount &expectedFileSize) const
function< bool(const ProgressData &)> ReceiverFnc
Most simple version of progress reporting The percentage in most cases.
Definition: progressdata.h:139
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
#define ERR
Definition: Logger.h:98
unsigned int MediaAccessId
Media manager access Id type.
Definition: MediaSource.h:29
std::vector< std::string > Arguments
Pathname localPath(MediaAccessId accessId, const Pathname &pathname) const
Shortcut for &#39;localRoot() + pathname&#39;, but returns an empty pathname if media is not attached...
Interface of repoindex.xml file reader.
void remember(const Exception &old_r)
Store an other Exception as history.
Definition: Exception.cc:105
void release(MediaAccessId accessId, const std::string &ejectDev="")
Release the attached media and optionally eject.
Service type enumeration.
Definition: ServiceType.h:26
Read repository data from a .repo file.
Url url() const
The service url.
Definition: ServiceInfo.cc:99
void attach(MediaAccessId accessId)
Attach the media using the concrete handler (checks all devices).
bool stderrGetUpTo(std::string &retval_r, const char delim_r, bool returnDelim_r=false)
Read data up to delim_r from stderr (nonblocking).
RIMServiceRepos(const Pathname &, const ServiceInfo &service, const ServiceRepos::ProcessRepo &callback, const ProgressData::ReceiverFnc &progress=ProgressData::ReceiverFnc())
Definition: ServiceRepos.cc:28
int close()
Wait for the progamm to complete.
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
Definition: Exception.h:436
Manages access to the &#39;physical&#39; media, e.g CDROM drives, Disk volumes, directory trees...
Definition: MediaManager.h:453
Base class for Exception.
Definition: Exception.h:145
std::string getPathName(EEncoding eflag=zypp::url::E_DECODED) const
Returns the path name from the URL.
Definition: Url.cc:604
Date::Duration ttl() const
Metadata TTL (repoindex.xml:xpath:/repoindex or 0).
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
static Pathname stripprefix(const Pathname &root_r, const Pathname &path_r)
Return path_r with any root_r dir prefix striped.
Definition: Pathname.cc:280
MediaAccessId open(const Url &url, const Pathname &preferred_attach_point="")
Opens the media access for specified with the url.
Url manipulation class.
Definition: Url.h:91
void close(MediaAccessId accessId)
Close the media access with specified id.
const std::string & msg() const
Return the message string provided to the ctor.
Definition: Exception.h:195