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