libzypp 17.36.7

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>
19#include <zypp/media/MediaManager.h>
20#include <zypp/media/MediaHandlerFactory.h>
21#include <zypp/media/MediaHandler.h>
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;
162 MediaVerifierRef verifier;
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
189 MediaVerifierBase::info() const
190 {
191 return std::string(typeid((*this)).name());
192 }
193
194
196 std::string
197 NoVerifier::info() const
198 {
199 return std::string("zypp::media::NoVerifier");
200 }
201
202
204 class MediaManager_Impl
205 {
206 private:
207 friend class MediaManager;
208
209 MediaAccessId last_accessid;
210 ManagedMediaMap mediaMap;
211
212 MediaManager_Impl()
213 : last_accessid(0)
214 {}
215
216 public:
217
218 MediaManager_Impl(const MediaManager_Impl &) = delete;
219 MediaManager_Impl(MediaManager_Impl &&) = delete;
220 MediaManager_Impl &operator=(const MediaManager_Impl &) = delete;
221 MediaManager_Impl &operator=(MediaManager_Impl &&) = delete;
222
223 ~MediaManager_Impl()
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
256 nextAccessId()
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 &
268 findMM(MediaAccessId accessId)
269 {
270 ManagedMediaMap::iterator it( mediaMap.find(accessId));
271 if( it == mediaMap.end())
272 {
273 ZYPP_THROW(MediaNotOpenException(
274 "Invalid media access id " + str::numstring(accessId)
275 ));
276 }
277 return it->second;
278 }
279
280 static inline time_t
281 getMountTableMTime()
282 {
283 return Mount::getMTime();
284 }
285
286 static inline MountEntries
287 getMountEntries()
288 {
289 return Mount::getEntries();
290 }
291
292 };
293
294
296 // STATIC
297 zypp::RW_pointer<MediaManager_Impl> MediaManager::m_impl;
298
299
301 MediaManager::MediaManager()
302 {
303 if( !m_impl)
304 {
305 m_impl.reset( new MediaManager_Impl());
306 }
307 }
308
309 // ---------------------------------------------------------------
310 MediaManager::~MediaManager()
311 {
312 }
313
314 // ---------------------------------------------------------------
315 MediaAccessId
316 MediaManager::open(const Url &url, const Pathname &preferred_attach_point)
317 {
318 // create new access handler for it
319 MediaVerifierRef verifier( new NoVerifier());
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
334 MediaManager::close(MediaAccessId accessId)
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 {
351 ZYPP_THROW(MediaIsSharedException(
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
368 MediaManager::isOpen(MediaAccessId accessId) const
369 {
370 ManagedMediaMap::iterator it( m_impl->mediaMap.find(accessId));
371 return it != m_impl->mediaMap.end();
372 }
373
374 // ---------------------------------------------------------------
375 std::string
376 MediaManager::protocol(MediaAccessId accessId) const
377 {
378 ManagedMedia &ref( m_impl->findMM(accessId));
379
380 return ref.handler().protocol();
381 }
382
383 // ---------------------------------------------------------------
384 bool
385 MediaManager::downloads(MediaAccessId accessId) const
386 {
387 ManagedMedia &ref( m_impl->findMM(accessId));
388
389 return ref.handler().downloads();
390 }
391
392 // ---------------------------------------------------------------
393 Url
394 MediaManager::url(MediaAccessId accessId) const
395 {
396 ManagedMedia &ref( m_impl->findMM(accessId));
397
398 return ref.handler().url();
399 }
400
401 // ---------------------------------------------------------------
402 void
403 MediaManager::addVerifier(MediaAccessId accessId,
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
420 MediaManager::delVerifier(MediaAccessId accessId)
421 {
422 ManagedMedia &ref( m_impl->findMM(accessId));
423
424 MediaVerifierRef verifier( new NoVerifier());
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
434 MediaManager::setAttachPrefix(const Pathname &attach_prefix)
435 {
436 return MediaHandler::setAttachPrefix(attach_prefix);
437 }
438
439 // ---------------------------------------------------------------
440 void MediaManager::attach(MediaAccessId accessId)
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
552 MediaManager::releaseAll()
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
589 MediaManager::disconnect(MediaAccessId accessId)
590 {
591 ManagedMedia &ref( m_impl->findMM(accessId));
592
593 ref.handler().disconnect();
594 }
595
596 // ---------------------------------------------------------------
597 bool
598 MediaManager::isAttached(MediaAccessId accessId) const
599 {
600 ManagedMedia &ref( m_impl->findMM(accessId));
601
602 return ref.handler().isAttached();
603 }
604
605 // ---------------------------------------------------------------
606 bool MediaManager::isSharedMedia(MediaAccessId accessId) const
607 {
608 ManagedMedia &ref( m_impl->findMM(accessId));
609
610 return ref.handler().isSharedMedia();
611 }
612
613 // ---------------------------------------------------------------
614 bool
615 MediaManager::isDesiredMedia(MediaAccessId accessId) const
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
642 MediaManager::isDesiredMedia(MediaAccessId accessId,
643 const MediaVerifierRef &verifier) const
644 {
645 MediaVerifierRef v(verifier);
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
671 MediaManager::isChangeable(MediaAccessId accessId)
672 {
673 return url(accessId).getScheme() == "cd" || url(accessId).getScheme() == "dvd";
674 }
675
676 // ---------------------------------------------------------------
677 Pathname
678 MediaManager::localRoot(MediaAccessId accessId) const
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
689 MediaManager::localPath(MediaAccessId accessId,
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
700 MediaManager::provideFile(MediaAccessId accessId,
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
715 MediaManager::provideFile(MediaAccessId accessId,
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
737 MediaManager::setDeltafile(MediaAccessId accessId,
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
758 MediaManager::provideDir(MediaAccessId accessId,
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
770 MediaManager::provideDirTree(MediaAccessId accessId,
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
782 MediaManager::releaseFile(MediaAccessId accessId,
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
794 MediaManager::releaseDir(MediaAccessId accessId,
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
807 MediaManager::releasePath(MediaAccessId accessId,
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
819 MediaManager::dirInfo(MediaAccessId accessId,
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
834 MediaManager::dirInfo(MediaAccessId accessId,
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
861 MediaManager::getDetectedDevices(MediaAccessId accessId,
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
872 MediaManager::getMountTableMTime()
873 {
874 return MediaManager_Impl::getMountTableMTime();
875 }
876
877 // ---------------------------------------------------------------
878 // STATIC
879 MountEntries
880 MediaManager::getMountEntries()
881 {
882 return MediaManager_Impl::getMountEntries();
883 }
884
885 // ---------------------------------------------------------------
886 bool
887 MediaManager::isUseableAttachPoint(const Pathname &path,
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 // ---------------------------------------------------------------
955 AttachedMedia
956 MediaManager::getAttachedMedia(MediaAccessId &accessId) const
957 {
958 ManagedMedia &ref( m_impl->findMM(accessId));
959
960 return ref.handler().attachedMedia();
961 }
962
963 // ---------------------------------------------------------------
964 AttachedMedia
965 MediaManager::findAttachedMedia(const MediaSourceRef &media) const
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
985 MediaManager::forceReleaseShared(const MediaSourceRef &media)
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*/
MediaHandlerFactory.h
MediaHandler.h
MediaManager.h
zypp::ByteCount
Store and operate with byte count.
Definition ByteCount.h:32
zypp::Exception
Base class for Exception.
Definition Exception.h:147
zypp::OnMediaLocation
Describes a resource file located on a medium.
Definition onmedialocation.h:37
zypp::OnMediaLocation::setDownloadSize
OnMediaLocation & setDownloadSize(ByteCount val_r)
Set the downloadSize.
Definition onmedialocation.cc:118
zypp::OnMediaLocation::setDeltafile
OnMediaLocation & setDeltafile(Pathname path)
Set the deltafile.
Definition onmedialocation.cc:154
zypp::Url
Url manipulation class.
Definition Url.h:93
zypp::filesystem::PathInfo
Wrapper class for stat/lstat.
Definition PathInfo.h:226
zypp::filesystem::Pathname
Pathname.
Definition Pathname.h:47
zypp::filesystem::Pathname::asString
const std::string & asString() const
String representation.
Definition Pathname.h:93
zypp::filesystem::Pathname::empty
bool empty() const
Test for an empty path.
Definition Pathname.h:116
zypp::media::MediaException
Just inherits Exception to separate media exceptions.
Definition mediaexception.h:37
zypp::media::MediaHandlerFactory::createHandler
static std::unique_ptr< MediaHandler > createHandler(const Url &o_url, const Pathname &preferred_attach_point)
Definition MediaHandlerFactory.cc:50
zypp::media::MediaHandler::setAttachPrefix
static bool setAttachPrefix(const Pathname &attach_prefix)
Definition MediaHandler.cc:283
zypp::media::MediaIsSharedException
Definition mediaexception.h:410
zypp::media::MediaManager_Impl
Definition MediaManager.cc:205
zypp::media::MediaManager_Impl::operator=
MediaManager_Impl & operator=(const MediaManager_Impl &)=delete
zypp::media::MediaManager_Impl::nextAccessId
MediaAccessId nextAccessId()
Definition MediaManager.cc:256
zypp::media::MediaManager_Impl::MediaManager_Impl
MediaManager_Impl(const MediaManager_Impl &)=delete
zypp::media::MediaManager_Impl::last_accessid
MediaAccessId last_accessid
Definition MediaManager.cc:209
zypp::media::MediaManager_Impl::MediaManager
friend class MediaManager
Definition MediaManager.cc:207
zypp::media::MediaManager_Impl::getMountEntries
static MountEntries getMountEntries()
Definition MediaManager.cc:287
zypp::media::MediaManager_Impl::~MediaManager_Impl
~MediaManager_Impl()
Definition MediaManager.cc:223
zypp::media::MediaManager_Impl::MediaManager_Impl
MediaManager_Impl()
Definition MediaManager.cc:212
zypp::media::MediaManager_Impl::hasId
bool hasId(MediaAccessId accessId) const
Definition MediaManager.cc:262
zypp::media::MediaManager_Impl::findMM
ManagedMedia & findMM(MediaAccessId accessId)
Definition MediaManager.cc:268
zypp::media::MediaManager_Impl::getMountTableMTime
static time_t getMountTableMTime()
Definition MediaManager.cc:281
zypp::media::MediaManager_Impl::MediaManager_Impl
MediaManager_Impl(MediaManager_Impl &&)=delete
zypp::media::MediaManager_Impl::operator=
MediaManager_Impl & operator=(MediaManager_Impl &&)=delete
zypp::media::MediaManager_Impl::mediaMap
ManagedMediaMap mediaMap
Definition MediaManager.cc:210
zypp::media::MediaManager::setDeltafile
ZYPP_DEPRECATED void setDeltafile(MediaAccessId accessId, const Pathname &filename) const
Definition MediaManager.cc:737
zypp::media::MediaManager::open
MediaAccessId open(const Url &url, const Pathname &preferred_attach_point="")
Opens the media access for specified with the url.
Definition MediaManager.cc:316
zypp::media::MediaManager::delVerifier
void delVerifier(MediaAccessId accessId)
Remove verifier for specified media id.
Definition MediaManager.cc:420
zypp::media::MediaManager::releaseFile
void releaseFile(MediaAccessId accessId, const Pathname &filename) const
FIXME: see MediaAccess class.
Definition MediaManager.cc:782
zypp::media::MediaManager::isChangeable
bool isChangeable(MediaAccessId accessId)
Simple check, based on media's URL scheme, telling whether the it is possible to physically change th...
Definition MediaManager.cc:671
zypp::media::MediaManager::forceReleaseShared
void forceReleaseShared(const MediaSourceRef &media)
Definition MediaManager.cc:985
zypp::media::MediaManager::releaseAll
void releaseAll()
Release all attached media.
Definition MediaManager.cc:552
zypp::media::MediaManager::m_impl
static zypp::RW_pointer< MediaManager_Impl > m_impl
Static reference to the implementation (singleton).
Definition MediaManager.h:925
zypp::media::MediaManager::disconnect
void disconnect(MediaAccessId accessId)
Disconnect a remote media.
Definition MediaManager.cc:589
zypp::media::MediaManager::attach
void attach(MediaAccessId accessId)
Attach the media using the concrete handler (checks all devices).
Definition MediaManager.cc:440
zypp::media::MediaManager::releaseDir
void releaseDir(MediaAccessId accessId, const Pathname &dirname) const
FIXME: see MediaAccess class.
Definition MediaManager.cc:794
zypp::media::MediaManager::isOpen
bool isOpen(MediaAccessId accessId) const
Query if the media access is open / exists.
Definition MediaManager.cc:368
zypp::media::MediaManager::releasePath
void releasePath(MediaAccessId accessId, const Pathname &pathname) const
FIXME: see MediaAccess class.
Definition MediaManager.cc:807
zypp::media::MediaManager::dirInfo
void dirInfo(MediaAccessId accessId, std::list< std::string > &retlist, const Pathname &dirname, bool dots=true) const
FIXME: see MediaAccess class.
Definition MediaManager.cc:819
zypp::media::MediaManager::close
void close(MediaAccessId accessId)
Close the media access with specified id.
Definition MediaManager.cc:334
zypp::media::MediaManager::url
Url url(MediaAccessId accessId) const
Returns the Media Access Url of the media access id.
Definition MediaManager.cc:394
zypp::media::MediaManager::isDesiredMedia
bool isDesiredMedia(MediaAccessId accessId) const
Ask the registered verifier if the attached media is the desired one or not.
Definition MediaManager.cc:615
zypp::media::MediaManager::provideFile
ZYPP_DEPRECATED void provideFile(MediaAccessId accessId, const Pathname &filename, const ByteCount &expectedFileSize) const
Definition MediaManager.cc:700
zypp::media::MediaManager::release
void release(MediaAccessId accessId, const std::string &ejectDev="")
Release the attached media and optionally eject.
Definition MediaManager.cc:509
zypp::media::MediaManager::precacheFiles
void precacheFiles(MediaAccessId accessId, const std::vector< OnMediaLocation > &files)
Tries to fetch the given files and precaches them.
Definition MediaManager.cc:747
zypp::media::MediaManager::isAttached
bool isAttached(MediaAccessId accessId) const
Check if media is attached or not.
Definition MediaManager.cc:598
zypp::media::MediaManager::getAttachedMedia
AttachedMedia getAttachedMedia(MediaAccessId &accessId) const
Definition MediaManager.cc:956
zypp::media::MediaManager::getMountTableMTime
static time_t getMountTableMTime()
Get the modification time of the /etc/mtab file.
Definition MediaManager.cc:872
zypp::media::MediaManager::~MediaManager
~MediaManager()
Destroys MediaManager envelope instance.
Definition MediaManager.cc:310
zypp::media::MediaManager::isSharedMedia
bool isSharedMedia(MediaAccessId accessId) const
Returns information if media is on a shared physical device or not.
Definition MediaManager.cc:606
zypp::media::MediaManager::protocol
std::string protocol(MediaAccessId accessId) const
Query the protocol name used by the media access handler.
Definition MediaManager.cc:376
zypp::media::MediaManager::doesFileExist
bool doesFileExist(MediaAccessId accessId, const Pathname &filename) const
FIXME: see MediaAccess class.
Definition MediaManager.cc:849
zypp::media::MediaManager::provideDir
void provideDir(MediaAccessId accessId, const Pathname &dirname) const
FIXME: see MediaAccess class.
Definition MediaManager.cc:758
zypp::media::MediaManager::findAttachedMedia
AttachedMedia findAttachedMedia(const MediaSourceRef &media) const
Definition MediaManager.cc:965
zypp::media::MediaManager::isUseableAttachPoint
bool isUseableAttachPoint(const Pathname &path, bool mtab=true) const
Check if the specified path is useable as attach point.
Definition MediaManager.cc:887
zypp::media::MediaManager::MediaManager
MediaManager()
Creates a MediaManager envelope instance.
Definition MediaManager.cc:301
zypp::media::MediaManager::localPath
Pathname localPath(MediaAccessId accessId, const Pathname &pathname) const
Shortcut for 'localRoot() + pathname', but returns an empty pathname if media is not attached.
Definition MediaManager.cc:689
zypp::media::MediaManager::provideDirTree
void provideDirTree(MediaAccessId accessId, const Pathname &dirname) const
FIXME: see MediaAccess class.
Definition MediaManager.cc:770
zypp::media::MediaManager::addVerifier
void addVerifier(MediaAccessId accessId, const MediaVerifierRef &verifier)
Add verifier implementation for the specified media id.
Definition MediaManager.cc:403
zypp::media::MediaManager::setAttachPrefix
bool setAttachPrefix(const Pathname &attach_prefix)
Set or resets the directory name, where the media manager handlers create their temporary attach poin...
Definition MediaManager.cc:434
zypp::media::MediaManager::getDetectedDevices
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 ...
Definition MediaManager.cc:861
zypp::media::MediaManager::localRoot
Pathname localRoot(MediaAccessId accessId) const
Return the local directory that corresponds to medias url, no matter if media isAttached or not.
Definition MediaManager.cc:678
zypp::media::MediaManager::downloads
bool downloads(MediaAccessId accessId) const
Hint if files are downloaded or not.
Definition MediaManager.cc:385
zypp::media::MediaManager::getMountEntries
static std::vector< MountEntry > getMountEntries()
Get current mount entries from /etc/mtab file.
Definition MediaManager.cc:880
zypp::media::MediaNotDesiredException
Definition mediaexception.h:396
zypp::media::MediaNotOpenException
Definition mediaexception.h:150
zypp::media::MediaVerifierBase::info
virtual std::string info() const
Returns a string with some info about the verifier.
Definition MediaManager.cc:189
zypp::media::Mount::getEntries
static MountEntries getEntries(const std::string &mtab="")
Return mount entries from /etc/mtab or /etc/fstab file.
Definition mount.cc:169
zypp::media::Mount::getMTime
static time_t getMTime()
Get the modification time of the /etc/mtab file.
Definition mount.cc:264
zypp::media::NoVerifier
Dummy default media verifier, which is always happy.
Definition MediaManager.h:78
zypp::media::NoVerifier::info
std::string info() const override
Returns the "zypp::media::NoVerifier" string.
Definition MediaManager.cc:197
zypp::filesystem::DirContent
std::list< DirEntry > DirContent
Returned by readdir.
Definition PathInfo.h:526
zypp::media
Definition MediaCD.cc:47
zypp::media::operator<<
std::ostream & operator<<(std::ostream &str, const MediaHandler &obj)
Definition MediaHandler.cc:1181
zypp::media::MediaAccessId
unsigned int MediaAccessId
Media manager access Id type.
Definition MediaSource.h:30
zypp::media::MediaSourceRef
zypp::RW_pointer< MediaSource > MediaSourceRef
Definition MediaSource.h:125
zypp::media::MediaVerifierRef
zypp::RW_pointer< MediaVerifierBase > MediaVerifierRef
A shared reference to the MediaVerifier implementation.
Definition MediaManager.h:107
zypp::sat::operator=
SolvableSpec & operator=(const SolvableSpec &)=default
zypp::str::numstring
std::string numstring(char n, int w=0)
Definition String.h:289
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition Application.cc:20
zypp::dumpOn
std::ostream & dumpOn(std::ostream &str, const Capability &obj)
Definition Capability.cc:580
zypp::RW_pointer
Wrapper for const correct access via Smart pointer types.
Definition PtrTypes.h:293
zypp::RW_pointer::swap
void swap(RW_pointer &rhs) noexcept
Definition PtrTypes.h:325
zypp::media::AttachedMedia
A simple structure containing references to a media source and its attach point.
Definition MediaSource.h:135
zypp::media::AttachedMedia::mediaSource
MediaSourceRef mediaSource
Definition MediaSource.h:145
zypp::media::AttachedMedia::attachPoint
AttachPointRef attachPoint
Definition MediaSource.h:146
ZYPP_RETHROW
#define ZYPP_RETHROW(EXCPT)
Drops a logline and rethrows, updating the CodeLocation.
Definition Exception.h:444
ZYPP_CAUGHT
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
Definition Exception.h:440
ZYPP_THROW
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition Exception.h:424
DBG
#define DBG
Definition Logger.h:99
MIL
#define MIL
Definition Logger.h:100
ERR
#define ERR
Definition Logger.h:102
WAR
#define WAR
Definition Logger.h:101
PathInfo.h
Pathname.h
Logger.h
String.h