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