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