]> git.saurik.com Git - apt.git/blob - apt-pkg/sourcelist.cc
* apt-pkg/deb/dpkgpm.cc:
[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 #include <apt-pkg/sourcelist.h>
12 #include <apt-pkg/error.h>
13 #include <apt-pkg/fileutl.h>
14 #include <apt-pkg/strutl.h>
15 #include <apt-pkg/configuration.h>
16
17 #include <apti18n.h>
18
19 #include <fstream>
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>
27 /*}}}*/
28
29 using namespace std;
30
31 // Global list of Items supported
32 static pkgSourceList::Type *ItmList[10];
33 pkgSourceList::Type **pkgSourceList::Type::GlobalList = ItmList;
34 unsigned long pkgSourceList::Type::GlobalListLen = 0;
35
36 // Type::Type - Constructor /*{{{*/
37 // ---------------------------------------------------------------------
38 /* Link this to the global list of items*/
39 pkgSourceList::Type::Type()
40 {
41 ItmList[GlobalListLen] = this;
42 GlobalListLen++;
43 }
44 /*}}}*/
45 // Type::GetType - Get a specific meta for a given type /*{{{*/
46 // ---------------------------------------------------------------------
47 /* */
48 pkgSourceList::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 /* */
59 bool 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
69 // Make sure that the URI is / postfixed
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. */
80 bool pkgSourceList::Type::ParseLine(vector<metaIndex *> &List,
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)
101 return _error->Error(_("Malformed line %lu in source list %s (absolute dist)"),CurLine,File.c_str());
102 Dist = SubstVar(Dist,"$(ARCH)",_config->Find("APT::Architecture"));
103 return CreateItem(List,URI,Dist,Section);
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 {
112 if (CreateItem(List,URI,Dist,Section) == false)
113 return false;
114 }
115 while (ParseQuoteWord(Buffer,Section) == true);
116
117 return true;
118 }
119 /*}}}*/
120
121 // SourceList::pkgSourceList - Constructors /*{{{*/
122 // ---------------------------------------------------------------------
123 /* */
124 pkgSourceList::pkgSourceList()
125 {
126 }
127
128 pkgSourceList::pkgSourceList(string File)
129 {
130 Read(File);
131 }
132 /*}}}*/
133 // SourceList::~pkgSourceList - Destructor /*{{{*/
134 // ---------------------------------------------------------------------
135 /* */
136 pkgSourceList::~pkgSourceList()
137 {
138 for (const_iterator I = SrcList.begin(); I != SrcList.end(); I++)
139 delete *I;
140 }
141 /*}}}*/
142 /*}}}*/
143 // SourceList::ReadMainList - Read the main source list from etc /*{{{*/
144 // ---------------------------------------------------------------------
145 /* */
146 bool pkgSourceList::ReadMainList()
147 {
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");
160 if (FileExists(Main) == true)
161 Res &= ReadAppend(Main);
162
163 string Parts = _config->FindDir("Dir::Etc::sourceparts");
164 if (FileExists(Parts) == true)
165 Res &= ReadSourceDir(Parts);
166
167 return Res;
168 }
169 /*}}}*/
170 // CNC:2003-03-03 - Needed to preserve backwards compatibility.
171 // SourceList::Reset - Clear the sourcelist contents /*{{{*/
172 // ---------------------------------------------------------------------
173 /* */
174 void 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().
182 // SourceList::Read - Parse the sourcelist file /*{{{*/
183 // ---------------------------------------------------------------------
184 /* */
185 bool pkgSourceList::Read(string File)
186 {
187 Reset();
188 return ReadAppend(File);
189 }
190 /*}}}*/
191 // SourceList::ReadAppend - Parse a sourcelist file /*{{{*/
192 // ---------------------------------------------------------------------
193 /* */
194 bool pkgSourceList::ReadAppend(string File)
195 {
196 // Open the stream for reading
197 ifstream F(File.c_str(),ios::in /*| ios::nocreate*/);
198 if (!F != 0)
199 return _error->Errno("ifstream::ifstream",_("Opening %s"),File.c_str());
200
201 #if 0 // Now Reset() does this.
202 for (const_iterator I = SrcList.begin(); I != SrcList.end(); I++)
203 delete *I;
204 SrcList.erase(SrcList.begin(),SrcList.end());
205 #endif
206 // CNC:2003-12-10 - 300 is too short.
207 char Buffer[1024];
208
209 int CurLine = 0;
210 while (F.eof() == false)
211 {
212 F.getline(Buffer,sizeof(Buffer));
213 CurLine++;
214 _strtabexpand(Buffer,sizeof(Buffer));
215 if (F.fail() && !F.eof())
216 return _error->Error(_("Line %u too long in source list %s."),
217 CurLine,File.c_str());
218
219
220 char *I;
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++);
225 *I = 0;
226
227 const char *C = _strstrip(Buffer);
228
229 // Comment or blank
230 if (C[0] == '#' || C[0] == 0)
231 continue;
232
233 // Grok it
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());
237
238 Type *Parse = Type::GetType(LineType.c_str());
239 if (Parse == 0)
240 return _error->Error(_("Type '%s' is not known on line %u in source list %s"),LineType.c_str(),CurLine,File.c_str());
241
242 // Vendor name specified
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
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 // }
265
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());
269 }
270
271 if (Parse->ParseLine(SrcList,C,CurLine,File) == false)
272 return false;
273 }
274 return true;
275 }
276 /*}}}*/
277 // SourceList::FindIndex - Get the index associated with a file /*{{{*/
278 // ---------------------------------------------------------------------
279 /* */
280 bool pkgSourceList::FindIndex(pkgCache::PkgFileIterator File,
281 pkgIndexFile *&Found) const
282 {
283 for (const_iterator I = SrcList.begin(); I != SrcList.end(); I++)
284 {
285 vector<pkgIndexFile *> *Indexes = (*I)->GetIndexFiles();
286 for (vector<pkgIndexFile *>::const_iterator J = Indexes->begin();
287 J != Indexes->end(); J++)
288 {
289 if ((*J)->FindInCache(*File.Cache()) == File)
290 {
291 Found = (*J);
292 return true;
293 }
294 }
295 }
296
297 return false;
298 }
299 /*}}}*/
300 // SourceList::GetIndexes - Load the index files into the downloader /*{{{*/
301 // ---------------------------------------------------------------------
302 /* */
303 bool pkgSourceList::GetIndexes(pkgAcquire *Owner, bool GetAll) const
304 {
305 for (const_iterator I = SrcList.begin(); I != SrcList.end(); I++)
306 if ((*I)->GetIndexes(Owner,GetAll) == false)
307 return false;
308 return true;
309 }
310 /*}}}*/
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 /* */
316 bool pkgSourceList::ReadSourceDir(string Dir)
317 {
318 DIR *D = opendir(Dir.c_str());
319 if (D == 0)
320 return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
321
322 vector<string> List;
323
324 for (struct dirent *Ent = readdir(D); Ent != 0; Ent = readdir(D))
325 {
326 if (Ent->d_name[0] == '.')
327 continue;
328
329 // CNC:2003-12-02 Only accept .list files as valid sourceparts
330 if (flExtension(Ent->d_name) != "list")
331 continue;
332
333 // Skip bad file names ala run-parts
334 const char *C = Ent->d_name;
335 for (; *C != 0; C++)
336 if (isalpha(*C) == 0 && isdigit(*C) == 0
337 && *C != '_' && *C != '-' && *C != '.')
338 break;
339 if (*C != 0)
340 continue;
341
342 // Make sure it is a file and not something else
343 string File = flCombine(Dir,Ent->d_name);
344 struct stat St;
345 if (stat(File.c_str(),&St) != 0 || S_ISREG(St.st_mode) == 0)
346 continue;
347
348 List.push_back(File);
349 }
350 closedir(D);
351
352 sort(List.begin(),List.end());
353
354 // Read the files
355 for (vector<string>::const_iterator I = List.begin(); I != List.end(); I++)
356 if (ReadAppend(*I) == false)
357 return false;
358 return true;
359
360 }
361 /*}}}*/
362