]> git.saurik.com Git - apt.git/blob - apt-pkg/sourcelist.cc
0502f0e1dca48ac4254ccdb2e2d4b6365901c517
[apt.git] / apt-pkg / sourcelist.cc
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 #include <apt-pkg/pkgcache.h>
22 #include <apt-pkg/cacheiterators.h>
23
24 #include <ctype.h>
25 #include <stddef.h>
26 #include <time.h>
27 #include <cstring>
28 #include <map>
29 #include <string>
30 #include <vector>
31 #include <fstream>
32 #include <algorithm>
33
34 #include <apti18n.h>
35 /*}}}*/
36
37 using namespace std;
38
39 // Global list of Items supported
40 static pkgSourceList::Type *ItmList[10];
41 pkgSourceList::Type **pkgSourceList::Type::GlobalList = ItmList;
42 unsigned long pkgSourceList::Type::GlobalListLen = 0;
43
44 // Type::Type - Constructor /*{{{*/
45 // ---------------------------------------------------------------------
46 /* Link this to the global list of items*/
47 pkgSourceList::Type::Type(char const * const pName, char const * const pLabel) : Name(pName), Label(pLabel)
48 {
49 ItmList[GlobalListLen] = this;
50 ++GlobalListLen;
51 }
52 pkgSourceList::Type::~Type() {}
53 /*}}}*/
54 // Type::GetType - Get a specific meta for a given type /*{{{*/
55 // ---------------------------------------------------------------------
56 /* */
57 pkgSourceList::Type *pkgSourceList::Type::GetType(const char *Type)
58 {
59 for (unsigned I = 0; I != GlobalListLen; ++I)
60 if (strcmp(GlobalList[I]->Name,Type) == 0)
61 return GlobalList[I];
62 return 0;
63 }
64 /*}}}*/
65 // Type::FixupURI - Normalize the URI and check it.. /*{{{*/
66 // ---------------------------------------------------------------------
67 /* */
68 bool pkgSourceList::Type::FixupURI(string &URI) const
69 {
70 if (URI.empty() == true)
71 return false;
72
73 if (URI.find(':') == string::npos)
74 return false;
75
76 URI = SubstVar(URI,"$(ARCH)",_config->Find("APT::Architecture"));
77
78 // Make sure that the URI is / postfixed
79 if (URI[URI.size() - 1] != '/')
80 URI += '/';
81
82 return true;
83 }
84 /*}}}*/
85 bool pkgSourceList::Type::ParseStanza(vector<metaIndex *> &List, /*{{{*/
86 pkgTagSection &Tags,
87 unsigned int const i,
88 FileFd &Fd)
89 {
90 map<string, string> Options;
91
92 string Enabled = Tags.FindS("Enabled");
93 if (Enabled.empty() == false && StringToBool(Enabled) == false)
94 return true;
95
96 std::map<char const * const, char const * const> mapping;
97 #define APT_PLUSMINUS(X, Y) \
98 mapping.insert(std::make_pair(X, Y)); \
99 mapping.insert(std::make_pair(X "Add", Y "+")); \
100 mapping.insert(std::make_pair(X "Remove", Y "-"))
101 APT_PLUSMINUS("Architectures", "arch");
102 APT_PLUSMINUS("Languages", "lang");
103 APT_PLUSMINUS("Targets", "target");
104 #undef APT_PLUSMINUS
105 mapping.insert(std::make_pair("Trusted", "trusted"));
106
107 for (std::map<char const * const, char const * const>::const_iterator m = mapping.begin(); m != mapping.end(); ++m)
108 if (Tags.Exists(m->first))
109 {
110 // for deb822 the " " is the delimiter, but the backend expects ","
111 std::string option = Tags.FindS(m->first);
112 std::replace(option.begin(), option.end(), ' ', ',');
113 Options[m->second] = option;
114 }
115
116 // now create one item per suite/section
117 string Suite = Tags.FindS("Suites");
118 Suite = SubstVar(Suite,"$(ARCH)",_config->Find("APT::Architecture"));
119 string const Component = Tags.FindS("Components");
120 string const URIS = Tags.FindS("URIs");
121
122 std::vector<std::string> const list_uris = VectorizeString(URIS, ' ');
123 std::vector<std::string> const list_suite = VectorizeString(Suite, ' ');
124 std::vector<std::string> const list_comp = VectorizeString(Component, ' ');
125
126 if (list_uris.empty())
127 // TRANSLATOR: %u is a line number, the first %s is a filename of a file with the extension "second %s" and the third %s is a unique identifier for bugreports
128 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), i, "sources", Fd.Name().c_str(), "URI");
129
130 for (std::vector<std::string>::const_iterator U = list_uris.begin();
131 U != list_uris.end(); ++U)
132 {
133 std::string URI = *U;
134 if (U->empty() || FixupURI(URI) == false)
135 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), i, "sources", Fd.Name().c_str(), "URI parse");
136
137 if (list_suite.empty())
138 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), i, "sources", Fd.Name().c_str(), "Suite");
139
140 for (std::vector<std::string>::const_iterator S = list_suite.begin();
141 S != list_suite.end(); ++S)
142 {
143 if (S->empty() == false && (*S)[S->size() - 1] == '/')
144 {
145 if (list_comp.empty() == false)
146 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), i, "sources", Fd.Name().c_str(), "absolute Suite Component");
147 if (CreateItem(List, URI, *S, "", Options) == false)
148 return false;
149 }
150 else
151 {
152 if (list_comp.empty())
153 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), i, "sources", Fd.Name().c_str(), "Component");
154
155 for (std::vector<std::string>::const_iterator C = list_comp.begin();
156 C != list_comp.end(); ++C)
157 {
158 if (CreateItem(List, URI, *S, *C, Options) == false)
159 {
160 return false;
161 }
162 }
163 }
164 }
165 }
166 return true;
167 }
168 /*}}}*/
169 // Type::ParseLine - Parse a single line /*{{{*/
170 // ---------------------------------------------------------------------
171 /* This is a generic one that is the 'usual' format for sources.list
172 Weird types may override this. */
173 bool pkgSourceList::Type::ParseLine(vector<metaIndex *> &List,
174 const char *Buffer,
175 unsigned int const CurLine,
176 string const &File) const
177 {
178 for (;Buffer != 0 && isspace(*Buffer); ++Buffer); // Skip whitespaces
179
180 // Parse option field if it exists
181 // e.g.: [ option1=value1 option2=value2 ]
182 map<string, string> Options;
183 if (Buffer != 0 && Buffer[0] == '[')
184 {
185 ++Buffer; // ignore the [
186 for (;Buffer != 0 && isspace(*Buffer); ++Buffer); // Skip whitespaces
187 while (*Buffer != ']')
188 {
189 // get one option, e.g. option1=value1
190 string option;
191 if (ParseQuoteWord(Buffer,option) == false)
192 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), CurLine, "list", File.c_str(), "[option] unparseable");
193
194 if (option.length() < 3)
195 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), CurLine, "list", File.c_str(), "[option] too short");
196
197 // accept options even if the last has no space before the ]-end marker
198 if (option.at(option.length()-1) == ']')
199 {
200 for (; *Buffer != ']'; --Buffer);
201 option.resize(option.length()-1);
202 }
203
204 size_t const needle = option.find('=');
205 if (needle == string::npos)
206 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), CurLine, "list", File.c_str(), "[option] not assignment");
207
208 string const key = string(option, 0, needle);
209 string const value = string(option, needle + 1, option.length());
210
211 if (key.empty() == true)
212 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), CurLine, "list", File.c_str(), "[option] no key");
213
214 if (value.empty() == true)
215 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), CurLine, "list", File.c_str(), "[option] no value");
216
217 Options[key] = value;
218 }
219 ++Buffer; // ignore the ]
220 for (;Buffer != 0 && isspace(*Buffer); ++Buffer); // Skip whitespaces
221 }
222
223 string URI;
224 string Dist;
225 string Section;
226
227 if (ParseQuoteWord(Buffer,URI) == false)
228 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), CurLine, "list", File.c_str(), "URI");
229 if (ParseQuoteWord(Buffer,Dist) == false)
230 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), CurLine, "list", File.c_str(), "Suite");
231
232 if (FixupURI(URI) == false)
233 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), CurLine, "list", File.c_str(), "URI parse");
234
235 // Check for an absolute dists specification.
236 if (Dist.empty() == false && Dist[Dist.size() - 1] == '/')
237 {
238 if (ParseQuoteWord(Buffer,Section) == true)
239 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), CurLine, "list", File.c_str(), "absolute Suite Component");
240 Dist = SubstVar(Dist,"$(ARCH)",_config->Find("APT::Architecture"));
241 return CreateItem(List, URI, Dist, Section, Options);
242 }
243
244 // Grab the rest of the dists
245 if (ParseQuoteWord(Buffer,Section) == false)
246 return _error->Error(_("Malformed entry %u in %s file %s (%s)"), CurLine, "list", File.c_str(), "Component");
247
248 do
249 {
250 if (CreateItem(List, URI, Dist, Section, Options) == false)
251 return false;
252 }
253 while (ParseQuoteWord(Buffer,Section) == true);
254
255 return true;
256 }
257 /*}}}*/
258 // SourceList::pkgSourceList - Constructors /*{{{*/
259 // ---------------------------------------------------------------------
260 /* */
261 pkgSourceList::pkgSourceList() : d(NULL)
262 {
263 }
264 /*}}}*/
265 // SourceList::~pkgSourceList - Destructor /*{{{*/
266 // ---------------------------------------------------------------------
267 /* */
268 pkgSourceList::~pkgSourceList()
269 {
270 for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
271 delete *I;
272 }
273 /*}}}*/
274 // SourceList::ReadMainList - Read the main source list from etc /*{{{*/
275 // ---------------------------------------------------------------------
276 /* */
277 bool pkgSourceList::ReadMainList()
278 {
279 // CNC:2003-03-03 - Multiple sources list support.
280 bool Res = true;
281 #if 0
282 Res = ReadVendors();
283 if (Res == false)
284 return false;
285 #endif
286
287 Reset();
288 // CNC:2003-11-28 - Entries in sources.list have priority over
289 // entries in sources.list.d.
290 string Main = _config->FindFile("Dir::Etc::sourcelist");
291 string Parts = _config->FindDir("Dir::Etc::sourceparts");
292
293 if (RealFileExists(Main) == true)
294 Res &= ReadAppend(Main);
295 else if (DirectoryExists(Parts) == false)
296 // Only warn if there are no sources.list.d.
297 _error->WarningE("DirectoryExists", _("Unable to read %s"), Parts.c_str());
298
299 if (DirectoryExists(Parts) == true)
300 Res &= ReadSourceDir(Parts);
301 else if (RealFileExists(Main) == false)
302 // Only warn if there is no sources.list file.
303 _error->WarningE("RealFileExists", _("Unable to read %s"), Main.c_str());
304
305 return Res;
306 }
307 /*}}}*/
308 // SourceList::Reset - Clear the sourcelist contents /*{{{*/
309 // ---------------------------------------------------------------------
310 /* */
311 void pkgSourceList::Reset()
312 {
313 for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
314 delete *I;
315 SrcList.erase(SrcList.begin(),SrcList.end());
316 }
317 /*}}}*/
318 // SourceList::Read - Parse the sourcelist file /*{{{*/
319 // ---------------------------------------------------------------------
320 /* */
321 bool pkgSourceList::Read(string const &File)
322 {
323 Reset();
324 return ReadAppend(File);
325 }
326 /*}}}*/
327 // SourceList::ReadAppend - Parse a sourcelist file /*{{{*/
328 // ---------------------------------------------------------------------
329 /* */
330 bool pkgSourceList::ReadAppend(string const &File)
331 {
332 if (flExtension(File) == "sources")
333 return ParseFileDeb822(File);
334 else
335 return ParseFileOldStyle(File);
336 }
337
338 // SourceList::ReadFileOldStyle - Read Traditional style sources.list /*{{{*/
339 // ---------------------------------------------------------------------
340 /* */
341 bool pkgSourceList::ParseFileOldStyle(std::string const &File)
342 {
343 // Open the stream for reading
344 ifstream F(File.c_str(),ios::in /*| ios::nocreate*/);
345 if (F.fail() == true)
346 return _error->Errno("ifstream::ifstream",_("Opening %s"),File.c_str());
347
348 std::string Buffer;
349 for (unsigned int CurLine = 1; std::getline(F, Buffer); ++CurLine)
350 {
351 // remove comments
352 size_t curpos = 0;
353 while ((curpos = Buffer.find('#', curpos)) != std::string::npos)
354 {
355 size_t const openbrackets = std::count(Buffer.begin(), Buffer.begin() + curpos, '[');
356 size_t const closedbrackets = std::count(Buffer.begin(), Buffer.begin() + curpos, ']');
357 if (openbrackets > closedbrackets)
358 {
359 // a # in an option, unlikely, but oh well, it was supported so stick to it
360 ++curpos;
361 continue;
362 }
363 Buffer.erase(curpos);
364 break;
365 }
366 // remove spaces before/after
367 curpos = Buffer.find_first_not_of(" \t\r");
368 if (curpos != 0)
369 Buffer.erase(0, curpos);
370 curpos = Buffer.find_last_not_of(" \t\r");
371 if (curpos != std::string::npos)
372 Buffer.erase(curpos + 1);
373
374 if (Buffer.empty())
375 continue;
376
377 // Grok it
378 std::string const LineType = Buffer.substr(0, Buffer.find(' '));
379 if (LineType.empty() || LineType == Buffer)
380 return _error->Error(_("Malformed line %u in source list %s (type)"),CurLine,File.c_str());
381
382 Type *Parse = Type::GetType(LineType.c_str());
383 if (Parse == 0)
384 return _error->Error(_("Type '%s' is not known on line %u in source list %s"),LineType.c_str(),CurLine,File.c_str());
385
386 if (Parse->ParseLine(SrcList, Buffer.c_str() + LineType.length(), CurLine, File) == false)
387 return false;
388 }
389 return true;
390 }
391 /*}}}*/
392 // SourceList::ParseFileDeb822 - Parse deb822 style sources.list /*{{{*/
393 // ---------------------------------------------------------------------
394 /* Returns: the number of stanzas parsed*/
395 bool pkgSourceList::ParseFileDeb822(string const &File)
396 {
397 pkgUserTagSection Tags;
398 unsigned int i = 1;
399
400 // see if we can read the file
401 FileFd Fd(File, FileFd::ReadOnly);
402 pkgTagFile Sources(&Fd);
403 if (_error->PendingError() == true)
404 return _error->Error(_("Malformed stanza %u in source list %s (type)"),i,File.c_str());
405
406 // read step by step
407 while (Sources.Step(Tags) == true)
408 {
409 if(Tags.Exists("Types") == false)
410 return _error->Error(_("Malformed stanza %u in source list %s (type)"),i,File.c_str());
411
412 string const types = Tags.FindS("Types");
413 std::vector<std::string> const list_types = VectorizeString(types, ' ');
414 for (std::vector<std::string>::const_iterator I = list_types.begin();
415 I != list_types.end(); ++I)
416 {
417 Type *Parse = Type::GetType((*I).c_str());
418 if (Parse == 0)
419 {
420 _error->Error(_("Type '%s' is not known on stanza %u in source list %s"), (*I).c_str(),i,Fd.Name().c_str());
421 return false;
422 }
423
424 if (!Parse->ParseStanza(SrcList, Tags, i, Fd))
425 return false;
426
427 ++i;
428 }
429 }
430 return true;
431 }
432 /*}}}*/
433 // SourceList::FindIndex - Get the index associated with a file /*{{{*/
434 // ---------------------------------------------------------------------
435 /* */
436 bool pkgSourceList::FindIndex(pkgCache::PkgFileIterator File,
437 pkgIndexFile *&Found) const
438 {
439 for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
440 {
441 vector<pkgIndexFile *> *Indexes = (*I)->GetIndexFiles();
442 for (vector<pkgIndexFile *>::const_iterator J = Indexes->begin();
443 J != Indexes->end(); ++J)
444 {
445 if ((*J)->FindInCache(*File.Cache()) == File)
446 {
447 Found = (*J);
448 return true;
449 }
450 }
451 }
452
453 return false;
454 }
455 /*}}}*/
456 // SourceList::GetIndexes - Load the index files into the downloader /*{{{*/
457 // ---------------------------------------------------------------------
458 /* */
459 bool pkgSourceList::GetIndexes(pkgAcquire *Owner, bool GetAll) const
460 {
461 for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
462 if ((*I)->GetIndexes(Owner,GetAll) == false)
463 return false;
464 return true;
465 }
466 /*}}}*/
467 // CNC:2003-03-03 - By Anton V. Denisov <avd@altlinux.org>.
468 // SourceList::ReadSourceDir - Read a directory with sources files
469 // Based on ReadConfigDir() /*{{{*/
470 // ---------------------------------------------------------------------
471 /* */
472 bool pkgSourceList::ReadSourceDir(string const &Dir)
473 {
474 std::vector<std::string> ext;
475 ext.push_back("list");
476 ext.push_back("sources");
477 std::vector<std::string> const List = GetListOfFilesInDir(Dir, ext, true);
478
479 // Read the files
480 for (vector<string>::const_iterator I = List.begin(); I != List.end(); ++I)
481 if (ReadAppend(*I) == false)
482 return false;
483 return true;
484
485 }
486 /*}}}*/
487 // GetLastModified() /*{{{*/
488 // ---------------------------------------------------------------------
489 /* */
490 time_t pkgSourceList::GetLastModifiedTime()
491 {
492 vector<string> List;
493
494 string Main = _config->FindFile("Dir::Etc::sourcelist");
495 string Parts = _config->FindDir("Dir::Etc::sourceparts");
496
497 // go over the parts
498 if (DirectoryExists(Parts) == true)
499 List = GetListOfFilesInDir(Parts, "list", true);
500
501 // calculate the time
502 time_t mtime_sources = GetModificationTime(Main);
503 for (vector<string>::const_iterator I = List.begin(); I != List.end(); ++I)
504 mtime_sources = std::max(mtime_sources, GetModificationTime(*I));
505
506 return mtime_sources;
507 }
508 /*}}}*/
509