00001
00002
00003
00004
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
00059 catch (const Exception & excpt_r) {
00060 ZYPP_CAUGHT(excpt_r);
00061 }
00062 }
00063 try {
00064 MIL << "Adding media verifier to redirected medium" << endl;
00065 media_mgr.delVerifier(media_id);
00066 media_mgr.addVerifier(media_id, _source.verifier(medianr));
00067 }
00068 catch (const Exception & excpt_r)
00069 {
00070 #warning FIXME: If media data is not set, verifier is not set. Should the media be refused instead?
00071 ZYPP_CAUGHT(excpt_r);
00072 WAR << "Verifier not found" << endl;
00073 }
00074
00075 medias[medianr] = media_id;
00076 }
00077
00078 void MediaSet::reattach(const Pathname &attach_point)
00079 {
00080 media::MediaManager media_mgr;
00081 media_mgr.setAttachPrefix(attach_point);
00082 for (MediaMap::iterator it = medias.begin(); it != medias.end(); it++)
00083 {
00084 Url url = media_mgr.url(it->second);
00085 std::string scheme = url.getScheme();
00086 if (scheme == "http" || scheme == "ftp" || scheme == "https" || scheme == "ftps")
00087 {
00088 media_mgr.release(it->second);
00089 media_mgr.attach(it->second);
00090 }
00091 }
00092 }
00093
00094 void MediaSet::reset()
00095 {
00096 media::MediaManager media_mgr;
00097 for (MediaMap::iterator it = medias.begin(); it != medias.end(); it++)
00098 {
00099 if( media_mgr.isOpen(it->second)) {
00100 try {
00101 DBG << "Closing media access id " << it->second << endl;
00102 media_mgr.close(it->second);
00103 }
00104
00105 catch (const Exception & excpt_r) {
00106 ZYPP_CAUGHT(excpt_r);
00107 }
00108 }
00109 }
00110 medias = MediaMap();
00111 }
00112
00113 void MediaSet::release()
00114 {
00115 MIL << "Releasing all medias of source" << endl;
00116 media::MediaManager media_mgr;
00117 for (MediaMap::iterator it = medias.begin(); it != medias.end(); it++)
00118 {
00119 if (media_mgr.isAttached(it->second))
00120 {
00121 MIL << "Releasing media " << it->second << endl;
00122 media_mgr.release(it->second, false);
00123 }
00124 else
00125 {
00126 MIL << "Media " << it->second << " not attached" << endl;
00127 }
00128 }
00129 }
00130
00131 media::MediaAccessId MediaSet::getMediaAccessId (media::MediaNr medianr, bool noattach)
00132 {
00133 media::MediaManager media_mgr;
00134
00135 if (medias.find(medianr) != medias.end())
00136 {
00137 media::MediaAccessId id = medias[medianr];
00138 if (! noattach && ! media_mgr.isAttached(id))
00139 media_mgr.attach(id);
00140 return id;
00141 }
00142 Url url = _source.url();
00143 url = rewriteUrl (url, medianr);
00144 media::MediaAccessId id = media_mgr.open(url, _source.path());
00145 try {
00146 MIL << "Adding media verifier" << endl;
00147 media_mgr.delVerifier(id);
00148 media_mgr.addVerifier(id, _source.verifier(medianr));
00149 }
00150 catch (const Exception & excpt_r)
00151 {
00152 #warning FIXME: If media data is not set, verifier is not set. Should the media be refused instead?
00153 ZYPP_CAUGHT(excpt_r);
00154 WAR << "Verifier not found" << endl;
00155 }
00156 medias[medianr] = id;
00157
00158 if (! noattach)
00159 media_mgr.attach(id);
00160
00161 return id;
00162 }
00163
00164 Url MediaSet::rewriteUrl (const Url & url_r, const media::MediaNr medianr)
00165 {
00166 std::string scheme = url_r.getScheme();
00167 if (scheme == "cd" || scheme == "dvd")
00168 return url_r;
00169
00170 DBG << "Rewriting url " << url_r << endl;
00171
00172 if( scheme == "iso")
00173 {
00174 std::string isofile = url_r.getQueryParam("iso");
00175 boost::regex e("^(.*(cd|dvd))([0-9]+)(\\.iso)$", boost::regex::icase);
00176 boost::smatch what;
00177 if(boost::regex_match(isofile, what, e, boost::match_extra))
00178 {
00179 Url url( url_r);
00180
00181 isofile = what[1] + str::numstring(medianr) + what[4];
00182 url.setQueryParam("iso", isofile);
00183
00184 DBG << "Url rewrite result: " << url << endl;
00185 return url;
00186 }
00187 }
00188 else
00189 {
00190 std::string pathname = url_r.getPathName();
00191 boost::regex e("^(.*(cd|dvd))([0-9]+)(/?)$", boost::regex::icase);
00192 boost::smatch what;
00193 if(boost::regex_match(pathname, what, e, boost::match_extra))
00194 {
00195 Url url( url_r);
00196
00197 pathname = what[1] + str::numstring(medianr) + what[4];
00198 url.setPathName(pathname);
00199
00200 DBG << "Url rewrite result: " << url << endl;
00201
00202 return url;
00203 }
00204 }
00205 return url_r;
00206 }
00207
00208 std::ostream & MediaSet::dumpOn( std::ostream & str ) const
00209 { return str << "MediaSet"; }
00210
00211
00213 }
00216 }