libzypp  17.32.4
mountingworker.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
9 #include "mountingworker.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 
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 
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 () ) {
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  req ->_spec.forEachVal([&]( const std::string &name, const auto &val ) {
57  if ( name == zyppng::AttachMsgFields::Url
60  return true;
61  vals.add( name, val );
62  return true;
63  });
64 
65  const auto &res = _driver->mountDevice( req->_spec.requestId(), attachUrl, attachId, label, vals );
66  if ( !res ) {
67  const auto &err = res.error();
69  provideFailed( req->_spec.requestId()
70  , err._code
71  , err._reason
72  , err._transient
73  , err._extra );
74  return;
75  }
76 
77  MIL << "Attach of " << attachUrl << " was successfull" << std::endl;
78 
79  attachSuccess( req->_spec.requestId(), res.get().asString() );
80  return;
81  }
83 
84  const auto url = zypp::Url( req->_spec.value( zyppng::DetachMsgFields::Url ).asString() );
85  const auto &attachId = url.getAuthority();
86 
87  if ( _driver->detachMedia( attachId ) ) {
88  detachSuccess ( req->_spec.requestId() );
89  } else {
90  provideFailed( req->_spec.requestId()
92  , "Attach ID not known."
93  , false
94  , {} );
95  return;
96  }
97 
98  _driver->releaseIdleDevices();
99  return;
100  }
101 
103 
104  const auto url = zypp::Url( req->_spec.value( zyppng::DetachMsgFields::Url ).asString() );
105  const auto &attachId = url.getAuthority();
106  const auto &path = zypp::Pathname(url.getPathName());
107  const auto &availMedia = _driver->attachedMedia();
108 
109  auto i = availMedia.find( attachId );
110  if ( i == availMedia.end() ) {
111  ERR << "Unknown Attach ID " << attachId << std::endl;
112  provideFailed( req->_spec.requestId()
114  , "Attach ID not known."
115  , false
116  , {} );
117  return;
118  }
119 
120  const auto &locPath = i->second._dev->_mountPoint / i->second._attachRoot / path;
121 
122  MIL << "Trying to find file: " << locPath << std::endl;
123 
124  zypp::PathInfo info( locPath );
125  if( info.isFile() ) {
126  provideSuccess ( req->_spec.requestId(), false, locPath );
127  return;
128  }
129 
130  if (info.isExist())
131  provideFailed( req->_spec.requestId()
133  , zypp::str::Str() << "Path " << path << " exists, but its not a file"
134  , false
135  , {} );
136  else
137  provideFailed( req->_spec.requestId()
139  , zypp::str::Str() << "File " << path << " not found on medium"
140  , false
141  , {} );
142 
143 
144  break;
145  }
146  default: {
148  provideFailed( req->_spec.requestId()
150  , "Request type not implemented"
151  , false
152  , {} );
153  return;
154  }
155  }
156  } catch ( const zypp::Exception &e ) {
158  provideFailed( req->_spec.requestId()
160  , e.asString()
161  , false
162  , {} );
163  return;
164  } catch ( const std::exception &e ) {
166  provideFailed( req->_spec.requestId()
168  , e.what()
169  , false
170  , {} );
171  return;
172  } catch ( ... ) {
174  provideFailed( req->_spec.requestId()
176  , "Unknown exception"
177  , false
178  , {} );
179  return;
180  }
181  }
182 
183  void MountingWorker::cancel( const std::deque<zyppng::worker::ProvideWorkerItemRef>::iterator &i )
184  {
185  ERR << "Bug, cancel should never be called for running items" << std::endl;
186  }
187 
189  {
190  _driver->immediateShutdown();
191  }
192 }
#define MIL
Definition: Logger.h:96
void add(const std::string &key, const Value &val)
constexpr std::string_view AttachId("attach_id")
void provideFailed(const uint32_t id, const uint code, const std::string &reason, const bool transient, const HeaderValueMap extra={})
#define ERR
Definition: Logger.h:98
zyppng::expected< zyppng::worker::WorkerCaps > initialize(const zyppng::worker::Configuration &conf) override
std::string asString() const
Returns a default string representation of the Url object.
Definition: Url.cc:501
Convenient building of std::string via std::ostringstream Basically a std::ostringstream autoconverti...
Definition: String.h:211
bool isExist() const
Return whether valid stat info exists.
Definition: PathInfo.h:282
std::string asString() const
Error message provided by dumpOn as string.
Definition: Exception.cc:94
constexpr std::string_view Label("label")
std::string getAuthority() const
Returns the encoded authority component of the URL.
Definition: Url.cc:545
#define MIL_PRV
Definition: providedbg_p.h:35
std::deque< ProvideWorkerItemRef > & requestQueue()
void attachSuccess(const uint32_t id, const std::optional< std::string > &localMountPoint={})
Base class for Exception.
Definition: Exception.h:146
constexpr std::string_view Url("url")
constexpr std::string_view Url("url")
void cancel(const std::deque< zyppng::worker::ProvideWorkerItemRef >::iterator &i) override
MountingWorker(std::string_view workerName, DeviceDriverRef driver)
Wrapper class for ::stat/::lstat.
Definition: PathInfo.h:221
void detachSuccess(const uint32_t id)
void provideSuccess(const uint32_t id, bool cacheHit, const zypp::Pathname &localFile, const HeaderValueMap extra={})
Url manipulation class.
Definition: Url.h:91