merge with debian-sid
[ntk/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 /*{{{*/
094a497d
AL
11#include <apt-pkg/sourcelist.h>
12#include <apt-pkg/error.h>
13#include <apt-pkg/fileutl.h>
cdcc6d34 14#include <apt-pkg/strutl.h>
7db98ffc 15#include <apt-pkg/configuration.h>
6c139d6e 16
b2e465d6
AL
17#include <apti18n.h>
18
90f057fd 19#include <fstream>
b2e465d6
AL
20 /*}}}*/
21
4d6f8bdf
AL
22using namespace std;
23
24// Global list of Items supported
b2e465d6
AL
25static pkgSourceList::Type *ItmList[10];
26pkgSourceList::Type **pkgSourceList::Type::GlobalList = ItmList;
27unsigned long pkgSourceList::Type::GlobalListLen = 0;
28
29// Type::Type - Constructor /*{{{*/
30// ---------------------------------------------------------------------
31/* Link this to the global list of items*/
32pkgSourceList::Type::Type()
33{
34 ItmList[GlobalListLen] = this;
35 GlobalListLen++;
36}
37 /*}}}*/
38// Type::GetType - Get a specific meta for a given type /*{{{*/
39// ---------------------------------------------------------------------
40/* */
41pkgSourceList::Type *pkgSourceList::Type::GetType(const char *Type)
42{
43 for (unsigned I = 0; I != GlobalListLen; I++)
44 if (strcmp(GlobalList[I]->Name,Type) == 0)
45 return GlobalList[I];
46 return 0;
47}
48 /*}}}*/
49// Type::FixupURI - Normalize the URI and check it.. /*{{{*/
50// ---------------------------------------------------------------------
51/* */
52bool pkgSourceList::Type::FixupURI(string &URI) const
53{
54 if (URI.empty() == true)
55 return false;
56
57 if (URI.find(':') == string::npos)
58 return false;
59
60 URI = SubstVar(URI,"$(ARCH)",_config->Find("APT::Architecture"));
61
a7c835af 62 // Make sure that the URI is / postfixed
b2e465d6
AL
63 if (URI[URI.size() - 1] != '/')
64 URI += '/';
65
66 return true;
67}
68 /*}}}*/
69// Type::ParseLine - Parse a single line /*{{{*/
70// ---------------------------------------------------------------------
71/* This is a generic one that is the 'usual' format for sources.list
72 Weird types may override this. */
7db98ffc 73bool pkgSourceList::Type::ParseLine(vector<metaIndex *> &List,
b2e465d6
AL
74 const char *Buffer,
75 unsigned long CurLine,
76 string File) const
77{
78 string URI;
79 string Dist;
80 string Section;
81
82 if (ParseQuoteWord(Buffer,URI) == false)
83 return _error->Error(_("Malformed line %lu in source list %s (URI)"),CurLine,File.c_str());
84 if (ParseQuoteWord(Buffer,Dist) == false)
85 return _error->Error(_("Malformed line %lu in source list %s (dist)"),CurLine,File.c_str());
86
87 if (FixupURI(URI) == false)
88 return _error->Error(_("Malformed line %lu in source list %s (URI parse)"),CurLine,File.c_str());
89
90 // Check for an absolute dists specification.
91 if (Dist.empty() == false && Dist[Dist.size() - 1] == '/')
92 {
93 if (ParseQuoteWord(Buffer,Section) == true)
db0db9fe 94 return _error->Error(_("Malformed line %lu in source list %s (absolute dist)"),CurLine,File.c_str());
b2e465d6 95 Dist = SubstVar(Dist,"$(ARCH)",_config->Find("APT::Architecture"));
7db98ffc 96 return CreateItem(List,URI,Dist,Section);
b2e465d6
AL
97 }
98
99 // Grab the rest of the dists
100 if (ParseQuoteWord(Buffer,Section) == false)
101 return _error->Error(_("Malformed line %lu in source list %s (dist parse)"),CurLine,File.c_str());
102
103 do
104 {
7db98ffc 105 if (CreateItem(List,URI,Dist,Section) == false)
b2e465d6
AL
106 return false;
107 }
108 while (ParseQuoteWord(Buffer,Section) == true);
109
110 return true;
111}
6c139d6e
AL
112 /*}}}*/
113
114// SourceList::pkgSourceList - Constructors /*{{{*/
115// ---------------------------------------------------------------------
116/* */
117pkgSourceList::pkgSourceList()
118{
119}
120
121pkgSourceList::pkgSourceList(string File)
122{
123 Read(File);
124}
125 /*}}}*/
1c193e02
AL
126// SourceList::~pkgSourceList - Destructor /*{{{*/
127// ---------------------------------------------------------------------
128/* */
129pkgSourceList::~pkgSourceList()
130{
131 for (const_iterator I = SrcList.begin(); I != SrcList.end(); I++)
132 delete *I;
1c193e02
AL
133}
134 /*}}}*/
a7c835af 135 /*}}}*/
6c139d6e
AL
136// SourceList::ReadMainList - Read the main source list from etc /*{{{*/
137// ---------------------------------------------------------------------
138/* */
139bool pkgSourceList::ReadMainList()
140{
34b53501
MV
141 // CNC:2003-03-03 - Multiple sources list support.
142 bool Res = true;
143#if 0
144 Res = ReadVendors();
145 if (Res == false)
146 return false;
147#endif
148
149 Reset();
150 // CNC:2003-11-28 - Entries in sources.list have priority over
151 // entries in sources.list.d.
152 string Main = _config->FindFile("Dir::Etc::sourcelist");
67793cf3
JAK
153 string Parts = _config->FindDir("Dir::Etc::sourceparts");
154
34b53501 155 if (FileExists(Main) == true)
6009e60d 156 Res &= ReadAppend(Main);
67793cf3
JAK
157 else if (FileExists(Parts) == false)
158 // Only warn if there are no sources.list.d.
6009e60d 159 _error->WarningE("FileExists",_("Unable to read %s"),Main.c_str());
34b53501 160
34b53501
MV
161 if (FileExists(Parts) == true)
162 Res &= ReadSourceDir(Parts);
67793cf3
JAK
163 else if (FileExists(Main) == false)
164 // Only warn if there is no sources.list file.
6009e60d
DK
165 _error->WarningE("FileExists",_("Unable to read %s"),Parts.c_str());
166
34b53501 167 return Res;
6c139d6e
AL
168}
169 /*}}}*/
34b53501
MV
170// CNC:2003-03-03 - Needed to preserve backwards compatibility.
171// SourceList::Reset - Clear the sourcelist contents /*{{{*/
172// ---------------------------------------------------------------------
173/* */
174void pkgSourceList::Reset()
175{
176 for (const_iterator I = SrcList.begin(); I != SrcList.end(); I++)
177 delete *I;
178 SrcList.erase(SrcList.begin(),SrcList.end());
179}
180 /*}}}*/
181// CNC:2003-03-03 - Function moved to ReadAppend() and Reset().
6c139d6e
AL
182// SourceList::Read - Parse the sourcelist file /*{{{*/
183// ---------------------------------------------------------------------
184/* */
185bool pkgSourceList::Read(string File)
34b53501
MV
186{
187 Reset();
188 return ReadAppend(File);
189}
190 /*}}}*/
191// SourceList::ReadAppend - Parse a sourcelist file /*{{{*/
192// ---------------------------------------------------------------------
193/* */
194bool pkgSourceList::ReadAppend(string File)
6c139d6e
AL
195{
196 // Open the stream for reading
4d6f8bdf 197 ifstream F(File.c_str(),ios::in /*| ios::nocreate*/);
6c139d6e 198 if (!F != 0)
b2e465d6 199 return _error->Errno("ifstream::ifstream",_("Opening %s"),File.c_str());
6c139d6e 200
34b53501 201#if 0 // Now Reset() does this.
1c193e02
AL
202 for (const_iterator I = SrcList.begin(); I != SrcList.end(); I++)
203 delete *I;
a7c835af 204 SrcList.erase(SrcList.begin(),SrcList.end());
34b53501
MV
205#endif
206 // CNC:2003-12-10 - 300 is too short.
207 char Buffer[1024];
6c139d6e
AL
208
209 int CurLine = 0;
210 while (F.eof() == false)
211 {
212 F.getline(Buffer,sizeof(Buffer));
213 CurLine++;
214 _strtabexpand(Buffer,sizeof(Buffer));
ba1045b4
AL
215 if (F.fail() && !F.eof())
216 return _error->Error(_("Line %u too long in source list %s."),
217 CurLine,File.c_str());
218
b2e465d6
AL
219
220 char *I;
34b53501
MV
221 // CNC:2003-02-20 - Do not break if '#' is inside [].
222 for (I = Buffer; *I != 0 && *I != '#'; I++)
223 if (*I == '[')
224 for (I++; *I != 0 && *I != ']'; I++);
b2e465d6
AL
225 *I = 0;
226
227 const char *C = _strstrip(Buffer);
6c139d6e
AL
228
229 // Comment or blank
b2e465d6 230 if (C[0] == '#' || C[0] == 0)
6c139d6e 231 continue;
b2e465d6 232
6c139d6e 233 // Grok it
b2e465d6
AL
234 string LineType;
235 if (ParseQuoteWord(C,LineType) == false)
236 return _error->Error(_("Malformed line %u in source list %s (type)"),CurLine,File.c_str());
6c139d6e 237
b2e465d6
AL
238 Type *Parse = Type::GetType(LineType.c_str());
239 if (Parse == 0)
39442e44 240 return _error->Error(_("Type '%s' is not known on line %u in source list %s"),LineType.c_str(),CurLine,File.c_str());
6c139d6e 241
7db98ffc 242 // Vendor name specified
a7c835af
AL
243 if (C[0] == '[')
244 {
245 string VendorID;
246
247 if (ParseQuoteWord(C,VendorID) == false)
248 return _error->Error(_("Malformed line %u in source list %s (vendor id)"),CurLine,File.c_str());
249
250 if (VendorID.length() < 2 || VendorID.end()[-1] != ']')
251 return _error->Error(_("Malformed line %u in source list %s (vendor id)"),CurLine,File.c_str());
252 VendorID = string(VendorID,1,VendorID.size()-2);
253
7db98ffc
MZ
254// for (vector<const Vendor *>::const_iterator iter = VendorList.begin();
255// iter != VendorList.end(); iter++)
256// {
257// if ((*iter)->GetVendorID() == VendorID)
258// {
259// if (_config->FindB("Debug::sourceList", false))
260// std::cerr << "Comparing VendorID \"" << VendorID << "\" with \"" << (*iter)->GetVendorID() << '"' << std::endl;
261// Verifier = *iter;
262// break;
263// }
264// }
a7c835af 265
7db98ffc
MZ
266// if (Verifier == 0)
267// return _error->Error(_("Unknown vendor ID '%s' in line %u of source list %s"),
268// VendorID.c_str(),CurLine,File.c_str());
a7c835af 269 }
7db98ffc
MZ
270
271 if (Parse->ParseLine(SrcList,C,CurLine,File) == false)
b2e465d6 272 return false;
6c139d6e
AL
273 }
274 return true;
275}
276 /*}}}*/
b2e465d6 277// SourceList::FindIndex - Get the index associated with a file /*{{{*/
6c139d6e
AL
278// ---------------------------------------------------------------------
279/* */
b2e465d6
AL
280bool pkgSourceList::FindIndex(pkgCache::PkgFileIterator File,
281 pkgIndexFile *&Found) const
6c139d6e 282{
a7c835af 283 for (const_iterator I = SrcList.begin(); I != SrcList.end(); I++)
6c139d6e 284 {
7db98ffc
MZ
285 vector<pkgIndexFile *> *Indexes = (*I)->GetIndexFiles();
286 for (vector<pkgIndexFile *>::const_iterator J = Indexes->begin();
287 J != Indexes->end(); J++)
b2e465d6 288 {
7db98ffc
MZ
289 if ((*J)->FindInCache(*File.Cache()) == File)
290 {
291 Found = (*J);
292 return true;
293 }
b2e465d6 294 }
be8922fd 295 }
7db98ffc 296
b2e465d6 297 return false;
36375005
AL
298}
299 /*}}}*/
b2e465d6 300// SourceList::GetIndexes - Load the index files into the downloader /*{{{*/
6c139d6e
AL
301// ---------------------------------------------------------------------
302/* */
7db98ffc 303bool pkgSourceList::GetIndexes(pkgAcquire *Owner, bool GetAll) const
6c139d6e 304{
a7c835af 305 for (const_iterator I = SrcList.begin(); I != SrcList.end(); I++)
7db98ffc 306 if ((*I)->GetIndexes(Owner,GetAll) == false)
b2e465d6
AL
307 return false;
308 return true;
6c139d6e
AL
309}
310 /*}}}*/
34b53501
MV
311// CNC:2003-03-03 - By Anton V. Denisov <avd@altlinux.org>.
312// SourceList::ReadSourceDir - Read a directory with sources files
313// Based on ReadConfigDir() /*{{{*/
314// ---------------------------------------------------------------------
315/* */
316bool pkgSourceList::ReadSourceDir(string Dir)
317{
52643bec 318 vector<string> const List = GetListOfFilesInDir(Dir, "list", true);
34b53501
MV
319
320 // Read the files
321 for (vector<string>::const_iterator I = List.begin(); I != List.end(); I++)
322 if (ReadAppend(*I) == false)
323 return false;
324 return true;
325
326}
327 /*}}}*/
328