Commit | Line | Data |
---|---|---|
6c139d6e AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
34b53501 | 3 | // $Id: sourcelist.cc,v 1.3 2002/08/15 20:51:37 niemeyer Exp $ |
6c139d6e AL |
4 | /* ###################################################################### |
5 | ||
6 | List of Sources | |
7 | ||
8 | ##################################################################### */ | |
9 | /*}}}*/ | |
10 | // Include Files /*{{{*/ | |
ea542140 DK |
11 | #include<config.h> |
12 | ||
094a497d AL |
13 | #include <apt-pkg/sourcelist.h> |
14 | #include <apt-pkg/error.h> | |
15 | #include <apt-pkg/fileutl.h> | |
cdcc6d34 | 16 | #include <apt-pkg/strutl.h> |
7db98ffc | 17 | #include <apt-pkg/configuration.h> |
472ff00e DK |
18 | #include <apt-pkg/metaindex.h> |
19 | #include <apt-pkg/indexfile.h> | |
a537ce19 | 20 | #include <apt-pkg/tagfile.h> |
6c139d6e | 21 | |
90f057fd | 22 | #include <fstream> |
41e6bd08 | 23 | #include <algorithm> |
ea542140 DK |
24 | |
25 | #include <apti18n.h> | |
b2e465d6 AL |
26 | /*}}}*/ |
27 | ||
4d6f8bdf AL |
28 | using namespace std; |
29 | ||
30 | // Global list of Items supported | |
b2e465d6 AL |
31 | static pkgSourceList::Type *ItmList[10]; |
32 | pkgSourceList::Type **pkgSourceList::Type::GlobalList = ItmList; | |
33 | unsigned long pkgSourceList::Type::GlobalListLen = 0; | |
34 | ||
35 | // Type::Type - Constructor /*{{{*/ | |
36 | // --------------------------------------------------------------------- | |
37 | /* Link this to the global list of items*/ | |
dcaa1185 | 38 | pkgSourceList::Type::Type() : Name(NULL), Label(NULL) |
b2e465d6 AL |
39 | { |
40 | ItmList[GlobalListLen] = this; | |
41 | GlobalListLen++; | |
42 | } | |
43 | /*}}}*/ | |
44 | // Type::GetType - Get a specific meta for a given type /*{{{*/ | |
45 | // --------------------------------------------------------------------- | |
46 | /* */ | |
47 | pkgSourceList::Type *pkgSourceList::Type::GetType(const char *Type) | |
48 | { | |
49 | for (unsigned I = 0; I != GlobalListLen; I++) | |
50 | if (strcmp(GlobalList[I]->Name,Type) == 0) | |
51 | return GlobalList[I]; | |
52 | return 0; | |
53 | } | |
54 | /*}}}*/ | |
55 | // Type::FixupURI - Normalize the URI and check it.. /*{{{*/ | |
56 | // --------------------------------------------------------------------- | |
57 | /* */ | |
58 | bool pkgSourceList::Type::FixupURI(string &URI) const | |
59 | { | |
60 | if (URI.empty() == true) | |
61 | return false; | |
62 | ||
63 | if (URI.find(':') == string::npos) | |
64 | return false; | |
65 | ||
66 | URI = SubstVar(URI,"$(ARCH)",_config->Find("APT::Architecture")); | |
67 | ||
a7c835af | 68 | // Make sure that the URI is / postfixed |
b2e465d6 AL |
69 | if (URI[URI.size() - 1] != '/') |
70 | URI += '/'; | |
71 | ||
72 | return true; | |
73 | } | |
74 | /*}}}*/ | |
7037aab5 MV |
75 | bool pkgSourceList::Type::ParseStanza(vector<metaIndex *> &List, |
76 | pkgTagSection &Tags, | |
77 | int i, | |
78 | FileFd &Fd) | |
79 | { | |
80 | map<string, string> Options; | |
81 | ||
866e9fad | 82 | string URI = Tags.FindS("URI"); |
7037aab5 MV |
83 | if (!FixupURI(URI)) |
84 | { | |
85 | _error->Error(_("Malformed stanza %u in source list %s (URI parse)"),i,Fd.Name().c_str()); | |
86 | return false; | |
87 | } | |
88 | ||
7037aab5 MV |
89 | // Define external/internal options |
90 | const char* option_deb822[] = { | |
866e9fad | 91 | "Architectures", "Architectures-Add", "Architectures-Remove", "Trusted", |
7037aab5 MV |
92 | }; |
93 | const char* option_internal[] = { | |
94 | "arch", "arch+", "arch-", "trusted", | |
95 | }; | |
96 | for (unsigned int j=0; j < sizeof(option_deb822)/sizeof(char*); j++) | |
97 | if (Tags.Exists(option_deb822[j])) | |
41e6bd08 MV |
98 | { |
99 | // for deb822 the " " is the delimiter, but the backend expects "," | |
100 | std::string option = Tags.FindS(option_deb822[j]); | |
101 | std::replace(option.begin(), option.end(), ' ', ','); | |
102 | Options[option_internal[j]] = option; | |
103 | } | |
7037aab5 | 104 | |
d73743dd | 105 | // now create one item per suite/section |
6c069a22 | 106 | string Suite = Tags.FindS("Suites"); |
d73743dd | 107 | Suite = SubstVar(Suite,"$(ARCH)",_config->Find("APT::Architecture")); |
6c069a22 | 108 | string const Section = Tags.FindS("Sections"); |
d73743dd MV |
109 | |
110 | std::vector<std::string> list_dist = StringSplit(Suite, " "); | |
111 | std::vector<std::string> list_section = StringSplit(Section, " "); | |
112 | for (std::vector<std::string>::const_iterator I = list_dist.begin(); | |
113 | I != list_dist.end(); I++) | |
114 | { | |
115 | for (std::vector<std::string>::const_iterator J = list_section.begin(); | |
116 | J != list_section.end(); J++) | |
117 | { | |
118 | if (CreateItem(List, URI, (*I), (*J), Options) == false) | |
119 | { | |
120 | return false; | |
121 | } | |
122 | } | |
123 | } | |
7037aab5 MV |
124 | return true; |
125 | } | |
126 | ||
b2e465d6 AL |
127 | // Type::ParseLine - Parse a single line /*{{{*/ |
128 | // --------------------------------------------------------------------- | |
129 | /* This is a generic one that is the 'usual' format for sources.list | |
130 | Weird types may override this. */ | |
7db98ffc | 131 | bool pkgSourceList::Type::ParseLine(vector<metaIndex *> &List, |
b2e465d6 | 132 | const char *Buffer, |
5dd4c8b8 DK |
133 | unsigned long const &CurLine, |
134 | string const &File) const | |
b2e465d6 | 135 | { |
5dd4c8b8 DK |
136 | for (;Buffer != 0 && isspace(*Buffer); ++Buffer); // Skip whitespaces |
137 | ||
138 | // Parse option field if it exists | |
139 | // e.g.: [ option1=value1 option2=value2 ] | |
140 | map<string, string> Options; | |
141 | if (Buffer != 0 && Buffer[0] == '[') | |
142 | { | |
143 | ++Buffer; // ignore the [ | |
144 | for (;Buffer != 0 && isspace(*Buffer); ++Buffer); // Skip whitespaces | |
145 | while (*Buffer != ']') | |
146 | { | |
147 | // get one option, e.g. option1=value1 | |
148 | string option; | |
149 | if (ParseQuoteWord(Buffer,option) == false) | |
150 | return _error->Error(_("Malformed line %lu in source list %s ([option] unparseable)"),CurLine,File.c_str()); | |
151 | ||
152 | if (option.length() < 3) | |
153 | return _error->Error(_("Malformed line %lu in source list %s ([option] too short)"),CurLine,File.c_str()); | |
154 | ||
6838dd87 DK |
155 | // accept options even if the last has no space before the ]-end marker |
156 | if (option.at(option.length()-1) == ']') | |
157 | { | |
158 | for (; *Buffer != ']'; --Buffer); | |
159 | option.resize(option.length()-1); | |
160 | } | |
161 | ||
5dd4c8b8 DK |
162 | size_t const needle = option.find('='); |
163 | if (needle == string::npos) | |
164 | return _error->Error(_("Malformed line %lu in source list %s ([%s] is not an assignment)"),CurLine,File.c_str(), option.c_str()); | |
165 | ||
166 | string const key = string(option, 0, needle); | |
167 | string const value = string(option, needle + 1, option.length()); | |
168 | ||
169 | if (key.empty() == true) | |
170 | return _error->Error(_("Malformed line %lu in source list %s ([%s] has no key)"),CurLine,File.c_str(), option.c_str()); | |
171 | ||
172 | if (value.empty() == true) | |
173 | return _error->Error(_("Malformed line %lu in source list %s ([%s] key %s has no value)"),CurLine,File.c_str(),option.c_str(),key.c_str()); | |
174 | ||
175 | Options[key] = value; | |
176 | } | |
177 | ++Buffer; // ignore the ] | |
178 | for (;Buffer != 0 && isspace(*Buffer); ++Buffer); // Skip whitespaces | |
179 | } | |
180 | ||
b2e465d6 AL |
181 | string URI; |
182 | string Dist; | |
5dd4c8b8 DK |
183 | string Section; |
184 | ||
b2e465d6 AL |
185 | if (ParseQuoteWord(Buffer,URI) == false) |
186 | return _error->Error(_("Malformed line %lu in source list %s (URI)"),CurLine,File.c_str()); | |
187 | if (ParseQuoteWord(Buffer,Dist) == false) | |
188 | return _error->Error(_("Malformed line %lu in source list %s (dist)"),CurLine,File.c_str()); | |
189 | ||
190 | if (FixupURI(URI) == false) | |
191 | return _error->Error(_("Malformed line %lu in source list %s (URI parse)"),CurLine,File.c_str()); | |
192 | ||
193 | // Check for an absolute dists specification. | |
194 | if (Dist.empty() == false && Dist[Dist.size() - 1] == '/') | |
195 | { | |
196 | if (ParseQuoteWord(Buffer,Section) == true) | |
db0db9fe | 197 | return _error->Error(_("Malformed line %lu in source list %s (absolute dist)"),CurLine,File.c_str()); |
b2e465d6 | 198 | Dist = SubstVar(Dist,"$(ARCH)",_config->Find("APT::Architecture")); |
5dd4c8b8 | 199 | return CreateItem(List, URI, Dist, Section, Options); |
b2e465d6 AL |
200 | } |
201 | ||
202 | // Grab the rest of the dists | |
203 | if (ParseQuoteWord(Buffer,Section) == false) | |
204 | return _error->Error(_("Malformed line %lu in source list %s (dist parse)"),CurLine,File.c_str()); | |
205 | ||
206 | do | |
207 | { | |
5dd4c8b8 | 208 | if (CreateItem(List, URI, Dist, Section, Options) == false) |
b2e465d6 AL |
209 | return false; |
210 | } | |
211 | while (ParseQuoteWord(Buffer,Section) == true); | |
212 | ||
213 | return true; | |
214 | } | |
6c139d6e | 215 | /*}}}*/ |
6c139d6e AL |
216 | // SourceList::pkgSourceList - Constructors /*{{{*/ |
217 | // --------------------------------------------------------------------- | |
218 | /* */ | |
219 | pkgSourceList::pkgSourceList() | |
220 | { | |
221 | } | |
222 | ||
223 | pkgSourceList::pkgSourceList(string File) | |
224 | { | |
225 | Read(File); | |
226 | } | |
227 | /*}}}*/ | |
1c193e02 AL |
228 | // SourceList::~pkgSourceList - Destructor /*{{{*/ |
229 | // --------------------------------------------------------------------- | |
230 | /* */ | |
231 | pkgSourceList::~pkgSourceList() | |
232 | { | |
f7f0d6c7 | 233 | for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I) |
1c193e02 | 234 | delete *I; |
1c193e02 AL |
235 | } |
236 | /*}}}*/ | |
6c139d6e AL |
237 | // SourceList::ReadMainList - Read the main source list from etc /*{{{*/ |
238 | // --------------------------------------------------------------------- | |
239 | /* */ | |
240 | bool pkgSourceList::ReadMainList() | |
241 | { | |
34b53501 MV |
242 | // CNC:2003-03-03 - Multiple sources list support. |
243 | bool Res = true; | |
244 | #if 0 | |
245 | Res = ReadVendors(); | |
246 | if (Res == false) | |
247 | return false; | |
248 | #endif | |
249 | ||
250 | Reset(); | |
251 | // CNC:2003-11-28 - Entries in sources.list have priority over | |
252 | // entries in sources.list.d. | |
253 | string Main = _config->FindFile("Dir::Etc::sourcelist"); | |
67793cf3 JAK |
254 | string Parts = _config->FindDir("Dir::Etc::sourceparts"); |
255 | ||
36f1098a | 256 | if (RealFileExists(Main) == true) |
6009e60d | 257 | Res &= ReadAppend(Main); |
448eaf8b | 258 | else if (DirectoryExists(Parts) == false) |
67793cf3 | 259 | // Only warn if there are no sources.list.d. |
448eaf8b | 260 | _error->WarningE("DirectoryExists", _("Unable to read %s"), Parts.c_str()); |
34b53501 | 261 | |
448eaf8b | 262 | if (DirectoryExists(Parts) == true) |
34b53501 | 263 | Res &= ReadSourceDir(Parts); |
36f1098a | 264 | else if (RealFileExists(Main) == false) |
67793cf3 | 265 | // Only warn if there is no sources.list file. |
36f1098a | 266 | _error->WarningE("RealFileExists", _("Unable to read %s"), Main.c_str()); |
6009e60d | 267 | |
34b53501 | 268 | return Res; |
6c139d6e AL |
269 | } |
270 | /*}}}*/ | |
34b53501 MV |
271 | // SourceList::Reset - Clear the sourcelist contents /*{{{*/ |
272 | // --------------------------------------------------------------------- | |
273 | /* */ | |
274 | void pkgSourceList::Reset() | |
275 | { | |
f7f0d6c7 | 276 | for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I) |
34b53501 MV |
277 | delete *I; |
278 | SrcList.erase(SrcList.begin(),SrcList.end()); | |
279 | } | |
280 | /*}}}*/ | |
6c139d6e AL |
281 | // SourceList::Read - Parse the sourcelist file /*{{{*/ |
282 | // --------------------------------------------------------------------- | |
283 | /* */ | |
284 | bool pkgSourceList::Read(string File) | |
34b53501 MV |
285 | { |
286 | Reset(); | |
287 | return ReadAppend(File); | |
288 | } | |
289 | /*}}}*/ | |
6f447813 MV |
290 | // SourceList::ReadAppend - Parse a sourcelist file /*{{{*/ |
291 | // --------------------------------------------------------------------- | |
292 | /* */ | |
293 | bool pkgSourceList::ReadAppend(string File) | |
294 | { | |
fce9f472 | 295 | if (_config->FindB("APT::Sources::Use-Deb822", true) == true) |
4194c9ae MV |
296 | { |
297 | int lines_parsed =ParseFileDeb822(File); | |
298 | if (lines_parsed < 0) | |
299 | return false; | |
300 | else if (lines_parsed > 0) | |
6f447813 | 301 | return true; |
4194c9ae MV |
302 | // no lines parsed ... fall through and use old style parser |
303 | } | |
1fa78a8a MV |
304 | return ParseFileOldStyle(File); |
305 | } | |
6f447813 | 306 | |
1fa78a8a MV |
307 | // SourceList::ReadFileOldStyle - Read Traditional style sources.list /*{{{*/ |
308 | // --------------------------------------------------------------------- | |
309 | /* */ | |
310 | bool pkgSourceList::ParseFileOldStyle(string File) | |
311 | { | |
6c139d6e | 312 | // Open the stream for reading |
4d6f8bdf | 313 | ifstream F(File.c_str(),ios::in /*| ios::nocreate*/); |
6c139d6e | 314 | if (!F != 0) |
b2e465d6 | 315 | return _error->Errno("ifstream::ifstream",_("Opening %s"),File.c_str()); |
a537ce19 | 316 | |
34b53501 MV |
317 | // CNC:2003-12-10 - 300 is too short. |
318 | char Buffer[1024]; | |
6c139d6e AL |
319 | |
320 | int CurLine = 0; | |
321 | while (F.eof() == false) | |
322 | { | |
323 | F.getline(Buffer,sizeof(Buffer)); | |
324 | CurLine++; | |
325 | _strtabexpand(Buffer,sizeof(Buffer)); | |
ba1045b4 AL |
326 | if (F.fail() && !F.eof()) |
327 | return _error->Error(_("Line %u too long in source list %s."), | |
328 | CurLine,File.c_str()); | |
329 | ||
b2e465d6 AL |
330 | |
331 | char *I; | |
34b53501 MV |
332 | // CNC:2003-02-20 - Do not break if '#' is inside []. |
333 | for (I = Buffer; *I != 0 && *I != '#'; I++) | |
334 | if (*I == '[') | |
125bf782 MV |
335 | { |
336 | char *b_end = strchr(I + 1, ']'); | |
337 | if (b_end != NULL) | |
338 | I = b_end; | |
339 | } | |
b2e465d6 AL |
340 | *I = 0; |
341 | ||
342 | const char *C = _strstrip(Buffer); | |
6c139d6e AL |
343 | |
344 | // Comment or blank | |
b2e465d6 | 345 | if (C[0] == '#' || C[0] == 0) |
6c139d6e | 346 | continue; |
b2e465d6 | 347 | |
6c139d6e | 348 | // Grok it |
b2e465d6 AL |
349 | string LineType; |
350 | if (ParseQuoteWord(C,LineType) == false) | |
351 | return _error->Error(_("Malformed line %u in source list %s (type)"),CurLine,File.c_str()); | |
6c139d6e | 352 | |
b2e465d6 AL |
353 | Type *Parse = Type::GetType(LineType.c_str()); |
354 | if (Parse == 0) | |
39442e44 | 355 | return _error->Error(_("Type '%s' is not known on line %u in source list %s"),LineType.c_str(),CurLine,File.c_str()); |
6c139d6e | 356 | |
5dd4c8b8 | 357 | if (Parse->ParseLine(SrcList, C, CurLine, File) == false) |
b2e465d6 | 358 | return false; |
6c139d6e AL |
359 | } |
360 | return true; | |
361 | } | |
362 | /*}}}*/ | |
1fa78a8a MV |
363 | // SourceList::ParseFileDeb822 - Parse deb822 style sources.list /*{{{*/ |
364 | // --------------------------------------------------------------------- | |
4194c9ae MV |
365 | /* Returns: the number of stanzas parsed*/ |
366 | int pkgSourceList::ParseFileDeb822(string File) | |
1fa78a8a | 367 | { |
fce9f472 | 368 | pkgTagSection Tags; |
fce9f472 MV |
369 | unsigned int i=0; |
370 | ||
371 | // see if we can read the file | |
372 | _error->PushToStack(); | |
1fa78a8a | 373 | FileFd Fd(File, FileFd::ReadOnly); |
fce9f472 | 374 | pkgTagFile Sources(&Fd); |
1fa78a8a MV |
375 | if (_error->PendingError() == true) |
376 | { | |
fce9f472 | 377 | _error->RevertToStack(); |
4194c9ae | 378 | return 0; |
1fa78a8a | 379 | } |
fce9f472 | 380 | _error->MergeWithStack(); |
1fa78a8a | 381 | |
fce9f472 | 382 | // read step by step |
1fa78a8a MV |
383 | while (Sources.Step(Tags) == true) |
384 | { | |
385 | if(!Tags.Exists("Type")) | |
386 | continue; | |
42e19c82 | 387 | |
1fa78a8a MV |
388 | string const type = Tags.FindS("Type"); |
389 | Type *Parse = Type::GetType(type.c_str()); | |
390 | if (Parse == 0) | |
4194c9ae MV |
391 | { |
392 | _error->Error(_("Type '%s' is not known on stanza %u in source list %s"),type.c_str(),i,Fd.Name().c_str()); | |
4194c9ae MV |
393 | return -1; |
394 | } | |
1fa78a8a | 395 | |
7037aab5 | 396 | if (!Parse->ParseStanza(SrcList, Tags, i, Fd)) |
4194c9ae | 397 | return -1; |
1fa78a8a MV |
398 | |
399 | i++; | |
400 | } | |
401 | ||
4194c9ae MV |
402 | // we are done, return the number of stanzas read |
403 | return i; | |
1fa78a8a MV |
404 | } |
405 | /*}}}*/ | |
b2e465d6 | 406 | // SourceList::FindIndex - Get the index associated with a file /*{{{*/ |
6c139d6e AL |
407 | // --------------------------------------------------------------------- |
408 | /* */ | |
b2e465d6 AL |
409 | bool pkgSourceList::FindIndex(pkgCache::PkgFileIterator File, |
410 | pkgIndexFile *&Found) const | |
6c139d6e | 411 | { |
f7f0d6c7 | 412 | for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I) |
6c139d6e | 413 | { |
7db98ffc MZ |
414 | vector<pkgIndexFile *> *Indexes = (*I)->GetIndexFiles(); |
415 | for (vector<pkgIndexFile *>::const_iterator J = Indexes->begin(); | |
f7f0d6c7 | 416 | J != Indexes->end(); ++J) |
b2e465d6 | 417 | { |
7db98ffc MZ |
418 | if ((*J)->FindInCache(*File.Cache()) == File) |
419 | { | |
420 | Found = (*J); | |
421 | return true; | |
422 | } | |
b2e465d6 | 423 | } |
be8922fd | 424 | } |
7db98ffc | 425 | |
b2e465d6 | 426 | return false; |
36375005 AL |
427 | } |
428 | /*}}}*/ | |
b2e465d6 | 429 | // SourceList::GetIndexes - Load the index files into the downloader /*{{{*/ |
6c139d6e AL |
430 | // --------------------------------------------------------------------- |
431 | /* */ | |
7db98ffc | 432 | bool pkgSourceList::GetIndexes(pkgAcquire *Owner, bool GetAll) const |
6c139d6e | 433 | { |
f7f0d6c7 | 434 | for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I) |
7db98ffc | 435 | if ((*I)->GetIndexes(Owner,GetAll) == false) |
b2e465d6 AL |
436 | return false; |
437 | return true; | |
6c139d6e AL |
438 | } |
439 | /*}}}*/ | |
34b53501 MV |
440 | // CNC:2003-03-03 - By Anton V. Denisov <avd@altlinux.org>. |
441 | // SourceList::ReadSourceDir - Read a directory with sources files | |
442 | // Based on ReadConfigDir() /*{{{*/ | |
443 | // --------------------------------------------------------------------- | |
444 | /* */ | |
445 | bool pkgSourceList::ReadSourceDir(string Dir) | |
446 | { | |
52643bec | 447 | vector<string> const List = GetListOfFilesInDir(Dir, "list", true); |
34b53501 MV |
448 | |
449 | // Read the files | |
f7f0d6c7 | 450 | for (vector<string>::const_iterator I = List.begin(); I != List.end(); ++I) |
34b53501 MV |
451 | if (ReadAppend(*I) == false) |
452 | return false; | |
453 | return true; | |
454 | ||
455 | } | |
456 | /*}}}*/ | |
2ec858bc MV |
457 | // GetLastModified() /*{{{*/ |
458 | // --------------------------------------------------------------------- | |
459 | /* */ | |
460 | time_t pkgSourceList::GetLastModifiedTime() | |
461 | { | |
5e7b0aa9 MV |
462 | vector<string> List; |
463 | ||
2ec858bc MV |
464 | string Main = _config->FindFile("Dir::Etc::sourcelist"); |
465 | string Parts = _config->FindDir("Dir::Etc::sourceparts"); | |
5e7b0aa9 MV |
466 | |
467 | // go over the parts | |
468 | if (DirectoryExists(Parts) == true) | |
469 | List = GetListOfFilesInDir(Parts, "list", true); | |
2ec858bc MV |
470 | |
471 | // calculate the time | |
472 | time_t mtime_sources = GetModificationTime(Main); | |
f7f0d6c7 | 473 | for (vector<string>::const_iterator I = List.begin(); I != List.end(); ++I) |
2ec858bc MV |
474 | mtime_sources = std::max(mtime_sources, GetModificationTime(*I)); |
475 | ||
476 | return mtime_sources; | |
477 | } | |
478 | /*}}}*/ | |
34b53501 | 479 |