Merge branch 'master' of gitorious.org:opensuse/libzypp
[platform/upstream/libzypp.git] / zypp / repo / RepoMirrorList.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/repo/RepoMirrorList.cc
10  *
11 */
12
13 #include <iostream>
14 #include <vector>
15 #include "zypp/repo/RepoMirrorList.h"
16 #include "zypp/media/MetaLinkParser.h"
17 #include "zypp/MediaSetAccess.h"
18 #include "zypp/base/LogTools.h"
19
20 using namespace std;
21
22 ///////////////////////////////////////////////////////////////////
23 namespace zypp
24 { /////////////////////////////////////////////////////////////////
25   ///////////////////////////////////////////////////////////////////
26   namespace repo
27   { /////////////////////////////////////////////////////////////////
28
29     RepoMirrorList::RepoMirrorList( const Url &url )
30     {
31       Pathname filepath (url.getPathName());
32       Url abs_url (url);
33       std::vector<Url> my_urls;
34
35       DBG << "Getting MirrorList from URL: " << abs_url << endl;
36
37       abs_url.setPathName("");
38       abs_url.setQueryParam("mediahandler", "curl");
39
40       MediaSetAccess access (abs_url);
41       Pathname tmpfile = access.provideFile(filepath);
42
43       InputStream tmpfstream (tmpfile);
44
45       if ( url.asString().find("/metalink") != string::npos )
46       {
47         media::MetaLinkParser metalink;
48         metalink.parse(tmpfstream);
49         my_urls = metalink.getUrls();
50       }
51       else
52       {
53         string tmpurl;
54         while (getline(tmpfstream.stream(), tmpurl))
55         {
56           my_urls.push_back(Url(tmpurl));
57         }
58       }
59
60       int valid_urls = 0;
61       for (std::vector<Url>::iterator it = my_urls.begin() ; it != my_urls.end() and valid_urls < 4 ; ++it)
62       {
63         if ( it->getScheme() != "rsync" )
64         {
65           size_t delpos = it->getPathName().find("repodata/repomd.xml");
66           if( delpos != string::npos )
67           {
68             it->setPathName( it->getPathName().erase(delpos)  );
69           }
70           urls.push_back(*it);
71           ++valid_urls;
72         }
73       }
74     }
75     
76     std::vector<Url> RepoMirrorList::getUrls() const
77     {
78       return urls;
79     }
80
81     RepoMirrorList::~RepoMirrorList()
82     {}
83
84    /////////////////////////////////////////////////////////////////
85   } // namespace repo
86   ///////////////////////////////////////////////////////////////////
87   /////////////////////////////////////////////////////////////////
88 } // namespace zypp
89 ///////////////////////////////////////////////////////////////////