MediaSet.cc

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #include <iostream>
00013 #include "zypp/base/Logger.h"
00014 
00015 #include "zypp/SourceFactory.h"
00016 #include "zypp/source/MediaSet.h"
00017 #include "zypp/ZYppCallbacks.h"
00018 
00019 #include <fstream>
00020 
00021 using std::endl;
00022 
00024 namespace zypp
00025 { 
00026 
00027   namespace source
00028   { 
00029 
00030     IMPL_PTR_TYPE(MediaSet);
00031 
00032     MediaSet::MediaSet(const Source_Ref & source_r)
00033     {
00034       _source = source_r;
00035     }
00036     MediaSet::~MediaSet()
00037     {
00038       MIL << "Called MediaSet destructor" << endl;
00039       release();
00040       MIL << "Closing all medias of source" << endl;
00041       media::MediaManager media_mgr;
00042       for (MediaMap::iterator it = medias.begin(); it != medias.end(); it++)
00043       {
00044         MIL << "Closing media " << it->second << endl;
00045         media_mgr.close(it->second);
00046       }
00047     }
00048 
00049     void MediaSet::redirect (media::MediaNr medianr, media::MediaAccessId media_id)
00050     {
00051       media::MediaManager media_mgr;
00052       MediaMap::iterator  it( medias.find(medianr));
00053       if( it != medias.end() && media_mgr.isOpen(it->second)) {
00054         try {
00055           DBG << "Closing media access id " << it->second << endl;
00056           media_mgr.close(it->second);
00057         }
00058         // paranoia ...
00059         catch (const Exception & excpt_r) {
00060           ZYPP_CAUGHT(excpt_r);
00061         }
00062       }
00063 
00064       medias[medianr] = media_id;
00065     }
00066 
00067     void MediaSet::reattach(const Pathname &attach_point)
00068     {
00069       media::MediaManager media_mgr;
00070       media_mgr.setAttachPrefix(attach_point);
00071       for (MediaMap::iterator it = medias.begin(); it != medias.end(); it++)
00072       {
00073         Url url = media_mgr.url(it->second);
00074         std::string scheme = url.getScheme();
00075         if (scheme == "http" || scheme == "ftp" || scheme == "https" || scheme == "ftps")
00076         {
00077           media_mgr.release(it->second);
00078           media_mgr.attach(it->second);
00079         }
00080       }
00081     }
00082 
00083     void MediaSet::reset()
00084     {
00085       media::MediaManager media_mgr;
00086       for (MediaMap::iterator it = medias.begin(); it != medias.end(); it++)
00087       {
00088         if( media_mgr.isOpen(it->second)) {
00089           try {
00090             DBG << "Closing media access id " << it->second << endl;
00091             media_mgr.close(it->second);
00092           }
00093           // paranoia ...
00094           catch (const Exception & excpt_r) {
00095             ZYPP_CAUGHT(excpt_r);
00096           }
00097         }
00098       }
00099       medias = MediaMap();
00100     }
00101 
00102     void MediaSet::release()
00103     {
00104       MIL << "Releasing all medias of source" << endl;
00105       media::MediaManager media_mgr;
00106       for (MediaMap::iterator it = medias.begin(); it != medias.end(); it++)
00107       {
00108         if (media_mgr.isAttached(it->second))
00109         {
00110           MIL << "Releasing media " << it->second << endl;
00111           media_mgr.release(it->second, false);
00112         }
00113         else
00114         {
00115           MIL << "Media " << it->second << " not attached" << endl;
00116         }
00117       }
00118     }
00119 
00120     media::MediaAccessId MediaSet::getMediaAccessId (media::MediaNr medianr, bool noattach)
00121     {
00122      media::MediaManager media_mgr;
00123 
00124      if (medias.find(medianr) != medias.end())
00125       {
00126         media::MediaAccessId id = medias[medianr];
00127         if (! noattach && ! media_mgr.isAttached(id))
00128           media_mgr.attach(id);
00129         return id;
00130       }
00131       Url url = _source.url();
00132       url = rewriteUrl (url, medianr);
00133       media::MediaAccessId id = media_mgr.open(url, _source.path());
00134       try {
00135         MIL << "Adding media verifier" << endl;
00136         media_mgr.delVerifier(id);
00137         media_mgr.addVerifier(id, _source.verifier(medianr));
00138       }
00139       catch (const Exception & excpt_r)
00140       {
00141 #warning FIXME: If media data is not set, verifier is not set. Should the media be refused instead?
00142         ZYPP_CAUGHT(excpt_r);
00143         WAR << "Verifier not found" << endl;
00144       }
00145       medias[medianr] = id;
00146       
00147       if (! noattach)
00148         media_mgr.attach(id);
00149 
00150       return id;
00151     }
00152 
00153     Url MediaSet::rewriteUrl (const Url & url_r, const media::MediaNr medianr)
00154     {
00155       std::string scheme = url_r.getScheme();
00156       if (scheme == "cd" || scheme == "dvd")
00157         return url_r;
00158 
00159       DBG << "Rewriting url " << url_r << endl;
00160 
00161       if( scheme == "iso")
00162       {
00163         std::string isofile = url_r.getQueryParam("iso");
00164         boost::regex e("^(.*(cd|dvd))([0-9]+)(\\.iso)$", boost::regex::icase);
00165         boost::smatch what;
00166         if(boost::regex_match(isofile, what, e, boost::match_extra))
00167         {
00168           Url url( url_r);
00169 
00170           isofile = what[1] + str::numstring(medianr) + what[4];
00171           url.setQueryParam("iso", isofile);
00172 
00173           DBG << "Url rewrite result: " << url << endl;
00174           return url;
00175         }
00176       }
00177       else
00178       {
00179         std::string pathname = url_r.getPathName();
00180         boost::regex e("^(.*(cd|dvd))([0-9]+)(/?)$", boost::regex::icase);
00181         boost::smatch what;
00182         if(boost::regex_match(pathname, what, e, boost::match_extra))
00183         {
00184           Url url( url_r);
00185 
00186           pathname = what[1] + str::numstring(medianr) + what[4];
00187           url.setPathName(pathname);
00188 
00189           DBG << "Url rewrite result: " << url << endl;
00190 
00191           return url;
00192         }
00193       }
00194       return url_r;
00195     }
00196 
00197     std::ostream & MediaSet::dumpOn( std::ostream & str ) const
00198     { return str << "MediaSet"; }
00199 
00200 
00202   } // namespace source
00205 } // namespace zypp

Generated on Thu May 4 16:03:26 2006 for zypp by  doxygen 1.4.6