]> git.saurik.com Git - apt.git/blob - apt-pkg/sourcelist.cc
refactor deb822 reading into its own function
[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 // Type::ParseLine - Parse a single line /*{{{*/
75 // ---------------------------------------------------------------------
76 /* This is a generic one that is the 'usual' format for sources.list
77 Weird types may override this. */
78 bool pkgSourceList::Type::ParseLine(vector<metaIndex *> &List,
79 const char *Buffer,
80 unsigned long const &CurLine,
81 string const &File) const
82 {
83 for (;Buffer != 0 && isspace(*Buffer); ++Buffer); // Skip whitespaces
84
85 // Parse option field if it exists
86 // e.g.: [ option1=value1 option2=value2 ]
87 map<string, string> Options;
88 if (Buffer != 0 && Buffer[0] == '[')
89 {
90 ++Buffer; // ignore the [
91 for (;Buffer != 0 && isspace(*Buffer); ++Buffer); // Skip whitespaces
92 while (*Buffer != ']')
93 {
94 // get one option, e.g. option1=value1
95 string option;
96 if (ParseQuoteWord(Buffer,option) == false)
97 return _error->Error(_("Malformed line %lu in source list %s ([option] unparseable)"),CurLine,File.c_str());
98
99 if (option.length() < 3)
100 return _error->Error(_("Malformed line %lu in source list %s ([option] too short)"),CurLine,File.c_str());
101
102 // accept options even if the last has no space before the ]-end marker
103 if (option.at(option.length()-1) == ']')
104 {
105 for (; *Buffer != ']'; --Buffer);
106 option.resize(option.length()-1);
107 }
108
109 size_t const needle = option.find('=');
110 if (needle == string::npos)
111 return _error->Error(_("Malformed line %lu in source list %s ([%s] is not an assignment)"),CurLine,File.c_str(), option.c_str());
112
113 string const key = string(option, 0, needle);
114 string const value = string(option, needle + 1, option.length());
115
116 if (key.empty() == true)
117 return _error->Error(_("Malformed line %lu in source list %s ([%s] has no key)"),CurLine,File.c_str(), option.c_str());
118
119 if (value.empty() == true)
120 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());
121
122 Options[key] = value;
123 }
124 ++Buffer; // ignore the ]
125 for (;Buffer != 0 && isspace(*Buffer); ++Buffer); // Skip whitespaces
126 }
127
128 string URI;
129 string Dist;
130 string Section;
131
132 if (ParseQuoteWord(Buffer,URI) == false)
133 return _error->Error(_("Malformed line %lu in source list %s (URI)"),CurLine,File.c_str());
134 if (ParseQuoteWord(Buffer,Dist) == false)
135 return _error->Error(_("Malformed line %lu in source list %s (dist)"),CurLine,File.c_str());
136
137 if (FixupURI(URI) == false)
138 return _error->Error(_("Malformed line %lu in source list %s (URI parse)"),CurLine,File.c_str());
139
140 // Check for an absolute dists specification.
141 if (Dist.empty() == false && Dist[Dist.size() - 1] == '/')
142 {
143 if (ParseQuoteWord(Buffer,Section) == true)
144 return _error->Error(_("Malformed line %lu in source list %s (absolute dist)"),CurLine,File.c_str());
145 Dist = SubstVar(Dist,"$(ARCH)",_config->Find("APT::Architecture"));
146 return CreateItem(List, URI, Dist, Section, Options);
147 }
148
149 // Grab the rest of the dists
150 if (ParseQuoteWord(Buffer,Section) == false)
151 return _error->Error(_("Malformed line %lu in source list %s (dist parse)"),CurLine,File.c_str());
152
153 do
154 {
155 if (CreateItem(List, URI, Dist, Section, Options) == false)
156 return false;
157 }
158 while (ParseQuoteWord(Buffer,Section) == true);
159
160 return true;
161 }
162 /*}}}*/
163
164 // SourceList::pkgSourceList - Constructors /*{{{*/
165 // ---------------------------------------------------------------------
166 /* */
167 pkgSourceList::pkgSourceList()
168 {
169 }
170
171 pkgSourceList::pkgSourceList(string File)
172 {
173 Read(File);
174 }
175 /*}}}*/
176 // SourceList::~pkgSourceList - Destructor /*{{{*/
177 // ---------------------------------------------------------------------
178 /* */
179 pkgSourceList::~pkgSourceList()
180 {
181 for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
182 delete *I;
183 }
184 /*}}}*/
185 /*}}}*/
186 // SourceList::ReadMainList - Read the main source list from etc /*{{{*/
187 // ---------------------------------------------------------------------
188 /* */
189 bool pkgSourceList::ReadMainList()
190 {
191 // CNC:2003-03-03 - Multiple sources list support.
192 bool Res = true;
193 #if 0
194 Res = ReadVendors();
195 if (Res == false)
196 return false;
197 #endif
198
199 Reset();
200 // CNC:2003-11-28 - Entries in sources.list have priority over
201 // entries in sources.list.d.
202 string Main = _config->FindFile("Dir::Etc::sourcelist");
203 string Parts = _config->FindDir("Dir::Etc::sourceparts");
204
205 if (RealFileExists(Main) == true)
206 Res &= ReadAppend(Main);
207 else if (DirectoryExists(Parts) == false)
208 // Only warn if there are no sources.list.d.
209 _error->WarningE("DirectoryExists", _("Unable to read %s"), Parts.c_str());
210
211 if (DirectoryExists(Parts) == true)
212 Res &= ReadSourceDir(Parts);
213 else if (RealFileExists(Main) == false)
214 // Only warn if there is no sources.list file.
215 _error->WarningE("RealFileExists", _("Unable to read %s"), Main.c_str());
216
217 return Res;
218 }
219 /*}}}*/
220 // CNC:2003-03-03 - Needed to preserve backwards compatibility.
221 // SourceList::Reset - Clear the sourcelist contents /*{{{*/
222 // ---------------------------------------------------------------------
223 /* */
224 void pkgSourceList::Reset()
225 {
226 for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
227 delete *I;
228 SrcList.erase(SrcList.begin(),SrcList.end());
229 }
230 /*}}}*/
231 // CNC:2003-03-03 - Function moved to ReadAppend() and Reset().
232 // SourceList::Read - Parse the sourcelist file /*{{{*/
233 // ---------------------------------------------------------------------
234 /* */
235 bool pkgSourceList::Read(string File)
236 {
237 Reset();
238 return ReadAppend(File);
239 }
240 /*}}}*/
241
242 // FIXME: move into pkgSourceList::Type::ParseFile()
243 bool pkgSourceList::ParseFileDeb822(FileFd &Fd)
244 {
245 pkgTagSection Tags;
246 map<string, string> Options;
247 unsigned int i=0;
248
249 pkgTagFile Sources(&Fd);
250 while (Sources.Step(Tags) == true)
251 {
252 if(!Tags.Exists("Type"))
253 continue;
254 string const type = Tags.FindS("Type");
255 Type *Parse = Type::GetType(type.c_str());
256 if (Parse == 0)
257 return _error->Error(_("Type '%s' is not known on stanza %u in source list %s"),type.c_str(),i,Fd.Name().c_str());
258
259 string URI = Tags.FindS("URL");
260 if (!Parse->FixupURI(URI))
261 return _error->Error(_("Malformed stanza %u in source list %s (URI parse)"),i,Fd.Name().c_str());
262 string const Dist = Tags.FindS("Dist");
263 string const Section = Tags.FindS("Section");
264 // check if there are any options we support
265 const char* option_str[] = {
266 "arch", "arch+", "arch-", "trusted" };
267 for (unsigned int j=0; j < sizeof(option_str)/sizeof(char*); j++)
268 if (Tags.Exists(option_str[j]))
269 Options[option_str[j]] = Tags.FindS(option_str[j]);
270
271 // now create one item per section
272 std::vector<std::string> list;
273 if (Section.find(","))
274 list = StringSplit(Section, ",");
275 else
276 list = StringSplit(Section, " ");
277 for (std::vector<std::string>::const_iterator I = list.begin();
278 I != list.end(); I++)
279 Parse->CreateItem(SrcList, URI, Dist, (*I), Options);
280
281 i++;
282 }
283
284 // we are done
285 if(i>0)
286 return true;
287
288 return false;
289 }
290
291
292 // SourceList::ReadAppend - Parse a sourcelist file /*{{{*/
293 // ---------------------------------------------------------------------
294 /* */
295 bool pkgSourceList::ReadAppend(string File)
296 {
297 // *first* try reading as deb822
298 // FIXME: proper error handling so that we do not error for good old-style
299 // sources
300 FileFd Fd(File, FileFd::ReadOnly);
301 if (_error->PendingError() == false)
302 {
303 if (ParseFileDeb822(Fd))
304 return true;
305 }
306
307
308 // *then* read as old-style sources.list
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::FindIndex - Get the index associated with a file /*{{{*/
362 // ---------------------------------------------------------------------
363 /* */
364 bool pkgSourceList::FindIndex(pkgCache::PkgFileIterator File,
365 pkgIndexFile *&Found) const
366 {
367 for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
368 {
369 vector<pkgIndexFile *> *Indexes = (*I)->GetIndexFiles();
370 for (vector<pkgIndexFile *>::const_iterator J = Indexes->begin();
371 J != Indexes->end(); ++J)
372 {
373 if ((*J)->FindInCache(*File.Cache()) == File)
374 {
375 Found = (*J);
376 return true;
377 }
378 }
379 }
380
381 return false;
382 }
383 /*}}}*/
384 // SourceList::GetIndexes - Load the index files into the downloader /*{{{*/
385 // ---------------------------------------------------------------------
386 /* */
387 bool pkgSourceList::GetIndexes(pkgAcquire *Owner, bool GetAll) const
388 {
389 for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
390 if ((*I)->GetIndexes(Owner,GetAll) == false)
391 return false;
392 return true;
393 }
394 /*}}}*/
395 // CNC:2003-03-03 - By Anton V. Denisov <avd@altlinux.org>.
396 // SourceList::ReadSourceDir - Read a directory with sources files
397 // Based on ReadConfigDir() /*{{{*/
398 // ---------------------------------------------------------------------
399 /* */
400 bool pkgSourceList::ReadSourceDir(string Dir)
401 {
402 vector<string> const List = GetListOfFilesInDir(Dir, "list", true);
403
404 // Read the files
405 for (vector<string>::const_iterator I = List.begin(); I != List.end(); ++I)
406 if (ReadAppend(*I) == false)
407 return false;
408 return true;
409
410 }
411 /*}}}*/
412 // GetLastModified() /*{{{*/
413 // ---------------------------------------------------------------------
414 /* */
415 time_t pkgSourceList::GetLastModifiedTime()
416 {
417 vector<string> List;
418
419 string Main = _config->FindFile("Dir::Etc::sourcelist");
420 string Parts = _config->FindDir("Dir::Etc::sourceparts");
421
422 // go over the parts
423 if (DirectoryExists(Parts) == true)
424 List = GetListOfFilesInDir(Parts, "list", true);
425
426 // calculate the time
427 time_t mtime_sources = GetModificationTime(Main);
428 for (vector<string>::const_iterator I = List.begin(); I != List.end(); ++I)
429 mtime_sources = std::max(mtime_sources, GetModificationTime(*I));
430
431 return mtime_sources;
432 }
433 /*}}}*/
434