libzypp 17.37.10

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 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
191 MediaVerifierBase::info() const
192 {
193 return std::string(typeid((*this)).name());
194 }
195
196
198 std::string
199 NoVerifier::info() const
200 {
201 return std::string("zypp::media::NoVerifier");
202 }
203
204
206 class MediaManager_Impl
207 {
208 private:
209 friend class MediaManager;
210
211 MediaAccessId last_accessid;
212 ManagedMediaMap mediaMap;
213
214 MediaManager_Impl()
215 : last_accessid(0)
216 {}
217
218 public:
219
220 MediaManager_Impl(const MediaManager_Impl &) = delete;
221 MediaManager_Impl(MediaManager_Impl &&) = delete;
222 MediaManager_Impl &operator=(const MediaManager_Impl &) = delete;
223 MediaManager_Impl &operator=(MediaManager_Impl &&) = delete;
224
225 ~MediaManager_Impl()
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
258 nextAccessId()
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 &
270 findMM(MediaAccessId accessId)
271 {
272 ManagedMediaMap::iterator it( mediaMap.find(accessId));
273 if( it == mediaMap.end())
274 {
275 ZYPP_THROW(MediaNotOpenException(
276 "Invalid media access id " + str::numstring(accessId)
277 ));
278 }
279 return it->second;
280 }
281
282 static inline time_t
283 getMountTableMTime()
284 {
285 return Mount::getMTime();
286 }
287
288 static inline MountEntries
289 getMountEntries()
290 {
291 return Mount::getEntries();
292 }
293
294 };
295
296
298 // STATIC
299 zypp::RW_pointer<MediaManager_Impl> MediaManager::m_impl;
300
301
303 MediaManager::MediaManager()
304 {
305 if( !m_impl)
306 {
307 m_impl.reset( new MediaManager_Impl());
308 }
309 }
310
311 // ---------------------------------------------------------------
312 MediaManager::~MediaManager()
313 {
314 }
315
316 // ---------------------------------------------------------------
317 MediaAccessId
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
341 MediaManager::close(MediaAccessId accessId)
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 {
358 ZYPP_THROW(MediaIsSharedException(
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
375 MediaManager::isOpen(MediaAccessId accessId) const
376 {
377 ManagedMediaMap::iterator it( m_impl->mediaMap.find(accessId));
378 return it != m_impl->mediaMap.end();
379 }
380
381 // ---------------------------------------------------------------
382 std::string
383 MediaManager::protocol(MediaAccessId accessId) const
384 {
385 ManagedMedia &ref( m_impl->findMM(accessId));
386
387 return ref.handler().protocol();
388 }
389
390 // ---------------------------------------------------------------
391 bool
392 MediaManager::downloads(MediaAccessId accessId) const
393 {
394 ManagedMedia &ref( m_impl->findMM(accessId));
395
396 return ref.handler().downloads();
397 }
398
399 // ---------------------------------------------------------------
400 Url
401 MediaManager::url(MediaAccessId accessId) const
402 {
403 ManagedMedia &ref( m_impl->findMM(accessId));
404
405 return ref.handler().url();
406 }
407
408 // ---------------------------------------------------------------
409 void
410 MediaManager::addVerifier(MediaAccessId accessId,
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
427 MediaManager::delVerifier(MediaAccessId accessId)
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
441 MediaManager::setAttachPrefix(const Pathname &attach_prefix)
442 {
443 return MediaHandler::setAttachPrefix(attach_prefix);
444 }
445
446 // ---------------------------------------------------------------
447 void MediaManager::attach(MediaAccessId accessId)
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
559 MediaManager::releaseAll()
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
596 MediaManager::disconnect(MediaAccessId accessId)
597 {
598 ManagedMedia &ref( m_impl->findMM(accessId));
599
600 ref.handler().disconnect();
601 }
602
603 // ---------------------------------------------------------------
604 bool
605 MediaManager::isAttached(MediaAccessId accessId) const
606 {
607 ManagedMedia &ref( m_impl->findMM(accessId));
608
609 return ref.handler().isAttached();
610 }
611
612 // ---------------------------------------------------------------
613 bool MediaManager::isSharedMedia(MediaAccessId accessId) const
614 {
615 ManagedMedia &ref( m_impl->findMM(accessId));
616
617 return ref.handler().isSharedMedia();
618 }
619
620 // ---------------------------------------------------------------
621 bool
622 MediaManager::isDesiredMedia(MediaAccessId accessId) const
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
649 MediaManager::isDesiredMedia(MediaAccessId accessId,
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
678 MediaManager::isChangeable(MediaAccessId accessId)
679 {
680 return url(accessId).getScheme() == "cd" || url(accessId).getScheme() == "dvd";
681 }
682
683 // ---------------------------------------------------------------
684 Pathname
685 MediaManager::localRoot(MediaAccessId accessId) const
686 {
687 ManagedMedia &ref( m_impl->findMM(accessId));
688
689 Pathname path;
690 path = ref.handler().localRoot();
691 return path;
692 }
693
694 // ---------------------------------------------------------------
695 Pathname
696 MediaManager::localPath(MediaAccessId accessId,
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
707 MediaManager::provideFile(MediaAccessId accessId,
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
722 MediaManager::provideFile(MediaAccessId accessId,
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
744 MediaManager::setDeltafile(MediaAccessId accessId,
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
765 MediaManager::provideDir(MediaAccessId accessId,
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
777 MediaManager::provideDirTree(MediaAccessId accessId,
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
789 MediaManager::releaseFile(MediaAccessId accessId,
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
801 MediaManager::releaseDir(MediaAccessId accessId,
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
814 MediaManager::releasePath(MediaAccessId accessId,
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
826 MediaManager::dirInfo(MediaAccessId accessId,
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
841 MediaManager::dirInfo(MediaAccessId accessId,
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
868 MediaManager::getDetectedDevices(MediaAccessId accessId,
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
879 MediaManager::getMountTableMTime()
880 {
881 return MediaManager_Impl::getMountTableMTime();
882 }
883
884 // ---------------------------------------------------------------
885 // STATIC
886 MountEntries
887 MediaManager::getMountEntries()
888 {
889 return MediaManager_Impl::getMountEntries();
890 }
891
892 // ---------------------------------------------------------------
893 bool
894 MediaManager::isUseableAttachPoint(const Pathname &path,
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 // ---------------------------------------------------------------
962 AttachedMedia
963 MediaManager::getAttachedMedia(MediaAccessId &accessId) const
964 {
965 ManagedMedia &ref( m_impl->findMM(accessId));
966
967 return ref.handler().attachedMedia();
968 }
969
970 // ---------------------------------------------------------------
971 AttachedMedia
972 MediaManager::findAttachedMedia(const MediaSourceRef &media) const
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
992 MediaManager::forceReleaseShared(const MediaSourceRef &media)
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*/
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:153
zypp::MirroredOrigin
Manages a data source characterized by an authoritative URL and a list of mirror URLs.
Definition MirroredOrigin.h:170
zypp::MirroredOrigin::authority
const OriginEndpoint & authority() const
Definition MirroredOrigin.cc:184
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:120
zypp::OnMediaLocation::setDeltafile
OnMediaLocation & setDeltafile(Pathname path)
Set the deltafile.
Definition onmedialocation.cc:156
zypp::OriginEndpoint::url
const zypp::Url & url() const
Definition MirroredOrigin.cc:51
zypp::Url
Url manipulation class.
Definition Url.h:93
zypp::Url::asString
std::string asString() const
Returns a default string representation of the Url object.
Definition Url.cc:515
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 MirroredOrigin &origin, const Pathname &preferred_attach_point)
Definition MediaHandlerFactory.cc:54
zypp::media::MediaHandler::setAttachPrefix
static bool setAttachPrefix(const Pathname &attach_prefix)
Definition MediaHandler.cc:283
zypp::media::MediaIsSharedException
Definition mediaexception.h:414
zypp::media::MediaManager_Impl
Definition MediaManager.cc:207
zypp::media::MediaManager_Impl::operator=
MediaManager_Impl & operator=(const MediaManager_Impl &)=delete
zypp::media::MediaManager_Impl::nextAccessId
MediaAccessId nextAccessId()
Definition MediaManager.cc:258
zypp::media::MediaManager_Impl::MediaManager_Impl
MediaManager_Impl(const MediaManager_Impl &)=delete
zypp::media::MediaManager_Impl::last_accessid
MediaAccessId last_accessid
Definition MediaManager.cc:211
zypp::media::MediaManager_Impl::MediaManager
friend class MediaManager
Definition MediaManager.cc:209
zypp::media::MediaManager_Impl::getMountEntries
static MountEntries getMountEntries()
Definition MediaManager.cc:289
zypp::media::MediaManager_Impl::~MediaManager_Impl
~MediaManager_Impl()
Definition MediaManager.cc:225
zypp::media::MediaManager_Impl::MediaManager_Impl
MediaManager_Impl()
Definition MediaManager.cc:214
zypp::media::MediaManager_Impl::hasId
bool hasId(MediaAccessId accessId) const
Definition MediaManager.cc:264
zypp::media::MediaManager_Impl::findMM
ManagedMedia & findMM(MediaAccessId accessId)
Definition MediaManager.cc:270
zypp::media::MediaManager_Impl::getMountTableMTime
static time_t getMountTableMTime()
Definition MediaManager.cc:283
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:212
zypp::media::MediaManager::setDeltafile
ZYPP_DEPRECATED void setDeltafile(MediaAccessId accessId, const Pathname &filename) const
Definition MediaManager.cc:744
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:318
zypp::media::MediaManager::delVerifier
void delVerifier(MediaAccessId accessId)
Remove verifier for specified media id.
Definition MediaManager.cc:427
zypp::media::MediaManager::releaseFile
void releaseFile(MediaAccessId accessId, const Pathname &filename) const
FIXME: see MediaAccess class.
Definition MediaManager.cc:789
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:678
zypp::media::MediaManager::forceReleaseShared
void forceReleaseShared(const MediaSourceRef &media)
Definition MediaManager.cc:992
zypp::media::MediaManager::releaseAll
void releaseAll()
Release all attached media.
Definition MediaManager.cc:559
zypp::media::MediaManager::m_impl
static zypp::RW_pointer< MediaManager_Impl > m_impl
Static reference to the implementation (singleton).
Definition MediaManager.h:946
zypp::media::MediaManager::disconnect
void disconnect(MediaAccessId accessId)
Disconnect a remote media.
Definition MediaManager.cc:596
zypp::media::MediaManager::attach
void attach(MediaAccessId accessId)
Attach the media using the concrete handler (checks all devices).
Definition MediaManager.cc:447
zypp::media::MediaManager::releaseDir
void releaseDir(MediaAccessId accessId, const Pathname &dirname) const
FIXME: see MediaAccess class.
Definition MediaManager.cc:801
zypp::media::MediaManager::isOpen
bool isOpen(MediaAccessId accessId) const
Query if the media access is open / exists.
Definition MediaManager.cc:375
zypp::media::MediaManager::releasePath
void releasePath(MediaAccessId accessId, const Pathname &pathname) const
FIXME: see MediaAccess class.
Definition MediaManager.cc:814
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:826
zypp::media::MediaManager::close
void close(MediaAccessId accessId)
Close the media access with specified id.
Definition MediaManager.cc:341
zypp::media::MediaManager::url
Url url(MediaAccessId accessId) const
Returns the primary Media Access Url of the media access id.
Definition MediaManager.cc:401
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:622
zypp::media::MediaManager::provideFile
ZYPP_DEPRECATED void provideFile(MediaAccessId accessId, const Pathname &filename, const ByteCount &expectedFileSize) const
Definition MediaManager.cc:707
zypp::media::MediaManager::release
void release(MediaAccessId accessId, const std::string &ejectDev="")
Release the attached media and optionally eject.
Definition MediaManager.cc:516
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:754
zypp::media::MediaManager::isAttached
bool isAttached(MediaAccessId accessId) const
Check if media is attached or not.
Definition MediaManager.cc:605
zypp::media::MediaManager::getAttachedMedia
AttachedMedia getAttachedMedia(MediaAccessId &accessId) const
Definition MediaManager.cc:963
zypp::media::MediaManager::getMountTableMTime
static time_t getMountTableMTime()
Get the modification time of the /etc/mtab file.
Definition MediaManager.cc:879
zypp::media::MediaManager::~MediaManager
~MediaManager()
Destroys MediaManager envelope instance.
Definition MediaManager.cc:312
zypp::media::MediaManager::isSharedMedia
bool isSharedMedia(MediaAccessId accessId) const
Returns information if media is on a shared physical device or not.
Definition MediaManager.cc:613
zypp::media::MediaManager::protocol
std::string protocol(MediaAccessId accessId) const
Query the protocol name used by the media access handler.
Definition MediaManager.cc:383
zypp::media::MediaManager::doesFileExist
bool doesFileExist(MediaAccessId accessId, const Pathname &filename) const
FIXME: see MediaAccess class.
Definition MediaManager.cc:856
zypp::media::MediaManager::provideDir
void provideDir(MediaAccessId accessId, const Pathname &dirname) const
FIXME: see MediaAccess class.
Definition MediaManager.cc:765
zypp::media::MediaManager::findAttachedMedia
AttachedMedia findAttachedMedia(const MediaSourceRef &media) const
Definition MediaManager.cc:972
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:894
zypp::media::MediaManager::MediaManager
MediaManager()
Creates a MediaManager envelope instance.
Definition MediaManager.cc:303
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:696
zypp::media::MediaManager::provideDirTree
void provideDirTree(MediaAccessId accessId, const Pathname &dirname) const
FIXME: see MediaAccess class.
Definition MediaManager.cc:777
zypp::media::MediaManager::addVerifier
void addVerifier(MediaAccessId accessId, const MediaVerifierRef &verifier)
Add verifier implementation for the specified media id.
Definition MediaManager.cc:410
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:441
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:868
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:685
zypp::media::MediaManager::downloads
bool downloads(MediaAccessId accessId) const
Hint if files are downloaded or not.
Definition MediaManager.cc:392
zypp::media::MediaManager::getMountEntries
static std::vector< MountEntry > getMountEntries()
Get current mount entries from /etc/mtab file.
Definition MediaManager.cc:887
zypp::media::MediaNotDesiredException
Definition mediaexception.h:400
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:191
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:199
zypp::filesystem::DirContent
std::list< DirEntry > DirContent
Returned by readdir.
Definition PathInfo.h:526
zypp::media
Definition MediaNetworkRequestExecutor.h:22
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:290
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:479
ZYPP_CAUGHT
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
Definition Exception.h:475
ZYPP_THROW
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition Exception.h:459
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