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