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