]>
Commit | Line | Data |
---|---|---|
b2e465d6 AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
7db98ffc | 3 | // $Id: debindexfile.cc,v 1.5.2.3 2004/01/04 19:11:00 mdz Exp $ |
b2e465d6 AL |
4 | /* ###################################################################### |
5 | ||
6 | Debian Specific sources.list types and the three sorts of Debian | |
7 | index files. | |
8 | ||
9 | ##################################################################### */ | |
10 | /*}}}*/ | |
11 | // Include Files /*{{{*/ | |
ea542140 DK |
12 | #include <config.h> |
13 | ||
b2e465d6 AL |
14 | #include <apt-pkg/debindexfile.h> |
15 | #include <apt-pkg/debsrcrecords.h> | |
16 | #include <apt-pkg/deblistparser.h> | |
17 | #include <apt-pkg/debrecords.h> | |
b2e465d6 | 18 | #include <apt-pkg/configuration.h> |
b2e465d6 | 19 | #include <apt-pkg/error.h> |
453b82a3 DK |
20 | #include <apt-pkg/fileutl.h> |
21 | #include <apt-pkg/indexfile.h> | |
453b82a3 DK |
22 | #include <apt-pkg/pkgcache.h> |
23 | #include <apt-pkg/cacheiterators.h> | |
453b82a3 DK |
24 | #include <apt-pkg/pkgrecords.h> |
25 | #include <apt-pkg/srcrecords.h> | |
e011829d | 26 | |
453b82a3 DK |
27 | #include <stdio.h> |
28 | #include <iostream> | |
29 | #include <string> | |
ac7f8f79 | 30 | #include <sstream> |
88a8975f | 31 | |
b2e465d6 AL |
32 | #include <sys/stat.h> |
33 | /*}}}*/ | |
34 | ||
c9443c01 | 35 | // Sources Index /*{{{*/ |
e3c1cfc7 | 36 | debSourcesIndex::debSourcesIndex(IndexTarget const &Target,bool const Trusted) : |
c9443c01 | 37 | pkgDebianIndexTargetFile(Target, Trusted), d(NULL) |
b2e465d6 AL |
38 | { |
39 | } | |
c9443c01 | 40 | std::string debSourcesIndex::SourceInfo(pkgSrcRecords::Parser const &Record, |
b2e465d6 AL |
41 | pkgSrcRecords::File const &File) const |
42 | { | |
c9443c01 DK |
43 | // The result looks like: http://foo/debian/ stable/main src 1.1.1 (dsc) |
44 | std::string Res = Target.Description; | |
e3c1cfc7 DK |
45 | Res.erase(Target.Description.rfind(' ')); |
46 | ||
b2e465d6 AL |
47 | Res += " "; |
48 | Res += Record.Package(); | |
49 | Res += " "; | |
50 | Res += Record.Version(); | |
51 | if (File.Type.empty() == false) | |
52 | Res += " (" + File.Type + ")"; | |
53 | return Res; | |
54 | } | |
b2e465d6 AL |
55 | pkgSrcRecords::Parser *debSourcesIndex::CreateSrcParser() const |
56 | { | |
c9443c01 | 57 | std::string const SourcesURI = IndexFileName(); |
b0f4b486 MV |
58 | if (FileExists(SourcesURI)) |
59 | return new debSrcRecordParser(SourcesURI, this); | |
60 | return NULL; | |
c9443c01 DK |
61 | } |
62 | bool debSourcesIndex::OpenListFile(FileFd &, std::string const &) | |
63 | { | |
64 | return true; | |
65 | } | |
66 | pkgCacheListParser * debSourcesIndex::CreateListParser(FileFd &) | |
67 | { | |
68 | return NULL; | |
69 | } | |
70 | uint8_t debSourcesIndex::GetIndexFlags() const | |
71 | { | |
72 | return 0; | |
b2e465d6 AL |
73 | } |
74 | /*}}}*/ | |
c9443c01 | 75 | // Packages Index /*{{{*/ |
e3c1cfc7 | 76 | debPackagesIndex::debPackagesIndex(IndexTarget const &Target, bool const Trusted) : |
c9443c01 | 77 | pkgDebianIndexTargetFile(Target, Trusted), d(NULL) |
b2e465d6 AL |
78 | { |
79 | } | |
c9443c01 | 80 | std::string debPackagesIndex::ArchiveInfo(pkgCache::VerIterator const &Ver) const |
b2e465d6 | 81 | { |
c9443c01 | 82 | std::string Res = Target.Description; |
8bd823d0 DK |
83 | { |
84 | auto const space = Target.Description.rfind(' '); | |
85 | if (space != std::string::npos) | |
86 | Res.erase(space); | |
87 | } | |
e3c1cfc7 | 88 | |
b2e465d6 AL |
89 | Res += " "; |
90 | Res += Ver.ParentPkg().Name(); | |
91 | Res += " "; | |
c9443c01 | 92 | std::string const Dist = Target.Option(IndexTarget::RELEASE); |
e3c1cfc7 | 93 | if (Dist.empty() == false && Dist[Dist.size() - 1] != '/') |
dd13742e | 94 | Res.append(Ver.Arch()).append(" "); |
b2e465d6 AL |
95 | Res += Ver.VerStr(); |
96 | return Res; | |
97 | } | |
c9443c01 | 98 | uint8_t debPackagesIndex::GetIndexFlags() const |
b2e465d6 | 99 | { |
c9443c01 | 100 | return 0; |
b2e465d6 AL |
101 | } |
102 | /*}}}*/ | |
c9443c01 | 103 | // Translation-* Index /*{{{*/ |
e3c1cfc7 | 104 | debTranslationsIndex::debTranslationsIndex(IndexTarget const &Target) : |
c9443c01 | 105 | pkgDebianIndexTargetFile(Target, true), d(NULL) |
45df0ad2 | 106 | {} |
c9443c01 | 107 | bool debTranslationsIndex::HasPackages() const |
11680bfd | 108 | { |
e3c1cfc7 | 109 | return Exists(); |
a52f938b | 110 | } |
c9443c01 DK |
111 | bool debTranslationsIndex::OpenListFile(FileFd &Pkg, std::string const &FileName) |
112 | { | |
113 | if (FileExists(FileName)) | |
114 | return pkgDebianIndexTargetFile::OpenListFile(Pkg, FileName); | |
a52f938b OS |
115 | return true; |
116 | } | |
c9443c01 | 117 | uint8_t debTranslationsIndex::GetIndexFlags() const |
c51c6f08 | 118 | { |
c9443c01 | 119 | return pkgCache::Flag::NotSource | pkgCache::Flag::NoPackages; |
c51c6f08 | 120 | } |
c9443c01 | 121 | std::string debTranslationsIndex::GetArchitecture() const |
b2e465d6 | 122 | { |
c9443c01 | 123 | return std::string(); |
b2e465d6 | 124 | } |
c9443c01 | 125 | pkgCacheListParser * debTranslationsIndex::CreateListParser(FileFd &Pkg) |
b2e465d6 | 126 | { |
c9443c01 | 127 | if (Pkg.IsOpen() == false) |
c48ef894 | 128 | return nullptr; |
c9443c01 DK |
129 | _error->PushToStack(); |
130 | pkgCacheListParser * const Parser = new debTranslationsParser(&Pkg); | |
131 | bool const newError = _error->PendingError(); | |
132 | _error->MergeWithStack(); | |
c48ef894 DK |
133 | if (newError) |
134 | { | |
135 | delete Parser; | |
136 | return nullptr; | |
137 | } | |
138 | else | |
139 | return Parser; | |
b2e465d6 AL |
140 | } |
141 | /*}}}*/ | |
c9443c01 DK |
142 | // dpkg/status Index /*{{{*/ |
143 | debStatusIndex::debStatusIndex(std::string const &File) : pkgDebianIndexRealFile(File, true), d(NULL) | |
b2e465d6 | 144 | { |
b2e465d6 | 145 | } |
c9443c01 | 146 | std::string debStatusIndex::GetArchitecture() const |
b2e465d6 | 147 | { |
c9443c01 | 148 | return std::string(); |
b2e465d6 | 149 | } |
c9443c01 | 150 | std::string debStatusIndex::GetComponent() const |
0d29b9d4 | 151 | { |
c9443c01 | 152 | return "now"; |
0d29b9d4 | 153 | } |
c9443c01 | 154 | uint8_t debStatusIndex::GetIndexFlags() const |
0d29b9d4 | 155 | { |
c9443c01 | 156 | return pkgCache::Flag::NotSource; |
0d29b9d4 | 157 | } |
1c73b0fc JAK |
158 | |
159 | pkgCacheListParser * debStatusIndex::CreateListParser(FileFd &Pkg) | |
160 | { | |
161 | if (Pkg.IsOpen() == false) | |
c48ef894 | 162 | return nullptr; |
1c73b0fc JAK |
163 | _error->PushToStack(); |
164 | pkgCacheListParser * const Parser = new debStatusListParser(&Pkg); | |
165 | bool const newError = _error->PendingError(); | |
166 | _error->MergeWithStack(); | |
c48ef894 DK |
167 | if (newError) |
168 | { | |
169 | delete Parser; | |
170 | return nullptr; | |
171 | } | |
172 | else | |
173 | return Parser; | |
1c73b0fc | 174 | } |
c9443c01 DK |
175 | /*}}}*/ |
176 | // DebPkgFile Index - a single .deb file as an index /*{{{*/ | |
177 | debDebPkgFileIndex::debDebPkgFileIndex(std::string const &DebFile) | |
178 | : pkgDebianIndexRealFile(DebFile, true), d(NULL), DebFile(DebFile) | |
0d29b9d4 | 179 | { |
0d29b9d4 | 180 | } |
2f6a2fbb | 181 | bool debDebPkgFileIndex::GetContent(std::ostream &content, std::string const &debfile) |
0d29b9d4 | 182 | { |
c9443c01 DK |
183 | struct stat Buf; |
184 | if (stat(debfile.c_str(), &Buf) != 0) | |
185 | return false; | |
186 | ||
2f6a2fbb DK |
187 | // get the control data out of the deb file via dpkg-deb -I |
188 | std::string dpkg = _config->Find("Dir::Bin::dpkg","dpkg-deb"); | |
fdff5b03 MV |
189 | std::vector<const char *> Args; |
190 | Args.push_back(dpkg.c_str()); | |
fdff5b03 | 191 | Args.push_back("-I"); |
2f6a2fbb | 192 | Args.push_back(debfile.c_str()); |
fdff5b03 MV |
193 | Args.push_back("control"); |
194 | Args.push_back(NULL); | |
a9d990a2 MV |
195 | FileFd PipeFd; |
196 | pid_t Child; | |
fdff5b03 | 197 | if(Popen((const char**)&Args[0], PipeFd, Child, FileFd::ReadOnly) == false) |
a9d990a2 | 198 | return _error->Error("Popen failed"); |
2f6a2fbb | 199 | |
2f91076d | 200 | content << "Filename: " << debfile << "\n"; |
b58e2c7c | 201 | content << "Size: " << std::to_string(Buf.st_size) << "\n"; |
2f91076d | 202 | bool first_line_seen = false; |
2f6a2fbb DK |
203 | char buffer[1024]; |
204 | do { | |
205 | unsigned long long actual = 0; | |
206 | if (PipeFd.Read(buffer, sizeof(buffer)-1, &actual) == false) | |
207 | return _error->Errno("read", "Failed to read dpkg pipe"); | |
208 | if (actual == 0) | |
209 | break; | |
210 | buffer[actual] = '\0'; | |
2f91076d DK |
211 | char const * b = buffer; |
212 | if (first_line_seen == false) | |
213 | { | |
214 | for (; *b != '\0' && (*b == '\n' || *b == '\r'); ++b) | |
215 | /* skip over leading newlines */; | |
216 | if (*b == '\0') | |
217 | continue; | |
218 | first_line_seen = true; | |
219 | } | |
220 | content << b; | |
2f6a2fbb | 221 | } while(true); |
a9d990a2 | 222 | ExecWait(Child, "Popen"); |
0d29b9d4 | 223 | |
2f6a2fbb DK |
224 | return true; |
225 | } | |
c9443c01 | 226 | bool debDebPkgFileIndex::OpenListFile(FileFd &Pkg, std::string const &FileName) |
2f6a2fbb | 227 | { |
2f6a2fbb | 228 | // write the control data to a tempfile |
c9443c01 | 229 | if (GetTempFile("deb-file-" + flNotDir(FileName), true, &Pkg) == NULL) |
0d29b9d4 | 230 | return false; |
2f6a2fbb | 231 | std::ostringstream content; |
c9443c01 | 232 | if (GetContent(content, FileName) == false) |
2f6a2fbb DK |
233 | return false; |
234 | std::string const contentstr = content.str(); | |
5465192b DK |
235 | if (contentstr.empty()) |
236 | return true; | |
c9443c01 DK |
237 | if (Pkg.Write(contentstr.c_str(), contentstr.length()) == false || Pkg.Seek(0) == false) |
238 | return false; | |
0d29b9d4 MV |
239 | return true; |
240 | } | |
c9443c01 DK |
241 | pkgCacheListParser * debDebPkgFileIndex::CreateListParser(FileFd &Pkg) |
242 | { | |
243 | if (Pkg.IsOpen() == false) | |
c48ef894 | 244 | return nullptr; |
c9443c01 DK |
245 | _error->PushToStack(); |
246 | pkgCacheListParser * const Parser = new debDebFileParser(&Pkg, DebFile); | |
247 | bool const newError = _error->PendingError(); | |
248 | _error->MergeWithStack(); | |
c48ef894 DK |
249 | if (newError) |
250 | { | |
251 | delete Parser; | |
252 | return nullptr; | |
253 | } | |
254 | else | |
255 | return Parser; | |
c9443c01 DK |
256 | } |
257 | uint8_t debDebPkgFileIndex::GetIndexFlags() const | |
258 | { | |
259 | return pkgCache::Flag::LocalSource; | |
260 | } | |
261 | std::string debDebPkgFileIndex::GetArchitecture() const | |
262 | { | |
263 | return std::string(); | |
264 | } | |
265 | std::string debDebPkgFileIndex::GetComponent() const | |
266 | { | |
267 | return "local-deb"; | |
268 | } | |
0d29b9d4 MV |
269 | pkgCache::PkgFileIterator debDebPkgFileIndex::FindInCache(pkgCache &Cache) const |
270 | { | |
c9443c01 | 271 | std::string const FileName = IndexFileName(); |
0d29b9d4 MV |
272 | pkgCache::PkgFileIterator File = Cache.FileBegin(); |
273 | for (; File.end() == false; ++File) | |
274 | { | |
c9443c01 | 275 | if (File.FileName() == NULL || FileName != File.FileName()) |
0d29b9d4 | 276 | continue; |
c9443c01 DK |
277 | // we can't do size checks here as file size != content size |
278 | return File; | |
0d29b9d4 | 279 | } |
5465192b | 280 | |
0d29b9d4 MV |
281 | return File; |
282 | } | |
64b66a46 DK |
283 | std::string debDebPkgFileIndex::ArchiveInfo_impl(pkgCache::VerIterator const &Ver) const |
284 | { | |
285 | std::string Res = IndexFileName() + " "; | |
286 | Res.append(Ver.ParentPkg().Name()).append(" "); | |
287 | Res.append(Ver.Arch()).append(" "); | |
288 | Res.append(Ver.VerStr()); | |
289 | return Res; | |
290 | } | |
c9443c01 DK |
291 | /*}}}*/ |
292 | // DscFile Index - a single .dsc file as an index /*{{{*/ | |
5465192b | 293 | debDscFileIndex::debDscFileIndex(std::string const &DscFile) |
c9443c01 | 294 | : pkgDebianIndexRealFile(DscFile, true), d(NULL) |
a49e7948 MV |
295 | { |
296 | } | |
a49e7948 MV |
297 | pkgSrcRecords::Parser *debDscFileIndex::CreateSrcParser() const |
298 | { | |
c9443c01 | 299 | if (Exists() == false) |
a49e7948 | 300 | return NULL; |
c9443c01 | 301 | return new debDscRecordParser(File, this); |
f359b7e8 DK |
302 | } |
303 | std::string debDscFileIndex::GetComponent() const | |
304 | { | |
305 | return "local-dsc"; | |
306 | } | |
307 | std::string debDscFileIndex::GetArchitecture() const | |
308 | { | |
309 | return "source"; | |
310 | } | |
311 | uint8_t debDscFileIndex::GetIndexFlags() const | |
312 | { | |
313 | return pkgCache::Flag::LocalSource; | |
314 | } | |
315 | /*}}}*/ | |
316 | // ControlFile Index - a directory with a debian/control file /*{{{*/ | |
317 | std::string debDebianSourceDirIndex::GetComponent() const | |
318 | { | |
319 | return "local-control"; | |
a49e7948 MV |
320 | } |
321 | /*}}}*/ | |
a249b3e6 DK |
322 | // String Package Index - a string of Packages file content /*{{{*/ |
323 | std::string debStringPackageIndex::GetArchitecture() const | |
324 | { | |
325 | return std::string(); | |
326 | } | |
327 | std::string debStringPackageIndex::GetComponent() const | |
328 | { | |
329 | return "apt-tmp-index"; | |
330 | } | |
331 | uint8_t debStringPackageIndex::GetIndexFlags() const | |
332 | { | |
333 | return pkgCache::Flag::NotSource; | |
334 | } | |
335 | const pkgIndexFile::Type *debStringPackageIndex::GetType() const | |
336 | { | |
337 | return pkgIndexFile::Type::GetType("Debian Package Index"); | |
338 | } | |
339 | debStringPackageIndex::debStringPackageIndex(std::string const &content) : | |
340 | pkgDebianIndexRealFile("", false), d(NULL) | |
341 | { | |
342 | char fn[1024]; | |
343 | std::string const tempdir = GetTempDir(); | |
344 | snprintf(fn, sizeof(fn), "%s/%s.XXXXXX", tempdir.c_str(), "apt-tmp-index"); | |
345 | int const fd = mkstemp(fn); | |
346 | File = fn; | |
347 | FileFd::Write(fd, content.data(), content.length()); | |
348 | close(fd); | |
349 | } | |
350 | debStringPackageIndex::~debStringPackageIndex() | |
351 | { | |
352 | RemoveFile("~debStringPackageIndex", File); | |
353 | } | |
354 | /*}}}*/ | |
5465192b | 355 | |
b2e465d6 | 356 | // Index File types for Debian /*{{{*/ |
dce45dbe | 357 | class APT_HIDDEN debIFTypeSrc : public pkgIndexFile::Type |
b2e465d6 AL |
358 | { |
359 | public: | |
b2e465d6 AL |
360 | debIFTypeSrc() {Label = "Debian Source Index";}; |
361 | }; | |
dce45dbe | 362 | class APT_HIDDEN debIFTypePkg : public pkgIndexFile::Type |
b2e465d6 AL |
363 | { |
364 | public: | |
c9443c01 | 365 | virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator const &File) const APT_OVERRIDE |
b2e465d6 AL |
366 | { |
367 | return new debRecordParser(File.FileName(),*File.Cache()); | |
368 | }; | |
369 | debIFTypePkg() {Label = "Debian Package Index";}; | |
370 | }; | |
dce45dbe | 371 | class APT_HIDDEN debIFTypeTrans : public debIFTypePkg |
97234432 MV |
372 | { |
373 | public: | |
374 | debIFTypeTrans() {Label = "Debian Translation Index";}; | |
375 | }; | |
dce45dbe | 376 | class APT_HIDDEN debIFTypeStatus : public pkgIndexFile::Type |
b2e465d6 AL |
377 | { |
378 | public: | |
c9443c01 | 379 | virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator const &File) const APT_OVERRIDE |
b2e465d6 AL |
380 | { |
381 | return new debRecordParser(File.FileName(),*File.Cache()); | |
382 | }; | |
383 | debIFTypeStatus() {Label = "Debian dpkg status file";}; | |
384 | }; | |
dce45dbe | 385 | class APT_HIDDEN debIFTypeDebPkgFile : public pkgIndexFile::Type |
0d29b9d4 MV |
386 | { |
387 | public: | |
c9443c01 | 388 | virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator const &File) const APT_OVERRIDE |
0d29b9d4 | 389 | { |
2f6a2fbb | 390 | return new debDebFileRecordParser(File.FileName()); |
0d29b9d4 | 391 | }; |
463c8d80 | 392 | debIFTypeDebPkgFile() {Label = "Debian deb file";}; |
0d29b9d4 | 393 | }; |
dce45dbe | 394 | class APT_HIDDEN debIFTypeDscFile : public pkgIndexFile::Type |
a49e7948 MV |
395 | { |
396 | public: | |
c9443c01 | 397 | virtual pkgSrcRecords::Parser *CreateSrcPkgParser(std::string const &DscFile) const APT_OVERRIDE |
a49e7948 MV |
398 | { |
399 | return new debDscRecordParser(DscFile, NULL); | |
400 | }; | |
463c8d80 | 401 | debIFTypeDscFile() {Label = "Debian dsc file";}; |
a49e7948 | 402 | }; |
dce45dbe | 403 | class APT_HIDDEN debIFTypeDebianSourceDir : public pkgIndexFile::Type |
77da39b9 MV |
404 | { |
405 | public: | |
c9443c01 | 406 | virtual pkgSrcRecords::Parser *CreateSrcPkgParser(std::string const &SourceDir) const APT_OVERRIDE |
77da39b9 | 407 | { |
c9443c01 | 408 | return new debDscRecordParser(SourceDir + std::string("/debian/control"), NULL); |
77da39b9 | 409 | }; |
463c8d80 | 410 | debIFTypeDebianSourceDir() {Label = "Debian control file";}; |
77da39b9 | 411 | }; |
a49e7948 | 412 | |
dce45dbe DK |
413 | APT_HIDDEN debIFTypeSrc _apt_Src; |
414 | APT_HIDDEN debIFTypePkg _apt_Pkg; | |
415 | APT_HIDDEN debIFTypeTrans _apt_Trans; | |
416 | APT_HIDDEN debIFTypeStatus _apt_Status; | |
417 | APT_HIDDEN debIFTypeDebPkgFile _apt_DebPkgFile; | |
a49e7948 | 418 | // file based pseudo indexes |
dce45dbe DK |
419 | APT_HIDDEN debIFTypeDscFile _apt_DscFile; |
420 | APT_HIDDEN debIFTypeDebianSourceDir _apt_DebianSourceDir; | |
b2e465d6 AL |
421 | |
422 | const pkgIndexFile::Type *debSourcesIndex::GetType() const | |
423 | { | |
424 | return &_apt_Src; | |
425 | } | |
426 | const pkgIndexFile::Type *debPackagesIndex::GetType() const | |
427 | { | |
428 | return &_apt_Pkg; | |
429 | } | |
a52f938b OS |
430 | const pkgIndexFile::Type *debTranslationsIndex::GetType() const |
431 | { | |
97234432 | 432 | return &_apt_Trans; |
a52f938b | 433 | } |
b2e465d6 AL |
434 | const pkgIndexFile::Type *debStatusIndex::GetType() const |
435 | { | |
436 | return &_apt_Status; | |
437 | } | |
0d29b9d4 MV |
438 | const pkgIndexFile::Type *debDebPkgFileIndex::GetType() const |
439 | { | |
440 | return &_apt_DebPkgFile; | |
070536e6 | 441 | } |
a49e7948 MV |
442 | const pkgIndexFile::Type *debDscFileIndex::GetType() const |
443 | { | |
444 | return &_apt_DscFile; | |
445 | } | |
77da39b9 MV |
446 | const pkgIndexFile::Type *debDebianSourceDirIndex::GetType() const |
447 | { | |
448 | return &_apt_DebianSourceDir; | |
0d29b9d4 | 449 | } |
b2e465d6 | 450 | /*}}}*/ |
862bafea DK |
451 | |
452 | debStatusIndex::~debStatusIndex() {} | |
453 | debPackagesIndex::~debPackagesIndex() {} | |
454 | debTranslationsIndex::~debTranslationsIndex() {} | |
455 | debSourcesIndex::~debSourcesIndex() {} | |
456 | ||
457 | debDebPkgFileIndex::~debDebPkgFileIndex() {} | |
c8a4ce6c | 458 | debDscFileIndex::~debDscFileIndex() {} |