]> git.saurik.com Git - apt.git/blame - apt-pkg/sourcelist.cc
[apt-pkg/sourcelist.cc] only display the warning about missing sources.list
[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
AL
81 const char *Buffer,
82 unsigned long CurLine,
83 string File) const
84{
85 string URI;
86 string Dist;
87 string Section;
88
89 if (ParseQuoteWord(Buffer,URI) == false)
90 return _error->Error(_("Malformed line %lu in source list %s (URI)"),CurLine,File.c_str());
91 if (ParseQuoteWord(Buffer,Dist) == false)
92 return _error->Error(_("Malformed line %lu in source list %s (dist)"),CurLine,File.c_str());
93
94 if (FixupURI(URI) == false)
95 return _error->Error(_("Malformed line %lu in source list %s (URI parse)"),CurLine,File.c_str());
96
97 // Check for an absolute dists specification.
98 if (Dist.empty() == false && Dist[Dist.size() - 1] == '/')
99 {
100 if (ParseQuoteWord(Buffer,Section) == true)
db0db9fe 101 return _error->Error(_("Malformed line %lu in source list %s (absolute dist)"),CurLine,File.c_str());
b2e465d6 102 Dist = SubstVar(Dist,"$(ARCH)",_config->Find("APT::Architecture"));
7db98ffc 103 return CreateItem(List,URI,Dist,Section);
b2e465d6
AL
104 }
105
106 // Grab the rest of the dists
107 if (ParseQuoteWord(Buffer,Section) == false)
108 return _error->Error(_("Malformed line %lu in source list %s (dist parse)"),CurLine,File.c_str());
109
110 do
111 {
7db98ffc 112 if (CreateItem(List,URI,Dist,Section) == false)
b2e465d6
AL
113 return false;
114 }
115 while (ParseQuoteWord(Buffer,Section) == true);
116
117 return true;
118}
6c139d6e
AL
119 /*}}}*/
120
121// SourceList::pkgSourceList - Constructors /*{{{*/
122// ---------------------------------------------------------------------
123/* */
124pkgSourceList::pkgSourceList()
125{
126}
127
128pkgSourceList::pkgSourceList(string File)
129{
130 Read(File);
131}
132 /*}}}*/
1c193e02
AL
133// SourceList::~pkgSourceList - Destructor /*{{{*/
134// ---------------------------------------------------------------------
135/* */
136pkgSourceList::~pkgSourceList()
137{
138 for (const_iterator I = SrcList.begin(); I != SrcList.end(); I++)
139 delete *I;
1c193e02
AL
140}
141 /*}}}*/
a7c835af 142 /*}}}*/
6c139d6e
AL
143// SourceList::ReadMainList - Read the main source list from etc /*{{{*/
144// ---------------------------------------------------------------------
145/* */
146bool pkgSourceList::ReadMainList()
147{
34b53501
MV
148 // CNC:2003-03-03 - Multiple sources list support.
149 bool Res = true;
150#if 0
151 Res = ReadVendors();
152 if (Res == false)
153 return false;
154#endif
155
156 Reset();
157 // CNC:2003-11-28 - Entries in sources.list have priority over
158 // entries in sources.list.d.
159 string Main = _config->FindFile("Dir::Etc::sourcelist");
29d0b55e 160 string noSourceMsg;
34b53501 161 if (FileExists(Main) == true)
6009e60d
DK
162 Res &= ReadAppend(Main);
163 else
29d0b55e
DK
164 {
165 // only print the warning if we can't load a valid sourcefile in the end
6009e60d 166 _error->WarningE("FileExists",_("Unable to read %s"),Main.c_str());
29d0b55e
DK
167 _error->PopMessage(noSourceMsg);
168 }
34b53501
MV
169
170 string Parts = _config->FindDir("Dir::Etc::sourceparts");
171 if (FileExists(Parts) == true)
172 Res &= ReadSourceDir(Parts);
6009e60d
DK
173 else
174 _error->WarningE("FileExists",_("Unable to read %s"),Parts.c_str());
175
29d0b55e
DK
176 if (SrcList.empty() == true && noSourceMsg.empty() == false)
177 _error->Warning(noSourceMsg.c_str());
178
34b53501 179 return Res;
6c139d6e
AL
180}
181 /*}}}*/
34b53501
MV
182// CNC:2003-03-03 - Needed to preserve backwards compatibility.
183// SourceList::Reset - Clear the sourcelist contents /*{{{*/
184// ---------------------------------------------------------------------
185/* */
186void pkgSourceList::Reset()
187{
188 for (const_iterator I = SrcList.begin(); I != SrcList.end(); I++)
189 delete *I;
190 SrcList.erase(SrcList.begin(),SrcList.end());
191}
192 /*}}}*/
193// CNC:2003-03-03 - Function moved to ReadAppend() and Reset().
6c139d6e
AL
194// SourceList::Read - Parse the sourcelist file /*{{{*/
195// ---------------------------------------------------------------------
196/* */
197bool pkgSourceList::Read(string File)
34b53501
MV
198{
199 Reset();
200 return ReadAppend(File);
201}
202 /*}}}*/
203// SourceList::ReadAppend - Parse a sourcelist file /*{{{*/
204// ---------------------------------------------------------------------
205/* */
206bool pkgSourceList::ReadAppend(string File)
6c139d6e
AL
207{
208 // Open the stream for reading
4d6f8bdf 209 ifstream F(File.c_str(),ios::in /*| ios::nocreate*/);
6c139d6e 210 if (!F != 0)
b2e465d6 211 return _error->Errno("ifstream::ifstream",_("Opening %s"),File.c_str());
6c139d6e 212
34b53501 213#if 0 // Now Reset() does this.
1c193e02
AL
214 for (const_iterator I = SrcList.begin(); I != SrcList.end(); I++)
215 delete *I;
a7c835af 216 SrcList.erase(SrcList.begin(),SrcList.end());
34b53501
MV
217#endif
218 // CNC:2003-12-10 - 300 is too short.
219 char Buffer[1024];
6c139d6e
AL
220
221 int CurLine = 0;
222 while (F.eof() == false)
223 {
224 F.getline(Buffer,sizeof(Buffer));
225 CurLine++;
226 _strtabexpand(Buffer,sizeof(Buffer));
ba1045b4
AL
227 if (F.fail() && !F.eof())
228 return _error->Error(_("Line %u too long in source list %s."),
229 CurLine,File.c_str());
230
b2e465d6
AL
231
232 char *I;
34b53501
MV
233 // CNC:2003-02-20 - Do not break if '#' is inside [].
234 for (I = Buffer; *I != 0 && *I != '#'; I++)
235 if (*I == '[')
236 for (I++; *I != 0 && *I != ']'; I++);
b2e465d6
AL
237 *I = 0;
238
239 const char *C = _strstrip(Buffer);
6c139d6e
AL
240
241 // Comment or blank
b2e465d6 242 if (C[0] == '#' || C[0] == 0)
6c139d6e 243 continue;
b2e465d6 244
6c139d6e 245 // Grok it
b2e465d6
AL
246 string LineType;
247 if (ParseQuoteWord(C,LineType) == false)
248 return _error->Error(_("Malformed line %u in source list %s (type)"),CurLine,File.c_str());
6c139d6e 249
b2e465d6
AL
250 Type *Parse = Type::GetType(LineType.c_str());
251 if (Parse == 0)
39442e44 252 return _error->Error(_("Type '%s' is not known on line %u in source list %s"),LineType.c_str(),CurLine,File.c_str());
6c139d6e 253
7db98ffc 254 // Vendor name specified
a7c835af
AL
255 if (C[0] == '[')
256 {
257 string VendorID;
258
259 if (ParseQuoteWord(C,VendorID) == false)
260 return _error->Error(_("Malformed line %u in source list %s (vendor id)"),CurLine,File.c_str());
261
262 if (VendorID.length() < 2 || VendorID.end()[-1] != ']')
263 return _error->Error(_("Malformed line %u in source list %s (vendor id)"),CurLine,File.c_str());
264 VendorID = string(VendorID,1,VendorID.size()-2);
265
7db98ffc
MZ
266// for (vector<const Vendor *>::const_iterator iter = VendorList.begin();
267// iter != VendorList.end(); iter++)
268// {
269// if ((*iter)->GetVendorID() == VendorID)
270// {
271// if (_config->FindB("Debug::sourceList", false))
272// std::cerr << "Comparing VendorID \"" << VendorID << "\" with \"" << (*iter)->GetVendorID() << '"' << std::endl;
273// Verifier = *iter;
274// break;
275// }
276// }
a7c835af 277
7db98ffc
MZ
278// if (Verifier == 0)
279// return _error->Error(_("Unknown vendor ID '%s' in line %u of source list %s"),
280// VendorID.c_str(),CurLine,File.c_str());
a7c835af 281 }
7db98ffc
MZ
282
283 if (Parse->ParseLine(SrcList,C,CurLine,File) == false)
b2e465d6 284 return false;
6c139d6e
AL
285 }
286 return true;
287}
288 /*}}}*/
b2e465d6 289// SourceList::FindIndex - Get the index associated with a file /*{{{*/
6c139d6e
AL
290// ---------------------------------------------------------------------
291/* */
b2e465d6
AL
292bool pkgSourceList::FindIndex(pkgCache::PkgFileIterator File,
293 pkgIndexFile *&Found) const
6c139d6e 294{
a7c835af 295 for (const_iterator I = SrcList.begin(); I != SrcList.end(); I++)
6c139d6e 296 {
7db98ffc
MZ
297 vector<pkgIndexFile *> *Indexes = (*I)->GetIndexFiles();
298 for (vector<pkgIndexFile *>::const_iterator J = Indexes->begin();
299 J != Indexes->end(); J++)
b2e465d6 300 {
7db98ffc
MZ
301 if ((*J)->FindInCache(*File.Cache()) == File)
302 {
303 Found = (*J);
304 return true;
305 }
b2e465d6 306 }
be8922fd 307 }
7db98ffc 308
b2e465d6 309 return false;
36375005
AL
310}
311 /*}}}*/
b2e465d6 312// SourceList::GetIndexes - Load the index files into the downloader /*{{{*/
6c139d6e
AL
313// ---------------------------------------------------------------------
314/* */
7db98ffc 315bool pkgSourceList::GetIndexes(pkgAcquire *Owner, bool GetAll) const
6c139d6e 316{
a7c835af 317 for (const_iterator I = SrcList.begin(); I != SrcList.end(); I++)
7db98ffc 318 if ((*I)->GetIndexes(Owner,GetAll) == false)
b2e465d6
AL
319 return false;
320 return true;
6c139d6e
AL
321}
322 /*}}}*/
34b53501
MV
323// CNC:2003-03-03 - By Anton V. Denisov <avd@altlinux.org>.
324// SourceList::ReadSourceDir - Read a directory with sources files
325// Based on ReadConfigDir() /*{{{*/
326// ---------------------------------------------------------------------
327/* */
328bool pkgSourceList::ReadSourceDir(string Dir)
329{
330 DIR *D = opendir(Dir.c_str());
331 if (D == 0)
332 return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
333
334 vector<string> List;
335
336 for (struct dirent *Ent = readdir(D); Ent != 0; Ent = readdir(D))
337 {
338 if (Ent->d_name[0] == '.')
339 continue;
340
341 // CNC:2003-12-02 Only accept .list files as valid sourceparts
342 if (flExtension(Ent->d_name) != "list")
343 continue;
344
345 // Skip bad file names ala run-parts
346 const char *C = Ent->d_name;
347 for (; *C != 0; C++)
348 if (isalpha(*C) == 0 && isdigit(*C) == 0
349 && *C != '_' && *C != '-' && *C != '.')
350 break;
351 if (*C != 0)
352 continue;
353
354 // Make sure it is a file and not something else
355 string File = flCombine(Dir,Ent->d_name);
356 struct stat St;
357 if (stat(File.c_str(),&St) != 0 || S_ISREG(St.st_mode) == 0)
358 continue;
359
360 List.push_back(File);
361 }
362 closedir(D);
363
364 sort(List.begin(),List.end());
365
366 // Read the files
367 for (vector<string>::const_iterator I = List.begin(); I != List.end(); I++)
368 if (ReadAppend(*I) == false)
369 return false;
370 return true;
371
372}
373 /*}}}*/
374