libzypp 17.37.10

mountingworker.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
9#include "mountingworker.h"
10#include <zypp-media/ng/private/providedbg_p.h>
11#include <zypp-media/ng/MediaVerifier>
12#include <zypp-core/fs/PathInfo.h>
13
14#undef ZYPP_BASE_LOGGER_LOGGROUP
15#define ZYPP_BASE_LOGGER_LOGGROUP "MountingWorker"
16
17 namespace zyppng::worker
18{
19
20 MountingWorker::MountingWorker( std::string_view workerName, DeviceDriverRef driver )
21 : ProvideWorker( workerName )
22 , _driver(driver)
23 { }
24
25 MountingWorker::~MountingWorker()
26 {
27 _driver->immediateShutdown();
28 }
29
30 zyppng::expected<zyppng::worker::WorkerCaps> MountingWorker::initialize( const zyppng::worker::Configuration &conf )
31 {
32 return _driver->initialize(conf);
33 }
34
35 void MountingWorker::provide()
36 {
37 _driver->detectDevices();
38 auto &queue = requestQueue();
39
40 if ( !queue.size() )
41 return;
42
43 auto req = queue.front();
44 queue.pop_front();
45
46 MIL_PRV << "Received provide: " << req->_spec.code() << std::endl;
47
48 try {
49 switch ( req->_spec.code () ) {
50 case zyppng::ProvideMessage::Code::Attach: {
51
52 const auto attachUrl = zypp::Url( req->_spec.value( zyppng::AttachMsgFields::Url ).asString() );
53 const auto label = req->_spec.value( zyppng::AttachMsgFields::Label, "No label" ).asString();
54 const auto attachId = req->_spec.value( zyppng::AttachMsgFields::AttachId ).asString();
55 HeaderValueMap vals;
56 for ( const auto &i : req->_spec.headers() ) {
57 if ( i.first == zyppng::AttachMsgFields::Url
58 || i.first == zyppng::AttachMsgFields::Label
59 || i.first == zyppng::AttachMsgFields::AttachId )
60 continue;
61 vals.set( i.first, i.second );
62 }
63
64 const auto &res = _driver->mountDevice( req->_spec.requestId(), attachUrl, attachId, label, vals );
65 if ( !res ) {
66 const auto &err = res.error();
67 req->_state = zyppng::worker::ProvideWorkerItem::Finished;
68 provideFailed( req->_spec.requestId()
69 , err._code
70 , err._reason
71 , err._transient
72 , err._extra );
73 return;
74 }
75
76 MIL << "Attach of " << attachUrl << " was successfull" << std::endl;
77
78 attachSuccess( req->_spec.requestId(), res.get().asString() );
79 return;
80 }
81 case zyppng::ProvideMessage::Code::Detach: {
82
83 const auto url = zypp::Url( req->_spec.value( zyppng::DetachMsgFields::Url ).asString() );
84 const auto &attachId = url.getAuthority();
85
86 if ( _driver->detachMedia( attachId ) ) {
87 detachSuccess ( req->_spec.requestId() );
88 } else {
89 provideFailed( req->_spec.requestId()
90 , zyppng::ProvideMessage::Code::NotFound
91 , "Attach ID not known."
92 , false
93 , {} );
94 return;
95 }
96
97 _driver->releaseIdleDevices();
98 return;
99 }
100
101 case zyppng::ProvideMessage::Code::Prov: {
102
103 const auto url = zypp::Url( req->_spec.value( zyppng::DetachMsgFields::Url ).asString() );
104 const auto &attachId = url.getAuthority();
105 const auto &path = zypp::Pathname(url.getPathName());
106 const auto &availMedia = _driver->attachedMedia();
107
108 auto i = availMedia.find( attachId );
109 if ( i == availMedia.end() ) {
110 ERR << "Unknown Attach ID " << attachId << std::endl;
111 provideFailed( req->_spec.requestId()
112 , zyppng::ProvideMessage::Code::NotFound
113 , "Attach ID not known."
114 , false
115 , {} );
116 return;
117 }
118
119 const auto &locPath = i->second._dev->_mountPoint / i->second._attachRoot / path;
120
121 MIL << "Trying to find file: " << locPath << std::endl;
122
123 zypp::PathInfo info( locPath );
124 if( info.isFile() ) {
125 provideSuccess ( req->_spec.requestId(), false, locPath );
126 return;
127 }
128
129 if (info.isExist())
130 provideFailed( req->_spec.requestId()
131 , zyppng::ProvideMessage::Code::NotAFile
132 , zypp::str::Str() << "Path " << path << " exists, but its not a file"
133 , false
134 , {} );
135 else
136 provideFailed( req->_spec.requestId()
137 , zyppng::ProvideMessage::Code::NotFound
138 , zypp::str::Str() << "File " << path << " not found on medium"
139 , false
140 , {} );
141
142
143 break;
144 }
145 default: {
146 req->_state = zyppng::worker::ProvideWorkerItem::Finished;
147 provideFailed( req->_spec.requestId()
148 , zyppng::ProvideMessage::Code::BadRequest
149 , "Request type not implemented"
150 , false
151 , {} );
152 return;
153 }
154 }
155 } catch ( const zypp::Exception &e ) {
156 req->_state = zyppng::worker::ProvideWorkerItem::Finished;
157 provideFailed( req->_spec.requestId()
158 , zyppng::ProvideMessage::Code::BadRequest
159 , e.asString()
160 , false
161 , {} );
162 return;
163 } catch ( const std::exception &e ) {
164 req->_state = zyppng::worker::ProvideWorkerItem::Finished;
165 provideFailed( req->_spec.requestId()
166 , zyppng::ProvideMessage::Code::BadRequest
167 , e.what()
168 , false
169 , {} );
170 return;
171 } catch ( ... ) {
172 req->_state = zyppng::worker::ProvideWorkerItem::Finished;
173 provideFailed( req->_spec.requestId()
174 , zyppng::ProvideMessage::Code::BadRequest
175 , "Unknown exception"
176 , false
177 , {} );
178 return;
179 }
180 }
181
182 void MountingWorker::cancel( const std::deque<zyppng::worker::ProvideWorkerItemRef>::iterator &i )
183 {
184 ERR << "Bug, cancel should never be called for running items" << std::endl;
185 }
186
187 void MountingWorker::immediateShutdown()
188 {
189 _driver->immediateShutdown();
190 }
191}
zypp::Exception
Base class for Exception.
Definition Exception.h:153
zypp::Exception::asString
std::string asString() const
Error message provided by dumpOn as string.
Definition Exception.cc:124
zypp::Exception::what
const char * what() const override
Return message string.
Definition Exception.h:322
zypp::Url
Url manipulation class.
Definition Url.h:93
zypp::filesystem::PathInfo
Wrapper class for stat/lstat.
Definition PathInfo.h:226
zypp::filesystem::PathInfo::isFile
bool isFile() const
Definition PathInfo.h:295
zypp::filesystem::PathInfo::isExist
bool isExist() const
Return whether valid stat info exists.
Definition PathInfo.h:286
zypp::filesystem::Pathname
Pathname.
Definition Pathname.h:47
zyppng::HeaderValueMap
Definition headervaluemap.h:67
zyppng::HeaderValueMap::set
void set(const std::string &key, Value val)
Definition headervaluemap.cc:152
zyppng::expected
Definition expected.h:27
zyppng::worker::MountingWorker::MountingWorker
MountingWorker(std::string_view workerName, DeviceDriverRef driver)
Definition mountingworker.cc:20
zyppng::worker::MountingWorker::_driver
DeviceDriverRef _driver
Definition mountingworker.h:36
zyppng::worker::MountingWorker::initialize
zyppng::expected< zyppng::worker::WorkerCaps > initialize(const zyppng::worker::Configuration &conf) override
Definition mountingworker.cc:30
zyppng::worker::MountingWorker::~MountingWorker
~MountingWorker()
Definition mountingworker.cc:25
zyppng::worker::MountingWorker::provide
void provide() override
Definition mountingworker.cc:35
zyppng::worker::MountingWorker::cancel
void cancel(const std::deque< zyppng::worker::ProvideWorkerItemRef >::iterator &i) override
Definition mountingworker.cc:182
zyppng::worker::MountingWorker::immediateShutdown
void immediateShutdown() override
Definition mountingworker.cc:187
zyppng::worker::ProvideWorkerItem::Finished
@ Finished
Definition provideworker.h:56
zyppng::worker::ProvideWorker::detachSuccess
void detachSuccess(const uint32_t id)
Definition provideworker.cc:187
zyppng::worker::ProvideWorker::attachSuccess
void attachSuccess(const uint32_t id, const std::optional< std::string > &localMountPoint={})
Definition provideworker.cc:177
zyppng::worker::ProvideWorker::ProvideWorker
ProvideWorker(std::string_view workerName)
Definition provideworker.cc:37
zyppng::worker::ProvideWorker::provideSuccess
void provideSuccess(const uint32_t id, bool cacheHit, const zypp::Pathname &localFile, const HeaderValueMap extra={})
Definition provideworker.cc:136
zyppng::worker::ProvideWorker::provideFailed
void provideFailed(const uint32_t id, const ProvideMessage::Code code, const std::string &reason, const bool transient, const HeaderValueMap extra={})
Definition provideworker.cc:149
zyppng::worker::ProvideWorker::requestQueue
std::deque< ProvideWorkerItemRef > & requestQueue()
Definition provideworker.cc:101
mountingworker.h
zypp::url
Url details namespace.
Definition UrlBase.cc:58
zyppng::AttachMsgFields::AttachId
constexpr std::string_view AttachId("attach_id")
zyppng::AttachMsgFields::Label
constexpr std::string_view Label("label")
zyppng::AttachMsgFields::Url
constexpr std::string_view Url("url")
zyppng::DetachMsgFields::Url
constexpr std::string_view Url("url")
zyppng::worker
Definition devicedriver.cc:23
zyppng::worker::Configuration
zyppng::ProviderConfiguration Configuration
Definition provideworker.h:31
providedbg_p.h
MIL_PRV
#define MIL_PRV
Definition providedbg_p.h:35
zypp::str::Str
Convenient building of std::string via std::ostringstream Basically a std::ostringstream autoconverti...
Definition String.h:213
MIL
#define MIL
Definition Logger.h:100
ERR
#define ERR
Definition Logger.h:102
PathInfo.h