libzypp  16.22.5
MediaSetAccess.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
9 
10 #include <iostream>
11 #include <fstream>
12 
13 #include "zypp/base/LogTools.h"
14 #include "zypp/base/Regex.h"
16 #include "zypp/ZYppCallbacks.h"
17 #include "zypp/MediaSetAccess.h"
18 #include "zypp/PathInfo.h"
19 //#include "zypp/source/MediaSetAccessReportReceivers.h"
20 
21 using namespace std;
22 
24 namespace zypp
25 {
26 
27 IMPL_PTR_TYPE(MediaSetAccess);
28 
30 
31  MediaSetAccess::MediaSetAccess(const Url &url,
32  const Pathname & prefered_attach_point)
33  : _url(url)
34  , _prefAttachPoint(prefered_attach_point)
35  {}
36 
37  MediaSetAccess::MediaSetAccess(const std::string & label_r,
38  const Url &url,
39  const Pathname & prefered_attach_point)
40  : _url(url)
41  , _prefAttachPoint(prefered_attach_point)
42  , _label( label_r )
43  {}
44 
46  {
47  try
48  {
49  media::MediaManager manager;
50  for ( const auto & mm : _medias )
51  manager.close( mm.second );
52  }
53  catch(...) {} // don't let exception escape a dtor.
54  }
55 
56 
58  {
59  if (_medias.find(media_nr) != _medias.end())
60  {
61  // the media already exists, set theverifier
62  media::MediaAccessId id = _medias[media_nr];
63  media::MediaManager media_mgr;
64  media_mgr.addVerifier( id, verifier );
65  // remove any saved verifier for this media
66  _verifiers.erase(media_nr);
67  }
68  else
69  {
70  // save the verifier in the map, and set it when
71  // the media number is first attached
72  _verifiers[media_nr] = verifier;
73  }
74  }
75 
76  void MediaSetAccess::releaseFile( const OnMediaLocation & on_media_file )
77  {
78  releaseFile( on_media_file.filename(), on_media_file.medianr() );
79  }
80 
81  void MediaSetAccess::releaseFile( const Pathname & file, unsigned media_nr)
82  {
83  media::MediaManager media_mgr;
85 
86  media = getMediaAccessId( media_nr);
87  DBG << "Going to release file " << file
88  << " from media number " << media_nr << endl;
89 
90  if ( ! media_mgr.isAttached(media) )
91  return; //disattached media is free
92 
93  media_mgr.releaseFile (media, file);
94  }
95 
96  void MediaSetAccess::dirInfo( filesystem::DirContent &retlist, const Pathname &dirname,
97  bool dots, unsigned media_nr )
98  {
99  media::MediaManager media_mgr;
100  media::MediaAccessId media;
101  media = getMediaAccessId(media_nr);
102 
103  // try to attach the media
104  if ( ! media_mgr.isAttached(media) )
105  media_mgr.attach(media);
106 
107  media_mgr.dirInfo(media, retlist, dirname, dots);
108  }
109 
111  {
112  Pathname result;
114  void operator()( media::MediaAccessId media, const Pathname &file )
115  {
116  media::MediaManager media_mgr;
117  media_mgr.provideFile(media, file, expectedFileSize);
118  result = media_mgr.localPath(media, file);
119  }
120  };
121 
123  {
124  Pathname result;
125  void operator()( media::MediaAccessId media, const Pathname &file )
126  {
127  media::MediaManager media_mgr;
128  media_mgr.provideDirTree(media, file);
129  result = media_mgr.localPath(media, file);
130  }
131  };
132 
134  {
135  Pathname result;
136  void operator()( media::MediaAccessId media, const Pathname &file )
137  {
138  media::MediaManager media_mgr;
139  media_mgr.provideDir(media, file);
140  result = media_mgr.localPath(media, file);
141  }
142  };
143 
145  {
146  bool result;
148  : result(false)
149  {}
150 
151  void operator()( media::MediaAccessId media, const Pathname &file )
152  {
153  media::MediaManager media_mgr;
154  result = media_mgr.doesFileExist(media, file);
155  }
156  };
157 
158 
159 
160  Pathname MediaSetAccess::provideFile( const OnMediaLocation & resource, ProvideFileOptions options, const Pathname &deltafile )
161  {
163  op.expectedFileSize = resource.downloadSize();
164  provide( boost::ref(op), resource, options, deltafile );
165  return op.result;
166  }
167 
168  Pathname MediaSetAccess::provideFile(const Pathname & file, unsigned media_nr, ProvideFileOptions options )
169  {
170  OnMediaLocation resource;
172  resource.setLocation(file, media_nr);
173  provide( boost::ref(op), resource, options, Pathname() );
174  return op.result;
175  }
176 
177  Pathname MediaSetAccess::provideOptionalFile( const Pathname & file, unsigned media_nr )
178  {
179  try
180  {
181  if ( doesFileExist( file, media_nr ) )
182  return provideFile( file, media_nr, PROVIDE_NON_INTERACTIVE );
183  }
184  catch ( const media::MediaFileNotFoundException & excpt_r )
185  { ZYPP_CAUGHT( excpt_r ); }
186  catch ( const media::MediaNotAFileException & excpt_r )
187  { ZYPP_CAUGHT( excpt_r ); }
188  return Pathname();
189  }
190 
191  bool MediaSetAccess::doesFileExist(const Pathname & file, unsigned media_nr )
192  {
194  OnMediaLocation resource;
195  resource.setLocation(file, media_nr);
196  provide( boost::ref(op), resource, PROVIDE_DEFAULT, Pathname());
197  return op.result;
198  }
199 
201  const OnMediaLocation &resource,
202  ProvideFileOptions options,
203  const Pathname &deltafile )
204  {
205  Pathname file(resource.filename());
206  unsigned media_nr(resource.medianr());
207 
209  media::MediaManager media_mgr;
210 
211  media::MediaAccessId media;
212 
213  do
214  {
215  // get the mediaId, but don't try to attach it here
216  media = getMediaAccessId( media_nr);
217  bool deltafileset = false;
218 
219  try
220  {
221  DBG << "Going to try to provide " << (resource.optional() ? "optional" : "") << " file " << file
222  << " from media number " << media_nr << endl;
223  // try to attach the media
224  if ( ! media_mgr.isAttached(media) )
225  media_mgr.attach(media);
226  media_mgr.setDeltafile(media, deltafile);
227  deltafileset = true;
228  op(media, file);
229  media_mgr.setDeltafile(media, Pathname());
230  break;
231  }
232  catch ( media::MediaException & excp )
233  {
234  ZYPP_CAUGHT(excp);
235  if (deltafileset)
236  media_mgr.setDeltafile(media, Pathname());
238  unsigned int devindex = 0;
239  vector<string> devices;
240  media_mgr.getDetectedDevices(media, devices, devindex);
241 
242  do
243  {
244  if (user != media::MediaChangeReport::EJECT) // no use in calling this again
245  {
246  DBG << "Media couldn't provide file " << file << " , releasing." << endl;
247  try
248  {
249  media_mgr.release(media);
250  }
251  catch (const Exception & excpt_r)
252  {
253  ZYPP_CAUGHT(excpt_r);
254  MIL << "Failed to release media " << media << endl;
255  }
256  }
257 
258  // set up the reason
260 
261  if( typeid(excp) == typeid( media::MediaFileNotFoundException ) ||
262  typeid(excp) == typeid( media::MediaNotAFileException ) )
263  {
265  }
266  else if( typeid(excp) == typeid( media::MediaNotDesiredException) ||
267  typeid(excp) == typeid( media::MediaNotAttachedException) )
268  {
270  }
271  else if( typeid(excp) == typeid( media::MediaTimeoutException) ||
272  typeid(excp) == typeid( media::MediaTemporaryProblemException))
273  {
275  }
276 
277  // Propagate the original error if _no_ callback receiver is connected, or
278  // non_interactive mode (for optional files) is used (except for wrong media).
280  || (( options & PROVIDE_NON_INTERACTIVE ) && reason != media::MediaChangeReport::WRONG ) )
281  {
282  MIL << "Can't provide file. Non-Interactive mode." << endl;
283  ZYPP_RETHROW(excp);
284  }
285  else
286  {
287  // release all media before requesting another (#336881)
288  media_mgr.releaseAll();
289 
290  user = report->requestMedia (
291  _url,
292  media_nr,
293  _label,
294  reason,
295  excp.asUserHistory(),
296  devices,
297  devindex
298  );
299  }
300 
301  MIL << "ProvideFile exception caught, callback answer: " << user << endl;
302 
303  if( user == media::MediaChangeReport::ABORT )
304  {
305  DBG << "Aborting" << endl;
306  AbortRequestException aexcp("Aborting requested by user");
307  aexcp.remember(excp);
308  ZYPP_THROW(aexcp);
309  }
310  else if ( user == media::MediaChangeReport::IGNORE )
311  {
312  DBG << "Skipping" << endl;
313  SkipRequestException nexcp("User-requested skipping of a file");
314  nexcp.remember(excp);
315  ZYPP_THROW(nexcp);
316  }
317  else if ( user == media::MediaChangeReport::EJECT )
318  {
319  DBG << "Eject: try to release" << endl;
320  try
321  {
322  media_mgr.releaseAll();
323  media_mgr.release (media, devindex < devices.size() ? devices[devindex] : "");
324  }
325  catch ( const Exception & e)
326  {
327  ZYPP_CAUGHT(e);
328  }
329  }
330  else if ( user == media::MediaChangeReport::RETRY ||
332  {
333  // retry
334  DBG << "Going to try again" << endl;
335  // invalidate current media access id
336  media_mgr.close(media);
337  _medias.erase(media_nr);
338 
339  // not attaching, media set will do that for us
340  // this could generate uncaught exception (#158620)
341  break;
342  }
343  else
344  {
345  DBG << "Don't know, let's ABORT" << endl;
346  ZYPP_RETHROW ( excp );
347  }
348  } while( user == media::MediaChangeReport::EJECT );
349  }
350 
351  // retry or change URL
352  } while( true );
353  }
354 
355  Pathname MediaSetAccess::provideDir(const Pathname & dir,
356  bool recursive,
357  unsigned media_nr,
358  ProvideFileOptions options )
359  {
360  OnMediaLocation resource;
361  resource.setLocation(dir, media_nr);
362  if ( recursive )
363  {
365  provide( boost::ref(op), resource, options, Pathname());
366  return op.result;
367  }
369  provide( boost::ref(op), resource, options, Pathname());
370  return op.result;
371  }
372 
374  {
375  if ( _medias.find( medianr ) != _medias.end() )
376  {
377  return _medias[medianr];
378  }
379 
380  Url url( medianr > 1 ? rewriteUrl( _url, medianr ) : _url );
381  media::MediaManager media_mgr;
382  media::MediaAccessId id = media_mgr.open( url, _prefAttachPoint );
383  _medias[medianr] = id;
384 
385  try
386  {
387  if ( _verifiers.find(medianr) != _verifiers.end() )
388  {
389  // a verifier is set for this media
390  // FIXME check the case where the verifier exists
391  // but we have no access id for the media
392  media_mgr.delVerifier( id );
393  media_mgr.addVerifier( id, _verifiers[medianr] );
394  // remove any saved verifier for this media
395  _verifiers.erase( medianr );
396  }
397  }
398  catch ( const Exception &e )
399  {
400  ZYPP_CAUGHT(e);
401  WAR << "Verifier not found" << endl;
402  }
403 
404  return id;
405  }
406 
407 
408  Url MediaSetAccess::rewriteUrl (const Url & url_r, const media::MediaNr medianr)
409  {
410  std::string scheme = url_r.getScheme();
411  if (scheme == "cd" || scheme == "dvd")
412  return url_r;
413 
414  DBG << "Rewriting url " << url_r << endl;
415 
416  if( scheme == "iso")
417  {
418  // TODO the iso parameter will not be required in the future, this
419  // code has to be adapted together with the MediaISO change.
420  // maybe some MediaISOURL interface should be used.
421  std::string isofile = url_r.getQueryParam("iso");
422  str::regex e("^(.*)(cd|dvd|media)[0-9]+\\.iso$", str::regex::icase);
423 
424  str::smatch what;
425  if(str::regex_match(isofile, what, e))
426  {
427  Url url( url_r);
428  isofile = what[1] + what[2] + str::numstring(medianr) + ".iso";
429  url.setQueryParam("iso", isofile);
430  DBG << "Url rewrite result: " << url << endl;
431  return url;
432  }
433  }
434  else
435  {
436  std::string pathname = url_r.getPathName();
437  str::regex e("^(.*)(cd|dvd|media)[0-9]+(/)?$", str::regex::icase);
438  str::smatch what;
439  if(str::regex_match(pathname, what, e))
440  {
441  Url url( url_r);
442  pathname = what[1] + what[2] + str::numstring(medianr) + what[3];
443  url.setPathName(pathname);
444  DBG << "Url rewrite result: " << url << endl;
445  return url;
446  }
447  }
448  return url_r;
449  }
450 
452  {
453  DBG << "Releasing all media IDs held by this MediaSetAccess" << endl;
454  media::MediaManager manager;
455  for (MediaMap::const_iterator m = _medias.begin(); m != _medias.end(); ++m)
456  manager.release(m->second, "");
457  }
458 
459  std::ostream & MediaSetAccess::dumpOn( std::ostream & str ) const
460  {
461  str << "MediaSetAccess (URL='" << _url << "', attach_point_hint='" << _prefAttachPoint << "')";
462  return str;
463  }
464 
466 } // namespace zypp
void releaseFile(MediaAccessId accessId, const Pathname &filename) const
FIXME: see MediaAccess class.
#define MIL
Definition: Logger.h:64
void provideDir(MediaAccessId accessId, const Pathname &dirname) const
FIXME: see MediaAccess class.
void provide(ProvideOperation op, const OnMediaLocation &resource, ProvideFileOptions options, const Pathname &deltafile)
void setQueryParam(const std::string &param, const std::string &value)
Set or add value for the specified query parameter.
Definition: Url.cc:832
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:350
Describes a path on a certain media amongs as the information required to download it...
Regular expression.
Definition: Regex.h:86
MediaMap _medias
Mapping between media number and Media Access ID.
Url _url
Media or media set URL.
Pathname provideOptionalFile(const Pathname &file, unsigned media_nr=1)
Provides an optional file from media media_nr.
void provideFile(MediaAccessId accessId, const Pathname &filename, const ByteCount &expectedFileSize) const
Provide provide file denoted by relative path below of the 'attach point' of the specified media and ...
Store and operate with byte count.
Definition: ByteCount.h:30
std::string getPathName(EEncoding eflag=zypp::url::E_DECODED) const
Returns the path name from the URL.
Definition: Url.cc:598
Pathname provideDir(const Pathname &dir, bool recursive, unsigned media_nr=1, ProvideFileOptions options=PROVIDE_DEFAULT)
Provides direcotry dir from media number media_nr.
unsigned medianr() const
media number where the resource is located.
void setDeltafile(MediaAccessId accessId, const Pathname &filename) const
IO error which can happen on worse connection like timeout exceed.
bool doesFileExist(const Pathname &file, unsigned media_nr=1)
Checks if a file exists on the specified media, with user callbacks.
Url url
Definition: MediaCurl.cc:207
Pathname _prefAttachPoint
Prefered mount point.
void release()
Release all attached media of this set.
static Url rewriteUrl(const Url &url_r, const media::MediaNr medianr)
Replaces media number in specified url with given medianr.
void dirInfo(MediaAccessId accessId, std::list< std::string > &retlist, const Pathname &dirname, bool dots=true) const
FIXME: see MediaAccess class.
virtual std::ostream & dumpOn(std::ostream &str) const
Overload to realize std::ostream & operator<<.
unsigned int MediaAccessId
Media manager access Id type.
Definition: MediaSource.h:29
void operator()(media::MediaAccessId media, const Pathname &file)
void releaseFile(const OnMediaLocation &resource)
Release file from media.
void release(MediaAccessId accessId, const std::string &ejectDev="")
Release the attached media and optionally eject.
bool doesFileExist(MediaAccessId accessId, const Pathname &filename) const
FIXME: see MediaAccess class.
#define ZYPP_RETHROW(EXCPT)
Drops a logline and rethrows, updating the CodeLocation.
Definition: Exception.h:358
void setPathName(const std::string &path, EEncoding eflag=zypp::url::E_DECODED)
Set the path name.
Definition: Url.cc:758
Do not differentiate case.
Definition: Regex.h:91
Just inherits Exception to separate media exceptions.
void dirInfo(filesystem::DirContent &retlist, const Pathname &dirname, bool dots=true, unsigned media_nr=1)
Fills retlist with directory information.
Pathname localPath(MediaAccessId accessId, const Pathname &pathname) const
Shortcut for 'localRoot() + pathname', but returns an empty pathname if media is not attached...
void getDetectedDevices(MediaAccessId accessId, std::vector< std::string > &devices, unsigned int &index) const
Fill in a vector of detected ejectable devices and the index of the currently attached device within ...
#define WAR
Definition: Logger.h:65
std::list< DirEntry > DirContent
Returned by readdir.
Definition: PathInfo.h:547
void addVerifier(MediaAccessId accessId, const MediaVerifierRef &verifier)
Add verifier implementation for the specified media id.
void operator()(media::MediaAccessId media, const Pathname &file)
std::string getQueryParam(const std::string &param, EEncoding eflag=zypp::url::E_DECODED) const
Return the value for the specified query parameter.
Definition: Url.cc:654
std::string numstring(char n, int w=0)
Definition: String.h:305
void attach(MediaAccessId accessId)
Attach the media using the concrete handler (checks all devices).
function< void(media::MediaAccessId, const Pathname &)> ProvideOperation
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
Definition: Exception.h:354
Regular expression match result.
Definition: Regex.h:145
Manages access to the 'physical' media, e.g CDROM drives, Disk volumes, directory trees...
Definition: MediaManager.h:473
Base class for Exception.
Definition: Exception.h:143
void operator()(media::MediaAccessId media, const Pathname &file)
void setVerifier(unsigned media_nr, media::MediaVerifierRef verifier)
Sets a MediaVerifier verifier for given media number.
callback::SendReport< DownloadProgressReport > * report
Definition: MediaCurl.cc:211
const Pathname & filename() const
The path to the resource relatve to the url and path.
MediaVerifierRef verifier
Wrapper for const correct access via Smart pointer types.
Definition: PtrTypes.h:285
media::MediaAccessId getMediaAccessId(media::MediaNr medianr)
std::string asUserHistory() const
A single (multiline) string composed of asUserString and historyAsString.
Definition: Exception.cc:91
bool regex_match(const std::string &s, smatch &matches, const regex &regex)
regex ZYPP_STR_REGEX regex ZYPP_STR_REGEX
Definition: Regex.h:70
void provideDirTree(MediaAccessId accessId, const Pathname &dirname) const
FIXME: see MediaAccess class.
const bool optional() const
whether this is an optional resource.
bool isAttached(MediaAccessId accessId) const
Check if media is attached or not.
void operator()(media::MediaAccessId media, const Pathname &file)
std::string getScheme() const
Returns the scheme name of the URL.
Definition: Url.cc:527
void releaseAll()
Release all attached media.
Pathname provideFile(const OnMediaLocation &resource, ProvideFileOptions options=PROVIDE_DEFAULT, const Pathname &deltafile=Pathname())
Provides a file from a media location.
MediaSetAccess(const Url &url, const Pathname &prefered_attach_point="")
Creates a callback enabled media access for specified url.
const ByteCount & downloadSize() const
The size of the resource on the server.
MediaAccessId open(const Url &url, const Pathname &preferred_attach_point="")
Opens the media access for specified with the url.
VerifierMap _verifiers
Mapping between media number and corespondent verifier.
Url manipulation class.
Definition: Url.h:87
void delVerifier(MediaAccessId accessId)
Remove verifier for specified media id.
OnMediaLocation & setLocation(const Pathname &val_r, unsigned mediaNumber_r=1)
Set filename and media number (defaults to 1).
#define IMPL_PTR_TYPE(NAME)
unsigned int MediaNr
Definition: MediaManager.h:40
void close(MediaAccessId accessId)
Close the media access with specified id.
#define DBG
Definition: Logger.h:63
The user is not asked anything, and the error exception is just propagated.