1 | |
---|
2 | #include "Sources.hpp" |
---|
3 | #include "Commands.hpp" |
---|
4 | |
---|
5 | #include <iostream> |
---|
6 | |
---|
7 | #include <glibmm.h> |
---|
8 | #include <giomm.h> |
---|
9 | |
---|
10 | using namespace std; |
---|
11 | using namespace net::lliurex::apt; |
---|
12 | using namespace net::lliurex::apt::command; |
---|
13 | |
---|
14 | Match::Match(Sources * sources,FindLineStatus status) |
---|
15 | { |
---|
16 | this->sources=sources; |
---|
17 | this->status=status; |
---|
18 | } |
---|
19 | |
---|
20 | void net::lliurex::apt::command::Init() |
---|
21 | { |
---|
22 | Glib::init(); |
---|
23 | Gio::init(); |
---|
24 | } |
---|
25 | |
---|
26 | |
---|
27 | vector<Sources> net::lliurex::apt::command::LoadConf() |
---|
28 | { |
---|
29 | vector<Sources> conf; |
---|
30 | |
---|
31 | Glib::RefPtr<Gio::File> file = Gio::File::create_for_path(CONF_PATH); |
---|
32 | Glib::RefPtr<Gio::FileEnumerator> dir = file->enumerate_children("*"); |
---|
33 | Glib::RefPtr<Gio::FileInfo> info = dir->next_file(); |
---|
34 | |
---|
35 | while(info) |
---|
36 | { |
---|
37 | |
---|
38 | Sources src(CONF_PATH+info->get_name()); |
---|
39 | conf.push_back(src); |
---|
40 | info=dir->next_file(); |
---|
41 | } |
---|
42 | |
---|
43 | return conf; |
---|
44 | } |
---|
45 | |
---|
46 | void net::lliurex::apt::command::Show() |
---|
47 | { |
---|
48 | Sources src(SOURCES_LIST); |
---|
49 | src.Print(); |
---|
50 | } |
---|
51 | |
---|
52 | void net::lliurex::apt::command::List(vector<Sources> & conf) |
---|
53 | { |
---|
54 | for(int n=0;n<conf.size();n++) |
---|
55 | { |
---|
56 | |
---|
57 | cout<<"["<<conf[n].name["C"]<<"]"<<endl; |
---|
58 | if(conf[n].optional) |
---|
59 | { |
---|
60 | cout<<"* optional"<<endl; |
---|
61 | } |
---|
62 | |
---|
63 | conf[n].Print(); |
---|
64 | cout<<endl; |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | vector<Match> net::lliurex::apt::command::Check(Sources & src,vector<Sources> & conf) |
---|
69 | { |
---|
70 | |
---|
71 | vector<Match> status; |
---|
72 | |
---|
73 | for(int n=0;n<conf.size();n++) |
---|
74 | { |
---|
75 | Sources & cf = conf[n]; |
---|
76 | int f=0; |
---|
77 | int p=0; |
---|
78 | int nf=0; |
---|
79 | for(int m=0;m<cf.lines.size();m++) |
---|
80 | { |
---|
81 | FindLineStatus s = src.Find(cf.lines[m]); |
---|
82 | |
---|
83 | switch(s) |
---|
84 | { |
---|
85 | case FindLineStatus::Found: |
---|
86 | f++; |
---|
87 | break; |
---|
88 | |
---|
89 | case FindLineStatus::Partial: |
---|
90 | p++; |
---|
91 | break; |
---|
92 | |
---|
93 | case FindLineStatus::NotFound: |
---|
94 | nf++; |
---|
95 | break; |
---|
96 | |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | if(f>0 && f==cf.lines.size()) |
---|
101 | { |
---|
102 | |
---|
103 | status.push_back(Match(&conf[n],FindLineStatus::Found)); |
---|
104 | } |
---|
105 | else |
---|
106 | { |
---|
107 | if(f>0 || p>0) |
---|
108 | { |
---|
109 | status.push_back(Match(&conf[n],FindLineStatus::Partial)); |
---|
110 | } |
---|
111 | else |
---|
112 | { |
---|
113 | status.push_back(Match(&conf[n],FindLineStatus::NotFound)); |
---|
114 | } |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | return status; |
---|
120 | } |
---|
121 | |
---|
122 | |
---|
123 | Sources net::lliurex::apt::command::Set(Sources & isrc,vector<Sources> & conf,vector<string> & targets,bool reset) |
---|
124 | { |
---|
125 | //there can only be one! |
---|
126 | Sources * mandatory=nullptr; |
---|
127 | vector<Sources *> optional; |
---|
128 | |
---|
129 | /* |
---|
130 | check if exists, test for valid combinations |
---|
131 | and fill target Sources |
---|
132 | */ |
---|
133 | for(string target : targets) |
---|
134 | { |
---|
135 | //cout<<"* ["<<target<<"]"<<endl; |
---|
136 | |
---|
137 | bool found = false; |
---|
138 | |
---|
139 | for(int n=0;n<conf.size();n++) |
---|
140 | { |
---|
141 | if(conf[n].name["C"]==target) |
---|
142 | { |
---|
143 | found = true; |
---|
144 | |
---|
145 | if(!conf[n].optional) |
---|
146 | { |
---|
147 | if(mandatory==nullptr) |
---|
148 | { |
---|
149 | mandatory=&conf[n]; |
---|
150 | } |
---|
151 | else |
---|
152 | { |
---|
153 | throw runtime_error("Only one not optional source should be selected"); |
---|
154 | } |
---|
155 | } |
---|
156 | else |
---|
157 | { |
---|
158 | optional.push_back(&conf[n]); |
---|
159 | } |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | if(!found) |
---|
164 | { |
---|
165 | throw runtime_error("Target ["+target+"] not found"); |
---|
166 | } |
---|
167 | } |
---|
168 | |
---|
169 | /* |
---|
170 | this flag wipes out sources configuration |
---|
171 | */ |
---|
172 | if(reset) |
---|
173 | { |
---|
174 | Sources src; |
---|
175 | |
---|
176 | if(mandatory!=nullptr) |
---|
177 | src=src+(*mandatory); |
---|
178 | |
---|
179 | for(int n=0;n<optional.size();n++) |
---|
180 | { |
---|
181 | src=src+(*optional[n]); |
---|
182 | } |
---|
183 | |
---|
184 | |
---|
185 | |
---|
186 | return src; |
---|
187 | |
---|
188 | } |
---|
189 | else |
---|
190 | { |
---|
191 | /* |
---|
192 | this second step removes from the sources.list |
---|
193 | those lines matching at conf sources |
---|
194 | */ |
---|
195 | Sources src=isrc; |
---|
196 | |
---|
197 | vector<SourceLine> refactor; |
---|
198 | |
---|
199 | for(int n=0;n<src.lines.size();n++) |
---|
200 | { |
---|
201 | |
---|
202 | SourceLine * line = &src.lines[n]; |
---|
203 | |
---|
204 | int nf = 0; |
---|
205 | for(int m=0;m<conf.size();m++) |
---|
206 | { |
---|
207 | if(conf[m].optional)continue; |
---|
208 | |
---|
209 | FindLineStatus status=conf[m].Find(*line); |
---|
210 | |
---|
211 | if(status!=FindLineStatus::NotFound)nf++; |
---|
212 | } |
---|
213 | |
---|
214 | if(nf!=0) |
---|
215 | { |
---|
216 | //cout<<"* removing "<<line->uri<<" "<<line->dist<<endl; |
---|
217 | } |
---|
218 | else |
---|
219 | { |
---|
220 | refactor.push_back(*line); |
---|
221 | } |
---|
222 | |
---|
223 | } |
---|
224 | |
---|
225 | /* |
---|
226 | rebuilding sources.list |
---|
227 | */ |
---|
228 | src.lines=refactor; |
---|
229 | |
---|
230 | if(mandatory!=nullptr) |
---|
231 | src=src+(*mandatory); |
---|
232 | |
---|
233 | for(int n=0;n<optional.size();n++) |
---|
234 | { |
---|
235 | src=src+(*optional[n]); |
---|
236 | } |
---|
237 | |
---|
238 | |
---|
239 | |
---|
240 | return src; |
---|
241 | } |
---|
242 | } |
---|
243 | |
---|
244 | |
---|
245 | |
---|
246 | Sources net::lliurex::apt::command::Add(Sources & isrc,string line) |
---|
247 | { |
---|
248 | Sources src=isrc; |
---|
249 | src.Add(line); |
---|
250 | |
---|
251 | return src; |
---|
252 | } |
---|