17 #include <zypp-media/MediaException> 21 #include <zypp-media/Mount> 23 #include <zypp/base/String.h> 24 #include <zypp/base/Logger.h> 25 #include <zypp/Pathname.h> 26 #include <zypp/PathInfo.h> 41 std::ostream &
operator<<( std::ostream &
str,
const ManagedMedia & obj );
56 ManagedMedia( ManagedMedia &&m )
62 static ManagedMedia makeManagedMedia (
const Url &o_url,
const Pathname &preferred_attach_point,
const MediaVerifierRef &v )
66 ERR <<
"Failed to create media handler" << std::endl;
67 ZYPP_THROW( MediaSystemException(o_url,
"Failed to create media handler"));
69 return ManagedMedia( std::move(handler), v );
72 ManagedMedia &operator= ( ManagedMedia &&other ) =
default;
74 operator bool ()
const {
78 inline MediaHandler &handler() {
80 ZYPP_THROW(MediaNotOpenException(
"Accessing ManagedMedia after it was closed"));
84 inline const MediaHandler &handler()
const {
86 ZYPP_THROW(MediaNotOpenException(
"Accessing ManagedMedia after it was closed"));
90 std::ostream &
dumpOn( std::ostream &
str )
const {
92 return str <<
"ManagedMedia( closed )";
108 catch (
const MediaException & excpt_r)
111 WAR <<
"Close: " << *
this <<
" (" << excpt_r <<
")" << std::endl;
114 MIL <<
"Close: " << *
this <<
" (OK)" << std::endl;
120 if( !handler().isAttached())
122 DBG <<
"checkAttached(" <<
id <<
") not attached" << std::endl;
136 const auto &hdl = handler();
142 media::MediaNotDesiredException newEx ( hdl.url() );
149 DBG <<
"checkDesired(" <<
id <<
"): not desired (report by " <<
verifier->info() <<
")" << std::endl;
150 ZYPP_THROW( MediaNotDesiredException( hdl.url() ) );
153 DBG <<
"checkDesired(" <<
id <<
"): desired (report by " <<
verifier->info() <<
")" << std::endl;
155 DBG <<
"checkDesired(" <<
id <<
"): desired (cached)" << std::endl;
173 std::ostream &
operator<<( std::ostream &
str,
const ManagedMedia & obj ) {
174 return obj.dumpOn(
str );
178 typedef std::map<MediaAccessId, ManagedMedia> ManagedMediaMap;
189 return std::string(
typeid((*
this)).name());
197 return std::string(
"zypp::media::NoVerifier");
220 ManagedMediaMap::iterator it;
227 if( it->second && it->second.handler().dependsOnParent() )
232 it->second.handler().resetParentId();
259 inline ManagedMedia &
262 ManagedMediaMap::iterator it(
mediaMap.find(accessId));
278 WAR <<
"Failed to retrieve modification time of '/etc/mtab'" 284 static inline MountEntries
287 return Mount::getEntries();
318 ManagedMedia tmp = ManagedMedia::makeManagedMedia(
url, preferred_attach_point,
verifier );
322 m_impl->mediaMap.insert( std::make_pair( nextId, std::move(tmp) ) );
325 DBG <<
"Opened new media access using id " << nextId
344 ManagedMediaMap::iterator m(
m_impl->mediaMap.begin());
345 for( ; m !=
m_impl->mediaMap.end(); ++m)
347 if( m->second.handler().dependsOnParent(accessId,
true))
350 m->second.handler().url().asString()
355 DBG <<
"Close to access handler using id " 356 << accessId <<
" requested" << std::endl;
358 ManagedMedia &ref(
m_impl->findMM(accessId));
361 m_impl->mediaMap.erase(accessId);
368 ManagedMediaMap::iterator it(
m_impl->mediaMap.find(accessId));
369 return it !=
m_impl->mediaMap.end();
376 ManagedMedia &ref(
m_impl->findMM(accessId));
378 return ref.handler().protocol();
385 ManagedMedia &ref(
m_impl->findMM(accessId));
387 return ref.handler().downloads();
394 ManagedMedia &ref(
m_impl->findMM(accessId));
396 return ref.handler().url();
405 ZYPP_THROW(MediaException(
"Invalid verifier reference"));
407 ManagedMedia &ref(
m_impl->findMM(accessId));
412 DBG <<
"MediaVerifier change: id=" << accessId <<
", verifier=" 420 ManagedMedia &ref(
m_impl->findMM(accessId));
426 DBG <<
"MediaVerifier change: id=" << accessId <<
", verifier=" 440 ManagedMedia &ref(
m_impl->findMM(accessId));
441 auto &hdl = ref.handler();
443 DBG <<
"attach(id=" << accessId <<
")" << std::endl;
449 ref.checkDesired(accessId);
452 catch (
const MediaException & ex)
456 if (!hdl.hasMoreDevices())
459 if (hdl.isAttached())
463 MIL <<
"checkDesired(" << accessId <<
") of first device failed," 464 " going to try others with attach(true)" << std::endl;
466 while (hdl.hasMoreDevices())
472 ref.checkDesired(accessId);
475 catch (
const MediaNotDesiredException & ex)
479 if (!hdl.hasMoreDevices())
481 MIL <<
"No desired media found after trying all detected devices." << std::endl;
486 DBG <<
"Skipping " << media.mediaSource->asString() <<
": not desired media." << std::endl;
490 catch (
const MediaException & ex)
494 if (!hdl.hasMoreDevices())
498 DBG <<
"Skipping " << media.mediaSource->asString() <<
" because of exception thrown by attach(true)" << std::endl;
500 if (hdl.isAttached()) hdl.release();
509 ManagedMedia &ref(
m_impl->findMM(accessId));
511 DBG <<
"release(id=" << accessId;
512 if (!ejectDev.empty())
513 DBG <<
", " << ejectDev;
514 DBG <<
")" << std::endl;
516 if(!ejectDev.empty())
524 ManagedMediaMap::iterator m(
m_impl->mediaMap.begin());
525 for( ; m !=
m_impl->mediaMap.end(); ++m)
527 auto &hdl = m->second.handler();
528 if( hdl.dependsOnParent(accessId,
false))
532 DBG <<
"Forcing release of handler depending on access id " 533 << accessId << std::endl;
534 m->second.desired =
false;
537 catch(
const MediaException &e)
545 ref.handler().release(ejectDev);
552 MIL <<
"Releasing all attached media" << std::endl;
553 auto releaseAction = [](
MediaAccessId mId_r, ManagedMedia & mManagedMedia_r,
bool ifDependsOnParent_r ) {
554 auto & hdl = mManagedMedia_r.handler();
555 if ( hdl.dependsOnParent() == ifDependsOnParent_r ) {
557 if ( hdl.isAttached() ) {
558 DBG <<
"Releasing media id " << mId_r << std::endl;
559 mManagedMedia_r.desired =
false;
563 DBG <<
"Media id " << mId_r <<
" not attached " << std::endl;
566 catch (
const MediaException & e ) {
568 ERR <<
"Failed to release media id " << mId_r << std::endl;
574 for (
auto & [ mId, mManagedMedia ] :
m_impl->mediaMap ) {
575 releaseAction( mId, mManagedMedia,
true );
578 for (
auto & [ mId, mManagedMedia ] :
m_impl->mediaMap ) {
579 releaseAction( mId, mManagedMedia,
false );
582 MIL <<
"Exit" << std::endl;
589 ManagedMedia &ref(
m_impl->findMM(accessId));
591 ref.handler().disconnect();
598 ManagedMedia &ref(
m_impl->findMM(accessId));
600 return ref.handler().isAttached();
606 ManagedMedia &ref(
m_impl->findMM(accessId));
608 return ref.handler().isSharedMedia();
615 ManagedMedia &ref(
m_impl->findMM(accessId));
617 if( !ref.handler().isAttached())
624 ref.desired = ref.verifier->isDesiredMedia( ref.handler() );
631 DBG <<
"isDesiredMedia(" << accessId <<
"): " 632 << (ref.desired ?
"" :
"not ")
633 <<
"desired (report by " 634 << ref.verifier->info() <<
")" << std::endl;
645 ZYPP_THROW(MediaException(
"Invalid verifier reference"));
647 ManagedMedia &ref(
m_impl->findMM(accessId));
650 if( ref.handler().isAttached())
653 desired = v->isDesiredMedia( ref.handler() );
660 DBG <<
"isDesiredMedia(" << accessId <<
"): " 662 <<
"desired (report by " 663 << v->info() <<
")" << std::endl;
678 ManagedMedia &ref(
m_impl->findMM(accessId));
681 path = ref.handler().localRoot();
690 ManagedMedia &ref(
m_impl->findMM(accessId));
693 path = ref.handler().localPath(pathname);
700 const ByteCount &expectedFileSize )
const 702 ManagedMedia &ref(
m_impl->findMM(accessId));
716 ManagedMedia &ref(
m_impl->findMM(accessId));
726 ManagedMedia &ref(
m_impl->findMM(accessId));
728 ref.checkDesired(accessId);
730 ref.handler().provideFile( file );
738 ManagedMedia &ref(
m_impl->findMM(accessId));
740 ref.checkDesired(accessId);
742 ref.deltafile = filename;
747 ManagedMedia &ref(
m_impl->findMM(accessId));
749 ref.checkDesired(accessId);
751 ref.handler().precacheFiles( files );
759 ManagedMedia &ref(
m_impl->findMM(accessId));
761 ref.checkDesired(accessId);
763 ref.handler().provideDir(dirname);
771 ManagedMedia &ref(
m_impl->findMM(accessId));
773 ref.checkDesired(accessId);
775 ref.handler().provideDirTree(dirname);
783 ManagedMedia &ref(
m_impl->findMM(accessId));
785 ref.checkAttached(accessId);
787 ref.handler().releaseFile(filename);
795 ManagedMedia &ref(
m_impl->findMM(accessId));
797 ref.checkAttached(accessId);
799 ref.handler().releaseDir(dirname);
808 ManagedMedia &ref(
m_impl->findMM(accessId));
810 ref.checkAttached(accessId);
812 ref.handler().releasePath(pathname);
818 std::list<std::string> &retlist,
822 ManagedMedia &ref(
m_impl->findMM(accessId));
825 ref.checkAttached(accessId);
827 ref.handler().dirInfo(retlist, dirname, dots);
837 ManagedMedia &ref(
m_impl->findMM(accessId));
840 ref.checkAttached(accessId);
842 ref.handler().dirInfo(retlist, dirname, dots);
849 ManagedMedia &ref(
m_impl->findMM(accessId));
852 ref.checkAttached(accessId);
854 return ref.handler().doesFileExist(filename);
860 std::vector<std::string> & devices,
861 unsigned int & index)
const 863 ManagedMedia &ref(
m_impl->findMM(accessId));
864 return ref.handler().getDetectedDevices(devices, index);
894 ManagedMediaMap::const_iterator m(
m_impl->mediaMap.begin());
895 for( ; m !=
m_impl->mediaMap.end(); ++m)
909 if( mnt.size() > our.size() &&
910 mnt.at(our.size()) ==
'/' &&
911 !mnt.compare(0, our.size(), our))
926 MountEntries entries(
m_impl->getMountEntries());
927 MountEntries::const_iterator e;
928 for( e = entries.begin(); e != entries.end(); ++e)
939 if( mnt.size() > our.size() &&
940 mnt.at(our.size()) ==
'/' &&
941 !mnt.compare(0, our.size(), our))
956 ManagedMedia &ref(
m_impl->findMM(accessId));
958 return ref.handler().attachedMedia();
965 if( !media || media->type.empty())
968 ManagedMediaMap::const_iterator m(
m_impl->mediaMap.begin());
969 for( ; m !=
m_impl->mediaMap.end(); ++m)
971 if( !m->second.handler().isAttached())
985 if( !media || media->type.empty())
988 ManagedMediaMap::iterator m(
m_impl->mediaMap.begin());
989 for( ; m !=
m_impl->mediaMap.end(); ++m)
991 if( !m->second.handler().isAttached())
997 m->second.handler().release();
998 m->second.desired =
false;
std::string getScheme() const
Returns the scheme name of the URL.
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Store and operate with byte count.
String related utilities and Regular expression matching.
bool empty() const
Test for an empty path.
#define ZYPP_RETHROW(EXCPT)
Drops a logline and rethrows, updating the CodeLocation.
std::string asString() const
Returns a default string representation of the Url object.
const std::string & asString() const
String representation.
std::list< DirEntry > DirContent
Returned by readdir.
std::ostream & dumpOn(std::ostream &str, const Capability &obj)
std::string numstring(char n, int w=0)
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
Base class for Exception.
Wrapper for const correct access via Smart pointer types.
Wrapper class for ::stat/::lstat.
Easy-to use interface to the ZYPP dependency resolver.