]> git.saurik.com Git - apt.git/blob - apt-pkg/sourcelist.cc
add support for Enabled: no in deb822 sources.list
[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
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 URI = Tags.FindS("URI");
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 }
87
88 string Enabled = Tags.FindS("Enabled");
89 if (Enabled.size() > 0 && StringToBool(Enabled) == false)
90 return true;
91
92 // Define external/internal options
93 const char* option_deb822[] = {
94 "Architectures", "Architectures-Add", "Architectures-Remove", "Trusted",
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
103 // now create one item per suite/section
104 string Suite = Tags.FindS("Suites");
105 Suite = SubstVar(Suite,"$(ARCH)",_config->Find("APT::Architecture"));
106 string const Section = Tags.FindS("Sections");
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 }
122 return true;
123 }
124
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. */
129 bool pkgSourceList::Type::ParseLine(vector<metaIndex *> &List,
130 const char *Buffer,
131 unsigned long const &CurLine,
132 string const &File) const
133 {
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
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
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
179 string URI;
180 string Dist;
181 string Section;
182
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)
195 return _error->Error(_("Malformed line %lu in source list %s (absolute dist)"),CurLine,File.c_str());
196 Dist = SubstVar(Dist,"$(ARCH)",_config->Find("APT::Architecture"));
197 return CreateItem(List, URI, Dist, Section, Options);
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 {
206 if (CreateItem(List, URI, Dist, Section, Options) == false)
207 return false;
208 }
209 while (ParseQuoteWord(Buffer,Section) == true);
210
211 return true;
212 }
213 /*}}}*/
214 // SourceList::pkgSourceList - Constructors /*{{{*/
215 // ---------------------------------------------------------------------
216 /* */
217 pkgSourceList::pkgSourceList()
218 {
219 }
220
221 pkgSourceList::pkgSourceList(string File)
222 {
223 Read(File);
224 }
225 /*}}}*/
226 // SourceList::~pkgSourceList - Destructor /*{{{*/
227 // ---------------------------------------------------------------------
228 /* */
229 pkgSourceList::~pkgSourceList()
230 {
231 for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
232 delete *I;
233 }
234 /*}}}*/
235 // SourceList::ReadMainList - Read the main source list from etc /*{{{*/
236 // ---------------------------------------------------------------------
237 /* */
238 bool pkgSourceList::ReadMainList()
239 {
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");
252 string Parts = _config->FindDir("Dir::Etc::sourceparts");
253
254 if (RealFileExists(Main) == true)
255 Res &= ReadAppend(Main);
256 else if (DirectoryExists(Parts) == false)
257 // Only warn if there are no sources.list.d.
258 _error->WarningE("DirectoryExists", _("Unable to read %s"), Parts.c_str());
259
260 if (DirectoryExists(Parts) == true)
261 Res &= ReadSourceDir(Parts);
262 else if (RealFileExists(Main) == false)
263 // Only warn if there is no sources.list file.
264 _error->WarningE("RealFileExists", _("Unable to read %s"), Main.c_str());
265
266 return Res;
267 }
268 /*}}}*/
269 // SourceList::Reset - Clear the sourcelist contents /*{{{*/
270 // ---------------------------------------------------------------------
271 /* */
272 void pkgSourceList::Reset()
273 {
274 for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
275 delete *I;
276 SrcList.erase(SrcList.begin(),SrcList.end());
277 }
278 /*}}}*/
279 // SourceList::Read - Parse the sourcelist file /*{{{*/
280 // ---------------------------------------------------------------------
281 /* */
282 bool pkgSourceList::Read(string File)
283 {
284 Reset();
285 return ReadAppend(File);
286 }
287 /*}}}*/
288 // SourceList::ReadAppend - Parse a sourcelist file /*{{{*/
289 // ---------------------------------------------------------------------
290 /* */
291 bool pkgSourceList::ReadAppend(string File)
292 {
293 if (_config->FindB("APT::Sources::Use-Deb822", true) == true)
294 {
295 int lines_parsed =ParseFileDeb822(File);
296 if (lines_parsed < 0)
297 return false;
298 else if (lines_parsed > 0)
299 return true;
300 // no lines parsed ... fall through and use old style parser
301 }
302 return ParseFileOldStyle(File);
303 }
304
305 // SourceList::ReadFileOldStyle - Read Traditional style sources.list /*{{{*/
306 // ---------------------------------------------------------------------
307 /* */
308 bool pkgSourceList::ParseFileOldStyle(string File)
309 {
310 // Open the stream for reading
311 ifstream F(File.c_str(),ios::in /*| ios::nocreate*/);
312 if (!F != 0)
313 return _error->Errno("ifstream::ifstream",_("Opening %s"),File.c_str());
314
315 // CNC:2003-12-10 - 300 is too short.
316 char Buffer[1024];
317
318 int CurLine = 0;
319 while (F.eof() == false)
320 {
321 F.getline(Buffer,sizeof(Buffer));
322 CurLine++;
323 _strtabexpand(Buffer,sizeof(Buffer));
324 if (F.fail() && !F.eof())
325 return _error->Error(_("Line %u too long in source list %s."),
326 CurLine,File.c_str());
327
328
329 char *I;
330 // CNC:2003-02-20 - Do not break if '#' is inside [].
331 for (I = Buffer; *I != 0 && *I != '#'; I++)
332 if (*I == '[')
333 {
334 char *b_end = strchr(I + 1, ']');
335 if (b_end != NULL)
336 I = b_end;
337 }
338 *I = 0;
339
340 const char *C = _strstrip(Buffer);
341
342 // Comment or blank
343 if (C[0] == '#' || C[0] == 0)
344 continue;
345
346 // Grok it
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());
350
351 Type *Parse = Type::GetType(LineType.c_str());
352 if (Parse == 0)
353 return _error->Error(_("Type '%s' is not known on line %u in source list %s"),LineType.c_str(),CurLine,File.c_str());
354
355 if (Parse->ParseLine(SrcList, C, CurLine, File) == false)
356 return false;
357 }
358 return true;
359 }
360 /*}}}*/
361 // SourceList::ParseFileDeb822 - Parse deb822 style sources.list /*{{{*/
362 // ---------------------------------------------------------------------
363 /* Returns: the number of stanzas parsed*/
364 int pkgSourceList::ParseFileDeb822(string File)
365 {
366 pkgTagSection Tags;
367 unsigned int i=0;
368
369 // see if we can read the file
370 _error->PushToStack();
371 FileFd Fd(File, FileFd::ReadOnly);
372 pkgTagFile Sources(&Fd);
373 if (_error->PendingError() == true)
374 {
375 _error->RevertToStack();
376 return 0;
377 }
378 _error->MergeWithStack();
379
380 // read step by step
381 while (Sources.Step(Tags) == true)
382 {
383 if(!Tags.Exists("Type"))
384 continue;
385
386 string const type = Tags.FindS("Type");
387 Type *Parse = Type::GetType(type.c_str());
388 if (Parse == 0)
389 {
390 _error->Error(_("Type '%s' is not known on stanza %u in source list %s"),type.c_str(),i,Fd.Name().c_str());
391 return -1;
392 }
393
394 if (!Parse->ParseStanza(SrcList, Tags, i, Fd))
395 return -1;
396
397 i++;
398 }
399
400 // we are done, return the number of stanzas read
401 return i;
402 }
403 /*}}}*/
404 // SourceList::FindIndex - Get the index associated with a file /*{{{*/
405 // ---------------------------------------------------------------------
406 /* */
407 bool pkgSourceList::FindIndex(pkgCache::PkgFileIterator File,
408 pkgIndexFile *&Found) const
409 {
410 for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
411 {
412 vector<pkgIndexFile *> *Indexes = (*I)->GetIndexFiles();
413 for (vector<pkgIndexFile *>::const_iterator J = Indexes->begin();
414 J != Indexes->end(); ++J)
415 {
416 if ((*J)->FindInCache(*File.Cache()) == File)
417 {
418 Found = (*J);
419 return true;
420 }
421 }
422 }
423
424 return false;
425 }
426 /*}}}*/
427 // SourceList::GetIndexes - Load the index files into the downloader /*{{{*/
428 // ---------------------------------------------------------------------
429 /* */
430 bool pkgSourceList::GetIndexes(pkgAcquire *Owner, bool GetAll) const
431 {
432 for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
433 if ((*I)->GetIndexes(Owner,GetAll) == false)
434 return false;
435 return true;
436 }
437 /*}}}*/
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 {
445 vector<string> const List = GetListOfFilesInDir(Dir, "list", true);
446
447 // Read the files
448 for (vector<string>::const_iterator I = List.begin(); I != List.end(); ++I)
449 if (ReadAppend(*I) == false)
450 return false;
451 return true;
452
453 }
454 /*}}}*/
455 // GetLastModified() /*{{{*/
456 // ---------------------------------------------------------------------
457 /* */
458 time_t pkgSourceList::GetLastModifiedTime()
459 {
460 vector<string> List;
461
462 string Main = _config->FindFile("Dir::Etc::sourcelist");
463 string Parts = _config->FindDir("Dir::Etc::sourceparts");
464
465 // go over the parts
466 if (DirectoryExists(Parts) == true)
467 List = GetListOfFilesInDir(Parts, "list", true);
468
469 // calculate the time
470 time_t mtime_sources = GetModificationTime(Main);
471 for (vector<string>::const_iterator I = List.begin(); I != List.end(); ++I)
472 mtime_sources = std::max(mtime_sources, GetModificationTime(*I));
473
474 return mtime_sources;
475 }
476 /*}}}*/
477