MediaManager.cc

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #include <zypp/media/MediaException.h>
00013 #include <zypp/media/MediaManager.h>
00014 #include <zypp/media/MediaHandler.h>
00015 #include <zypp/media/Mount.h>
00016 #include <zypp/thread/Mutex.h>
00017 #include <zypp/thread/MutexLock.h>
00018 #include <zypp/target/hal/HalContext.h>
00019 
00020 #include <zypp/base/String.h>
00021 #include <zypp/base/Logger.h>
00022 #include <zypp/Pathname.h>
00023 #include <zypp/PathInfo.h>
00024 
00025 #include <map>
00026 #include <list>
00027 #include <iostream>
00028 #include <typeinfo>
00029 
00030 #define  DISABLE_AUTOMOUNTER      0
00031 
00032 
00034 namespace zypp
00035 { 
00036 
00038   namespace media
00039   { 
00040 
00041     using zypp::thread::Mutex;
00042     using zypp::thread::MutexLock;
00043 
00045     namespace // anonymous
00046     { 
00047 
00048 
00049       // -------------------------------------------------------------
00050       // STATIC
00051       static Mutex  g_Mutex;
00052 
00053 
00054       // -------------------------------------------------------------
00055       struct ManagedMedia
00056       {
00057         ~ManagedMedia()
00058         {}
00059 
00060         ManagedMedia()
00061           : desired (false)
00062         {}
00063 
00064         ManagedMedia(const ManagedMedia &m)
00065           : desired (m.desired)
00066           , handler (m.handler)
00067           , verifier(m.verifier)
00068         {}
00069 
00070         ManagedMedia(const MediaAccessRef &h, const MediaVerifierRef &v)
00071           : desired (false)
00072           , handler (h)
00073           , verifier(v)
00074         {}
00075 
00076         inline void
00077         checkAttached(MediaAccessId id)
00078         {
00079           if( !handler->isAttached())
00080           {
00081             DBG << "checkAttached(" << id << ") not attached" << std::endl;
00082             desired = false;
00083             ZYPP_THROW(MediaNotAttachedException(
00084               handler->url()
00085             ));
00086           }
00087         }
00088 
00089         inline void
00090         checkDesired(MediaAccessId id)
00091         {
00092           checkAttached(id);
00093 
00094           if( !desired)
00095           {
00096             try {
00097               desired = verifier->isDesiredMedia(handler);
00098             }
00099             catch(const zypp::Exception &e) {
00100               ZYPP_CAUGHT(e);
00101               desired = false;
00102             }
00103 
00104             if( !desired)
00105             {
00106               DBG << "checkDesired(" << id << "): not desired (report by "
00107                   << verifier->info() << ")" << std::endl;
00108               ZYPP_THROW(MediaNotDesiredException(
00109                 handler->url()
00110               ));
00111             }
00112 
00113             DBG << "checkDesired(" << id << "): desired (report by "
00114                 << verifier->info() << ")" << std::endl;
00115           } else {
00116             DBG << "checkDesired(" << id << "): desired (cached)" << std::endl;
00117           }
00118         }
00119 
00120         bool             desired;
00121         MediaAccessRef   handler;
00122         MediaVerifierRef verifier;
00123       };
00124 
00125 
00126       // -------------------------------------------------------------
00127       typedef std::map<MediaAccessId, ManagedMedia> ManagedMediaMap;
00128 
00129 
00130       // -------------------------------------------------------------
00131       enum AutoMounterCleanUp
00132       {
00133         NONE, ENABLE, REMOVE
00134       };
00135 
00136       #define HAL_AUTOMOUNTER_UDI  "/org/freedesktop/Hal/devices/computer"
00137       #define HAL_AUTOMOUNTER_KEY  "storage.disable_volume_handling"
00138 
00139       // -------------------------------------------------------------
00140       AutoMounterCleanUp
00141       disableAutoMounter()
00142       {
00143         using namespace zypp::target::hal;
00144 
00145         AutoMounterCleanUp cleanup(NONE);
00146 #if DISABLE_AUTOMOUNTER
00147         try
00148         {
00149           HalContext hal(true);
00150           bool disabled(false);
00151 
00152           // query
00153           XXX << "Checking HAL volume handling property"
00154               << std::endl;
00155           try
00156           {
00157             disabled = hal.getDevicePropertyBool(
00158               HAL_AUTOMOUNTER_UDI, HAL_AUTOMOUNTER_KEY
00159             );
00160 
00161             if( disabled)
00162             {
00163               MIL << "HAL volume handling is already disabled"
00164                   << std::endl;
00165             }
00166             else
00167             {
00168               cleanup = ENABLE;
00169               XXX << "HAL volume handling is enabled"
00170                   << std::endl;
00171             }
00172           }
00173           catch(const HalException &e)
00174           {
00175             ZYPP_CAUGHT(e);
00176             XXX << "HAL volume handling is enabled (no property)"
00177                 << std::endl;
00178             disabled = false;
00179             cleanup  = REMOVE;
00180           }
00181 
00182           // disable
00183           if( !disabled)
00184           {
00185             XXX << "Trying to disable HAL volume handling"
00186                 << std::endl;
00187             try
00188             {
00189               hal.setDevicePropertyBool(
00190                 HAL_AUTOMOUNTER_UDI, HAL_AUTOMOUNTER_KEY,
00191                 true
00192               );
00193 
00194               MIL << "Disabled HAL volume handling (automounter)"
00195                   << std::endl;
00196             }
00197             catch(const HalException &e)
00198             {
00199               ZYPP_CAUGHT(e);
00200               WAR << "Unable to disable HAL volume handling (automounter)"
00201                   << std::endl;
00202 
00203               cleanup  = NONE;
00204             }
00205           }
00206         }
00207         catch(const HalException &e)
00208         {
00209           ZYPP_CAUGHT(e);
00210           WAR << "Unable to disable HAL volume handling (automounter)"
00211               << std::endl;
00212         }
00213 #endif // DISABLE_AUTOMOUNTER
00214         return cleanup;
00215       }
00216 
00217       // -------------------------------------------------------------
00218       void
00219       restoreAutoMounter(AutoMounterCleanUp cleanup)
00220       {
00221         using namespace zypp::target::hal;
00222 
00223         if(cleanup == NONE)
00224           return;
00225 
00226 #if DISABLE_AUTOMOUNTER
00227         try
00228         {
00229           HalContext hal(true);
00230 
00231           if(cleanup == ENABLE)
00232           {
00233             XXX << "Trying to restore HAL volume handling -- enable"
00234                 << std::endl;
00235 
00236             hal.setDevicePropertyBool(
00237               HAL_AUTOMOUNTER_UDI, HAL_AUTOMOUNTER_KEY,
00238               false
00239             );
00240           }
00241           else
00242           if(cleanup == REMOVE)
00243           {
00244             XXX << "Trying to restore HAL volume handling -- remove"
00245                 << std::endl;
00246 
00247             hal.removeDeviceProperty(
00248               HAL_AUTOMOUNTER_UDI, HAL_AUTOMOUNTER_KEY
00249             );
00250           }
00251 
00252           cleanup = NONE;
00253           MIL << "Restored HAL volume handling (automounter)"
00254               << std::endl;
00255         }
00256         catch(const HalException &e)
00257         {
00258           ZYPP_CAUGHT(e);
00259           WAR << "Unable to restore HAL volume handling (automounter)"
00260               << std::endl;
00261         }
00262 #endif // DISABLE_AUTOMOUNTER
00263       }
00264 
00266     } // anonymous
00268 
00269 
00271     std::string
00272     MediaVerifierBase::info() const
00273     {
00274       return std::string(typeid((*this)).name());
00275     }
00276 
00277 
00279     std::string
00280     NoVerifier::info() const
00281     {
00282       return std::string("zypp::media::NoVerifier");
00283     }
00284 
00285 
00287     class MediaManager_Impl
00288     {
00289     private:
00290       friend class MediaManager;
00291 
00292       MediaAccessId       last_accessid;
00293       AutoMounterCleanUp  am_cleanup;
00294       ManagedMediaMap     mediaMap;
00295 
00296       MediaManager_Impl()
00297         : last_accessid(0)
00298       {
00299         // disable automounter
00300         am_cleanup = disableAutoMounter();
00301       }
00302 
00303     public:
00304       ~MediaManager_Impl()
00305       {
00306         MutexLock glock(g_Mutex);
00307 
00308         try
00309         {
00310           // remove depending (iso) handlers first
00311           ManagedMediaMap::iterator it;
00312           bool found;
00313           do
00314           {
00315             found = false;
00316             for(it = mediaMap.begin(); it != mediaMap.end(); )
00317             {
00318               if( it->second.handler->dependsOnParent())
00319               {
00320                 found = true;
00321                 // let it forget its parent, we will
00322                 // destroy it later (in clear())...
00323                 it->second.handler->resetParentId();
00324                 mediaMap.erase( it++ ); // postfix! Incrementing before erase
00325               } else {
00326                 ++it;
00327               }
00328             }
00329           } while(found);
00330 
00331           // remove all other handlers
00332           mediaMap.clear();
00333 
00334           // restore automounter state
00335           restoreAutoMounter(am_cleanup);
00336         }
00337         catch( ... )
00338         {}
00339       }
00340 
00341       inline MediaAccessId
00342       nextAccessId()
00343       {
00344         return ++last_accessid;
00345       }
00346 
00347       inline bool
00348       hasId(MediaAccessId accessId) const
00349       {
00350         return mediaMap.find(accessId) != mediaMap.end();
00351       }
00352 
00353       inline ManagedMedia &
00354       findMM(MediaAccessId accessId)
00355       {
00356         ManagedMediaMap::iterator it( mediaMap.find(accessId));
00357         if( it == mediaMap.end())
00358         {
00359           ZYPP_THROW(MediaNotOpenException(
00360             "Invalid media access id " + str::numstring(accessId)
00361           ));
00362         }
00363         return it->second;
00364       }
00365 
00366       static inline time_t
00367       getMountTableMTime()
00368       {
00369         time_t mtime = zypp::PathInfo("/etc/mtab").mtime();
00370         if( mtime <= 0)
00371         {
00372           WAR << "Failed to retrieve modification time of '/etc/mtab'"
00373               << std::endl;
00374         }
00375         return mtime;
00376       }
00377 
00378       static inline MountEntries
00379       getMountEntries()
00380       {
00381         // use "/etc/mtab" by default,
00382         // fallback to "/proc/mounts"
00383         return Mount::getEntries(/* "/etc/mtab" */);
00384       }
00385 
00386     };
00387 
00388 
00390     // STATIC
00391     zypp::RW_pointer<MediaManager_Impl> MediaManager::m_impl(NULL);
00392 
00393 
00395     MediaManager::MediaManager()
00396     {
00397       MutexLock glock(g_Mutex);
00398       if( !m_impl)
00399       {
00400         m_impl.reset( new MediaManager_Impl());
00401       }
00402     }
00403 
00404     // ---------------------------------------------------------------
00405     MediaManager::~MediaManager()
00406     {
00407     }
00408 
00409     // ---------------------------------------------------------------
00410     MediaAccessId
00411     MediaManager::open(const Url &url, const Pathname &preferred_attach_point)
00412     {
00413       MutexLock glock(g_Mutex);
00414 
00415       // create new access handler for it
00416       MediaAccessRef handler( new MediaAccess());
00417       MediaVerifierRef verifier( new NoVerifier());
00418       ManagedMedia tmp( handler, verifier);
00419 
00420       tmp.handler->open(url, preferred_attach_point);
00421 
00422       MediaAccessId nextId = m_impl->nextAccessId();
00423 
00424       m_impl->mediaMap[nextId] = tmp;
00425 
00426       DBG << "Opened new media access using id " << nextId
00427           << " to " << url.asString() << std::endl;
00428       return nextId;
00429     }
00430 
00431     // ---------------------------------------------------------------
00432     void
00433     MediaManager::close(MediaAccessId accessId)
00434     {
00435       MutexLock glock(g_Mutex);
00436 
00437       //
00438       // The MediaISO handler internally requests an accessId
00439       // of a "parent" handler providing the iso file.
00440       // The parent handler accessId is private to MediaISO,
00441       // but the attached media source may be shared reference.
00442       // This means, that if the accessId exactly matches the
00443       // parent handler id, close was used on uninitialized
00444       // accessId variable (or the accessId was guessed) and
00445       // the close request to this id will be rejected here.
00446       //
00447       ManagedMediaMap::iterator m(m_impl->mediaMap.begin());
00448       for( ; m != m_impl->mediaMap.end(); ++m)
00449       {
00450         if( m->second.handler->dependsOnParent(accessId, true))
00451         {
00452           ZYPP_THROW(MediaIsSharedException(
00453             m->second.handler->url().asString()
00454           ));
00455         }
00456       }
00457 
00458       DBG << "Close to access handler using id "
00459           << accessId << " requested" << std::endl;
00460 
00461       ManagedMedia &ref( m_impl->findMM(accessId));
00462       ref.handler->close();
00463 
00464       m_impl->mediaMap.erase(accessId);
00465     }
00466 
00467     // ---------------------------------------------------------------
00468     bool
00469     MediaManager::isOpen(MediaAccessId accessId) const
00470     {
00471       MutexLock glock(g_Mutex);
00472 
00473       ManagedMediaMap::iterator it( m_impl->mediaMap.find(accessId));
00474       return it != m_impl->mediaMap.end() &&
00475              it->second.handler->isOpen();
00476     }
00477 
00478     // ---------------------------------------------------------------
00479     std::string
00480     MediaManager::protocol(MediaAccessId accessId) const
00481     {
00482       MutexLock glock(g_Mutex);
00483 
00484       ManagedMedia &ref( m_impl->findMM(accessId));
00485 
00486       return ref.handler->protocol();
00487     }
00488 
00489     // ---------------------------------------------------------------
00490           bool
00491     MediaManager::downloads(MediaAccessId accessId) const
00492     {
00493       MutexLock glock(g_Mutex);
00494 
00495       ManagedMedia &ref( m_impl->findMM(accessId));
00496 
00497       return ref.handler->downloads();
00498     }
00499 
00500     // ---------------------------------------------------------------
00501     // STATIC
00502     bool
00503     MediaManager::downloads(const Url &url)
00504     {
00505       return MediaAccess::downloads( url);
00506     }
00507 
00508     // ---------------------------------------------------------------
00509     Url
00510     MediaManager::url(MediaAccessId accessId) const
00511     {
00512       MutexLock glock(g_Mutex);
00513 
00514       ManagedMedia &ref( m_impl->findMM(accessId));
00515 
00516       return ref.handler->url();
00517     }
00518 
00519     // ---------------------------------------------------------------
00520     void
00521     MediaManager::addVerifier(MediaAccessId           accessId,
00522                               const MediaVerifierRef &verifier)
00523     {
00524       MutexLock glock(g_Mutex);
00525 
00526       if( !verifier)
00527         ZYPP_THROW(MediaException("Invalid verifier reference"));
00528 
00529       ManagedMedia &ref( m_impl->findMM(accessId));
00530 
00531       ref.desired = false;
00532       MediaVerifierRef(verifier).swap(ref.verifier);
00533 
00534       DBG << "MediaVerifier change: id=" << accessId << ", verifier="
00535           << verifier->info() << std::endl;
00536     }
00537 
00538     // ---------------------------------------------------------------
00539     void
00540     MediaManager::delVerifier(MediaAccessId accessId)
00541     {
00542       MutexLock glock(g_Mutex);
00543 
00544       ManagedMedia &ref( m_impl->findMM(accessId));
00545 
00546       MediaVerifierRef verifier( new NoVerifier());
00547       ref.desired  = false;
00548       ref.verifier.swap(verifier);
00549 
00550       DBG << "MediaVerifier change: id=" << accessId << ", verifier="
00551           << verifier->info() << std::endl;
00552     }
00553 
00554     // ---------------------------------------------------------------
00555     bool
00556     MediaManager::setAttachPrefix(const Pathname &attach_prefix)
00557     {
00558       MutexLock glock(g_Mutex);
00559 
00560       return MediaHandler::setAttachPrefix(attach_prefix);
00561     }
00562 
00563     // ---------------------------------------------------------------
00564     void
00565     MediaManager::attach(MediaAccessId accessId, bool next)
00566     {
00567       MutexLock glock(g_Mutex);
00568 
00569       ManagedMedia &ref( m_impl->findMM(accessId));
00570 
00571       DBG << "attach(id=" << accessId << ")" << std::endl;
00572       return ref.handler->attach(next);
00573     }
00574 
00575     // ---------------------------------------------------------------
00576     void
00577     MediaManager::release(MediaAccessId accessId, bool eject)
00578     {
00579       MutexLock glock(g_Mutex);
00580 
00581       ManagedMedia &ref( m_impl->findMM(accessId));
00582 
00583       DBG << "release(id=" << accessId
00584           << (eject ? ", eject)" : ")") << std::endl;
00585       if( eject)
00586       {
00587         //
00588         // release MediaISO handlers, that are using the one
00589         // specified with accessId, because it provides the
00590         // iso file and it will disappear now (forced release
00591         // with eject).
00592         //
00593         ManagedMediaMap::iterator m(m_impl->mediaMap.begin());
00594         for( ; m != m_impl->mediaMap.end(); ++m)
00595         {
00596           if( m->second.handler->dependsOnParent(accessId, false))
00597           {
00598             try
00599             {
00600               DBG << "Forcing release of handler depending on access id "
00601                   << accessId << std::endl;
00602               m->second.desired  = false;
00603               m->second.handler->release(!eject);
00604             }
00605             catch(const MediaException &e)
00606             {
00607               ZYPP_CAUGHT(e);
00608             }
00609           }
00610         }
00611       }
00612       ref.desired  = false;
00613       ref.handler->release(eject);
00614     }
00615 
00616     // ---------------------------------------------------------------
00617     void
00618     MediaManager::disconnect(MediaAccessId accessId)
00619     {
00620       MutexLock glock(g_Mutex);
00621 
00622       ManagedMedia &ref( m_impl->findMM(accessId));
00623 
00624       ref.handler->disconnect();
00625     }
00626 
00627     // ---------------------------------------------------------------
00628     bool
00629     MediaManager::isAttached(MediaAccessId accessId) const
00630     {
00631       MutexLock glock(g_Mutex);
00632 
00633       ManagedMedia &ref( m_impl->findMM(accessId));
00634 
00635       return ref.handler->isAttached();
00636     }
00637 
00638     // ---------------------------------------------------------------
00639     bool MediaManager::isSharedMedia(MediaAccessId accessId) const
00640     {
00641       MutexLock glock(g_Mutex);
00642 
00643       ManagedMedia &ref( m_impl->findMM(accessId));
00644 
00645       return ref.handler->isSharedMedia();
00646     }
00647 
00648     // ---------------------------------------------------------------
00649     bool
00650     MediaManager::isDesiredMedia(MediaAccessId accessId) const
00651     {
00652       MutexLock glock(g_Mutex);
00653 
00654       ManagedMedia &ref( m_impl->findMM(accessId));
00655 
00656       if( !ref.handler->isAttached())
00657       {
00658         ref.desired = false;
00659       }
00660       else
00661       {
00662         try {
00663           ref.desired = ref.verifier->isDesiredMedia(ref.handler);
00664         }
00665         catch(const zypp::Exception &e) {
00666           ZYPP_CAUGHT(e);
00667           ref.desired = false;
00668         }
00669       }
00670       DBG << "isDesiredMedia(" << accessId << "): "
00671           << (ref.desired ? "" : "not ")
00672           << "desired (report by "
00673           << ref.verifier->info() << ")" << std::endl;
00674       return ref.desired;
00675     }
00676 
00677     // ---------------------------------------------------------------
00678     bool
00679     MediaManager::isDesiredMedia(MediaAccessId           accessId,
00680                                  const MediaVerifierRef &verifier) const
00681     {
00682       MutexLock glock(g_Mutex);
00683 
00684       MediaVerifierRef v(verifier);
00685       if( !v)
00686         ZYPP_THROW(MediaException("Invalid verifier reference"));
00687 
00688       ManagedMedia &ref( m_impl->findMM(accessId));
00689 
00690       bool desired = false;
00691       if( ref.handler->isAttached())
00692       {
00693         try {
00694           desired = v->isDesiredMedia(ref.handler);
00695         }
00696         catch(const zypp::Exception &e) {
00697           ZYPP_CAUGHT(e);
00698           desired = false;
00699         }
00700       }
00701       DBG << "isDesiredMedia(" << accessId << "): "
00702           << (desired ? "" : "not ")
00703           << "desired (report by "
00704           << v->info() << ")" << std::endl;
00705       return desired;
00706     }
00707 
00708     // ---------------------------------------------------------------
00709     Pathname
00710     MediaManager::localRoot(MediaAccessId accessId) const
00711     {
00712       MutexLock glock(g_Mutex);
00713 
00714       ManagedMedia &ref( m_impl->findMM(accessId));
00715 
00716       Pathname path;
00717       path = ref.handler->localRoot();
00718       return path;
00719     }
00720 
00721     // ---------------------------------------------------------------
00722     Pathname
00723     MediaManager::localPath(MediaAccessId accessId,
00724                             const Pathname & pathname) const
00725     {
00726       MutexLock glock(g_Mutex);
00727 
00728       ManagedMedia &ref( m_impl->findMM(accessId));
00729 
00730       Pathname path;
00731       path = ref.handler->localPath(pathname);
00732       return path;
00733     }
00734 
00735     // ---------------------------------------------------------------
00736     void
00737     MediaManager::provideFile(MediaAccessId   accessId,
00738                               const Pathname &filename,
00739                               bool            cached,
00740                               bool            checkonly) const
00741     {
00742       MutexLock glock(g_Mutex);
00743 
00744       ManagedMedia &ref( m_impl->findMM(accessId));
00745 
00746       ref.checkDesired(accessId);
00747 
00748       ref.handler->provideFile(filename, cached, checkonly);
00749     }
00750 
00751     // ---------------------------------------------------------------
00752     void
00753     MediaManager::provideDir(MediaAccessId   accessId,
00754                              const Pathname &dirname) const
00755     {
00756       MutexLock glock(g_Mutex);
00757 
00758       ManagedMedia &ref( m_impl->findMM(accessId));
00759 
00760       ref.checkDesired(accessId);
00761 
00762       ref.handler->provideDir(dirname);
00763     }
00764 
00765     // ---------------------------------------------------------------
00766     void
00767     MediaManager::provideDirTree(MediaAccessId   accessId,
00768                                  const Pathname &dirname) const
00769     {
00770       MutexLock glock(g_Mutex);
00771 
00772       ManagedMedia &ref( m_impl->findMM(accessId));
00773 
00774       ref.checkDesired(accessId);
00775 
00776       ref.handler->provideDirTree(dirname);
00777     }
00778 
00779     // ---------------------------------------------------------------
00780     void
00781     MediaManager::releaseFile(MediaAccessId   accessId,
00782                               const Pathname &filename) const
00783     {
00784       MutexLock glock(g_Mutex);
00785 
00786       ManagedMedia &ref( m_impl->findMM(accessId));
00787 
00788       ref.checkAttached(accessId);
00789 
00790       ref.handler->releaseFile(filename);
00791     }
00792 
00793     // ---------------------------------------------------------------
00794     void
00795     MediaManager::releaseDir(MediaAccessId   accessId,
00796                              const Pathname &dirname) const
00797     {
00798       MutexLock glock(g_Mutex);
00799 
00800       ManagedMedia &ref( m_impl->findMM(accessId));
00801 
00802       ref.checkAttached(accessId);
00803 
00804       ref.handler->releaseDir(dirname);
00805     }
00806 
00807 
00808     // ---------------------------------------------------------------
00809     void
00810     MediaManager::releasePath(MediaAccessId   accessId,
00811                               const Pathname &pathname) const
00812     {
00813       MutexLock glock(g_Mutex);
00814 
00815       ManagedMedia &ref( m_impl->findMM(accessId));
00816 
00817       ref.checkAttached(accessId);
00818 
00819       ref.handler->releasePath(pathname);
00820     }
00821 
00822     // ---------------------------------------------------------------
00823     void
00824     MediaManager::dirInfo(MediaAccessId           accessId,
00825                           std::list<std::string> &retlist,
00826                           const Pathname         &dirname,
00827                           bool                    dots) const
00828     {
00829       MutexLock glock(g_Mutex);
00830 
00831       ManagedMedia &ref( m_impl->findMM(accessId));
00832 
00833       // FIXME: ref.checkDesired(accessId); ???
00834       ref.checkAttached(accessId);
00835 
00836       ref.handler->dirInfo(retlist, dirname, dots);
00837     }
00838 
00839     // ---------------------------------------------------------------
00840     void
00841     MediaManager::dirInfo(MediaAccessId           accessId,
00842                           filesystem::DirContent &retlist,
00843                           const Pathname         &dirname,
00844                           bool                    dots) const
00845     {
00846       MutexLock glock(g_Mutex);
00847 
00848       ManagedMedia &ref( m_impl->findMM(accessId));
00849 
00850       // FIXME: ref.checkDesired(accessId); ???
00851       ref.checkAttached(accessId);
00852 
00853       ref.handler->dirInfo(retlist, dirname, dots);
00854     }
00855 
00856     // ---------------------------------------------------------------
00857     bool
00858     MediaManager::doesFileExist(MediaAccessId  accessId, const Pathname & filename ) const
00859     {
00860       MutexLock glock(g_Mutex);
00861       ManagedMedia &ref( m_impl->findMM(accessId));
00862 
00863       // FIXME: ref.checkDesired(accessId); ???
00864       ref.checkAttached(accessId);
00865 
00866       return ref.handler->doesFileExist(filename);
00867     }
00868     
00869     // ---------------------------------------------------------------
00870     // STATIC
00871     time_t
00872     MediaManager::getMountTableMTime()
00873     {
00874       MutexLock glock(g_Mutex);
00875       return MediaManager_Impl::getMountTableMTime();
00876     }
00877 
00878     // ---------------------------------------------------------------
00879     // STATIC
00880     MountEntries
00881     MediaManager::getMountEntries()
00882     {
00883       MutexLock glock(g_Mutex);
00884 
00885       return MediaManager_Impl::getMountEntries();
00886     }
00887 
00888     // ---------------------------------------------------------------
00889     bool
00890     MediaManager::isUseableAttachPoint(const Pathname &path,
00891                                        bool            mtab) const
00892     {
00893       if( path.empty() || path == "/" || !PathInfo(path).isDir())
00894         return false;
00895 
00896       MutexLock glock(g_Mutex);
00897 
00898       //
00899       // check against our current attach points
00900       //
00901       ManagedMediaMap::const_iterator m(m_impl->mediaMap.begin());
00902       for( ; m != m_impl->mediaMap.end(); ++m)
00903       {
00904         AttachedMedia ret = m->second.handler->attachedMedia();
00905         if( ret.mediaSource && ret.attachPoint)
00906         {
00907           std::string mnt(ret.attachPoint->path.asString());
00908           std::string our(path.asString());
00909 
00910           if( our == mnt)
00911           {
00912             // already used as attach point
00913             return false;
00914           }
00915           else
00916           if( mnt.size() > our.size()   &&
00917               mnt.at(our.size()) == '/' &&
00918              !mnt.compare(0, our.size(), our))
00919           {
00920             // mountpoint is bellow of path
00921             // (would hide the content)
00922             return false;
00923           }
00924         }
00925       }
00926 
00927       if( !mtab)
00928         return true;
00929 
00930       //
00931       // check against system mount entries
00932       //
00933       MountEntries  entries( m_impl->getMountEntries());
00934       MountEntries::const_iterator e;
00935       for( e = entries.begin(); e != entries.end(); ++e)
00936       {
00937         std::string mnt(Pathname(e->dir).asString());
00938         std::string our(path.asString());
00939 
00940         if( our == mnt)
00941         {
00942           // already used as mountpoint
00943           return false;
00944         }
00945         else
00946         if( mnt.size() > our.size()   &&
00947             mnt.at(our.size()) == '/' &&
00948            !mnt.compare(0, our.size(), our))
00949         {
00950           // mountpoint is bellow of path
00951           // (would hide the content)
00952           return false;
00953         }
00954       }
00955 
00956       return true;
00957     }
00958 
00959     // ---------------------------------------------------------------
00960     AttachedMedia
00961     MediaManager::getAttachedMedia(MediaAccessId &accessId) const
00962     {
00963       MutexLock glock(g_Mutex);
00964 
00965       ManagedMedia &ref( m_impl->findMM(accessId));
00966 
00967       return ref.handler->attachedMedia();
00968     }
00969 
00970     // ---------------------------------------------------------------
00971     AttachedMedia
00972     MediaManager::findAttachedMedia(const MediaSourceRef &media) const
00973     {
00974       MutexLock glock(g_Mutex);
00975 
00976       if( !media || media->type.empty())
00977         return AttachedMedia();
00978 
00979       ManagedMediaMap::const_iterator m(m_impl->mediaMap.begin());
00980       for( ; m != m_impl->mediaMap.end(); ++m)
00981       {
00982         if( !m->second.handler->isAttached())
00983           continue;
00984 
00985         AttachedMedia ret = m->second.handler->attachedMedia();
00986         if( ret.mediaSource && ret.mediaSource->equals( *media))
00987             return ret;
00988       }
00989       return AttachedMedia();
00990     }
00991 
00992     // ---------------------------------------------------------------
00993     void
00994     MediaManager::forceReleaseShared(const MediaSourceRef &media)
00995     {
00996       MutexLock glock(g_Mutex);
00997 
00998       if( !media || media->type.empty())
00999         return;
01000 
01001       ManagedMediaMap::iterator m(m_impl->mediaMap.begin());
01002       for( ; m != m_impl->mediaMap.end(); ++m)
01003       {
01004         if( !m->second.handler->isAttached())
01005           continue;
01006 
01007         AttachedMedia ret = m->second.handler->attachedMedia();
01008         if( ret.mediaSource && ret.mediaSource->equals( *media))
01009         {
01010           m->second.handler->release(false);
01011           m->second.desired  = false;
01012         }
01013       }
01014     }
01015 
01017   } // namespace media
01019 
01021 } // namespace zypp
01023 /*
01024 ** vim: set ts=2 sts=2 sw=2 ai et:
01025 */

Generated on Tue Nov 28 16:49:29 2006 for zypp by  doxygen 1.5.0