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