- Pass the media set to the Downloaders instead of having one in the object
[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 Pathname &path )
22     : _path(path)
23 {
24
25 }
26
27 RepoStatus Downloader::status( MediaSetAccess &media )
28 {
29   Pathname content = media.provideFile( _path + "/content");
30   return RepoStatus(content);
31 }
32
33 void Downloader::download( MediaSetAccess &media,
34                            const Pathname &dest_dir,
35                            const ProgressData::ReceiverFnc & progress )
36 {
37   downloadMediaInfo( dest_dir, media );
38   
39   SignatureFileChecker sigchecker;
40
41   Pathname sig = _path + "/content.asc";
42   if ( media.doesFileExist(sig) )
43   {
44     this->enqueue( OnMediaLocation( sig, 1 ) );
45     this->start( dest_dir, media );
46     this->reset();
47
48     sigchecker = SignatureFileChecker( dest_dir + sig );
49   }
50
51   Pathname key = _path + "/content.key";
52   if ( media.doesFileExist(key) )
53   {
54     this->enqueue( OnMediaLocation( key, 1 ) );
55     this->start( dest_dir, media );
56     this->reset();
57     sigchecker.addPublicKey(dest_dir + key);
58   }
59
60
61   this->enqueue( OnMediaLocation( _path + "/content", 1 ), sigchecker );
62   this->start( dest_dir, media );
63   this->reset();
64
65   std::ifstream file((dest_dir +  _path + "/content").asString().c_str());
66   std::string buffer;
67   Pathname descr_dir;
68
69   // FIXME Note this code assumes DESCR comes before as META
70   string value;
71   while (file && !file.eof())
72   {
73     getline(file, buffer);
74     if ( buffer.substr( 0, 5 ) == "DESCR" )
75     {
76       std::vector<std::string> words;
77       if ( str::split( buffer, std::back_inserter(words) ) != 2 )
78       {
79         // error
80         ZYPP_THROW(Exception("bad DESCR line"));
81       }
82       descr_dir = words[1];
83     }
84     else if ( buffer.substr( 0, 4 ) == "META" )
85     {
86       std::vector<std::string> words;
87       if ( str::split( buffer, std::back_inserter(words) ) != 4 )
88       {
89         // error
90         ZYPP_THROW(Exception("bad META line"));
91       }
92       OnMediaLocation location( _path + descr_dir + words[3], 1 );
93       location.setChecksum( CheckSum( words[1], words[2] ) );
94       this->enqueueDigested(location);
95     }
96     else if (buffer.substr( 0, 3 ) == "KEY")
97     {
98       std::vector<std::string> words;
99       if ( str::split( buffer, std::back_inserter(words) ) != 4 )
100       {
101         // error
102         ZYPP_THROW(Exception("bad KEY line"));
103       }
104       OnMediaLocation location( _path + words[3], 1 );
105       location.setChecksum( CheckSum( words[1], words[2] ) );
106       this->enqueueDigested(location);
107     }
108   }
109   file.close();
110   this->start( dest_dir, media );
111 }
112
113 }// ns susetags
114 }// ns source
115 } // ns zypp