]>
Commit | Line | Data |
---|---|---|
6c139d6e AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
b35d2f5f | 3 | // $Id: sourcelist.cc,v 1.5 1998/07/26 04:49:33 jgg Exp $ |
6c139d6e AL |
4 | /* ###################################################################### |
5 | ||
6 | List of Sources | |
7 | ||
8 | ##################################################################### */ | |
9 | /*}}}*/ | |
10 | // Include Files /*{{{*/ | |
11 | #ifdef __GNUG__ | |
094a497d | 12 | #pragma implementation "apt-pkg/sourcelist.h" |
6c139d6e AL |
13 | #endif |
14 | ||
094a497d AL |
15 | #include <apt-pkg/sourcelist.h> |
16 | #include <apt-pkg/error.h> | |
17 | #include <apt-pkg/fileutl.h> | |
18 | #include <apt-pkg/configuration.h> | |
6c139d6e | 19 | #include <strutl.h> |
6c139d6e AL |
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 | { | |
b35d2f5f | 44 | return Read(_config->FindDir("Dir::Etc::sourcelist")); |
6c139d6e AL |
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()); | |
9c14e3d6 | 93 | Itm.Dist = SubstVar(Itm.Dist,"$(ARCH)",_config->Find("APT::Architecture")); |
6c139d6e AL |
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 | /*}}}*/ | |
6c139d6e AL |
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 << 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 | ||
9c14e3d6 | 146 | S = SubstVar(S,"$(ARCH)",_config->Find("APT::Architecture")); |
6c139d6e AL |
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 | |
9c14e3d6 AL |
168 | Res = URI + "dists/" + Dist + '/' + Section + |
169 | "/binary-" + _config->Find("APT::Architecture") + '/'; | |
6c139d6e AL |
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::ArchiveInfo - Shorter version of the archive spec /*{{{*/ | |
199 | // --------------------------------------------------------------------- | |
200 | /* This is a shorter version that is designed to be < 60 chars or so */ | |
201 | string pkgSourceList::Item::ArchiveInfo(pkgCache::VerIterator Ver) const | |
202 | { | |
203 | string Res; | |
204 | switch (Type) | |
205 | { | |
206 | case Deb: | |
207 | Res += SiteOnly(URI) + ' '; | |
208 | if (Dist[Dist.size() - 1] == '/') | |
209 | Res += Dist; | |
210 | else | |
211 | Res += Dist + '/' + Section; | |
212 | ||
213 | Res += " "; | |
214 | Res += Ver.ParentPkg().Name(); | |
215 | break; | |
216 | }; | |
217 | return Res; | |
218 | } | |
219 | /*}}}*/ | |
220 | // SourceList::Item::ArchiveURI - Returns a URI to the given archive /*{{{*/ | |
221 | // --------------------------------------------------------------------- | |
222 | /* */ | |
223 | string pkgSourceList::Item::ArchiveURI(string File) const | |
224 | { | |
225 | string Res; | |
226 | switch (Type) | |
227 | { | |
228 | case Deb: | |
229 | Res = URI + File; | |
230 | break; | |
231 | }; | |
232 | return Res; | |
233 | } | |
234 | /*}}}*/ | |
235 | // SourceList::Item::SiteOnly - Strip off the path part of a URI /*{{{*/ | |
236 | // --------------------------------------------------------------------- | |
237 | /* */ | |
238 | string pkgSourceList::Item::SiteOnly(string URI) const | |
239 | { | |
240 | unsigned int Pos = URI.find(':'); | |
241 | if (Pos == string::npos || Pos + 3 > URI.length()) | |
242 | return URI; | |
243 | if (URI[Pos + 1] != '/' || URI[Pos + 2] != '/') | |
244 | return URI; | |
245 | ||
246 | Pos = URI.find('/',Pos + 3); | |
247 | if (Pos == string::npos) | |
248 | return URI; | |
249 | return string(URI,0,Pos); | |
250 | } | |
251 | /*}}}*/ |