]> git.saurik.com Git - apt.git/blob - apt-pkg/sourcelist.cc
Support for memory-only caching
[apt.git] / apt-pkg / sourcelist.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: sourcelist.cc,v 1.13 1999/04/07 05:30:17 jgg 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/configuration.h>
19 #include <apt-pkg/strutl.h>
20
21 #include <fstream.h>
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <sys/stat.h>
25 /*}}}*/
26
27 // SourceList::pkgSourceList - Constructors /*{{{*/
28 // ---------------------------------------------------------------------
29 /* */
30 pkgSourceList::pkgSourceList()
31 {
32 }
33
34 pkgSourceList::pkgSourceList(string File)
35 {
36 Read(File);
37 }
38 /*}}}*/
39 // SourceList::ReadMainList - Read the main source list from etc /*{{{*/
40 // ---------------------------------------------------------------------
41 /* */
42 bool pkgSourceList::ReadMainList()
43 {
44 return Read(_config->FindFile("Dir::Etc::sourcelist"));
45 }
46 /*}}}*/
47 // SourceList::Read - Parse the sourcelist file /*{{{*/
48 // ---------------------------------------------------------------------
49 /* */
50 bool pkgSourceList::Read(string File)
51 {
52 // Open the stream for reading
53 ifstream F(File.c_str(),ios::in | ios::nocreate);
54 if (!F != 0)
55 return _error->Errno("ifstream::ifstream","Opening %s",File.c_str());
56
57 List.erase(List.begin(),List.end());
58 char Buffer[300];
59
60 int CurLine = 0;
61 while (F.eof() == false)
62 {
63 F.getline(Buffer,sizeof(Buffer));
64 CurLine++;
65 _strtabexpand(Buffer,sizeof(Buffer));
66 _strstrip(Buffer);
67
68 // Comment or blank
69 if (Buffer[0] == '#' || Buffer[0] == 0)
70 continue;
71
72 // Grok it
73 string Type;
74 string URI;
75 Item Itm;
76 char *C = Buffer;
77 if (ParseQuoteWord(C,Type) == false)
78 return _error->Error("Malformed line %u in source list %s (type)",CurLine,File.c_str());
79 if (ParseQuoteWord(C,URI) == false)
80 return _error->Error("Malformed line %u in source list %s (URI)",CurLine,File.c_str());
81 if (ParseQuoteWord(C,Itm.Dist) == false)
82 return _error->Error("Malformed line %u in source list %s (dist)",CurLine,File.c_str());
83 if (Itm.SetType(Type) == false)
84 return _error->Error("Malformed line %u in source list %s (type parse)",CurLine,File.c_str());
85 if (Itm.SetURI(URI) == false)
86 return _error->Error("Malformed line %u in source list %s (URI parse)",CurLine,File.c_str());
87
88 // Check for an absolute dists specification.
89 if (Itm.Dist.empty() == false && Itm.Dist[Itm.Dist.size() - 1] == '/')
90 {
91 if (ParseQuoteWord(C,Itm.Section) == true)
92 return _error->Error("Malformed line %u in source list %s (Absolute dist)",CurLine,File.c_str());
93 Itm.Dist = SubstVar(Itm.Dist,"$(ARCH)",_config->Find("APT::Architecture"));
94 List.push_back(Itm);
95 continue;
96 }
97
98 // Grab the rest of the dists
99 if (ParseQuoteWord(C,Itm.Section) == false)
100 return _error->Error("Malformed line %u in source list %s (dist parse)",CurLine,File.c_str());
101
102 do
103 {
104 List.push_back(Itm);
105 }
106 while (ParseQuoteWord(C,Itm.Section) == true);
107 }
108 return true;
109 }
110 /*}}}*/
111 // SourceList::Item << - Writes the item to a stream /*{{{*/
112 // ---------------------------------------------------------------------
113 /* This is not suitable for rebuilding the sourcelist file but it good for
114 debugging. */
115 ostream &operator <<(ostream &O,pkgSourceList::Item &Itm)
116 {
117 O << (int)Itm.Type << ' ' << Itm.URI << ' ' << Itm.Dist << ' ' << Itm.Section;
118 return O;
119 }
120 /*}}}*/
121 // SourceList::Item::SetType - Sets the distribution type /*{{{*/
122 // ---------------------------------------------------------------------
123 /* */
124 bool pkgSourceList::Item::SetType(string S)
125 {
126 if (S == "deb")
127 {
128 Type = Deb;
129 return true;
130 }
131
132 if (S == "deb-src")
133 {
134 Type = DebSrc;
135 return true;
136 }
137
138 return false;
139 }
140 /*}}}*/
141 // SourceList::Item::SetURI - Set the URI /*{{{*/
142 // ---------------------------------------------------------------------
143 /* For simplicity we strip the scheme off the uri */
144 bool pkgSourceList::Item::SetURI(string S)
145 {
146 if (S.empty() == true)
147 return false;
148
149 if (S.find(':') == string::npos)
150 return false;
151
152 S = SubstVar(S,"$(ARCH)",_config->Find("APT::Architecture"));
153
154 // Make sure that the URN is / postfixed
155 URI = S;
156 if (URI[URI.size() - 1] != '/')
157 URI += '/';
158
159 return true;
160 }
161 /*}}}*/
162 // SourceList::Item::PackagesURI - Returns a URI to the packages file /*{{{*/
163 // ---------------------------------------------------------------------
164 /* */
165 string pkgSourceList::Item::PackagesURI() const
166 {
167 string Res;
168 switch (Type)
169 {
170 case Deb:
171 if (Dist[Dist.size() - 1] == '/')
172 Res = URI + Dist;
173 else
174 Res = URI + "dists/" + Dist + '/' + Section +
175 "/binary-" + _config->Find("APT::Architecture") + '/';
176
177 Res += "Packages";
178 break;
179
180 case DebSrc:
181 if (Dist[Dist.size() - 1] == '/')
182 Res = URI + Dist;
183 else
184 Res = URI + "dists/" + Dist + '/' + Section +
185 "/source/";
186
187 Res += "Sources";
188 break;
189 };
190 return Res;
191 }
192 /*}}}*/
193 // SourceList::Item::PackagesInfo - Shorter version of the URI /*{{{*/
194 // ---------------------------------------------------------------------
195 /* This is a shorter version that is designed to be < 60 chars or so */
196 string pkgSourceList::Item::PackagesInfo() const
197 {
198 string Res;
199 switch (Type)
200 {
201 case Deb:
202 Res += SiteOnly(URI) + ' ';
203 if (Dist[Dist.size() - 1] == '/')
204 Res += Dist;
205 else
206 Res += Dist + '/' + Section;
207
208 Res += " Packages";
209 break;
210
211 case DebSrc:
212 Res += SiteOnly(URI) + ' ';
213 if (Dist[Dist.size() - 1] == '/')
214 Res += Dist;
215 else
216 Res += Dist + '/' + Section;
217
218 Res += " Sources";
219 break;
220 };
221 return Res;
222 }
223 /*}}}*/
224 // SourceList::Item::ReleaseURI - Returns a URI to the release file /*{{{*/
225 // ---------------------------------------------------------------------
226 /* */
227 string pkgSourceList::Item::ReleaseURI() const
228 {
229 string Res;
230 switch (Type)
231 {
232 case Deb:
233 if (Dist[Dist.size() - 1] == '/')
234 Res = URI + Dist;
235 else
236 Res = URI + "dists/" + Dist + '/' + Section +
237 "/binary-" + _config->Find("APT::Architecture") + '/';
238
239 Res += "Release";
240 break;
241
242 case DebSrc:
243 if (Dist[Dist.size() - 1] == '/')
244 Res = URI + Dist;
245 else
246 Res = URI + "dists/" + Dist + '/' + Section +
247 "/source/";
248
249 Res += "Release";
250 break;
251 };
252 return Res;
253 }
254 /*}}}*/
255 // SourceList::Item::ReleaseInfo - Shorter version of the URI /*{{{*/
256 // ---------------------------------------------------------------------
257 /* This is a shorter version that is designed to be < 60 chars or so */
258 string pkgSourceList::Item::ReleaseInfo() const
259 {
260 string Res;
261 switch (Type)
262 {
263 case Deb:
264 case DebSrc:
265 Res += SiteOnly(URI) + ' ';
266 if (Dist[Dist.size() - 1] == '/')
267 Res += Dist;
268 else
269 Res += Dist + '/' + Section;
270
271 Res += " Release";
272 break;
273 };
274 return Res;
275 }
276 /*}}}*/
277 // SourceList::Item::ArchiveInfo - Shorter version of the archive spec /*{{{*/
278 // ---------------------------------------------------------------------
279 /* This is a shorter version that is designed to be < 60 chars or so */
280 string pkgSourceList::Item::ArchiveInfo(pkgCache::VerIterator Ver) const
281 {
282 string Res;
283 switch (Type)
284 {
285 case DebSrc:
286 case Deb:
287 Res += SiteOnly(URI) + ' ';
288 if (Dist[Dist.size() - 1] == '/')
289 Res += Dist;
290 else
291 Res += Dist + '/' + Section;
292
293 Res += " ";
294 Res += Ver.ParentPkg().Name();
295 Res += " ";
296 Res += Ver.VerStr();
297
298 break;
299 };
300 return Res;
301 }
302 /*}}}*/
303 // SourceList::Item::ArchiveURI - Returns a URI to the given archive /*{{{*/
304 // ---------------------------------------------------------------------
305 /* */
306 string pkgSourceList::Item::ArchiveURI(string File) const
307 {
308 string Res;
309 switch (Type)
310 {
311 case Deb:
312 case DebSrc:
313 Res = URI + File;
314 break;
315 };
316 return Res;
317 }
318 /*}}}*/
319 // SourceList::Item::SourceInfo - Returns an info line for a source /*{{{*/
320 // ---------------------------------------------------------------------
321 /* */
322 string pkgSourceList::Item::SourceInfo(string Pkg,string Ver,string Comp) const
323 {
324 string Res;
325 switch (Type)
326 {
327 case DebSrc:
328 case Deb:
329 Res += SiteOnly(URI) + ' ';
330 if (Dist[Dist.size() - 1] == '/')
331 Res += Dist;
332 else
333 Res += Dist + '/' + Section;
334
335 Res += " ";
336 Res += Pkg;
337 Res += " ";
338 Res += Ver;
339 if (Comp.empty() == false)
340 Res += " (" + Comp + ")";
341 break;
342 };
343 return Res;
344 }
345 /*}}}*/
346 // SourceList::Item::SiteOnly - Strip off the path part of a URI /*{{{*/
347 // ---------------------------------------------------------------------
348 /* */
349 string pkgSourceList::Item::SiteOnly(string URI) const
350 {
351 unsigned int Pos = URI.find(':');
352 if (Pos == string::npos || Pos + 3 > URI.length())
353 return URI;
354 if (URI[Pos + 1] != '/' || URI[Pos + 2] != '/')
355 return URI;
356
357 Pos = URI.find('/',Pos + 3);
358 if (Pos == string::npos)
359 return URI;
360 return string(URI,0,Pos);
361 }
362 /*}}}*/