]> git.saurik.com Git - apt.git/blame - apt-pkg/sourcelist.cc
add changelog
[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 == '[')
404528bd 273 I = strchr(I + 1, ']');
b2e465d6
AL
274 *I = 0;
275
276 const char *C = _strstrip(Buffer);
6c139d6e
AL
277
278 // Comment or blank
b2e465d6 279 if (C[0] == '#' || C[0] == 0)
6c139d6e 280 continue;
b2e465d6 281
6c139d6e 282 // Grok it
b2e465d6
AL
283 string LineType;
284 if (ParseQuoteWord(C,LineType) == false)
285 return _error->Error(_("Malformed line %u in source list %s (type)"),CurLine,File.c_str());
6c139d6e 286
b2e465d6
AL
287 Type *Parse = Type::GetType(LineType.c_str());
288 if (Parse == 0)
39442e44 289 return _error->Error(_("Type '%s' is not known on line %u in source list %s"),LineType.c_str(),CurLine,File.c_str());
6c139d6e 290
5dd4c8b8 291 if (Parse->ParseLine(SrcList, C, CurLine, File) == false)
b2e465d6 292 return false;
6c139d6e
AL
293 }
294 return true;
295}
296 /*}}}*/
b2e465d6 297// SourceList::FindIndex - Get the index associated with a file /*{{{*/
6c139d6e
AL
298// ---------------------------------------------------------------------
299/* */
b2e465d6
AL
300bool pkgSourceList::FindIndex(pkgCache::PkgFileIterator File,
301 pkgIndexFile *&Found) const
6c139d6e 302{
f7f0d6c7 303 for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
6c139d6e 304 {
7db98ffc
MZ
305 vector<pkgIndexFile *> *Indexes = (*I)->GetIndexFiles();
306 for (vector<pkgIndexFile *>::const_iterator J = Indexes->begin();
f7f0d6c7 307 J != Indexes->end(); ++J)
b2e465d6 308 {
7db98ffc
MZ
309 if ((*J)->FindInCache(*File.Cache()) == File)
310 {
311 Found = (*J);
312 return true;
313 }
b2e465d6 314 }
be8922fd 315 }
7db98ffc 316
b2e465d6 317 return false;
36375005
AL
318}
319 /*}}}*/
b2e465d6 320// SourceList::GetIndexes - Load the index files into the downloader /*{{{*/
6c139d6e
AL
321// ---------------------------------------------------------------------
322/* */
7db98ffc 323bool pkgSourceList::GetIndexes(pkgAcquire *Owner, bool GetAll) const
6c139d6e 324{
f7f0d6c7 325 for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I)
7db98ffc 326 if ((*I)->GetIndexes(Owner,GetAll) == false)
b2e465d6
AL
327 return false;
328 return true;
6c139d6e
AL
329}
330 /*}}}*/
34b53501
MV
331// CNC:2003-03-03 - By Anton V. Denisov <avd@altlinux.org>.
332// SourceList::ReadSourceDir - Read a directory with sources files
333// Based on ReadConfigDir() /*{{{*/
334// ---------------------------------------------------------------------
335/* */
336bool pkgSourceList::ReadSourceDir(string Dir)
337{
52643bec 338 vector<string> const List = GetListOfFilesInDir(Dir, "list", true);
34b53501
MV
339
340 // Read the files
f7f0d6c7 341 for (vector<string>::const_iterator I = List.begin(); I != List.end(); ++I)
34b53501
MV
342 if (ReadAppend(*I) == false)
343 return false;
344 return true;
345
346}
347 /*}}}*/
2ec858bc
MV
348// GetLastModified() /*{{{*/
349// ---------------------------------------------------------------------
350/* */
351time_t pkgSourceList::GetLastModifiedTime()
352{
5e7b0aa9
MV
353 vector<string> List;
354
2ec858bc
MV
355 string Main = _config->FindFile("Dir::Etc::sourcelist");
356 string Parts = _config->FindDir("Dir::Etc::sourceparts");
5e7b0aa9
MV
357
358 // go over the parts
359 if (DirectoryExists(Parts) == true)
360 List = GetListOfFilesInDir(Parts, "list", true);
2ec858bc
MV
361
362 // calculate the time
363 time_t mtime_sources = GetModificationTime(Main);
f7f0d6c7 364 for (vector<string>::const_iterator I = List.begin(); I != List.end(); ++I)
2ec858bc
MV
365 mtime_sources = std::max(mtime_sources, GetModificationTime(*I));
366
367 return mtime_sources;
368}
369 /*}}}*/
34b53501 370