]> git.saurik.com Git - apt.git/blame - apt-pkg/deb/debindexfile.cc
not all targets are deb-src targets
[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
DK
123 if (Pkg.IsOpen() == false)
124 return NULL;
125 _error->PushToStack();
126 pkgCacheListParser * const Parser = new debTranslationsParser(&Pkg);
127 bool const newError = _error->PendingError();
128 _error->MergeWithStack();
129 return newError ? NULL : Parser;
b2e465d6
AL
130}
131 /*}}}*/
c9443c01
DK
132// dpkg/status Index /*{{{*/
133debStatusIndex::debStatusIndex(std::string const &File) : pkgDebianIndexRealFile(File, true), d(NULL)
b2e465d6 134{
b2e465d6 135}
c9443c01 136std::string debStatusIndex::GetArchitecture() const
b2e465d6 137{
c9443c01 138 return std::string();
b2e465d6 139}
c9443c01 140std::string debStatusIndex::GetComponent() const
0d29b9d4 141{
c9443c01 142 return "now";
0d29b9d4 143}
c9443c01 144uint8_t debStatusIndex::GetIndexFlags() const
0d29b9d4 145{
c9443c01 146 return pkgCache::Flag::NotSource;
0d29b9d4 147}
c9443c01
DK
148 /*}}}*/
149// DebPkgFile Index - a single .deb file as an index /*{{{*/
150debDebPkgFileIndex::debDebPkgFileIndex(std::string const &DebFile)
151 : pkgDebianIndexRealFile(DebFile, true), d(NULL), DebFile(DebFile)
0d29b9d4 152{
0d29b9d4 153}
2f6a2fbb 154bool debDebPkgFileIndex::GetContent(std::ostream &content, std::string const &debfile)
0d29b9d4 155{
c9443c01
DK
156 struct stat Buf;
157 if (stat(debfile.c_str(), &Buf) != 0)
158 return false;
159
2f6a2fbb
DK
160 // get the control data out of the deb file via dpkg-deb -I
161 std::string dpkg = _config->Find("Dir::Bin::dpkg","dpkg-deb");
fdff5b03
MV
162 std::vector<const char *> Args;
163 Args.push_back(dpkg.c_str());
fdff5b03 164 Args.push_back("-I");
2f6a2fbb 165 Args.push_back(debfile.c_str());
fdff5b03
MV
166 Args.push_back("control");
167 Args.push_back(NULL);
a9d990a2
MV
168 FileFd PipeFd;
169 pid_t Child;
fdff5b03 170 if(Popen((const char**)&Args[0], PipeFd, Child, FileFd::ReadOnly) == false)
a9d990a2 171 return _error->Error("Popen failed");
2f6a2fbb
DK
172
173 char buffer[1024];
174 do {
175 unsigned long long actual = 0;
176 if (PipeFd.Read(buffer, sizeof(buffer)-1, &actual) == false)
177 return _error->Errno("read", "Failed to read dpkg pipe");
178 if (actual == 0)
179 break;
180 buffer[actual] = '\0';
181 content << buffer;
182 } while(true);
a9d990a2 183 ExecWait(Child, "Popen");
0d29b9d4 184
2f6a2fbb 185 content << "Filename: " << debfile << "\n";
2f6a2fbb
DK
186 content << "Size: " << Buf.st_size << "\n";
187
188 return true;
189}
c9443c01 190bool debDebPkgFileIndex::OpenListFile(FileFd &Pkg, std::string const &FileName)
2f6a2fbb 191{
2f6a2fbb 192 // write the control data to a tempfile
c9443c01 193 if (GetTempFile("deb-file-" + flNotDir(FileName), true, &Pkg) == NULL)
0d29b9d4 194 return false;
2f6a2fbb 195 std::ostringstream content;
c9443c01 196 if (GetContent(content, FileName) == false)
2f6a2fbb
DK
197 return false;
198 std::string const contentstr = content.str();
5465192b
DK
199 if (contentstr.empty())
200 return true;
c9443c01
DK
201 if (Pkg.Write(contentstr.c_str(), contentstr.length()) == false || Pkg.Seek(0) == false)
202 return false;
0d29b9d4
MV
203 return true;
204}
c9443c01
DK
205pkgCacheListParser * debDebPkgFileIndex::CreateListParser(FileFd &Pkg)
206{
207 if (Pkg.IsOpen() == false)
208 return NULL;
209 _error->PushToStack();
210 pkgCacheListParser * const Parser = new debDebFileParser(&Pkg, DebFile);
211 bool const newError = _error->PendingError();
212 _error->MergeWithStack();
213 return newError ? NULL : Parser;
214}
215uint8_t debDebPkgFileIndex::GetIndexFlags() const
216{
217 return pkgCache::Flag::LocalSource;
218}
219std::string debDebPkgFileIndex::GetArchitecture() const
220{
221 return std::string();
222}
223std::string debDebPkgFileIndex::GetComponent() const
224{
225 return "local-deb";
226}
0d29b9d4
MV
227pkgCache::PkgFileIterator debDebPkgFileIndex::FindInCache(pkgCache &Cache) const
228{
c9443c01 229 std::string const FileName = IndexFileName();
0d29b9d4
MV
230 pkgCache::PkgFileIterator File = Cache.FileBegin();
231 for (; File.end() == false; ++File)
232 {
c9443c01 233 if (File.FileName() == NULL || FileName != File.FileName())
0d29b9d4 234 continue;
c9443c01
DK
235 // we can't do size checks here as file size != content size
236 return File;
0d29b9d4 237 }
5465192b 238
0d29b9d4
MV
239 return File;
240}
0d29b9d4 241
c9443c01
DK
242 /*}}}*/
243// DscFile Index - a single .dsc file as an index /*{{{*/
5465192b 244debDscFileIndex::debDscFileIndex(std::string const &DscFile)
c9443c01 245 : pkgDebianIndexRealFile(DscFile, true), d(NULL)
a49e7948
MV
246{
247}
a49e7948
MV
248pkgSrcRecords::Parser *debDscFileIndex::CreateSrcParser() const
249{
c9443c01 250 if (Exists() == false)
a49e7948 251 return NULL;
c9443c01 252 return new debDscRecordParser(File, this);
a49e7948
MV
253}
254 /*}}}*/
5465192b 255
b2e465d6 256// Index File types for Debian /*{{{*/
dce45dbe 257class APT_HIDDEN debIFTypeSrc : public pkgIndexFile::Type
b2e465d6
AL
258{
259 public:
b2e465d6
AL
260 debIFTypeSrc() {Label = "Debian Source Index";};
261};
dce45dbe 262class APT_HIDDEN debIFTypePkg : public pkgIndexFile::Type
b2e465d6
AL
263{
264 public:
c9443c01 265 virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator const &File) const APT_OVERRIDE
b2e465d6
AL
266 {
267 return new debRecordParser(File.FileName(),*File.Cache());
268 };
269 debIFTypePkg() {Label = "Debian Package Index";};
270};
dce45dbe 271class APT_HIDDEN debIFTypeTrans : public debIFTypePkg
97234432
MV
272{
273 public:
274 debIFTypeTrans() {Label = "Debian Translation Index";};
275};
dce45dbe 276class APT_HIDDEN debIFTypeStatus : public pkgIndexFile::Type
b2e465d6
AL
277{
278 public:
c9443c01 279 virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator const &File) const APT_OVERRIDE
b2e465d6
AL
280 {
281 return new debRecordParser(File.FileName(),*File.Cache());
282 };
283 debIFTypeStatus() {Label = "Debian dpkg status file";};
284};
dce45dbe 285class APT_HIDDEN debIFTypeDebPkgFile : public pkgIndexFile::Type
0d29b9d4
MV
286{
287 public:
c9443c01 288 virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator const &File) const APT_OVERRIDE
0d29b9d4 289 {
2f6a2fbb 290 return new debDebFileRecordParser(File.FileName());
0d29b9d4 291 };
463c8d80 292 debIFTypeDebPkgFile() {Label = "Debian deb file";};
0d29b9d4 293};
dce45dbe 294class APT_HIDDEN debIFTypeDscFile : public pkgIndexFile::Type
a49e7948
MV
295{
296 public:
c9443c01 297 virtual pkgSrcRecords::Parser *CreateSrcPkgParser(std::string const &DscFile) const APT_OVERRIDE
a49e7948
MV
298 {
299 return new debDscRecordParser(DscFile, NULL);
300 };
463c8d80 301 debIFTypeDscFile() {Label = "Debian dsc file";};
a49e7948 302};
dce45dbe 303class APT_HIDDEN debIFTypeDebianSourceDir : public pkgIndexFile::Type
77da39b9
MV
304{
305 public:
c9443c01 306 virtual pkgSrcRecords::Parser *CreateSrcPkgParser(std::string const &SourceDir) const APT_OVERRIDE
77da39b9 307 {
c9443c01 308 return new debDscRecordParser(SourceDir + std::string("/debian/control"), NULL);
77da39b9 309 };
463c8d80 310 debIFTypeDebianSourceDir() {Label = "Debian control file";};
77da39b9 311};
a49e7948 312
dce45dbe
DK
313APT_HIDDEN debIFTypeSrc _apt_Src;
314APT_HIDDEN debIFTypePkg _apt_Pkg;
315APT_HIDDEN debIFTypeTrans _apt_Trans;
316APT_HIDDEN debIFTypeStatus _apt_Status;
317APT_HIDDEN debIFTypeDebPkgFile _apt_DebPkgFile;
a49e7948 318// file based pseudo indexes
dce45dbe
DK
319APT_HIDDEN debIFTypeDscFile _apt_DscFile;
320APT_HIDDEN debIFTypeDebianSourceDir _apt_DebianSourceDir;
b2e465d6
AL
321
322const pkgIndexFile::Type *debSourcesIndex::GetType() const
323{
324 return &_apt_Src;
325}
326const pkgIndexFile::Type *debPackagesIndex::GetType() const
327{
328 return &_apt_Pkg;
329}
a52f938b
OS
330const pkgIndexFile::Type *debTranslationsIndex::GetType() const
331{
97234432 332 return &_apt_Trans;
a52f938b 333}
b2e465d6
AL
334const pkgIndexFile::Type *debStatusIndex::GetType() const
335{
336 return &_apt_Status;
337}
0d29b9d4
MV
338const pkgIndexFile::Type *debDebPkgFileIndex::GetType() const
339{
340 return &_apt_DebPkgFile;
070536e6 341}
a49e7948
MV
342const pkgIndexFile::Type *debDscFileIndex::GetType() const
343{
344 return &_apt_DscFile;
345}
77da39b9
MV
346const pkgIndexFile::Type *debDebianSourceDirIndex::GetType() const
347{
348 return &_apt_DebianSourceDir;
0d29b9d4 349}
b2e465d6 350 /*}}}*/
862bafea
DK
351
352debStatusIndex::~debStatusIndex() {}
353debPackagesIndex::~debPackagesIndex() {}
354debTranslationsIndex::~debTranslationsIndex() {}
355debSourcesIndex::~debSourcesIndex() {}
356
357debDebPkgFileIndex::~debDebPkgFileIndex() {}
c8a4ce6c 358debDscFileIndex::~debDscFileIndex() {}