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