libzypp  16.19.0
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 
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  release();
50  }
51  catch(...) {} // don't let exception escape a dtor.
52  }
53 
54 
56  {
57  if (_medias.find(media_nr) != _medias.end())
58  {
59  // the media already exists, set theverifier
60  media::MediaAccessId id = _medias[media_nr];
61  media::MediaManager media_mgr;
62  media_mgr.addVerifier( id, verifier );
63  // remove any saved verifier for this media
64  _verifiers.erase(media_nr);
65  }
66  else
67  {
68  // save the verifier in the map, and set it when
69  // the media number is first attached
70  _verifiers[media_nr] = verifier;
71  }
72  }
73 
74  void MediaSetAccess::releaseFile( const OnMediaLocation & on_media_file )
75  {
76  releaseFile( on_media_file.filename(), on_media_file.medianr() );
77  }
78 
79  void MediaSetAccess::releaseFile( const Pathname & file, unsigned media_nr)
80  {
81  media::MediaManager media_mgr;
83 
84  media = getMediaAccessId( media_nr);
85  DBG << "Going to release file " << file
86  << " from media number " << media_nr << endl;
87 
88  if ( ! media_mgr.isAttached(media) )
89  return; //disattached media is free
90 
91  media_mgr.releaseFile (media, file);
92  }
93 
94  void MediaSetAccess::dirInfo( filesystem::DirContent &retlist, const Pathname &dirname,
95  bool dots, unsigned media_nr )
96  {
97  media::MediaManager media_mgr;
99  media = getMediaAccessId(media_nr);
100 
101  // try to attach the media
102  if ( ! media_mgr.isAttached(media) )
103  media_mgr.attach(media);
104 
105  media_mgr.dirInfo(media, retlist, dirname, dots);
106  }
107 
109  {
110  Pathname result;
112  void operator()( media::MediaAccessId media, const Pathname &file )
113  {
114  media::MediaManager media_mgr;
115  media_mgr.provideFile(media, file, expectedFileSize);
116  result = media_mgr.localPath(media, file);
117  }
118  };
119 
121  {
122  Pathname result;
123  void operator()( media::MediaAccessId media, const Pathname &file )
124  {
125  media::MediaManager media_mgr;
126  media_mgr.provideDirTree(media, file);
127  result = media_mgr.localPath(media, file);
128  }
129  };
130 
132  {
133  Pathname result;
134  void operator()( media::MediaAccessId media, const Pathname &file )
135  {
136  media::MediaManager media_mgr;
137  media_mgr.provideDir(media, file);
138  result = media_mgr.localPath(media, file);
139  }
140  };
141 
143  {
144  bool result;
146  : result(false)
147  {}
148 
149  void operator()( media::MediaAccessId media, const Pathname &file )
150  {
151  media::MediaManager media_mgr;
152  result = media_mgr.doesFileExist(media, file);
153  }
154  };
155 
156 
157 
158  Pathname MediaSetAccess::provideFile( const OnMediaLocation & resource, ProvideFileOptions options, const Pathname &deltafile )
159  {
161  op.expectedFileSize = resource.downloadSize();
162  provide( boost::ref(op), resource, options, deltafile );
163  return op.result;
164  }
165 
166  Pathname MediaSetAccess::provideFile(const Pathname & file, unsigned media_nr, ProvideFileOptions options )
167  {
168  OnMediaLocation resource;
170  resource.setLocation(file, media_nr);
171  provide( boost::ref(op), resource, options, Pathname() );
172  return op.result;
173  }
174 
175  Pathname MediaSetAccess::provideOptionalFile( const Pathname & file, unsigned media_nr )
176  {
177  try
178  {
179  if ( doesFileExist( file, media_nr ) )
180  return provideFile( file, media_nr, PROVIDE_NON_INTERACTIVE );
181  }
182  catch ( const media::MediaFileNotFoundException & excpt_r )
183  { ZYPP_CAUGHT( excpt_r ); }
184  catch ( const media::MediaNotAFileException & excpt_r )
185  { ZYPP_CAUGHT( excpt_r ); }
186  return Pathname();
187  }
188 
189  bool MediaSetAccess::doesFileExist(const Pathname & file, unsigned media_nr )
190  {
192  OnMediaLocation resource;
193  resource.setLocation(file, media_nr);
194  provide( boost::ref(op), resource, PROVIDE_DEFAULT, Pathname());
195  return op.result;
196  }
197 
199  const OnMediaLocation &resource,
200  ProvideFileOptions options,
201  const Pathname &deltafile )
202  {
203  Pathname file(resource.filename());
204  unsigned media_nr(resource.medianr());
205 
207  media::MediaManager media_mgr;
208 
209  media::MediaAccessId media;
210 
211  do
212  {
213  // get the mediaId, but don't try to attach it here
214  media = getMediaAccessId( media_nr);
215  bool deltafileset = false;
216 
217  try
218  {
219  DBG << "Going to try to provide " << (resource.optional() ? "optional" : "") << " file " << file
220  << " from media number " << media_nr << endl;
221  // try to attach the media
222  if ( ! media_mgr.isAttached(media) )
223  media_mgr.attach(media);
224  media_mgr.setDeltafile(media, deltafile);
225  deltafileset = true;
226  op(media, file);
227  media_mgr.setDeltafile(media, Pathname());
228  break;
229  }
230  catch ( media::MediaException & excp )
231  {
232  ZYPP_CAUGHT(excp);
233  if (deltafileset)
234  media_mgr.setDeltafile(media, Pathname());
236  unsigned int devindex = 0;
237  vector<string> devices;
238  media_mgr.getDetectedDevices(media, devices, devindex);
239 
240  do
241  {
242  if (user != media::MediaChangeReport::EJECT) // no use in calling this again
243  {
244  DBG << "Media couldn't provide file " << file << " , releasing." << endl;
245  try
246  {
247  media_mgr.release(media);
248  }
249  catch (const Exception & excpt_r)
250  {
251  ZYPP_CAUGHT(excpt_r);
252  MIL << "Failed to release media " << media << endl;
253  }
254  }
255 
256  // set up the reason
258 
259  if( typeid(excp) == typeid( media::MediaFileNotFoundException ) ||
260  typeid(excp) == typeid( media::MediaNotAFileException ) )
261  {
263  }
264  else if( typeid(excp) == typeid( media::MediaNotDesiredException) ||
265  typeid(excp) == typeid( media::MediaNotAttachedException) )
266  {
268  }
269  else if( typeid(excp) == typeid( media::MediaTimeoutException) ||
270  typeid(excp) == typeid( media::MediaTemporaryProblemException))
271  {
273  }
274 
275  // Propagate the original error if _no_ callback receiver is connected, or
276  // non_interactive mode (for optional files) is used (except for wrong media).
278  || (( options & PROVIDE_NON_INTERACTIVE ) && reason != media::MediaChangeReport::WRONG ) )
279  {
280  MIL << "Can't provide file. Non-Interactive mode." << endl;
281  ZYPP_RETHROW(excp);
282  }
283  else
284  {
285  // release all media before requesting another (#336881)
286  media_mgr.releaseAll();
287 
288  user = report->requestMedia (
289  _url,
290  media_nr,
291  _label,
292  reason,
293  excp.asUserHistory(),
294  devices,
295  devindex
296  );
297  }
298 
299  MIL << "ProvideFile exception caught, callback answer: " << user << endl;
300 
301  if( user == media::MediaChangeReport::ABORT )
302  {
303  DBG << "Aborting" << endl;
304  AbortRequestException aexcp("Aborting requested by user");
305  aexcp.remember(excp);
306  ZYPP_THROW(aexcp);
307  }
308  else if ( user == media::MediaChangeReport::IGNORE )
309  {
310  DBG << "Skipping" << endl;
311  SkipRequestException nexcp("User-requested skipping of a file");
312  nexcp.remember(excp);
313  ZYPP_THROW(nexcp);
314  }
315  else if ( user == media::MediaChangeReport::EJECT )
316  {
317  DBG << "Eject: try to release" << endl;
318  try
319  {
320  media_mgr.releaseAll();
321  media_mgr.release (media, devindex < devices.size() ? devices[devindex] : "");
322  }
323  catch ( const Exception & e)
324  {
325  ZYPP_CAUGHT(e);
326  }
327  }
328  else if ( user == media::MediaChangeReport::RETRY ||
330  {
331  // retry
332  DBG << "Going to try again" << endl;
333  // invalidate current media access id
334  media_mgr.close(media);
335  _medias.erase(media_nr);
336 
337  // not attaching, media set will do that for us
338  // this could generate uncaught exception (#158620)
339  break;
340  }
341  else
342  {
343  DBG << "Don't know, let's ABORT" << endl;
344  ZYPP_RETHROW ( excp );
345  }
346  } while( user == media::MediaChangeReport::EJECT );
347  }
348 
349  // retry or change URL
350  } while( true );
351  }
352 
353  Pathname MediaSetAccess::provideDir(const Pathname & dir,
354  bool recursive,
355  unsigned media_nr,
356  ProvideFileOptions options )
357  {
358  OnMediaLocation resource;
359  resource.setLocation(dir, media_nr);
360  if ( recursive )
361  {
363  provide( boost::ref(op), resource, options, Pathname());
364  return op.result;
365  }
367  provide( boost::ref(op), resource, options, Pathname());
368  return op.result;
369  }
370 
372  {
373  if ( _medias.find( medianr ) != _medias.end() )
374  {
375  return _medias[medianr];
376  }
377 
378  Url url( medianr > 1 ? rewriteUrl( _url, medianr ) : _url );
379  media::MediaManager media_mgr;
380  media::MediaAccessId id = media_mgr.open( url, _prefAttachPoint );
381  _medias[medianr] = id;
382 
383  try
384  {
385  if ( _verifiers.find(medianr) != _verifiers.end() )
386  {
387  // a verifier is set for this media
388  // FIXME check the case where the verifier exists
389  // but we have no access id for the media
390  media_mgr.delVerifier( id );
391  media_mgr.addVerifier( id, _verifiers[medianr] );
392  // remove any saved verifier for this media
393  _verifiers.erase( medianr );
394  }
395  }
396  catch ( const Exception &e )
397  {
398  ZYPP_CAUGHT(e);
399  WAR << "Verifier not found" << endl;
400  }
401 
402  return id;
403  }
404 
405 
406  Url MediaSetAccess::rewriteUrl (const Url & url_r, const media::MediaNr medianr)
407  {
408  std::string scheme = url_r.getScheme();
409  if (scheme == "cd" || scheme == "dvd")
410  return url_r;
411 
412  DBG << "Rewriting url " << url_r << endl;
413 
414  if( scheme == "iso")
415  {
416  // TODO the iso parameter will not be required in the future, this
417  // code has to be adapted together with the MediaISO change.
418  // maybe some MediaISOURL interface should be used.
419  std::string isofile = url_r.getQueryParam("iso");
420  str::regex e("^(.*)(cd|dvd|media)[0-9]+\\.iso$", str::regex::icase);
421 
422  str::smatch what;
423  if(str::regex_match(isofile, what, e))
424  {
425  Url url( url_r);
426  isofile = what[1] + what[2] + str::numstring(medianr) + ".iso";
427  url.setQueryParam("iso", isofile);
428  DBG << "Url rewrite result: " << url << endl;
429  return url;
430  }
431  }
432  else
433  {
434  std::string pathname = url_r.getPathName();
435  str::regex e("^(.*)(cd|dvd|media)[0-9]+(/)?$", str::regex::icase);
436  str::smatch what;
437  if(str::regex_match(pathname, what, e))
438  {
439  Url url( url_r);
440  pathname = what[1] + what[2] + str::numstring(medianr) + what[3];
441  url.setPathName(pathname);
442  DBG << "Url rewrite result: " << url << endl;
443  return url;
444  }
445  }
446  return url_r;
447  }
448 
450  {
451  DBG << "Releasing all media IDs held by this MediaSetAccess" << endl;
452  media::MediaManager manager;
453  for (MediaMap::const_iterator m = _medias.begin(); m != _medias.end(); ++m)
454  manager.release(m->second, "");
455  }
456 
457  std::ostream & MediaSetAccess::dumpOn( std::ostream & str ) const
458  {
459  str << "MediaSetAccess (URL='" << _url << "', attach_point_hint='" << _prefAttachPoint << "')";
460  return str;
461  }
462 
464 } // 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 &#39;attach point&#39; 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.
String related utilities and Regular expression matching.
bool doesFileExist(const Pathname &file, unsigned media_nr=1)
Checks if a file exists on the specified media, with user callbacks.
Definition: Arch.h:344
Url url
Definition: MediaCurl.cc:199
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 &#39;localRoot() + pathname&#39;, 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 &#39;physical&#39; 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:203
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.
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
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.