libzypp  17.34.1
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  {
23  Impl() = default;
24  Impl(const Impl &) = delete;
25  Impl(Impl &&) = delete;
26  Impl &operator=(const Impl &) = delete;
27  Impl &operator=(Impl &&) = delete;
28  virtual ~Impl() {}
29  };
30 
32 
34  {
35  RIMServiceRepos( const Pathname & /*root_r*/,
36  const ServiceInfo & service,
37  const ServiceRepos::ProcessRepo & callback,
39  {
40  // repoindex.xml must be fetched always without using cookies (bnc #573897)
41  Url serviceUrl( service.url() );
42  serviceUrl.setQueryParam( "cookies", "0" );
43 
44  // download the repo index file
45  media::MediaManager mediamanager;
46  media::MediaAccessId mid = mediamanager.open( serviceUrl );
47  mediamanager.attach( mid );
48  mediamanager.provideFile( mid, OnMediaLocation("repo/repoindex.xml") );
49  Pathname path = mediamanager.localPath(mid, "repo/repoindex.xml" );
50  try {
51  parser::RepoindexFileReader reader(path, callback);
52  service.setProbedTtl( reader.ttl() ); // hack! Modifying the const Service to set parsed TTL
53  mediamanager.release( mid );
54  mediamanager.close( mid );
55  } catch ( const Exception &e ) {
56  //Reader throws a bare exception, we need to translate it into something our calling
57  //code expects and handles (bnc#1116840)
58  ZYPP_CAUGHT ( e );
60  ex.remember( e );
61  ZYPP_THROW( ex );
62  }
63  }
64  };
65 
67 
69  {
70  PluginServiceRepos( const Pathname & root_r,
71  const ServiceInfo & service,
72  const ServiceRepos::ProcessRepo & callback,
74  {
75  // bsc#1080693: Service script needs to be executed chrooted to the RepoManagers rootDir.
76  // The service is not aware of the rootDir, so it's explicitly passed and needs to be
77  // stripped from the URLs path.
78  stringstream buffer;
79 
81  args.reserve( 3 );
82  args.push_back( "/bin/sh" );
83  args.push_back( "-c" );
84  args.push_back( Pathname::stripprefix( root_r, service.url().getPathName() ).asString() );
85  ExternalProgramWithStderr prog( args, root_r );
86  prog >> buffer;
87 
88  if ( prog.close() != 0 )
89  {
90  // ServicePluginInformalException:
91  // Ignore this error but we'd like to report it somehow...
92  std::string errbuffer;
93  prog.stderrGetUpTo( errbuffer, '\0' );
94  ERR << "Capture plugin error:[" << endl << errbuffer << endl << ']' << endl;
95  ZYPP_THROW( repo::ServicePluginInformalException( service, errbuffer ) );
96  }
97  parser::RepoFileReader parser( buffer, callback );
98  }
99  };
100 
102 
104  const ServiceInfo & service,
105  const ServiceRepos::ProcessRepo & callback,
106  const ProgressData::ReceiverFnc &progress )
107  : _impl( ( service.type() == ServiceType::PLUGIN )
108  ? static_cast<ServiceRepos::Impl*>( new PluginServiceRepos( root_r, service, callback, progress ) )
109  : static_cast<ServiceRepos::Impl*>( new RIMServiceRepos( root_r, service, callback, progress ) ) )
110  {}
111 
113  {}
114 
115  } // namespace repo
117 } //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:117
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:842
Reads through a repoindex.xml file and collects repositories.
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:429
Describes a resource file located on a medium.
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
Impl & operator=(const Impl &)=delete
ExternalProgram extended to offer reading programs stderr.
function< bool(const ProgressData &)> ReceiverFnc
Most simple version of progress reporting The percentage in most cases.
Definition: progressdata.h:140
PluginServiceRepos(const Pathname &root_r, const ServiceInfo &service, const ServiceRepos::ProcessRepo &callback, const ProgressData::ReceiverFnc &progress=ProgressData::ReceiverFnc())
Definition: ServiceRepos.cc:70
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
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...
#define ERR
Definition: Logger.h:100
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:124
void release(MediaAccessId accessId, const std::string &ejectDev="")
Release the attached media and optionally eject.
unsigned int MediaAccessId
Media manager access Id type.
Definition: MediaSource.h:30
Service type enumeration.
Definition: ServiceType.h:26
int close() override
Wait for the progamm to complete.
Read repository data from a .repo file.
std::vector< std::string > Arguments
Url url() const
The service url.
Definition: ServiceInfo.cc:102
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:35
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
Definition: Exception.h:437
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:146
std::string getPathName(EEncoding eflag=zypp::url::E_DECODED) const
Returns the path name from the URL.
Definition: Url.cc:608
Date::Duration ttl() const
Metadata TTL (repoindex.xml:xpath:/repoindex or 0).
Easy-to use interface to the ZYPP dependency resolver.
Definition: Application.cc:19
static Pathname stripprefix(const Pathname &root_r, const Pathname &path_r)
Return path_r with any root_r dir prefix striped.
Definition: Pathname.cc:281
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:196