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