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 );
55 ManagedMedia( ManagedMedia &&m )
61 static ManagedMedia makeManagedMedia (
const Url &o_url,
const Pathname &preferred_attach_point,
const MediaVerifierRef &v )
65 ERR <<
"Failed to create media handler" << std::endl;
66 ZYPP_THROW( MediaSystemException(o_url,
"Failed to create media handler"));
68 return ManagedMedia( std::move(handler), v );
71 ManagedMedia &operator= ( ManagedMedia &&other ) =
default;
73 operator bool ()
const {
77 inline MediaHandler &handler() {
79 ZYPP_THROW(MediaNotOpenException(
"Accessing ManagedMedia after it was closed"));
83 inline const MediaHandler &handler()
const {
85 ZYPP_THROW(MediaNotOpenException(
"Accessing ManagedMedia after it was closed"));
89 std::ostream &
dumpOn( std::ostream &
str )
const {
91 return str <<
"ManagedMedia( closed )";
107 catch (
const MediaException & excpt_r)
110 WAR <<
"Close: " << *
this <<
" (" << excpt_r <<
")" << std::endl;
113 MIL <<
"Close: " << *
this <<
" (OK)" << std::endl;
119 if( !handler().isAttached())
121 DBG <<
"checkAttached(" <<
id <<
") not attached" << std::endl;
135 const auto &hdl = handler();
141 media::MediaNotDesiredException newEx ( hdl.url() );
148 DBG <<
"checkDesired(" <<
id <<
"): not desired (report by " <<
verifier->info() <<
")" << std::endl;
149 ZYPP_THROW( MediaNotDesiredException( hdl.url() ) );
152 DBG <<
"checkDesired(" <<
id <<
"): desired (report by " <<
verifier->info() <<
")" << std::endl;
154 DBG <<
"checkDesired(" <<
id <<
"): desired (cached)" << std::endl;
172 std::ostream &
operator<<( std::ostream &
str,
const ManagedMedia & obj ) {
173 return obj.dumpOn(
str );
177 typedef std::map<MediaAccessId, ManagedMedia> ManagedMediaMap;
188 return std::string(
typeid((*
this)).name());
196 return std::string(
"zypp::media::NoVerifier");
219 ManagedMediaMap::iterator it;
226 if( it->second && it->second.handler().dependsOnParent() )
231 it->second.handler().resetParentId();
258 inline ManagedMedia &
261 ManagedMediaMap::iterator it(
mediaMap.find(accessId));
277 WAR <<
"Failed to retrieve modification time of '/etc/mtab'" 283 static inline MountEntries
317 ManagedMedia tmp = ManagedMedia::makeManagedMedia(
url, preferred_attach_point,
verifier );
321 m_impl->mediaMap.insert( std::make_pair( nextId, std::move(tmp) ) );
324 DBG <<
"Opened new media access using id " << nextId
343 ManagedMediaMap::iterator m(
m_impl->mediaMap.begin());
344 for( ; m !=
m_impl->mediaMap.end(); ++m)
346 if( m->second.handler().dependsOnParent(accessId,
true))
349 m->second.handler().url().asString()
354 DBG <<
"Close to access handler using id " 355 << accessId <<
" requested" << std::endl;
357 ManagedMedia &ref(
m_impl->findMM(accessId));
360 m_impl->mediaMap.erase(accessId);
367 ManagedMediaMap::iterator it(
m_impl->mediaMap.find(accessId));
368 return it !=
m_impl->mediaMap.end();
375 ManagedMedia &ref(
m_impl->findMM(accessId));
377 return ref.handler().protocol();
384 ManagedMedia &ref(
m_impl->findMM(accessId));
386 return ref.handler().downloads();
393 ManagedMedia &ref(
m_impl->findMM(accessId));
395 return ref.handler().url();
406 ManagedMedia &ref(
m_impl->findMM(accessId));
411 DBG <<
"MediaVerifier change: id=" << accessId <<
", verifier=" 419 ManagedMedia &ref(
m_impl->findMM(accessId));
425 DBG <<
"MediaVerifier change: id=" << accessId <<
", verifier=" 439 ManagedMedia &ref(
m_impl->findMM(accessId));
440 auto &hdl = ref.handler();
442 DBG <<
"attach(id=" << accessId <<
")" << std::endl;
448 ref.checkDesired(accessId);
455 if (!hdl.hasMoreDevices())
458 if (hdl.isAttached())
462 MIL <<
"checkDesired(" << accessId <<
") of first device failed," 463 " going to try others with attach(true)" << std::endl;
465 while (hdl.hasMoreDevices())
471 ref.checkDesired(accessId);
478 if (!hdl.hasMoreDevices())
480 MIL <<
"No desired media found after trying all detected devices." << std::endl;
485 DBG <<
"Skipping " << media.mediaSource->asString() <<
": not desired media." << std::endl;
493 if (!hdl.hasMoreDevices())
497 DBG <<
"Skipping " << media.mediaSource->asString() <<
" because of exception thrown by attach(true)" << std::endl;
499 if (hdl.isAttached()) hdl.release();
508 ManagedMedia &ref(
m_impl->findMM(accessId));
510 DBG <<
"release(id=" << accessId;
511 if (!ejectDev.empty())
512 DBG <<
", " << ejectDev;
513 DBG <<
")" << std::endl;
515 if(!ejectDev.empty())
523 ManagedMediaMap::iterator m(
m_impl->mediaMap.begin());
524 for( ; m !=
m_impl->mediaMap.end(); ++m)
526 auto &hdl = m->second.handler();
527 if( hdl.dependsOnParent(accessId,
false))
531 DBG <<
"Forcing release of handler depending on access id " 532 << accessId << std::endl;
533 m->second.desired =
false;
544 ref.handler().release(ejectDev);
551 MIL <<
"Releasing all attached media" << std::endl;
553 ManagedMediaMap::iterator m(
m_impl->mediaMap.begin());
554 for( ; m !=
m_impl->mediaMap.end(); ++m)
556 auto &hdl = m->second.handler();
557 if( hdl.dependsOnParent())
564 DBG <<
"Releasing media id " << m->first << std::endl;
565 m->second.desired =
false;
570 DBG <<
"Media id " << m->first <<
" not attached " << std::endl;
576 ERR <<
"Failed to release media id " << m->first << std::endl;
580 MIL <<
"Exit" << std::endl;
587 ManagedMedia &ref(
m_impl->findMM(accessId));
589 ref.handler().disconnect();
596 ManagedMedia &ref(
m_impl->findMM(accessId));
598 return ref.handler().isAttached();
604 ManagedMedia &ref(
m_impl->findMM(accessId));
606 return ref.handler().isSharedMedia();
613 ManagedMedia &ref(
m_impl->findMM(accessId));
615 if( !ref.handler().isAttached())
622 ref.desired = ref.verifier->isDesiredMedia( ref.handler() );
629 DBG <<
"isDesiredMedia(" << accessId <<
"): " 630 << (ref.desired ?
"" :
"not ")
631 <<
"desired (report by " 632 << ref.verifier->info() <<
")" << std::endl;
645 ManagedMedia &ref(
m_impl->findMM(accessId));
648 if( ref.handler().isAttached())
651 desired = v->isDesiredMedia( ref.handler() );
658 DBG <<
"isDesiredMedia(" << accessId <<
"): " 660 <<
"desired (report by " 661 << v->info() <<
")" << std::endl;
676 ManagedMedia &ref(
m_impl->findMM(accessId));
679 path = ref.handler().localRoot();
688 ManagedMedia &ref(
m_impl->findMM(accessId));
691 path = ref.handler().localPath(pathname);
698 const ByteCount &expectedFileSize )
const 700 ManagedMedia &ref(
m_impl->findMM(accessId));
714 ManagedMedia &ref(
m_impl->findMM(accessId));
724 ManagedMedia &ref(
m_impl->findMM(accessId));
726 ref.checkDesired(accessId);
728 ref.handler().provideFile( file );
736 ManagedMedia &ref(
m_impl->findMM(accessId));
738 ref.checkDesired(accessId);
740 ref.deltafile = filename;
745 ManagedMedia &ref(
m_impl->findMM(accessId));
747 ref.checkDesired(accessId);
749 ref.handler().precacheFiles( files );
757 ManagedMedia &ref(
m_impl->findMM(accessId));
759 ref.checkDesired(accessId);
761 ref.handler().provideDir(dirname);
769 ManagedMedia &ref(
m_impl->findMM(accessId));
771 ref.checkDesired(accessId);
773 ref.handler().provideDirTree(dirname);
781 ManagedMedia &ref(
m_impl->findMM(accessId));
783 ref.checkAttached(accessId);
785 ref.handler().releaseFile(filename);
793 ManagedMedia &ref(
m_impl->findMM(accessId));
795 ref.checkAttached(accessId);
797 ref.handler().releaseDir(dirname);
806 ManagedMedia &ref(
m_impl->findMM(accessId));
808 ref.checkAttached(accessId);
810 ref.handler().releasePath(pathname);
816 std::list<std::string> &retlist,
820 ManagedMedia &ref(
m_impl->findMM(accessId));
823 ref.checkAttached(accessId);
825 ref.handler().dirInfo(retlist, dirname, dots);
835 ManagedMedia &ref(
m_impl->findMM(accessId));
838 ref.checkAttached(accessId);
840 ref.handler().dirInfo(retlist, dirname, dots);
847 ManagedMedia &ref(
m_impl->findMM(accessId));
850 ref.checkAttached(accessId);
852 return ref.handler().doesFileExist(filename);
858 std::vector<std::string> & devices,
859 unsigned int & index)
const 861 ManagedMedia &ref(
m_impl->findMM(accessId));
862 return ref.handler().getDetectedDevices(devices, index);
892 ManagedMediaMap::const_iterator m(
m_impl->mediaMap.begin());
893 for( ; m !=
m_impl->mediaMap.end(); ++m)
907 if( mnt.size() > our.size() &&
908 mnt.at(our.size()) ==
'/' &&
909 !mnt.compare(0, our.size(), our))
924 MountEntries entries(
m_impl->getMountEntries());
925 MountEntries::const_iterator e;
926 for( e = entries.begin(); e != entries.end(); ++e)
937 if( mnt.size() > our.size() &&
938 mnt.at(our.size()) ==
'/' &&
939 !mnt.compare(0, our.size(), our))
954 ManagedMedia &ref(
m_impl->findMM(accessId));
956 return ref.handler().attachedMedia();
963 if( !media || media->type.empty())
966 ManagedMediaMap::const_iterator m(
m_impl->mediaMap.begin());
967 for( ; m !=
m_impl->mediaMap.end(); ++m)
969 if( !m->second.handler().isAttached())
983 if( !media || media->type.empty())
986 ManagedMediaMap::iterator m(
m_impl->mediaMap.begin());
987 for( ; m !=
m_impl->mediaMap.end(); ++m)
989 if( !m->second.handler().isAttached())
995 m->second.handler().release();
996 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.