1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/repo/RepoMirrorList.cc
16 #include "zypp/repo/RepoMirrorList.h"
17 #include "zypp/media/MetaLinkParser.h"
18 #include "zypp/MediaSetAccess.h"
19 #include "zypp/base/LogTools.h"
20 #include "zypp/ZConfig.h"
21 #include "zypp/PathInfo.h"
25 ///////////////////////////////////////////////////////////////////
27 { /////////////////////////////////////////////////////////////////
28 ///////////////////////////////////////////////////////////////////
30 { /////////////////////////////////////////////////////////////////
32 RepoMirrorList::RepoMirrorList( const Url &url, const Pathname &metadatapath )
34 std::vector<Url> my_urls;
35 Pathname tmpfile, cachefile;
37 if ( url.asString().find("/metalink") != string::npos )
38 cachefile = metadatapath / "mirrorlist.xml";
40 cachefile = metadatapath / "mirrorlist.txt";
41 //cachefile = ZConfig::instance().repoMetadataPath() / Pathname(escaped_alias) / "mirrorlist.txt";
43 zypp::filesystem::PathInfo cacheinfo (cachefile);
45 if ( !cacheinfo.isFile() || cacheinfo.mtime() < time(NULL) - (long) ZConfig::instance().repo_refresh_delay() * 60 )
47 Pathname filepath (url.getPathName());
50 DBG << "Getting MirrorList from URL: " << abs_url << endl;
52 abs_url.setPathName("");
53 abs_url.setQueryParam("mediahandler", "curl");
55 MediaSetAccess access (abs_url);
56 tmpfile = access.provideFile(filepath);
58 // Create directory, if not existing
59 zypp::filesystem::assert_dir(metadatapath);
61 DBG << "Copy MirrorList file to " << cachefile << endl;
62 zypp::filesystem::copy(tmpfile, cachefile);
65 if ( url.asString().find("/metalink") != string::npos )
67 my_urls = parseXML(cachefile);
71 my_urls = parseTXT(cachefile);
77 DBG << "Removing Cachefile as it contains no URLs" << endl;
78 zypp::filesystem::unlink(cachefile);
82 RepoMirrorList::RepoMirrorList( const Url &url )
84 std::vector<Url> my_urls;
87 Pathname filepath (url.getPathName());
90 DBG << "Getting MirrorList from URL: " << abs_url << endl;
92 abs_url.setPathName("");
93 abs_url.setQueryParam("mediahandler", "curl");
95 MediaSetAccess access (abs_url);
96 tmpfile = access.provideFile(filepath);
98 if ( url.asString().find("/metalink") != string::npos )
100 my_urls = parseXML(tmpfile);
104 my_urls = parseTXT(tmpfile);
110 void RepoMirrorList::setUrls( std::vector<Url> my_urls )
113 for (std::vector<Url>::iterator it = my_urls.begin() ; it != my_urls.end() and valid_urls < 4 ; ++it)
115 if ( it->getScheme() != "rsync" )
117 size_t delpos = it->getPathName().find("repodata/repomd.xml");
118 if( delpos != string::npos )
120 it->setPathName( it->getPathName().erase(delpos) );
128 std::vector<Url> RepoMirrorList::parseXML( const Pathname &tmpfile ) const
130 InputStream tmpfstream (tmpfile);
131 media::MetaLinkParser metalink;
132 metalink.parse(tmpfstream);
133 return metalink.getUrls();
136 std::vector<Url> RepoMirrorList::parseTXT( const Pathname &tmpfile ) const
138 InputStream tmpfstream (tmpfile);
139 std::vector<Url> my_urls;
141 while (getline(tmpfstream.stream(), tmpurl))
143 my_urls.push_back(Url(tmpurl));
148 std::vector<Url> RepoMirrorList::getUrls() const
153 RepoMirrorList::~RepoMirrorList()
156 /////////////////////////////////////////////////////////////////
158 ///////////////////////////////////////////////////////////////////
159 /////////////////////////////////////////////////////////////////
161 ///////////////////////////////////////////////////////////////////