Imported Upstream version 14.45.0
[platform/upstream/libzypp.git] / zypp / repo / ServiceRepos.cc
1 #include <iostream>
2 #include <sstream>
3 #include "zypp/base/Logger.h"
4 #include "zypp/repo/ServiceRepos.h"
5 #include "zypp/repo/RepoException.h"
6 #include "zypp/media/MediaException.h"
7 #include "zypp/parser/RepoFileReader.h"
8 #include "zypp/media/MediaManager.h"
9 #include "zypp/parser/RepoindexFileReader.h"
10 #include "zypp/ExternalProgram.h"
11
12 using std::stringstream;
13 using std::endl;
14
15 namespace zypp
16 {
17 namespace repo
18 {
19
20 class ServiceRepos::Impl
21 {
22 public:
23     Impl()
24     {
25     }
26
27     virtual ~Impl()
28     {
29     }
30 };
31
32 class RIMServiceRepos : public ServiceRepos::Impl
33 {
34 public:
35     ServiceRepos::ProcessRepo _callback;
36
37     RIMServiceRepos(const ServiceInfo &service,
38                     const ServiceRepos::ProcessRepo & callback,
39                     const ProgressData::ReceiverFnc &progress = ProgressData::ReceiverFnc() )
40         : _callback(callback)
41     {
42       // repoindex.xml must be fetched always without using cookies (bnc #573897)
43       Url serviceUrl( service.url() );
44       serviceUrl.setQueryParam( "cookies", "0" );
45
46       // download the repo index file
47       media::MediaManager mediamanager;
48       media::MediaAccessId mid = mediamanager.open( serviceUrl );
49       mediamanager.attach( mid );
50       mediamanager.provideFile( mid, "repo/repoindex.xml" );
51       Pathname path = mediamanager.localPath(mid, "repo/repoindex.xml" );
52       parser::RepoindexFileReader reader(path, _callback);
53       mediamanager.release( mid );
54       mediamanager.close( mid );
55     }
56
57     ~RIMServiceRepos()
58     {
59
60     }
61 };
62
63 class PluginServiceRepos : public ServiceRepos::Impl
64 {
65 public:
66     ServiceRepos::ProcessRepo _callback;
67
68     PluginServiceRepos(const ServiceInfo &service,
69                       const ServiceRepos::ProcessRepo & callback,
70                       const ProgressData::ReceiverFnc &progress = ProgressData::ReceiverFnc() )
71         : _callback(callback)
72     {
73       Url serviceUrl( service.url() );
74       stringstream buffer;
75
76       ExternalProgram::Arguments args;
77       args.reserve( 3 );
78       args.push_back( "/bin/sh" );
79       args.push_back( "-c" );
80       args.push_back( serviceUrl.getPathName() );
81       ExternalProgramWithStderr prog( args );
82       prog >> buffer;
83
84       if ( prog.close() != 0 )
85       {
86         // ServicePluginInformalException:
87         // Ignore this error but we'd like to report it somehow...
88         std::string errbuffer;
89         prog.stderrGetUpTo( errbuffer, '\0' );
90         ERR << "Capture plugin error:[" << endl << errbuffer << endl << ']' << endl;
91         ZYPP_THROW( repo::ServicePluginInformalException( service, errbuffer ) );
92       }
93
94       parser::RepoFileReader parser(buffer, _callback);
95     }
96
97     ~PluginServiceRepos()
98     {}
99 };
100
101
102 ServiceRepos::ServiceRepos(const ServiceInfo &service,
103                            const ServiceRepos::ProcessRepo & callback,
104                            const ProgressData::ReceiverFnc &progress)
105     : _impl( (service.type() == ServiceType::PLUGIN) ? (ServiceRepos::Impl *)(new PluginServiceRepos(service, callback, progress)) : (ServiceRepos::Impl *)(new RIMServiceRepos(service, callback, progress)))
106 {
107 }
108
109 ServiceRepos::~ServiceRepos()
110 {
111 }
112
113
114 }
115 }