18#include <zypp-media/MediaException>
22#include <zypp-media/Mount>
42 std::ostream &
operator<<( std::ostream &
str,
const ManagedMedia & obj );
57 ManagedMedia(
const ManagedMedia &) =
delete;
58 ManagedMedia &
operator=(
const ManagedMedia &) =
delete;
60 ManagedMedia(ManagedMedia &&m) noexcept
64 static ManagedMedia makeManagedMedia (
const Url &o_url,
const Pathname &preferred_attach_point,
const MediaVerifierRef &v )
68 ERR <<
"Failed to create media handler" << std::endl;
69 ZYPP_THROW( MediaSystemException(o_url,
"Failed to create media handler"));
71 return ManagedMedia( std::move(handler), v );
74 ManagedMedia &operator= ( ManagedMedia &&other ) =
default;
76 operator bool ()
const {
77 return ( _handler ?
true :
false );
80 inline MediaHandler &handler() {
82 ZYPP_THROW(MediaNotOpenException(
"Accessing ManagedMedia after it was closed"));
86 inline const MediaHandler &handler()
const {
88 ZYPP_THROW(MediaNotOpenException(
"Accessing ManagedMedia after it was closed"));
92 std::ostream &
dumpOn( std::ostream &
str )
const {
94 return str <<
"ManagedMedia( closed )";
110 catch (
const MediaException & excpt_r)
113 WAR <<
"Close: " << *
this <<
" (" << excpt_r <<
")" << std::endl;
116 MIL <<
"Close: " << *
this <<
" (OK)" << std::endl;
122 if( !handler().isAttached())
124 DBG <<
"checkAttached(" <<
id <<
") not attached" << std::endl;
138 const auto &hdl = handler();
144 media::MediaNotDesiredException newEx ( hdl.url() );
151 DBG <<
"checkDesired(" <<
id <<
"): not desired (report by " <<
verifier->info() <<
")" << std::endl;
152 ZYPP_THROW( MediaNotDesiredException( hdl.url() ) );
155 DBG <<
"checkDesired(" <<
id <<
"): desired (report by " <<
verifier->info() <<
")" << std::endl;
157 DBG <<
"checkDesired(" <<
id <<
"): desired (cached)" << std::endl;
175 std::ostream &
operator<<( std::ostream &
str,
const ManagedMedia & obj ) {
176 return obj.dumpOn(
str );
180 using ManagedMediaMap = std::map<MediaAccessId, ManagedMedia>;
191 return std::string(
typeid((*
this)).name());
199 return std::string(
"zypp::media::NoVerifier");
228 ManagedMediaMap::iterator it;
235 if( it->second && it->second.handler().dependsOnParent() )
240 it->second.handler().resetParentId();
267 inline ManagedMedia &
270 ManagedMediaMap::iterator it(
mediaMap.find(accessId));
286 static inline MountEntries
320 ManagedMedia tmp = ManagedMedia::makeManagedMedia( url, preferred_attach_point,
verifier );
324 m_impl->mediaMap.insert( std::make_pair( nextId, std::move(tmp) ) );
327 DBG <<
"Opened new media access using id " << nextId
328 <<
" to " << url.
asString() << std::endl;
346 ManagedMediaMap::iterator m(
m_impl->mediaMap.begin());
347 for( ; m !=
m_impl->mediaMap.end(); ++m)
349 if( m->second.handler().dependsOnParent(accessId,
true))
352 m->second.handler().url().asString()
357 DBG <<
"Close to access handler using id "
358 << accessId <<
" requested" << std::endl;
360 ManagedMedia &ref(
m_impl->findMM(accessId));
363 m_impl->mediaMap.erase(accessId);
370 ManagedMediaMap::iterator it(
m_impl->mediaMap.find(accessId));
371 return it !=
m_impl->mediaMap.end();
378 ManagedMedia &ref(
m_impl->findMM(accessId));
380 return ref.handler().protocol();
387 ManagedMedia &ref(
m_impl->findMM(accessId));
389 return ref.handler().downloads();
396 ManagedMedia &ref(
m_impl->findMM(accessId));
398 return ref.handler().url();
409 ManagedMedia &ref(
m_impl->findMM(accessId));
414 DBG <<
"MediaVerifier change: id=" << accessId <<
", verifier="
422 ManagedMedia &ref(
m_impl->findMM(accessId));
428 DBG <<
"MediaVerifier change: id=" << accessId <<
", verifier="
442 ManagedMedia &ref(
m_impl->findMM(accessId));
443 auto &hdl = ref.handler();
445 DBG <<
"attach(id=" << accessId <<
")" << std::endl;
451 ref.checkDesired(accessId);
458 if (!hdl.hasMoreDevices())
461 if (hdl.isAttached())
465 MIL <<
"checkDesired(" << accessId <<
") of first device failed,"
466 " going to try others with attach(true)" << std::endl;
468 while (hdl.hasMoreDevices())
474 ref.checkDesired(accessId);
481 if (!hdl.hasMoreDevices())
483 MIL <<
"No desired media found after trying all detected devices." << std::endl;
488 DBG <<
"Skipping " << media.
mediaSource->asString() <<
": not desired media." << std::endl;
496 if (!hdl.hasMoreDevices())
500 DBG <<
"Skipping " << media.
mediaSource->asString() <<
" because of exception thrown by attach(true)" << std::endl;
502 if (hdl.isAttached()) hdl.release();
511 ManagedMedia &ref(
m_impl->findMM(accessId));
513 DBG <<
"release(id=" << accessId;
514 if (!ejectDev.empty())
515 DBG <<
", " << ejectDev;
516 DBG <<
")" << std::endl;
518 if(!ejectDev.empty())
526 ManagedMediaMap::iterator m(
m_impl->mediaMap.begin());
527 for( ; m !=
m_impl->mediaMap.end(); ++m)
529 auto &hdl = m->second.handler();
530 if( hdl.dependsOnParent(accessId,
false))
534 DBG <<
"Forcing release of handler depending on access id "
535 << accessId << std::endl;
536 m->second.desired =
false;
547 ref.handler().release(ejectDev);
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 ) {
559 if ( hdl.isAttached() ) {
560 DBG <<
"Releasing media id " << mId_r << std::endl;
561 mManagedMedia_r.desired =
false;
565 DBG <<
"Media id " << mId_r <<
" not attached " << std::endl;
570 ERR <<
"Failed to release media id " << mId_r << std::endl;
576 for (
auto & [ mId, mManagedMedia ] :
m_impl->mediaMap ) {
577 releaseAction( mId, mManagedMedia,
true );
580 for (
auto & [ mId, mManagedMedia ] :
m_impl->mediaMap ) {
581 releaseAction( mId, mManagedMedia,
false );
584 MIL <<
"Exit" << std::endl;
591 ManagedMedia &ref(
m_impl->findMM(accessId));
593 ref.handler().disconnect();
600 ManagedMedia &ref(
m_impl->findMM(accessId));
602 return ref.handler().isAttached();
608 ManagedMedia &ref(
m_impl->findMM(accessId));
610 return ref.handler().isSharedMedia();
617 ManagedMedia &ref(
m_impl->findMM(accessId));
619 if( !ref.handler().isAttached())
626 ref.desired = ref.verifier->isDesiredMedia( ref.handler() );
633 DBG <<
"isDesiredMedia(" << accessId <<
"): "
634 << (ref.desired ?
"" :
"not ")
635 <<
"desired (report by "
636 << ref.verifier->info() <<
")" << std::endl;
649 ManagedMedia &ref(
m_impl->findMM(accessId));
652 if( ref.handler().isAttached())
655 desired = v->isDesiredMedia( ref.handler() );
662 DBG <<
"isDesiredMedia(" << accessId <<
"): "
664 <<
"desired (report by "
665 << v->info() <<
")" << std::endl;
680 ManagedMedia &ref(
m_impl->findMM(accessId));
683 path = ref.handler().localRoot();
692 ManagedMedia &ref(
m_impl->findMM(accessId));
695 path = ref.handler().localPath(pathname);
702 const ByteCount &expectedFileSize )
const
704 ManagedMedia &ref(
m_impl->findMM(accessId));
718 ManagedMedia &ref(
m_impl->findMM(accessId));
728 ManagedMedia &ref(
m_impl->findMM(accessId));
730 ref.checkDesired(accessId);
732 ref.handler().provideFile( file );
740 ManagedMedia &ref(
m_impl->findMM(accessId));
742 ref.checkDesired(accessId);
744 ref.deltafile = filename;
749 ManagedMedia &ref(
m_impl->findMM(accessId));
751 ref.checkDesired(accessId);
753 ref.handler().precacheFiles( files );
761 ManagedMedia &ref(
m_impl->findMM(accessId));
763 ref.checkDesired(accessId);
765 ref.handler().provideDir(dirname);
773 ManagedMedia &ref(
m_impl->findMM(accessId));
775 ref.checkDesired(accessId);
777 ref.handler().provideDirTree(dirname);
785 ManagedMedia &ref(
m_impl->findMM(accessId));
787 ref.checkAttached(accessId);
789 ref.handler().releaseFile(filename);
797 ManagedMedia &ref(
m_impl->findMM(accessId));
799 ref.checkAttached(accessId);
801 ref.handler().releaseDir(dirname);
810 ManagedMedia &ref(
m_impl->findMM(accessId));
812 ref.checkAttached(accessId);
814 ref.handler().releasePath(pathname);
820 std::list<std::string> &retlist,
824 ManagedMedia &ref(
m_impl->findMM(accessId));
827 ref.checkAttached(accessId);
829 ref.handler().dirInfo(retlist, dirname, dots);
839 ManagedMedia &ref(
m_impl->findMM(accessId));
842 ref.checkAttached(accessId);
844 ref.handler().dirInfo(retlist, dirname, dots);
851 ManagedMedia &ref(
m_impl->findMM(accessId));
854 ref.checkAttached(accessId);
856 return ref.handler().doesFileExist(filename);
862 std::vector<std::string> & devices,
863 unsigned int & index)
const
865 ManagedMedia &ref(
m_impl->findMM(accessId));
866 return ref.handler().getDetectedDevices(devices, index);
896 ManagedMediaMap::const_iterator m(
m_impl->mediaMap.begin());
897 for( ; m !=
m_impl->mediaMap.end(); ++m)
903 const std::string& our(path.
asString());
911 if( mnt.size() > our.size() &&
912 mnt.at(our.size()) ==
'/' &&
913 !mnt.compare(0, our.size(), our))
928 MountEntries entries(
m_impl->getMountEntries());
929 MountEntries::const_iterator e;
930 for( e = entries.begin(); e != entries.end(); ++e)
933 const std::string& our(path.
asString());
941 if( mnt.size() > our.size() &&
942 mnt.at(our.size()) ==
'/' &&
943 !mnt.compare(0, our.size(), our))
958 ManagedMedia &ref(
m_impl->findMM(accessId));
960 return ref.handler().attachedMedia();
967 if( !media || media->type.empty())
970 ManagedMediaMap::const_iterator m(
m_impl->mediaMap.begin());
971 for( ; m !=
m_impl->mediaMap.end(); ++m)
973 if( !m->second.handler().isAttached())
987 if( !media || media->type.empty())
990 ManagedMediaMap::iterator m(
m_impl->mediaMap.begin());
991 for( ; m !=
m_impl->mediaMap.end(); ++m)
993 if( !m->second.handler().isAttached())
999 m->second.handler().release();
1000 m->second.desired =
false;
Store and operate with byte count.
Base class for Exception.
std::string getScheme() const
Returns the scheme name of the URL.
std::string asString() const
Returns a default string representation of the Url object.
Wrapper class for stat/lstat.
const std::string & asString() const
String representation.
bool empty() const
Test for an empty path.
String related utilities and Regular expression matching.
std::list< DirEntry > DirContent
Returned by readdir.
SolvableSpec & operator=(const SolvableSpec &)=default
std::string numstring(char n, int w=0)
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.
#define ZYPP_RETHROW(EXCPT)
Drops a logline and rethrows, updating the CodeLocation.
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.