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