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 {
75 return ( _handler ?
true :
false );
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 static inline MountEntries
312 ManagedMedia tmp = ManagedMedia::makeManagedMedia( url, preferred_attach_point,
verifier );
316 m_impl->mediaMap.insert( std::make_pair( nextId, std::move(tmp) ) );
319 DBG <<
"Opened new media access using id " << nextId
320 <<
" to " << url.
asString() << std::endl;
338 ManagedMediaMap::iterator m(
m_impl->mediaMap.begin());
339 for( ; m !=
m_impl->mediaMap.end(); ++m)
341 if( m->second.handler().dependsOnParent(accessId,
true))
344 m->second.handler().url().asString()
349 DBG <<
"Close to access handler using id "
350 << accessId <<
" requested" << std::endl;
352 ManagedMedia &ref(
m_impl->findMM(accessId));
355 m_impl->mediaMap.erase(accessId);
362 ManagedMediaMap::iterator it(
m_impl->mediaMap.find(accessId));
363 return it !=
m_impl->mediaMap.end();
370 ManagedMedia &ref(
m_impl->findMM(accessId));
372 return ref.handler().protocol();
379 ManagedMedia &ref(
m_impl->findMM(accessId));
381 return ref.handler().downloads();
388 ManagedMedia &ref(
m_impl->findMM(accessId));
390 return ref.handler().url();
401 ManagedMedia &ref(
m_impl->findMM(accessId));
406 DBG <<
"MediaVerifier change: id=" << accessId <<
", verifier="
414 ManagedMedia &ref(
m_impl->findMM(accessId));
420 DBG <<
"MediaVerifier change: id=" << accessId <<
", verifier="
434 ManagedMedia &ref(
m_impl->findMM(accessId));
435 auto &hdl = ref.handler();
437 DBG <<
"attach(id=" << accessId <<
")" << std::endl;
443 ref.checkDesired(accessId);
450 if (!hdl.hasMoreDevices())
453 if (hdl.isAttached())
457 MIL <<
"checkDesired(" << accessId <<
") of first device failed,"
458 " going to try others with attach(true)" << std::endl;
460 while (hdl.hasMoreDevices())
466 ref.checkDesired(accessId);
473 if (!hdl.hasMoreDevices())
475 MIL <<
"No desired media found after trying all detected devices." << std::endl;
480 DBG <<
"Skipping " << media.
mediaSource->asString() <<
": not desired media." << std::endl;
488 if (!hdl.hasMoreDevices())
492 DBG <<
"Skipping " << media.
mediaSource->asString() <<
" because of exception thrown by attach(true)" << std::endl;
494 if (hdl.isAttached()) hdl.release();
503 ManagedMedia &ref(
m_impl->findMM(accessId));
505 DBG <<
"release(id=" << accessId;
506 if (!ejectDev.empty())
507 DBG <<
", " << ejectDev;
508 DBG <<
")" << std::endl;
510 if(!ejectDev.empty())
518 ManagedMediaMap::iterator m(
m_impl->mediaMap.begin());
519 for( ; m !=
m_impl->mediaMap.end(); ++m)
521 auto &hdl = m->second.handler();
522 if( hdl.dependsOnParent(accessId,
false))
526 DBG <<
"Forcing release of handler depending on access id "
527 << accessId << std::endl;
528 m->second.desired =
false;
539 ref.handler().release(ejectDev);
546 MIL <<
"Releasing all attached media" << std::endl;
547 auto releaseAction = [](
MediaAccessId mId_r, ManagedMedia & mManagedMedia_r,
bool ifDependsOnParent_r ) {
548 auto & hdl = mManagedMedia_r.handler();
549 if ( hdl.dependsOnParent() == ifDependsOnParent_r ) {
551 if ( hdl.isAttached() ) {
552 DBG <<
"Releasing media id " << mId_r << std::endl;
553 mManagedMedia_r.desired =
false;
557 DBG <<
"Media id " << mId_r <<
" not attached " << std::endl;
562 ERR <<
"Failed to release media id " << mId_r << std::endl;
568 for (
auto & [ mId, mManagedMedia ] :
m_impl->mediaMap ) {
569 releaseAction( mId, mManagedMedia,
true );
572 for (
auto & [ mId, mManagedMedia ] :
m_impl->mediaMap ) {
573 releaseAction( mId, mManagedMedia,
false );
576 MIL <<
"Exit" << std::endl;
583 ManagedMedia &ref(
m_impl->findMM(accessId));
585 ref.handler().disconnect();
592 ManagedMedia &ref(
m_impl->findMM(accessId));
594 return ref.handler().isAttached();
600 ManagedMedia &ref(
m_impl->findMM(accessId));
602 return ref.handler().isSharedMedia();
609 ManagedMedia &ref(
m_impl->findMM(accessId));
611 if( !ref.handler().isAttached())
618 ref.desired = ref.verifier->isDesiredMedia( ref.handler() );
625 DBG <<
"isDesiredMedia(" << accessId <<
"): "
626 << (ref.desired ?
"" :
"not ")
627 <<
"desired (report by "
628 << ref.verifier->info() <<
")" << std::endl;
641 ManagedMedia &ref(
m_impl->findMM(accessId));
644 if( ref.handler().isAttached())
647 desired = v->isDesiredMedia( ref.handler() );
654 DBG <<
"isDesiredMedia(" << accessId <<
"): "
656 <<
"desired (report by "
657 << v->info() <<
")" << std::endl;
672 ManagedMedia &ref(
m_impl->findMM(accessId));
675 path = ref.handler().localRoot();
684 ManagedMedia &ref(
m_impl->findMM(accessId));
687 path = ref.handler().localPath(pathname);
694 const ByteCount &expectedFileSize )
const
696 ManagedMedia &ref(
m_impl->findMM(accessId));
710 ManagedMedia &ref(
m_impl->findMM(accessId));
720 ManagedMedia &ref(
m_impl->findMM(accessId));
722 ref.checkDesired(accessId);
724 ref.handler().provideFile( file );
732 ManagedMedia &ref(
m_impl->findMM(accessId));
734 ref.checkDesired(accessId);
736 ref.deltafile = filename;
741 ManagedMedia &ref(
m_impl->findMM(accessId));
743 ref.checkDesired(accessId);
745 ref.handler().precacheFiles( files );
753 ManagedMedia &ref(
m_impl->findMM(accessId));
755 ref.checkDesired(accessId);
757 ref.handler().provideDir(dirname);
765 ManagedMedia &ref(
m_impl->findMM(accessId));
767 ref.checkDesired(accessId);
769 ref.handler().provideDirTree(dirname);
777 ManagedMedia &ref(
m_impl->findMM(accessId));
779 ref.checkAttached(accessId);
781 ref.handler().releaseFile(filename);
789 ManagedMedia &ref(
m_impl->findMM(accessId));
791 ref.checkAttached(accessId);
793 ref.handler().releaseDir(dirname);
802 ManagedMedia &ref(
m_impl->findMM(accessId));
804 ref.checkAttached(accessId);
806 ref.handler().releasePath(pathname);
812 std::list<std::string> &retlist,
816 ManagedMedia &ref(
m_impl->findMM(accessId));
819 ref.checkAttached(accessId);
821 ref.handler().dirInfo(retlist, dirname, dots);
831 ManagedMedia &ref(
m_impl->findMM(accessId));
834 ref.checkAttached(accessId);
836 ref.handler().dirInfo(retlist, dirname, dots);
843 ManagedMedia &ref(
m_impl->findMM(accessId));
846 ref.checkAttached(accessId);
848 return ref.handler().doesFileExist(filename);
854 std::vector<std::string> & devices,
855 unsigned int & index)
const
857 ManagedMedia &ref(
m_impl->findMM(accessId));
858 return ref.handler().getDetectedDevices(devices, index);
888 ManagedMediaMap::const_iterator m(
m_impl->mediaMap.begin());
889 for( ; m !=
m_impl->mediaMap.end(); ++m)
903 if( mnt.size() > our.size() &&
904 mnt.at(our.size()) ==
'/' &&
905 !mnt.compare(0, our.size(), our))
920 MountEntries entries(
m_impl->getMountEntries());
921 MountEntries::const_iterator e;
922 for( e = entries.begin(); e != entries.end(); ++e)
933 if( mnt.size() > our.size() &&
934 mnt.at(our.size()) ==
'/' &&
935 !mnt.compare(0, our.size(), our))
950 ManagedMedia &ref(
m_impl->findMM(accessId));
952 return ref.handler().attachedMedia();
959 if( !media || media->type.empty())
962 ManagedMediaMap::const_iterator m(
m_impl->mediaMap.begin());
963 for( ; m !=
m_impl->mediaMap.end(); ++m)
965 if( !m->second.handler().isAttached())
979 if( !media || media->type.empty())
982 ManagedMediaMap::iterator m(
m_impl->mediaMap.begin());
983 for( ; m !=
m_impl->mediaMap.end(); ++m)
985 if( !m->second.handler().isAttached())
991 m->second.handler().release();
992 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.
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.