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