]> git.saurik.com Git - apt.git/blame - apt-pkg/sourcelist.cc
first proof-of-concept for a fix for #731738
[apt.git] / apt-pkg / sourcelist.cc
CommitLineData
6c139d6e
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
34b53501 3// $Id: sourcelist.cc,v 1.3 2002/08/15 20:51:37 niemeyer Exp $
6c139d6e
AL
4/* ######################################################################
5
6 List of Sources
7
8 ##################################################################### */
9 /*}}}*/
10// Include Files /*{{{*/
ea542140
DK
11#include<config.h>
12
094a497d
AL
13#include <apt-pkg/sourcelist.h>
14#include <apt-pkg/error.h>
15#include <apt-pkg/fileutl.h>
cdcc6d34 16#include <apt-pkg/strutl.h>
7db98ffc 17#include <apt-pkg/configuration.h>
472ff00e
DK
18#include <apt-pkg/metaindex.h>
19#include <apt-pkg/indexfile.h>
6c139d6e 20
90f057fd 21#include <fstream>
ea542140
DK
22
23#include <apti18n.h>
b2e465d6
AL
24 /*}}}*/
25
4d6f8bdf
AL
26using namespace std;
27
28// Global list of Items supported
b2e465d6
AL
29static pkgSourceList::Type *ItmList[10];
30pkgSourceList::Type **pkgSourceList::Type::GlobalList = ItmList;
31unsigned long pkgSourceList::Type::GlobalListLen = 0;
32
33// Type::Type - Constructor /*{{{*/
34// ---------------------------------------------------------------------
35/* Link this to the global list of items*/
dcaa1185 36pkgSourceList::Type::Type() : Name(NULL), Label(NULL)
b2e465d6
AL
37{
38 ItmList[GlobalListLen] = this;
39 GlobalListLen++;
40}
41 /*}}}*/
42// Type::GetType - Get a specific meta for a given type /*{{{*/
43// ---------------------------------------------------------------------
44/* */
45pkgSourceList::Type *pkgSourceList::Type::GetType(const char *Type)
46{
47 for (unsigned I = 0; I != GlobalListLen; I++)
48 if (strcmp(GlobalList[I]->Name,Type) == 0)
49 return GlobalList[I];
50 return 0;
51}
52 /*}}}*/
53// Type::FixupURI - Normalize the URI and check it.. /*{{{*/
54// ---------------------------------------------------------------------
55/* */
56bool pkgSourceList::Type::FixupURI(string &URI) const
57{
58 if (URI.empty() == true)
59 return false;
60
61 if (URI.find(':') == string::npos)
62 return false;
63
64 URI = SubstVar(URI,"$(ARCH)",_config->Find("APT::Architecture"));
65
a7c835af 66 // Make sure that the URI is / postfixed
b2e465d6
AL
67 if (URI[URI.size() - 1] != '/')
68 URI += '/';
69
70 return true;
71}
72 /*}}}*/
73// Type::ParseLine - Parse a single line /*{{{*/
74// ---------------------------------------------------------------------
75/* This is a generic one that is the 'usual' format for sources.list
76 Weird types may override this. */
7db98ffc 77bool pkgSourceList::Type::ParseLine(vector<metaIndex *> &List,
b2e465d6 78 const char *Buffer,
5dd4c8b8
DK
79 unsigned long const &CurLine,
80 string const &File) const
b2e465d6 81{
5dd4c8b8
DK
82 for (;Buffer != 0 && isspace(*Buffer); ++Buffer); // Skip whitespaces
83
84 // Parse option field if it exists
85 // e.g.: [ option1=value1 option2=value2 ]
86 map<string, string> Options;
87 if (Buffer != 0 && Buffer[0] == '[')
88 {
89 ++Buffer; // ignore the [
90 for (;Buffer != 0 && isspace(*Buffer); ++Buffer); // Skip whitespaces
91 while (*Buffer != ']')
92 {
93 // get one option, e.g. option1=value1
94 string option;
95 if (ParseQuoteWord(Buffer,option) == false)
96 return _error->Error(_("Malformed line %lu in source list %s ([option] unparseable)"),CurLine,File.c_str());
97
98 if (option.length() < 3)
99 return _error->Error(_("Malformed line %lu in source list %s ([option] too short)"),CurLine,File.c_str());
100
6838dd87
DK
101 // accept options even if the last has no space before the ]-end marker
102 if (option.at(option.length()-1) == ']')
103 {
104 for (; *Buffer != ']'; --Buffer);
105 option.resize(option.length()-1);
106 }
107
5dd4c8b8
DK
108 size_t const needle = option.find('=');
109 if (needle == string::npos)
110 return _error->Error(_("Malformed line %lu in source list %s ([%s] is not an assignment)"),CurLine,File.c_str(), option.c_str());
111
112 string const key = string(option, 0, needle);
113 string const value = string(option, needle + 1, option.length());
114
115 if (key.empty() == true)
116 return _error->Error(_("Malformed line %lu in source list %s ([%s] has no key)"),CurLine,File.c_str(), option.c_str());
117
118 if (value.empty() == true)
119 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());
120
121 Options[key] = value;
122 }
123 ++Buffer; // ignore the ]
124 for (;Buffer != 0 && isspace(*Buffer); ++Buffer); // Skip whitespaces
125 }
126
b2e465d6
AL
127 string URI;
128 string Dist;
5dd4c8b8
DK
129 string Section;
130
b2e465d6
AL
131 if (ParseQuoteWord(Buffer,URI) == false)
132 return _error->Error(_("Malformed line %lu in source list %s (URI)"),CurLine,File.c_str());
133 if (ParseQuoteWord(Buffer,Dist) == false)
134 return _error->Error(_("Malformed line %lu in source list %s (dist)"),CurLine,File.c_str());
135
136 if (FixupURI(URI) == false)
137 return _error->Error(_("Malformed line %lu in source list %s (URI parse)"),CurLine,File.c_str());
138
139 // Check for an absolute dists specification.
140 if (Dist.empty() == false && Dist[Dist.size() - 1] == '/')
141 {
142 if (ParseQuoteWord(Buffer,Section) == true)
db0db9fe 143 return _error->Error(_("Malformed line %lu in source list %s (absolute dist)"),CurLine,File.c_str());
b2e465d6 144 Dist = SubstVar(Dist,"$(ARCH)",_config->Find("APT::Architecture"));
5dd4c8b8 145 return CreateItem(List, URI, Dist, Section, Options);
b2e465d6
AL
146 }
147
148 // Grab the rest of the dists
149 if (ParseQuoteWord(Buffer,Section) == false)
150 return _error->Error(_("Malformed line %lu in source list %s (dist parse)"),CurLine,File.c_str());
151
152 do
153 {
5dd4c8b8 154 if (CreateItem(List, URI, Dist, Section, Options) == false)
b2e465d6
AL
155 return false;
156 }
157 while (ParseQuoteWord(Buffer,Section) == true);
158
159 return true;
160}
6c139d6e
AL
161 /*}}}*/
162
163// SourceList::pkgSourceList - Constructors /*{{{*/
164// ---------------------------------------------------------------------
165/* */
166pkgSourceList::pkgSourceList()
167{
168}
169
170pkgSourceList::pkgSourceList(string File)
171{
172 Read(File);
173}
174 /*}}}*/
1c193e02
AL
175// SourceList::~pkgSourceList - Destructor /*{{{*/
176// ---------------------------------------------------------------------
177/* */
178pkgSourceList::~pkgSourceList()
179{
f7f0d6c7 180 for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
1c193e02 181 delete *I;
1c193e02
AL
182}
183 /*}}}*/
a7c835af 184 /*}}}*/
6c139d6e
AL
185// SourceList::ReadMainList - Read the main source list from etc /*{{{*/
186// ---------------------------------------------------------------------
187/* */
188bool pkgSourceList::ReadMainList()
189{
34b53501
MV
190 // CNC:2003-03-03 - Multiple sources list support.
191 bool Res = true;
192#if 0
193 Res = ReadVendors();
194 if (Res == false)
195 return false;
196#endif
197
198 Reset();
199 // CNC:2003-11-28 - Entries in sources.list have priority over
200 // entries in sources.list.d.
201 string Main = _config->FindFile("Dir::Etc::sourcelist");
67793cf3
JAK
202 string Parts = _config->FindDir("Dir::Etc::sourceparts");
203
36f1098a 204 if (RealFileExists(Main) == true)
6009e60d 205 Res &= ReadAppend(Main);
448eaf8b 206 else if (DirectoryExists(Parts) == false)
67793cf3 207 // Only warn if there are no sources.list.d.
448eaf8b 208 _error->WarningE("DirectoryExists", _("Unable to read %s"), Parts.c_str());
34b53501 209
448eaf8b 210 if (DirectoryExists(Parts) == true)
34b53501 211 Res &= ReadSourceDir(Parts);
36f1098a 212 else if (RealFileExists(Main) == false)
67793cf3 213 // Only warn if there is no sources.list file.
36f1098a 214 _error->WarningE("RealFileExists", _("Unable to read %s"), Main.c_str());
6009e60d 215
34b53501 216 return Res;
6c139d6e
AL
217}
218 /*}}}*/
34b53501
MV
219// CNC:2003-03-03 - Needed to preserve backwards compatibility.
220// SourceList::Reset - Clear the sourcelist contents /*{{{*/
221// ---------------------------------------------------------------------
222/* */
223void pkgSourceList::Reset()
224{
f7f0d6c7 225 for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
34b53501
MV
226 delete *I;
227 SrcList.erase(SrcList.begin(),SrcList.end());
228}
229 /*}}}*/
230// CNC:2003-03-03 - Function moved to ReadAppend() and Reset().
6c139d6e
AL
231// SourceList::Read - Parse the sourcelist file /*{{{*/
232// ---------------------------------------------------------------------
233/* */
234bool pkgSourceList::Read(string File)
34b53501
MV
235{
236 Reset();
237 return ReadAppend(File);
238}
239 /*}}}*/
240// SourceList::ReadAppend - Parse a sourcelist file /*{{{*/
241// ---------------------------------------------------------------------
242/* */
243bool pkgSourceList::ReadAppend(string File)
6c139d6e
AL
244{
245 // Open the stream for reading
4d6f8bdf 246 ifstream F(File.c_str(),ios::in /*| ios::nocreate*/);
6c139d6e 247 if (!F != 0)
b2e465d6 248 return _error->Errno("ifstream::ifstream",_("Opening %s"),File.c_str());
6c139d6e 249
34b53501 250#if 0 // Now Reset() does this.
1c193e02
AL
251 for (const_iterator I = SrcList.begin(); I != SrcList.end(); I++)
252 delete *I;
a7c835af 253 SrcList.erase(SrcList.begin(),SrcList.end());
34b53501
MV
254#endif
255 // CNC:2003-12-10 - 300 is too short.
256 char Buffer[1024];
6c139d6e
AL
257
258 int CurLine = 0;
259 while (F.eof() == false)
260 {
261 F.getline(Buffer,sizeof(Buffer));
262 CurLine++;
263 _strtabexpand(Buffer,sizeof(Buffer));
ba1045b4
AL
264 if (F.fail() && !F.eof())
265 return _error->Error(_("Line %u too long in source list %s."),
266 CurLine,File.c_str());
267
b2e465d6
AL
268
269 char *I;
34b53501
MV
270 // CNC:2003-02-20 - Do not break if '#' is inside [].
271 for (I = Buffer; *I != 0 && *I != '#'; I++)
272 if (*I == '[')
125bf782
MV
273 {
274 char *b_end = strchr(I + 1, ']');
275 if (b_end != NULL)
276 I = b_end;
277 }
b2e465d6
AL
278 *I = 0;
279
280 const char *C = _strstrip(Buffer);
6c139d6e
AL
281
282 // Comment or blank
b2e465d6 283 if (C[0] == '#' || C[0] == 0)
6c139d6e 284 continue;
b2e465d6 285
6c139d6e 286 // Grok it
b2e465d6
AL
287 string LineType;
288 if (ParseQuoteWord(C,LineType) == false)
289 return _error->Error(_("Malformed line %u in source list %s (type)"),CurLine,File.c_str());
6c139d6e 290
b2e465d6
AL
291 Type *Parse = Type::GetType(LineType.c_str());
292 if (Parse == 0)
39442e44 293 return _error->Error(_("Type '%s' is not known on line %u in source list %s"),LineType.c_str(),CurLine,File.c_str());
6c139d6e 294
5dd4c8b8 295 if (Parse->ParseLine(SrcList, C, CurLine, File) == false)
b2e465d6 296 return false;
6c139d6e
AL
297 }
298 return true;
299}
300 /*}}}*/
b2e465d6 301// SourceList::FindIndex - Get the index associated with a file /*{{{*/
6c139d6e
AL
302// ---------------------------------------------------------------------
303/* */
b2e465d6
AL
304bool pkgSourceList::FindIndex(pkgCache::PkgFileIterator File,
305 pkgIndexFile *&Found) const
6c139d6e 306{
f7f0d6c7 307 for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
6c139d6e 308 {
7db98ffc
MZ
309 vector<pkgIndexFile *> *Indexes = (*I)->GetIndexFiles();
310 for (vector<pkgIndexFile *>::const_iterator J = Indexes->begin();
f7f0d6c7 311 J != Indexes->end(); ++J)
b2e465d6 312 {
7db98ffc
MZ
313 if ((*J)->FindInCache(*File.Cache()) == File)
314 {
315 Found = (*J);
316 return true;
317 }
b2e465d6 318 }
be8922fd 319 }
7db98ffc 320
b2e465d6 321 return false;
36375005
AL
322}
323 /*}}}*/
b2e465d6 324// SourceList::GetIndexes - Load the index files into the downloader /*{{{*/
6c139d6e
AL
325// ---------------------------------------------------------------------
326/* */
7db98ffc 327bool pkgSourceList::GetIndexes(pkgAcquire *Owner, bool GetAll) const
6c139d6e 328{
f7f0d6c7 329 for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
7db98ffc 330 if ((*I)->GetIndexes(Owner,GetAll) == false)
b2e465d6
AL
331 return false;
332 return true;
6c139d6e
AL
333}
334 /*}}}*/
34b53501
MV
335// CNC:2003-03-03 - By Anton V. Denisov <avd@altlinux.org>.
336// SourceList::ReadSourceDir - Read a directory with sources files
337// Based on ReadConfigDir() /*{{{*/
338// ---------------------------------------------------------------------
339/* */
340bool pkgSourceList::ReadSourceDir(string Dir)
341{
52643bec 342 vector<string> const List = GetListOfFilesInDir(Dir, "list", true);
34b53501
MV
343
344 // Read the files
f7f0d6c7 345 for (vector<string>::const_iterator I = List.begin(); I != List.end(); ++I)
34b53501
MV
346 if (ReadAppend(*I) == false)
347 return false;
348 return true;
349
350}
351 /*}}}*/
2ec858bc
MV
352// GetLastModified() /*{{{*/
353// ---------------------------------------------------------------------
354/* */
355time_t pkgSourceList::GetLastModifiedTime()
356{
5e7b0aa9
MV
357 vector<string> List;
358
2ec858bc
MV
359 string Main = _config->FindFile("Dir::Etc::sourcelist");
360 string Parts = _config->FindDir("Dir::Etc::sourceparts");
5e7b0aa9
MV
361
362 // go over the parts
363 if (DirectoryExists(Parts) == true)
364 List = GetListOfFilesInDir(Parts, "list", true);
2ec858bc
MV
365
366 // calculate the time
367 time_t mtime_sources = GetModificationTime(Main);
f7f0d6c7 368 for (vector<string>::const_iterator I = List.begin(); I != List.end(); ++I)
2ec858bc
MV
369 mtime_sources = std::max(mtime_sources, GetModificationTime(*I));
370
371 return mtime_sources;
372}
373 /*}}}*/
34b53501 374