libzypp  17.30.2
MediaManager.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #include <map>
13 #include <list>
14 #include <iostream>
15 #include <typeinfo>
16 
17 #include <zypp-media/MediaException>
21 #include <zypp-media/Mount>
22 
23 #include <zypp/base/String.h>
24 #include <zypp/base/Logger.h>
25 #include <zypp/Pathname.h>
26 #include <zypp/PathInfo.h>
27 
29 namespace zypp
30 {
31 
33  namespace media
34  {
35 
37  namespace // anonymous
38  {
39 
40  struct ManagedMedia;
41  std::ostream & operator<<( std::ostream & str, const ManagedMedia & obj );
42 
43  // -------------------------------------------------------------
44  struct ManagedMedia
45  {
46  ~ManagedMedia()
47  {
48  try
49  {
50  if ( _handler )
51  close(); // !!! make sure handler gets properly deleted.
52  }
53  catch(...) {}
54  }
55 
56  ManagedMedia( ManagedMedia &&m )
57  : desired ( m.desired )
58  , verifier( std::move(m.verifier) )
59  , _handler ( std::move(m._handler) )
60  {}
61 
62  static ManagedMedia makeManagedMedia ( const Url &o_url, const Pathname &preferred_attach_point, const MediaVerifierRef &v )
63  {
64  auto handler = MediaHandlerFactory::createHandler( o_url, preferred_attach_point );
65  if ( !handler ) {
66  ERR << "Failed to create media handler" << std::endl;
67  ZYPP_THROW( MediaSystemException(o_url, "Failed to create media handler"));
68  }
69  return ManagedMedia( std::move(handler), v );
70  }
71 
72  ManagedMedia &operator= ( ManagedMedia &&other ) = default;
73 
74  operator bool () const {
75  return ( _handler ? true : false );
76  }
77 
78  inline MediaHandler &handler() {
79  if ( !_handler )
80  ZYPP_THROW(MediaNotOpenException("Accessing ManagedMedia after it was closed"));
81  return *_handler;
82  }
83 
84  inline const MediaHandler &handler() const {
85  if ( !_handler )
86  ZYPP_THROW(MediaNotOpenException("Accessing ManagedMedia after it was closed"));
87  return *_handler;
88  }
89 
90  std::ostream & dumpOn( std::ostream & str ) const {
91  if ( !_handler )
92  return str << "ManagedMedia( closed )";
93 
94  str << _handler->protocol() << "(" << *_handler << ")";
95  return str;
96  }
97 
98  inline void close ()
99  {
101  // !!! make shure handler gets properly deleted.
102  // I.e. release attached media before deleting the handler.
104 
105  try {
106  handler().release();
107  }
108  catch (const MediaException & excpt_r)
109  {
110  ZYPP_CAUGHT(excpt_r);
111  WAR << "Close: " << *this << " (" << excpt_r << ")" << std::endl;
112  ZYPP_RETHROW(excpt_r);
113  }
114  MIL << "Close: " << *this << " (OK)" << std::endl;
115  }
116 
117  inline void
118  checkAttached(MediaAccessId id)
119  {
120  if( !handler().isAttached())
121  {
122  DBG << "checkAttached(" << id << ") not attached" << std::endl;
123  desired = false;
124  ZYPP_THROW(MediaNotAttachedException(
125  handler().url()
126  ));
127  }
128  }
129 
130  inline void checkDesired( MediaAccessId id )
131  {
132  checkAttached( id );
133 
134  if ( !desired )
135  {
136  const auto &hdl = handler();
137  try {
138  desired = verifier->isDesiredMedia( handler() );
139  } catch ( const zypp::Exception &e ) {
140  ZYPP_CAUGHT( e );
141 
142  media::MediaNotDesiredException newEx ( hdl.url() );
143  newEx.remember( e );
144  ZYPP_THROW( newEx );
145  }
146 
147  if( !desired )
148  {
149  DBG << "checkDesired(" << id << "): not desired (report by " << verifier->info() << ")" << std::endl;
150  ZYPP_THROW( MediaNotDesiredException( hdl.url() ) );
151  }
152 
153  DBG << "checkDesired(" << id << "): desired (report by " << verifier->info() << ")" << std::endl;
154  } else {
155  DBG << "checkDesired(" << id << "): desired (cached)" << std::endl;
156  }
157  }
158 
159  bool desired;
161  Pathname deltafile;
162 
163  private:
164  ManagedMedia( std::unique_ptr<MediaHandler> &&h, const MediaVerifierRef &v)
165  : desired (false)
166  , verifier(v)
167  , _handler ( std::move(h) )
168  {}
169 
170  std::unique_ptr<MediaHandler> _handler;
171  };
172 
173  std::ostream & operator<<( std::ostream & str, const ManagedMedia & obj ) {
174  return obj.dumpOn( str );
175  }
176 
177  // -------------------------------------------------------------
178  typedef std::map<MediaAccessId, ManagedMedia> ManagedMediaMap;
179 
181  } // anonymous
183 
184 
186  std::string
188  {
189  return std::string(typeid((*this)).name());
190  }
191 
192 
194  std::string
196  {
197  return std::string("zypp::media::NoVerifier");
198  }
199 
200 
203  {
204  private:
205  friend class MediaManager;
206 
208  ManagedMediaMap mediaMap;
209 
211  : last_accessid(0)
212  {}
213 
214  public:
216  {
217  try
218  {
219  // remove depending (iso) handlers first
220  ManagedMediaMap::iterator it;
221  bool found;
222  do
223  {
224  found = false;
225  for(it = mediaMap.begin(); it != mediaMap.end(); )
226  {
227  if( it->second && it->second.handler().dependsOnParent() )
228  {
229  found = true;
230  // let it forget its parent, we will
231  // destroy it later (in clear())...
232  it->second.handler().resetParentId();
233  it = mediaMap.erase( it ); // postfix! Incrementing before erase
234  } else {
235  ++it;
236  }
237  }
238  } while(found);
239 
240  // remove all other handlers
241  mediaMap.clear();
242  }
243  catch( ... )
244  {}
245  }
246 
247  inline MediaAccessId
249  {
250  return ++last_accessid;
251  }
252 
253  inline bool
254  hasId(MediaAccessId accessId) const
255  {
256  return mediaMap.find(accessId) != mediaMap.end();
257  }
258 
259  inline ManagedMedia &
261  {
262  ManagedMediaMap::iterator it( mediaMap.find(accessId));
263  if( it == mediaMap.end())
264  {
265  ZYPP_THROW(MediaNotOpenException(
266  "Invalid media access id " + str::numstring(accessId)
267  ));
268  }
269  return it->second;
270  }
271 
272  static inline time_t
274  {
275  time_t mtime = zypp::PathInfo("/etc/mtab").mtime();
276  if( mtime <= 0)
277  {
278  WAR << "Failed to retrieve modification time of '/etc/mtab'"
279  << std::endl;
280  }
281  return mtime;
282  }
283 
284  static inline MountEntries
286  {
287  return Mount::getEntries();
288  }
289 
290  };
291 
292 
294  // STATIC
296 
297 
300  {
301  if( !m_impl)
302  {
303  m_impl.reset( new MediaManager_Impl());
304  }
305  }
306 
307  // ---------------------------------------------------------------
309  {
310  }
311 
312  // ---------------------------------------------------------------
314  MediaManager::open(const Url &url, const Pathname &preferred_attach_point)
315  {
316  // create new access handler for it
318  ManagedMedia tmp = ManagedMedia::makeManagedMedia( url, preferred_attach_point, verifier );
319 
320  MediaAccessId nextId = m_impl->nextAccessId();
321 
322  m_impl->mediaMap.insert( std::make_pair( nextId, std::move(tmp) ) );
323  //m_impl->mediaMap[nextId] = std::move(tmp);
324 
325  DBG << "Opened new media access using id " << nextId
326  << " to " << url.asString() << std::endl;
327  return nextId;
328  }
329 
330  // ---------------------------------------------------------------
331  void
333  {
334  //
335  // The MediaISO handler internally requests an accessId
336  // of a "parent" handler providing the iso file.
337  // The parent handler accessId is private to MediaISO,
338  // but the attached media source may be shared reference.
339  // This means, that if the accessId exactly matches the
340  // parent handler id, close was used on uninitialized
341  // accessId variable (or the accessId was guessed) and
342  // the close request to this id will be rejected here.
343  //
344  ManagedMediaMap::iterator m(m_impl->mediaMap.begin());
345  for( ; m != m_impl->mediaMap.end(); ++m)
346  {
347  if( m->second.handler().dependsOnParent(accessId, true))
348  {
349  ZYPP_THROW(MediaIsSharedException(
350  m->second.handler().url().asString()
351  ));
352  }
353  }
354 
355  DBG << "Close to access handler using id "
356  << accessId << " requested" << std::endl;
357 
358  ManagedMedia &ref( m_impl->findMM(accessId));
359  ref.close();
360 
361  m_impl->mediaMap.erase(accessId);
362  }
363 
364  // ---------------------------------------------------------------
365  bool
367  {
368  ManagedMediaMap::iterator it( m_impl->mediaMap.find(accessId));
369  return it != m_impl->mediaMap.end();
370  }
371 
372  // ---------------------------------------------------------------
373  std::string
375  {
376  ManagedMedia &ref( m_impl->findMM(accessId));
377 
378  return ref.handler().protocol();
379  }
380 
381  // ---------------------------------------------------------------
382  bool
384  {
385  ManagedMedia &ref( m_impl->findMM(accessId));
386 
387  return ref.handler().downloads();
388  }
389 
390  // ---------------------------------------------------------------
391  Url
393  {
394  ManagedMedia &ref( m_impl->findMM(accessId));
395 
396  return ref.handler().url();
397  }
398 
399  // ---------------------------------------------------------------
400  void
402  const MediaVerifierRef &verifier)
403  {
404  if( !verifier)
405  ZYPP_THROW(MediaException("Invalid verifier reference"));
406 
407  ManagedMedia &ref( m_impl->findMM(accessId));
408 
409  ref.desired = false;
410  MediaVerifierRef(verifier).swap(ref.verifier);
411 
412  DBG << "MediaVerifier change: id=" << accessId << ", verifier="
413  << verifier->info() << std::endl;
414  }
415 
416  // ---------------------------------------------------------------
417  void
419  {
420  ManagedMedia &ref( m_impl->findMM(accessId));
421 
423  ref.desired = false;
424  ref.verifier.swap(verifier);
425 
426  DBG << "MediaVerifier change: id=" << accessId << ", verifier="
427  << verifier->info() << std::endl;
428  }
429 
430  // ---------------------------------------------------------------
431  bool
433  {
434  return MediaHandler::setAttachPrefix(attach_prefix);
435  }
436 
437  // ---------------------------------------------------------------
439  {
440  ManagedMedia &ref( m_impl->findMM(accessId));
441  auto &hdl = ref.handler();
442 
443  DBG << "attach(id=" << accessId << ")" << std::endl;
444 
445  // try first mountable/mounted device
446  hdl.attach(false);
447  try
448  {
449  ref.checkDesired(accessId);
450  return;
451  }
452  catch (const MediaException & ex)
453  {
454  ZYPP_CAUGHT(ex);
455 
456  if (!hdl.hasMoreDevices())
457  ZYPP_RETHROW(ex);
458 
459  if (hdl.isAttached())
460  hdl.release();
461  }
462 
463  MIL << "checkDesired(" << accessId << ") of first device failed,"
464  " going to try others with attach(true)" << std::endl;
465 
466  while (hdl.hasMoreDevices())
467  {
468  try
469  {
470  // try to attach next device
471  hdl.attach(true);
472  ref.checkDesired(accessId);
473  return;
474  }
475  catch (const MediaNotDesiredException & ex)
476  {
477  ZYPP_CAUGHT(ex);
478 
479  if (!hdl.hasMoreDevices())
480  {
481  MIL << "No desired media found after trying all detected devices." << std::endl;
482  ZYPP_RETHROW(ex);
483  }
484 
485  AttachedMedia media(hdl.attachedMedia());
486  DBG << "Skipping " << media.mediaSource->asString() << ": not desired media." << std::endl;
487 
488  hdl.release();
489  }
490  catch (const MediaException & ex)
491  {
492  ZYPP_CAUGHT(ex);
493 
494  if (!hdl.hasMoreDevices())
495  ZYPP_RETHROW(ex);
496 
497  AttachedMedia media(hdl.attachedMedia());
498  DBG << "Skipping " << media.mediaSource->asString() << " because of exception thrown by attach(true)" << std::endl;
499 
500  if (hdl.isAttached()) hdl.release();
501  }
502  }
503  }
504 
505  // ---------------------------------------------------------------
506  void
507  MediaManager::release(MediaAccessId accessId, const std::string & ejectDev)
508  {
509  ManagedMedia &ref( m_impl->findMM(accessId));
510 
511  DBG << "release(id=" << accessId;
512  if (!ejectDev.empty())
513  DBG << ", " << ejectDev;
514  DBG << ")" << std::endl;
515 
516  if(!ejectDev.empty())
517  {
518  //
519  // release MediaISO handlers, that are using the one
520  // specified with accessId, because it provides the
521  // iso file and it will disappear now (forced release
522  // with eject).
523  //
524  ManagedMediaMap::iterator m(m_impl->mediaMap.begin());
525  for( ; m != m_impl->mediaMap.end(); ++m)
526  {
527  auto &hdl = m->second.handler();
528  if( hdl.dependsOnParent(accessId, false))
529  {
530  try
531  {
532  DBG << "Forcing release of handler depending on access id "
533  << accessId << std::endl;
534  m->second.desired = false;
535  hdl.release();
536  }
537  catch(const MediaException &e)
538  {
539  ZYPP_CAUGHT(e);
540  }
541  }
542  }
543  }
544  ref.desired = false;
545  ref.handler().release(ejectDev);
546  }
547 
548  // ---------------------------------------------------------------
549  void
551  {
552  MIL << "Releasing all attached media" << std::endl;
553  auto releaseAction = []( MediaAccessId mId_r, ManagedMedia & mManagedMedia_r, bool ifDependsOnParent_r ) {
554  auto & hdl = mManagedMedia_r.handler();
555  if ( hdl.dependsOnParent() == ifDependsOnParent_r ) {
556  try {
557  if ( hdl.isAttached() ) {
558  DBG << "Releasing media id " << mId_r << std::endl;
559  mManagedMedia_r.desired = false;
560  hdl.release();
561  }
562  else {
563  DBG << "Media id " << mId_r << " not attached " << std::endl;
564  }
565  }
566  catch ( const MediaException & e ) {
567  ZYPP_CAUGHT(e);
568  ERR << "Failed to release media id " << mId_r << std::endl;
569  }
570  }
571  };
572 
573  // 1st pass releases any stacked mounts (ISO)
574  for ( auto & [ mId, mManagedMedia ] : m_impl->mediaMap ) {
575  releaseAction( mId, mManagedMedia, /*ifDependsOnParent*/true );
576  }
577  // 2nd pass releases all the rest
578  for ( auto & [ mId, mManagedMedia ] : m_impl->mediaMap ) {
579  releaseAction( mId, mManagedMedia, /*ifDependsOnParent*/false );
580  }
581 
582  MIL << "Exit" << std::endl;
583  }
584 
585  // ---------------------------------------------------------------
586  void
588  {
589  ManagedMedia &ref( m_impl->findMM(accessId));
590 
591  ref.handler().disconnect();
592  }
593 
594  // ---------------------------------------------------------------
595  bool
597  {
598  ManagedMedia &ref( m_impl->findMM(accessId));
599 
600  return ref.handler().isAttached();
601  }
602 
603  // ---------------------------------------------------------------
605  {
606  ManagedMedia &ref( m_impl->findMM(accessId));
607 
608  return ref.handler().isSharedMedia();
609  }
610 
611  // ---------------------------------------------------------------
612  bool
614  {
615  ManagedMedia &ref( m_impl->findMM(accessId));
616 
617  if( !ref.handler().isAttached())
618  {
619  ref.desired = false;
620  }
621  else
622  {
623  try {
624  ref.desired = ref.verifier->isDesiredMedia( ref.handler() );
625  }
626  catch(const zypp::Exception &e) {
627  ZYPP_CAUGHT(e);
628  ref.desired = false;
629  }
630  }
631  DBG << "isDesiredMedia(" << accessId << "): "
632  << (ref.desired ? "" : "not ")
633  << "desired (report by "
634  << ref.verifier->info() << ")" << std::endl;
635  return ref.desired;
636  }
637 
638  // ---------------------------------------------------------------
639  bool
641  const MediaVerifierRef &verifier) const
642  {
644  if( !v)
645  ZYPP_THROW(MediaException("Invalid verifier reference"));
646 
647  ManagedMedia &ref( m_impl->findMM(accessId));
648 
649  bool desired = false;
650  if( ref.handler().isAttached())
651  {
652  try {
653  desired = v->isDesiredMedia( ref.handler() );
654  }
655  catch(const zypp::Exception &e) {
656  ZYPP_CAUGHT(e);
657  desired = false;
658  }
659  }
660  DBG << "isDesiredMedia(" << accessId << "): "
661  << (desired ? "" : "not ")
662  << "desired (report by "
663  << v->info() << ")" << std::endl;
664  return desired;
665  }
666 
667  // ---------------------------------------------------------------
668  bool
670  {
671  return url(accessId).getScheme() == "cd" || url(accessId).getScheme() == "dvd";
672  }
673 
674  // ---------------------------------------------------------------
675  Pathname
677  {
678  ManagedMedia &ref( m_impl->findMM(accessId));
679 
680  Pathname path;
681  path = ref.handler().localRoot();
682  return path;
683  }
684 
685  // ---------------------------------------------------------------
686  Pathname
688  const Pathname & pathname) const
689  {
690  ManagedMedia &ref( m_impl->findMM(accessId));
691 
692  Pathname path;
693  path = ref.handler().localPath(pathname);
694  return path;
695  }
696 
697  void
699  const Pathname &filename,
700  const ByteCount &expectedFileSize ) const
701  {
702  ManagedMedia &ref( m_impl->findMM(accessId));
703 
704  auto loc = OnMediaLocation( filename )
705  .setDownloadSize( expectedFileSize )
706  .setDeltafile( ref.deltafile );
707 
708  provideFile( accessId, loc );
709  }
710 
711  // ---------------------------------------------------------------
712  void
714  const Pathname &filename ) const
715  {
716  ManagedMedia &ref( m_impl->findMM(accessId));
717 
718  auto loc = OnMediaLocation( filename )
719  .setDeltafile( ref.deltafile );
720 
721  provideFile( accessId, loc );
722  }
723 
724  void MediaManager::provideFile( MediaAccessId accessId, const OnMediaLocation &file ) const
725  {
726  ManagedMedia &ref( m_impl->findMM(accessId));
727 
728  ref.checkDesired(accessId);
729 
730  ref.handler().provideFile( file );
731  }
732 
733  // ---------------------------------------------------------------
734  void
736  const Pathname &filename ) const
737  {
738  ManagedMedia &ref( m_impl->findMM(accessId));
739 
740  ref.checkDesired(accessId);
741 
742  ref.deltafile = filename;
743  }
744 
745  void MediaManager::precacheFiles(MediaAccessId accessId, const std::vector<OnMediaLocation> &files)
746  {
747  ManagedMedia &ref( m_impl->findMM(accessId));
748 
749  ref.checkDesired(accessId);
750 
751  ref.handler().precacheFiles( files );
752  }
753 
754  // ---------------------------------------------------------------
755  void
757  const Pathname &dirname) const
758  {
759  ManagedMedia &ref( m_impl->findMM(accessId));
760 
761  ref.checkDesired(accessId);
762 
763  ref.handler().provideDir(dirname);
764  }
765 
766  // ---------------------------------------------------------------
767  void
769  const Pathname &dirname) const
770  {
771  ManagedMedia &ref( m_impl->findMM(accessId));
772 
773  ref.checkDesired(accessId);
774 
775  ref.handler().provideDirTree(dirname);
776  }
777 
778  // ---------------------------------------------------------------
779  void
781  const Pathname &filename) const
782  {
783  ManagedMedia &ref( m_impl->findMM(accessId));
784 
785  ref.checkAttached(accessId);
786 
787  ref.handler().releaseFile(filename);
788  }
789 
790  // ---------------------------------------------------------------
791  void
793  const Pathname &dirname) const
794  {
795  ManagedMedia &ref( m_impl->findMM(accessId));
796 
797  ref.checkAttached(accessId);
798 
799  ref.handler().releaseDir(dirname);
800  }
801 
802 
803  // ---------------------------------------------------------------
804  void
806  const Pathname &pathname) const
807  {
808  ManagedMedia &ref( m_impl->findMM(accessId));
809 
810  ref.checkAttached(accessId);
811 
812  ref.handler().releasePath(pathname);
813  }
814 
815  // ---------------------------------------------------------------
816  void
818  std::list<std::string> &retlist,
819  const Pathname &dirname,
820  bool dots) const
821  {
822  ManagedMedia &ref( m_impl->findMM(accessId));
823 
824  // FIXME: ref.checkDesired(accessId); ???
825  ref.checkAttached(accessId);
826 
827  ref.handler().dirInfo(retlist, dirname, dots);
828  }
829 
830  // ---------------------------------------------------------------
831  void
833  filesystem::DirContent &retlist,
834  const Pathname &dirname,
835  bool dots) const
836  {
837  ManagedMedia &ref( m_impl->findMM(accessId));
838 
839  // FIXME: ref.checkDesired(accessId); ???
840  ref.checkAttached(accessId);
841 
842  ref.handler().dirInfo(retlist, dirname, dots);
843  }
844 
845  // ---------------------------------------------------------------
846  bool
847  MediaManager::doesFileExist(MediaAccessId accessId, const Pathname & filename ) const
848  {
849  ManagedMedia &ref( m_impl->findMM(accessId));
850 
851  // FIXME: ref.checkDesired(accessId); ???
852  ref.checkAttached(accessId);
853 
854  return ref.handler().doesFileExist(filename);
855  }
856 
857  // ---------------------------------------------------------------
858  void
860  std::vector<std::string> & devices,
861  unsigned int & index) const
862  {
863  ManagedMedia &ref( m_impl->findMM(accessId));
864  return ref.handler().getDetectedDevices(devices, index);
865  }
866 
867  // ---------------------------------------------------------------
868  // STATIC
869  time_t
871  {
873  }
874 
875  // ---------------------------------------------------------------
876  // STATIC
877  MountEntries
879  {
881  }
882 
883  // ---------------------------------------------------------------
884  bool
886  bool mtab) const
887  {
888  if( path.empty() || path == "/" || !PathInfo(path).isDir())
889  return false;
890 
891  //
892  // check against our current attach points
893  //
894  ManagedMediaMap::const_iterator m(m_impl->mediaMap.begin());
895  for( ; m != m_impl->mediaMap.end(); ++m)
896  {
897  AttachedMedia ret = m->second.handler().attachedMedia();
898  if( ret.mediaSource && ret.attachPoint)
899  {
900  std::string mnt(ret.attachPoint->path.asString());
901  std::string our(path.asString());
902 
903  if( our == mnt)
904  {
905  // already used as attach point
906  return false;
907  }
908  else
909  if( mnt.size() > our.size() &&
910  mnt.at(our.size()) == '/' &&
911  !mnt.compare(0, our.size(), our))
912  {
913  // mountpoint is bellow of path
914  // (would hide the content)
915  return false;
916  }
917  }
918  }
919 
920  if( !mtab)
921  return true;
922 
923  //
924  // check against system mount entries
925  //
926  MountEntries entries( m_impl->getMountEntries());
927  MountEntries::const_iterator e;
928  for( e = entries.begin(); e != entries.end(); ++e)
929  {
930  std::string mnt(Pathname(e->dir).asString());
931  std::string our(path.asString());
932 
933  if( our == mnt)
934  {
935  // already used as mountpoint
936  return false;
937  }
938  else
939  if( mnt.size() > our.size() &&
940  mnt.at(our.size()) == '/' &&
941  !mnt.compare(0, our.size(), our))
942  {
943  // mountpoint is bellow of path
944  // (would hide the content)
945  return false;
946  }
947  }
948 
949  return true;
950  }
951 
952  // ---------------------------------------------------------------
955  {
956  ManagedMedia &ref( m_impl->findMM(accessId));
957 
958  return ref.handler().attachedMedia();
959  }
960 
961  // ---------------------------------------------------------------
964  {
965  if( !media || media->type.empty())
966  return AttachedMedia();
967 
968  ManagedMediaMap::const_iterator m(m_impl->mediaMap.begin());
969  for( ; m != m_impl->mediaMap.end(); ++m)
970  {
971  if( !m->second.handler().isAttached())
972  continue;
973 
974  AttachedMedia ret = m->second.handler().attachedMedia();
975  if( ret.mediaSource && ret.mediaSource->equals( *media))
976  return ret;
977  }
978  return AttachedMedia();
979  }
980 
981  // ---------------------------------------------------------------
982  void
984  {
985  if( !media || media->type.empty())
986  return;
987 
988  ManagedMediaMap::iterator m(m_impl->mediaMap.begin());
989  for( ; m != m_impl->mediaMap.end(); ++m)
990  {
991  if( !m->second.handler().isAttached())
992  continue;
993 
994  AttachedMedia ret = m->second.handler().attachedMedia();
995  if( ret.mediaSource && ret.mediaSource->equals( *media))
996  {
997  m->second.handler().release();
998  m->second.desired = false;
999  }
1000  }
1001  }
1002 
1004  } // namespace media
1006 
1008 } // namespace zypp
1010 /*
1011 ** vim: set ts=2 sts=2 sw=2 ai et:
1012 */
std::unique_ptr< MediaHandler > _handler
MediaVerifierRef verifier
Pathname deltafile
bool desired
Store and operate with byte count.
Definition: ByteCount.h:31
Base class for Exception.
Definition: Exception.h:146
Describes a resource file located on a medium.
OnMediaLocation & setDownloadSize(ByteCount val_r)
Set the downloadSize.
OnMediaLocation & setDeltafile(Pathname path)
Set the deltafile.
Url manipulation class.
Definition: Url.h:92
std::string getScheme() const
Returns the scheme name of the URL.
Definition: Url.cc:533
std::string asString() const
Returns a default string representation of the Url object.
Definition: Url.cc:497
Wrapper class for ::stat/::lstat.
Definition: PathInfo.h:221
time_t mtime() const
Definition: PathInfo.h:376
const std::string & asString() const
String representation.
Definition: Pathname.h:91
bool empty() const
Test for an empty path.
Definition: Pathname.h:114
static std::unique_ptr< MediaHandler > createHandler(const Url &o_url, const Pathname &preferred_attach_point)
static bool setAttachPrefix(const Pathname &attach_prefix)
ManagedMedia & findMM(MediaAccessId accessId)
static MountEntries getMountEntries()
bool hasId(MediaAccessId accessId) const
static time_t getMountTableMTime()
Manages access to the 'physical' media, e.g CDROM drives, Disk volumes, directory trees,...
Definition: MediaManager.h:454
ZYPP_DEPRECATED void setDeltafile(MediaAccessId accessId, const Pathname &filename) const
MediaAccessId open(const Url &url, const Pathname &preferred_attach_point="")
Opens the media access for specified with the url.
void delVerifier(MediaAccessId accessId)
Remove verifier for specified media id.
void releaseFile(MediaAccessId accessId, const Pathname &filename) const
FIXME: see MediaAccess class.
bool isChangeable(MediaAccessId accessId)
Simple check, based on media's URL scheme, telling whether the it is possible to physically change th...
void forceReleaseShared(const MediaSourceRef &media)
void releaseAll()
Release all attached media.
static zypp::RW_pointer< MediaManager_Impl > m_impl
Static reference to the implementation (singleton).
Definition: MediaManager.h:925
void disconnect(MediaAccessId accessId)
Disconnect a remote media.
void attach(MediaAccessId accessId)
Attach the media using the concrete handler (checks all devices).
void releaseDir(MediaAccessId accessId, const Pathname &dirname) const
FIXME: see MediaAccess class.
bool isOpen(MediaAccessId accessId) const
Query if the media access is open / exists.
void releasePath(MediaAccessId accessId, const Pathname &pathname) const
FIXME: see MediaAccess class.
void dirInfo(MediaAccessId accessId, std::list< std::string > &retlist, const Pathname &dirname, bool dots=true) const
FIXME: see MediaAccess class.
void close(MediaAccessId accessId)
Close the media access with specified id.
Url url(MediaAccessId accessId) const
Returns the Media Access Url of the media access id.
bool isDesiredMedia(MediaAccessId accessId) const
Ask the registered verifier if the attached media is the desired one or not.
ZYPP_DEPRECATED void provideFile(MediaAccessId accessId, const Pathname &filename, const ByteCount &expectedFileSize) const
void release(MediaAccessId accessId, const std::string &ejectDev="")
Release the attached media and optionally eject.
void precacheFiles(MediaAccessId accessId, const std::vector< OnMediaLocation > &files)
Tries to fetch the given files and precaches them.
bool isAttached(MediaAccessId accessId) const
Check if media is attached or not.
AttachedMedia getAttachedMedia(MediaAccessId &accessId) const
static time_t getMountTableMTime()
Get the modification time of the /etc/mtab file.
~MediaManager()
Destroys MediaManager envelope instance.
bool isSharedMedia(MediaAccessId accessId) const
Returns information if media is on a shared physical device or not.
std::string protocol(MediaAccessId accessId) const
Query the protocol name used by the media access handler.
bool doesFileExist(MediaAccessId accessId, const Pathname &filename) const
FIXME: see MediaAccess class.
void provideDir(MediaAccessId accessId, const Pathname &dirname) const
FIXME: see MediaAccess class.
AttachedMedia findAttachedMedia(const MediaSourceRef &media) const
bool isUseableAttachPoint(const Pathname &path, bool mtab=true) const
Check if the specified path is useable as attach point.
MediaManager()
Creates a MediaManager envelope instance.
Pathname localPath(MediaAccessId accessId, const Pathname &pathname) const
Shortcut for 'localRoot() + pathname', but returns an empty pathname if media is not attached.
void provideDirTree(MediaAccessId accessId, const Pathname &dirname) const
FIXME: see MediaAccess class.
void addVerifier(MediaAccessId accessId, const MediaVerifierRef &verifier)
Add verifier implementation for the specified media id.
bool setAttachPrefix(const Pathname &attach_prefix)
Set or resets the directory name, where the media manager handlers create their temporary attach poin...
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 ...
Pathname localRoot(MediaAccessId accessId) const
Return the local directory that corresponds to medias url, no matter if media isAttached or not.
bool downloads(MediaAccessId accessId) const
Hint if files are downloaded or not.
static std::vector< MountEntry > getMountEntries()
Get current mount entries from /etc/mtab file.
virtual std::string info() const
Returns a string with some info about the verifier.
Dummy default media verifier, which is always happy.
Definition: MediaManager.h:78
std::string info() const override
Returns the "zypp::media::NoVerifier" string.
Definition: Arch.h:352
String related utilities and Regular expression matching.
std::list< DirEntry > DirContent
Returned by readdir.
Definition: PathInfo.h:518
zypp::RW_pointer< MediaVerifierBase > MediaVerifierRef
A shared reference to the MediaVerifier implementation.
Definition: MediaManager.h:107
std::ostream & operator<<(std::ostream &str, const MediaHandler &obj)
unsigned int MediaAccessId
Media manager access Id type.
Definition: MediaSource.h:29
std::string numstring(char n, int w=0)
Definition: String.h:289
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
std::ostream & dumpOn(std::ostream &str, const Capability &obj)
Definition: Capability.cc:567
Wrapper for const correct access via Smart pointer types.
Definition: PtrTypes.h:286
A simple structure containing references to a media source and its attach point.
Definition: MediaSource.h:134
MediaSourceRef mediaSource
Definition: MediaSource.h:144
AttachPointRef attachPoint
Definition: MediaSource.h:145
#define ZYPP_RETHROW(EXCPT)
Drops a logline and rethrows, updating the CodeLocation.
Definition: Exception.h:440
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
Definition: Exception.h:436
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:428
#define DBG
Definition: Logger.h:95
#define MIL
Definition: Logger.h:96
#define ERR
Definition: Logger.h:98
#define WAR
Definition: Logger.h:97