]> git.saurik.com Git - apt.git/blame - apt-pkg/sourcelist.cc
criss-cross merge with my sid branch
[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 74 const char *Buffer,
5dd4c8b8
DK
75 unsigned long const &CurLine,
76 string const &File) const
b2e465d6 77{
5dd4c8b8
DK
78 for (;Buffer != 0 && isspace(*Buffer); ++Buffer); // Skip whitespaces
79
80 // Parse option field if it exists
81 // e.g.: [ option1=value1 option2=value2 ]
82 map<string, string> Options;
83 if (Buffer != 0 && Buffer[0] == '[')
84 {
85 ++Buffer; // ignore the [
86 for (;Buffer != 0 && isspace(*Buffer); ++Buffer); // Skip whitespaces
87 while (*Buffer != ']')
88 {
89 // get one option, e.g. option1=value1
90 string option;
91 if (ParseQuoteWord(Buffer,option) == false)
92 return _error->Error(_("Malformed line %lu in source list %s ([option] unparseable)"),CurLine,File.c_str());
93
94 if (option.length() < 3)
95 return _error->Error(_("Malformed line %lu in source list %s ([option] too short)"),CurLine,File.c_str());
96
97 size_t const needle = option.find('=');
98 if (needle == string::npos)
99 return _error->Error(_("Malformed line %lu in source list %s ([%s] is not an assignment)"),CurLine,File.c_str(), option.c_str());
100
101 string const key = string(option, 0, needle);
102 string const value = string(option, needle + 1, option.length());
103
104 if (key.empty() == true)
105 return _error->Error(_("Malformed line %lu in source list %s ([%s] has no key)"),CurLine,File.c_str(), option.c_str());
106
107 if (value.empty() == true)
108 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());
109
110 Options[key] = value;
111 }
112 ++Buffer; // ignore the ]
113 for (;Buffer != 0 && isspace(*Buffer); ++Buffer); // Skip whitespaces
114 }
115
b2e465d6
AL
116 string URI;
117 string Dist;
5dd4c8b8
DK
118 string Section;
119
b2e465d6
AL
120 if (ParseQuoteWord(Buffer,URI) == false)
121 return _error->Error(_("Malformed line %lu in source list %s (URI)"),CurLine,File.c_str());
122 if (ParseQuoteWord(Buffer,Dist) == false)
123 return _error->Error(_("Malformed line %lu in source list %s (dist)"),CurLine,File.c_str());
124
125 if (FixupURI(URI) == false)
126 return _error->Error(_("Malformed line %lu in source list %s (URI parse)"),CurLine,File.c_str());
127
128 // Check for an absolute dists specification.
129 if (Dist.empty() == false && Dist[Dist.size() - 1] == '/')
130 {
131 if (ParseQuoteWord(Buffer,Section) == true)
db0db9fe 132 return _error->Error(_("Malformed line %lu in source list %s (absolute dist)"),CurLine,File.c_str());
b2e465d6 133 Dist = SubstVar(Dist,"$(ARCH)",_config->Find("APT::Architecture"));
5dd4c8b8 134 return CreateItem(List, URI, Dist, Section, Options);
b2e465d6
AL
135 }
136
137 // Grab the rest of the dists
138 if (ParseQuoteWord(Buffer,Section) == false)
139 return _error->Error(_("Malformed line %lu in source list %s (dist parse)"),CurLine,File.c_str());
140
141 do
142 {
5dd4c8b8 143 if (CreateItem(List, URI, Dist, Section, Options) == false)
b2e465d6
AL
144 return false;
145 }
146 while (ParseQuoteWord(Buffer,Section) == true);
147
148 return true;
149}
6c139d6e
AL
150 /*}}}*/
151
152// SourceList::pkgSourceList - Constructors /*{{{*/
153// ---------------------------------------------------------------------
154/* */
155pkgSourceList::pkgSourceList()
156{
157}
158
159pkgSourceList::pkgSourceList(string File)
160{
161 Read(File);
162}
163 /*}}}*/
1c193e02
AL
164// SourceList::~pkgSourceList - Destructor /*{{{*/
165// ---------------------------------------------------------------------
166/* */
167pkgSourceList::~pkgSourceList()
168{
169 for (const_iterator I = SrcList.begin(); I != SrcList.end(); I++)
170 delete *I;
1c193e02
AL
171}
172 /*}}}*/
a7c835af 173 /*}}}*/
6c139d6e
AL
174// SourceList::ReadMainList - Read the main source list from etc /*{{{*/
175// ---------------------------------------------------------------------
176/* */
177bool pkgSourceList::ReadMainList()
178{
34b53501
MV
179 // CNC:2003-03-03 - Multiple sources list support.
180 bool Res = true;
181#if 0
182 Res = ReadVendors();
183 if (Res == false)
184 return false;
185#endif
186
187 Reset();
188 // CNC:2003-11-28 - Entries in sources.list have priority over
189 // entries in sources.list.d.
190 string Main = _config->FindFile("Dir::Etc::sourcelist");
67793cf3
JAK
191 string Parts = _config->FindDir("Dir::Etc::sourceparts");
192
34b53501 193 if (FileExists(Main) == true)
6009e60d 194 Res &= ReadAppend(Main);
67793cf3
JAK
195 else if (FileExists(Parts) == false)
196 // Only warn if there are no sources.list.d.
6009e60d 197 _error->WarningE("FileExists",_("Unable to read %s"),Main.c_str());
34b53501 198
34b53501
MV
199 if (FileExists(Parts) == true)
200 Res &= ReadSourceDir(Parts);
67793cf3
JAK
201 else if (FileExists(Main) == false)
202 // Only warn if there is no sources.list file.
6009e60d
DK
203 _error->WarningE("FileExists",_("Unable to read %s"),Parts.c_str());
204
34b53501 205 return Res;
6c139d6e
AL
206}
207 /*}}}*/
34b53501
MV
208// CNC:2003-03-03 - Needed to preserve backwards compatibility.
209// SourceList::Reset - Clear the sourcelist contents /*{{{*/
210// ---------------------------------------------------------------------
211/* */
212void pkgSourceList::Reset()
213{
214 for (const_iterator I = SrcList.begin(); I != SrcList.end(); I++)
215 delete *I;
216 SrcList.erase(SrcList.begin(),SrcList.end());
217}
218 /*}}}*/
219// CNC:2003-03-03 - Function moved to ReadAppend() and Reset().
6c139d6e
AL
220// SourceList::Read - Parse the sourcelist file /*{{{*/
221// ---------------------------------------------------------------------
222/* */
223bool pkgSourceList::Read(string File)
34b53501
MV
224{
225 Reset();
226 return ReadAppend(File);
227}
228 /*}}}*/
229// SourceList::ReadAppend - Parse a sourcelist file /*{{{*/
230// ---------------------------------------------------------------------
231/* */
232bool pkgSourceList::ReadAppend(string File)
6c139d6e
AL
233{
234 // Open the stream for reading
4d6f8bdf 235 ifstream F(File.c_str(),ios::in /*| ios::nocreate*/);
6c139d6e 236 if (!F != 0)
b2e465d6 237 return _error->Errno("ifstream::ifstream",_("Opening %s"),File.c_str());
6c139d6e 238
34b53501 239#if 0 // Now Reset() does this.
1c193e02
AL
240 for (const_iterator I = SrcList.begin(); I != SrcList.end(); I++)
241 delete *I;
a7c835af 242 SrcList.erase(SrcList.begin(),SrcList.end());
34b53501
MV
243#endif
244 // CNC:2003-12-10 - 300 is too short.
245 char Buffer[1024];
6c139d6e
AL
246
247 int CurLine = 0;
248 while (F.eof() == false)
249 {
250 F.getline(Buffer,sizeof(Buffer));
251 CurLine++;
252 _strtabexpand(Buffer,sizeof(Buffer));
ba1045b4
AL
253 if (F.fail() && !F.eof())
254 return _error->Error(_("Line %u too long in source list %s."),
255 CurLine,File.c_str());
256
b2e465d6
AL
257
258 char *I;
34b53501
MV
259 // CNC:2003-02-20 - Do not break if '#' is inside [].
260 for (I = Buffer; *I != 0 && *I != '#'; I++)
261 if (*I == '[')
262 for (I++; *I != 0 && *I != ']'; I++);
b2e465d6
AL
263 *I = 0;
264
265 const char *C = _strstrip(Buffer);
6c139d6e
AL
266
267 // Comment or blank
b2e465d6 268 if (C[0] == '#' || C[0] == 0)
6c139d6e 269 continue;
b2e465d6 270
6c139d6e 271 // Grok it
b2e465d6
AL
272 string LineType;
273 if (ParseQuoteWord(C,LineType) == false)
274 return _error->Error(_("Malformed line %u in source list %s (type)"),CurLine,File.c_str());
6c139d6e 275
b2e465d6
AL
276 Type *Parse = Type::GetType(LineType.c_str());
277 if (Parse == 0)
39442e44 278 return _error->Error(_("Type '%s' is not known on line %u in source list %s"),LineType.c_str(),CurLine,File.c_str());
6c139d6e 279
5dd4c8b8 280 if (Parse->ParseLine(SrcList, C, CurLine, File) == false)
b2e465d6 281 return false;
6c139d6e
AL
282 }
283 return true;
284}
285 /*}}}*/
b2e465d6 286// SourceList::FindIndex - Get the index associated with a file /*{{{*/
6c139d6e
AL
287// ---------------------------------------------------------------------
288/* */
b2e465d6
AL
289bool pkgSourceList::FindIndex(pkgCache::PkgFileIterator File,
290 pkgIndexFile *&Found) const
6c139d6e 291{
a7c835af 292 for (const_iterator I = SrcList.begin(); I != SrcList.end(); I++)
6c139d6e 293 {
7db98ffc
MZ
294 vector<pkgIndexFile *> *Indexes = (*I)->GetIndexFiles();
295 for (vector<pkgIndexFile *>::const_iterator J = Indexes->begin();
296 J != Indexes->end(); J++)
b2e465d6 297 {
7db98ffc
MZ
298 if ((*J)->FindInCache(*File.Cache()) == File)
299 {
300 Found = (*J);
301 return true;
302 }
b2e465d6 303 }
be8922fd 304 }
7db98ffc 305
b2e465d6 306 return false;
36375005
AL
307}
308 /*}}}*/
b2e465d6 309// SourceList::GetIndexes - Load the index files into the downloader /*{{{*/
6c139d6e
AL
310// ---------------------------------------------------------------------
311/* */
7db98ffc 312bool pkgSourceList::GetIndexes(pkgAcquire *Owner, bool GetAll) const
6c139d6e 313{
a7c835af 314 for (const_iterator I = SrcList.begin(); I != SrcList.end(); I++)
7db98ffc 315 if ((*I)->GetIndexes(Owner,GetAll) == false)
b2e465d6
AL
316 return false;
317 return true;
6c139d6e
AL
318}
319 /*}}}*/
34b53501
MV
320// CNC:2003-03-03 - By Anton V. Denisov <avd@altlinux.org>.
321// SourceList::ReadSourceDir - Read a directory with sources files
322// Based on ReadConfigDir() /*{{{*/
323// ---------------------------------------------------------------------
324/* */
325bool pkgSourceList::ReadSourceDir(string Dir)
326{
52643bec 327 vector<string> const List = GetListOfFilesInDir(Dir, "list", true);
34b53501
MV
328
329 // Read the files
330 for (vector<string>::const_iterator I = List.begin(); I != List.end(); I++)
331 if (ReadAppend(*I) == false)
332 return false;
333 return true;
334
335}
336 /*}}}*/
337