Upgraded to eg++ 1.1 and libstdc++2.9
[ntk/apt.git] / apt-pkg / sourcelist.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: sourcelist.cc,v 1.8 1998/10/20 04:33:15 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 <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 return true;
133 }
134 /*}}}*/
135 // SourceList::Item::SetURI - Set the URI /*{{{*/
136 // ---------------------------------------------------------------------
137 /* For simplicity we strip the scheme off the uri */
138 bool pkgSourceList::Item::SetURI(string S)
139 {
140 if (S.empty() == true)
141 return false;
142
143 if (S.find(':') == string::npos)
144 return false;
145
146 S = SubstVar(S,"$(ARCH)",_config->Find("APT::Architecture"));
147
148 // Make sure that the URN is / postfixed
149 URI = S;
150 if (URI[URI.size() - 1] != '/')
151 URI += '/';
152
153 return true;
154 }
155 /*}}}*/
156 // SourceList::Item::PackagesURI - Returns a URI to the packages file /*{{{*/
157 // ---------------------------------------------------------------------
158 /* */
159 string pkgSourceList::Item::PackagesURI() const
160 {
161 string Res;
162 switch (Type)
163 {
164 case Deb:
165 if (Dist[Dist.size() - 1] == '/')
166 Res = URI + Dist;
167 else
168 Res = URI + "dists/" + Dist + '/' + Section +
169 "/binary-" + _config->Find("APT::Architecture") + '/';
170
171 Res += "Packages";
172 break;
173 };
174 return Res;
175 }
176 /*}}}*/
177 // SourceList::Item::PackagesInfo - Shorter version of the URI /*{{{*/
178 // ---------------------------------------------------------------------
179 /* This is a shorter version that is designed to be < 60 chars or so */
180 string pkgSourceList::Item::PackagesInfo() const
181 {
182 string Res;
183 switch (Type)
184 {
185 case Deb:
186 Res += SiteOnly(URI) + ' ';
187 if (Dist[Dist.size() - 1] == '/')
188 Res += Dist;
189 else
190 Res += Dist + '/' + Section;
191
192 Res += " Packages";
193 break;
194 };
195 return Res;
196 }
197 /*}}}*/
198 // SourceList::Item::ReleaseURI - Returns a URI to the release file /*{{{*/
199 // ---------------------------------------------------------------------
200 /* */
201 string pkgSourceList::Item::ReleaseURI() const
202 {
203 string Res;
204 switch (Type)
205 {
206 case Deb:
207 if (Dist[Dist.size() - 1] == '/')
208 Res = URI + Dist;
209 else
210 Res = URI + "dists/" + Dist + '/' + Section +
211 "/binary-" + _config->Find("APT::Architecture") + '/';
212
213 Res += "Release";
214 break;
215 };
216 return Res;
217 }
218 /*}}}*/
219 // SourceList::Item::ReleaseInfo - Shorter version of the URI /*{{{*/
220 // ---------------------------------------------------------------------
221 /* This is a shorter version that is designed to be < 60 chars or so */
222 string pkgSourceList::Item::ReleaseInfo() const
223 {
224 string Res;
225 switch (Type)
226 {
227 case Deb:
228 Res += SiteOnly(URI) + ' ';
229 if (Dist[Dist.size() - 1] == '/')
230 Res += Dist;
231 else
232 Res += Dist + '/' + Section;
233
234 Res += " Release";
235 break;
236 };
237 return Res;
238 }
239 /*}}}*/
240 // SourceList::Item::ArchiveInfo - Shorter version of the archive spec /*{{{*/
241 // ---------------------------------------------------------------------
242 /* This is a shorter version that is designed to be < 60 chars or so */
243 string pkgSourceList::Item::ArchiveInfo(pkgCache::VerIterator Ver) const
244 {
245 string Res;
246 switch (Type)
247 {
248 case Deb:
249 Res += SiteOnly(URI) + ' ';
250 if (Dist[Dist.size() - 1] == '/')
251 Res += Dist;
252 else
253 Res += Dist + '/' + Section;
254
255 Res += " ";
256 Res += Ver.ParentPkg().Name();
257 break;
258 };
259 return Res;
260 }
261 /*}}}*/
262 // SourceList::Item::ArchiveURI - Returns a URI to the given archive /*{{{*/
263 // ---------------------------------------------------------------------
264 /* */
265 string pkgSourceList::Item::ArchiveURI(string File) const
266 {
267 string Res;
268 switch (Type)
269 {
270 case Deb:
271 Res = URI + File;
272 break;
273 };
274 return Res;
275 }
276 /*}}}*/
277 // SourceList::Item::SiteOnly - Strip off the path part of a URI /*{{{*/
278 // ---------------------------------------------------------------------
279 /* */
280 string pkgSourceList::Item::SiteOnly(string URI) const
281 {
282 unsigned int Pos = URI.find(':');
283 if (Pos == string::npos || Pos + 3 > URI.length())
284 return URI;
285 if (URI[Pos + 1] != '/' || URI[Pos + 2] != '/')
286 return URI;
287
288 Pos = URI.find('/',Pos + 3);
289 if (Pos == string::npos)
290 return URI;
291 return string(URI,0,Pos);
292 }
293 /*}}}*/