Merge from master
[platform/upstream/libzypp.git] / zypp / repo / susetags / Downloader.cc
1
2 #include <fstream>
3 #include "zypp/base/String.h"
4 #include "zypp/OnMediaLocation.h"
5 #include "zypp/MediaSetAccess.h"
6 #include "zypp/Fetcher.h"
7 #include "zypp/repo/MediaInfoDownloader.h"
8
9 #include "zypp/repo/susetags/Downloader.h"
10 #include "zypp/base/UserRequestException.h"
11
12 using namespace std;
13
14 namespace zypp
15 {
16 namespace repo
17 {
18 namespace susetags
19 {
20
21 Downloader::Downloader( const Url &url, const Pathname &path )
22     : _url(url), _path(path)
23 {
24
25 }
26
27 RepoStatus Downloader::status()
28 {
29   MediaSetAccess media(_url, _path);
30   Pathname content = media.provideFile("/content");
31   return RepoStatus(content);
32 }
33
34 void Downloader::download( const Pathname &dest_dir,
35                            const ProgressData::ReceiverFnc & progress )
36 {
37   MediaSetAccess media(_url);
38   Fetcher fetcher;
39   downloadMediaInfo( dest_dir, media );
40   
41   SignatureFileChecker sigchecker;
42
43   Pathname sig = _path + "/content.asc";
44   if ( media.doesFileExist(sig) )
45   {
46     fetcher.enqueue( OnMediaLocation( sig, 1 ) );
47     fetcher.start( dest_dir, media );
48     fetcher.reset();
49
50     sigchecker = SignatureFileChecker( dest_dir + sig );
51   }
52
53   Pathname key = _path + "/content.key";
54   if ( media.doesFileExist(key) )
55   {
56     fetcher.enqueue( OnMediaLocation( key, 1 ) );
57     fetcher.start( dest_dir, media );
58     fetcher.reset();
59     sigchecker.addPublicKey(dest_dir + key);
60   }
61
62
63   fetcher.enqueue( OnMediaLocation( _path + "/content", 1 ), sigchecker );
64   fetcher.start( dest_dir, media );
65   fetcher.reset();
66
67   std::ifstream file((dest_dir +  _path + "/content").asString().c_str());
68   std::string buffer;
69   Pathname descr_dir;
70
71   // FIXME Note this code assumes DESCR comes before as META
72   string value;
73   while (file && !file.eof())
74   {
75     getline(file, buffer);
76     if ( buffer.substr( 0, 5 ) == "DESCR" )
77     {
78       std::vector<std::string> words;
79       if ( str::split( buffer, std::back_inserter(words) ) != 2 )
80       {
81         // error
82         ZYPP_THROW(Exception("bad DESCR line"));
83       }
84       descr_dir = words[1];
85     }
86     else if ( buffer.substr( 0, 4 ) == "META" )
87     {
88       std::vector<std::string> words;
89       if ( str::split( buffer, std::back_inserter(words) ) != 4 )
90       {
91         // error
92         ZYPP_THROW(Exception("bad META line"));
93       }
94       OnMediaLocation location( _path + descr_dir + words[3], 1 );
95       location.setChecksum( CheckSum( words[1], words[2] ) );
96       fetcher.enqueueDigested(location);
97     }
98     else if (buffer.substr( 0, 3 ) == "KEY")
99     {
100       std::vector<std::string> words;
101       if ( str::split( buffer, std::back_inserter(words) ) != 4 )
102       {
103         // error
104         ZYPP_THROW(Exception("bad KEY line"));
105       }
106       OnMediaLocation location( _path + words[3], 1 );
107       location.setChecksum( CheckSum( words[1], words[2] ) );
108       fetcher.enqueueDigested(location);
109     }
110   }
111   file.close();
112   fetcher.start( dest_dir, media );
113 }
114
115 }// ns susetags
116 }// ns source
117 } // ns zypp