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