1 // -*- mode: cpp; mode: fold -*-
3 // $Id: debindexfile.cc,v 1.5.2.3 2004/01/04 19:11:00 mdz Exp $
4 /* ######################################################################
6 Debian Specific sources.list types and the three sorts of Debian
9 ##################################################################### */
11 // Include Files /*{{{*/
14 #include <apt-pkg/debindexfile.h>
15 #include <apt-pkg/debsrcrecords.h>
16 #include <apt-pkg/deblistparser.h>
17 #include <apt-pkg/debrecords.h>
18 #include <apt-pkg/configuration.h>
19 #include <apt-pkg/error.h>
20 #include <apt-pkg/fileutl.h>
21 #include <apt-pkg/indexfile.h>
22 #include <apt-pkg/pkgcache.h>
23 #include <apt-pkg/cacheiterators.h>
24 #include <apt-pkg/pkgrecords.h>
25 #include <apt-pkg/srcrecords.h>
36 // Sources Index /*{{{*/
37 debSourcesIndex::debSourcesIndex(IndexTarget
const &Target
,bool const Trusted
) :
38 pkgDebianIndexTargetFile(Target
, Trusted
), d(NULL
)
41 std::string
debSourcesIndex::SourceInfo(pkgSrcRecords::Parser
const &Record
,
42 pkgSrcRecords::File
const &File
) const
44 // The result looks like: http://foo/debian/ stable/main src 1.1.1 (dsc)
45 std::string Res
= Target
.Description
;
46 Res
.erase(Target
.Description
.rfind(' '));
49 Res
+= Record
.Package();
51 Res
+= Record
.Version();
52 if (File
.Type
.empty() == false)
53 Res
+= " (" + File
.Type
+ ")";
56 pkgSrcRecords::Parser
*debSourcesIndex::CreateSrcParser() const
58 std::string
const SourcesURI
= IndexFileName();
59 if (FileExists(SourcesURI
))
60 return new debSrcRecordParser(SourcesURI
, this);
63 bool debSourcesIndex::OpenListFile(FileFd
&, std::string
const &)
67 pkgCacheListParser
* debSourcesIndex::CreateListParser(FileFd
&)
71 uint8_t debSourcesIndex::GetIndexFlags() const
76 // Packages Index /*{{{*/
77 debPackagesIndex::debPackagesIndex(IndexTarget
const &Target
, bool const Trusted
) :
78 pkgDebianIndexTargetFile(Target
, Trusted
), d(NULL
)
81 std::string
debPackagesIndex::ArchiveInfo(pkgCache::VerIterator
const &Ver
) const
83 std::string Res
= Target
.Description
;
85 auto const space
= Target
.Description
.rfind(' ');
86 if (space
!= std::string::npos
)
91 Res
+= Ver
.ParentPkg().Name();
93 std::string
const Dist
= Target
.Option(IndexTarget::RELEASE
);
94 if (Dist
.empty() == false && Dist
[Dist
.size() - 1] != '/')
95 Res
.append(Ver
.Arch()).append(" ");
99 uint8_t debPackagesIndex::GetIndexFlags() const
104 // Translation-* Index /*{{{*/
105 debTranslationsIndex::debTranslationsIndex(IndexTarget
const &Target
) :
106 pkgDebianIndexTargetFile(Target
, true), d(NULL
)
108 bool debTranslationsIndex::HasPackages() const
112 bool debTranslationsIndex::OpenListFile(FileFd
&Pkg
, std::string
const &FileName
)
114 if (FileExists(FileName
))
115 return pkgDebianIndexTargetFile::OpenListFile(Pkg
, FileName
);
118 uint8_t debTranslationsIndex::GetIndexFlags() const
120 return pkgCache::Flag::NotSource
| pkgCache::Flag::NoPackages
;
122 std::string
debTranslationsIndex::GetArchitecture() const
124 return std::string();
126 pkgCacheListParser
* debTranslationsIndex::CreateListParser(FileFd
&Pkg
)
128 if (Pkg
.IsOpen() == false)
130 _error
->PushToStack();
131 pkgCacheListParser
* const Parser
= new debTranslationsParser(&Pkg
);
132 bool const newError
= _error
->PendingError();
133 _error
->MergeWithStack();
143 // dpkg/status Index /*{{{*/
144 debStatusIndex::debStatusIndex(std::string
const &File
) : pkgDebianIndexRealFile(File
, true), d(NULL
)
147 std::string
debStatusIndex::GetArchitecture() const
149 return std::string();
151 std::string
debStatusIndex::GetComponent() const
155 uint8_t debStatusIndex::GetIndexFlags() const
157 return pkgCache::Flag::NotSource
;
160 pkgCacheListParser
* debStatusIndex::CreateListParser(FileFd
&Pkg
)
162 if (Pkg
.IsOpen() == false)
164 _error
->PushToStack();
165 pkgCacheListParser
* const Parser
= new debStatusListParser(&Pkg
);
166 bool const newError
= _error
->PendingError();
167 _error
->MergeWithStack();
177 // DebPkgFile Index - a single .deb file as an index /*{{{*/
178 debDebPkgFileIndex::debDebPkgFileIndex(std::string
const &DebFile
)
179 : pkgDebianIndexRealFile(DebFile
, true), d(NULL
), DebFile(DebFile
)
182 bool debDebPkgFileIndex::GetContent(std::ostream
&content
, std::string
const &debfile
)
185 if (stat(debfile
.c_str(), &Buf
) != 0)
188 // get the control data out of the deb file via dpkg-deb -I
189 std::string dpkg
= _config
->Find("Dir::Bin::dpkg","dpkg-deb");
190 std::vector
<const char *> Args
;
191 Args
.push_back(dpkg
.c_str());
192 Args
.push_back("-I");
193 Args
.push_back(debfile
.c_str());
194 Args
.push_back("control");
195 Args
.push_back(NULL
);
198 if(Popen((const char**)&Args
[0], PipeFd
, Child
, FileFd::ReadOnly
) == false)
199 return _error
->Error("Popen failed");
201 content
<< "Filename: " << debfile
<< "\n";
202 content
<< "Size: " << std::to_string(Buf
.st_size
) << "\n";
203 bool first_line_seen
= false;
206 unsigned long long actual
= 0;
207 if (PipeFd
.Read(buffer
, sizeof(buffer
)-1, &actual
) == false)
208 return _error
->Errno("read", "Failed to read dpkg pipe");
211 buffer
[actual
] = '\0';
212 char const * b
= buffer
;
213 if (first_line_seen
== false)
215 for (; *b
!= '\0' && (*b
== '\n' || *b
== '\r'); ++b
)
216 /* skip over leading newlines */;
219 first_line_seen
= true;
223 ExecWait(Child
, "Popen");
227 bool debDebPkgFileIndex::OpenListFile(FileFd
&Pkg
, std::string
const &FileName
)
229 // write the control data to a tempfile
230 if (GetTempFile("deb-file-" + flNotDir(FileName
), true, &Pkg
) == NULL
)
232 std::ostringstream content
;
233 if (GetContent(content
, FileName
) == false)
235 std::string
const contentstr
= content
.str();
236 if (contentstr
.empty())
238 if (Pkg
.Write(contentstr
.c_str(), contentstr
.length()) == false || Pkg
.Seek(0) == false)
242 pkgCacheListParser
* debDebPkgFileIndex::CreateListParser(FileFd
&Pkg
)
244 if (Pkg
.IsOpen() == false)
246 _error
->PushToStack();
247 pkgCacheListParser
* const Parser
= new debDebFileParser(&Pkg
, DebFile
);
248 bool const newError
= _error
->PendingError();
249 _error
->MergeWithStack();
258 uint8_t debDebPkgFileIndex::GetIndexFlags() const
260 return pkgCache::Flag::LocalSource
;
262 std::string
debDebPkgFileIndex::GetArchitecture() const
264 return std::string();
266 std::string
debDebPkgFileIndex::GetComponent() const
270 pkgCache::PkgFileIterator
debDebPkgFileIndex::FindInCache(pkgCache
&Cache
) const
272 std::string
const FileName
= IndexFileName();
273 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
274 for (; File
.end() == false; ++File
)
276 if (File
.FileName() == NULL
|| FileName
!= File
.FileName())
278 // we can't do size checks here as file size != content size
284 std::string
debDebPkgFileIndex::ArchiveInfo_impl(pkgCache::VerIterator
const &Ver
) const
286 std::string Res
= IndexFileName() + " ";
287 Res
.append(Ver
.ParentPkg().Name()).append(" ");
288 Res
.append(Ver
.Arch()).append(" ");
289 Res
.append(Ver
.VerStr());
293 // DscFile Index - a single .dsc file as an index /*{{{*/
294 debDscFileIndex::debDscFileIndex(std::string
const &DscFile
)
295 : pkgDebianIndexRealFile(DscFile
, true), d(NULL
)
298 pkgSrcRecords::Parser
*debDscFileIndex::CreateSrcParser() const
300 if (Exists() == false)
302 return new debDscRecordParser(File
, this);
304 std::string
debDscFileIndex::GetComponent() const
308 std::string
debDscFileIndex::GetArchitecture() const
312 uint8_t debDscFileIndex::GetIndexFlags() const
314 return pkgCache::Flag::LocalSource
;
317 // ControlFile Index - a directory with a debian/control file /*{{{*/
318 std::string
debDebianSourceDirIndex::GetComponent() const
320 return "local-control";
323 // String Package Index - a string of Packages file content /*{{{*/
324 std::string
debStringPackageIndex::GetArchitecture() const
326 return std::string();
328 std::string
debStringPackageIndex::GetComponent() const
330 return "apt-tmp-index";
332 uint8_t debStringPackageIndex::GetIndexFlags() const
334 return pkgCache::Flag::NotSource
;
336 const pkgIndexFile::Type
*debStringPackageIndex::GetType() const
338 return pkgIndexFile::Type::GetType("Debian Package Index");
340 debStringPackageIndex::debStringPackageIndex(std::string
const &content
) :
341 pkgDebianIndexRealFile("", false), d(NULL
)
344 std::string
const tempdir
= GetTempDir();
345 snprintf(fn
, sizeof(fn
), "%s/%s.XXXXXX", tempdir
.c_str(), "apt-tmp-index");
346 int const fd
= mkstemp(fn
);
348 FileFd::Write(fd
, content
.data(), content
.length());
351 debStringPackageIndex::~debStringPackageIndex()
353 RemoveFile("~debStringPackageIndex", File
);
357 // Index File types for Debian /*{{{*/
358 class APT_HIDDEN debIFTypeSrc
: public pkgIndexFile::Type
361 debIFTypeSrc() {Label
= "Debian Source Index";};
363 class APT_HIDDEN debIFTypePkg
: public pkgIndexFile::Type
366 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator
const &File
) const APT_OVERRIDE
368 return new debRecordParser(File
.FileName(),*File
.Cache());
370 debIFTypePkg() {Label
= "Debian Package Index";};
372 class APT_HIDDEN debIFTypeTrans
: public debIFTypePkg
375 debIFTypeTrans() {Label
= "Debian Translation Index";};
377 class APT_HIDDEN debIFTypeStatus
: public pkgIndexFile::Type
380 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator
const &File
) const APT_OVERRIDE
382 return new debRecordParser(File
.FileName(),*File
.Cache());
384 debIFTypeStatus() {Label
= "Debian dpkg status file";};
386 class APT_HIDDEN debIFTypeDebPkgFile
: public pkgIndexFile::Type
389 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator
const &File
) const APT_OVERRIDE
391 return new debDebFileRecordParser(File
.FileName());
393 debIFTypeDebPkgFile() {Label
= "Debian deb file";};
395 class APT_HIDDEN debIFTypeDscFile
: public pkgIndexFile::Type
398 virtual pkgSrcRecords::Parser
*CreateSrcPkgParser(std::string
const &DscFile
) const APT_OVERRIDE
400 return new debDscRecordParser(DscFile
, NULL
);
402 debIFTypeDscFile() {Label
= "Debian dsc file";};
404 class APT_HIDDEN debIFTypeDebianSourceDir
: public pkgIndexFile::Type
407 virtual pkgSrcRecords::Parser
*CreateSrcPkgParser(std::string
const &SourceDir
) const APT_OVERRIDE
409 return new debDscRecordParser(SourceDir
+ std::string("/debian/control"), NULL
);
411 debIFTypeDebianSourceDir() {Label
= "Debian control file";};
414 APT_HIDDEN debIFTypeSrc _apt_Src
;
415 APT_HIDDEN debIFTypePkg _apt_Pkg
;
416 APT_HIDDEN debIFTypeTrans _apt_Trans
;
417 APT_HIDDEN debIFTypeStatus _apt_Status
;
418 APT_HIDDEN debIFTypeDebPkgFile _apt_DebPkgFile
;
419 // file based pseudo indexes
420 APT_HIDDEN debIFTypeDscFile _apt_DscFile
;
421 APT_HIDDEN debIFTypeDebianSourceDir _apt_DebianSourceDir
;
423 const pkgIndexFile::Type
*debSourcesIndex::GetType() const
427 const pkgIndexFile::Type
*debPackagesIndex::GetType() const
431 const pkgIndexFile::Type
*debTranslationsIndex::GetType() const
435 const pkgIndexFile::Type
*debStatusIndex::GetType() const
439 const pkgIndexFile::Type
*debDebPkgFileIndex::GetType() const
441 return &_apt_DebPkgFile
;
443 const pkgIndexFile::Type
*debDscFileIndex::GetType() const
445 return &_apt_DscFile
;
447 const pkgIndexFile::Type
*debDebianSourceDirIndex::GetType() const
449 return &_apt_DebianSourceDir
;
453 debStatusIndex::~debStatusIndex() {}
454 debPackagesIndex::~debPackagesIndex() {}
455 debTranslationsIndex::~debTranslationsIndex() {}
456 debSourcesIndex::~debSourcesIndex() {}
458 debDebPkgFileIndex::~debDebPkgFileIndex() {}
459 debDscFileIndex::~debDscFileIndex() {}