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>
35 // Sources Index /*{{{*/
36 debSourcesIndex::debSourcesIndex(IndexTarget
const &Target
,bool const Trusted
) :
37 pkgDebianIndexTargetFile(Target
, Trusted
), d(NULL
)
40 std::string
debSourcesIndex::SourceInfo(pkgSrcRecords::Parser
const &Record
,
41 pkgSrcRecords::File
const &File
) const
43 // The result looks like: http://foo/debian/ stable/main src 1.1.1 (dsc)
44 std::string Res
= Target
.Description
;
45 Res
.erase(Target
.Description
.rfind(' '));
48 Res
+= Record
.Package();
50 Res
+= Record
.Version();
51 if (File
.Type
.empty() == false)
52 Res
+= " (" + File
.Type
+ ")";
55 pkgSrcRecords::Parser
*debSourcesIndex::CreateSrcParser() const
57 std::string
const SourcesURI
= IndexFileName();
58 if (FileExists(SourcesURI
))
59 return new debSrcRecordParser(SourcesURI
, this);
62 bool debSourcesIndex::OpenListFile(FileFd
&, std::string
const &)
66 pkgCacheListParser
* debSourcesIndex::CreateListParser(FileFd
&)
70 uint8_t debSourcesIndex::GetIndexFlags() const
75 // Packages Index /*{{{*/
76 debPackagesIndex::debPackagesIndex(IndexTarget
const &Target
, bool const Trusted
) :
77 pkgDebianIndexTargetFile(Target
, Trusted
), d(NULL
)
80 std::string
debPackagesIndex::ArchiveInfo(pkgCache::VerIterator
const &Ver
) const
82 std::string Res
= Target
.Description
;
83 Res
.erase(Target
.Description
.rfind(' '));
86 Res
+= Ver
.ParentPkg().Name();
88 std::string
const Dist
= Target
.Option(IndexTarget::RELEASE
);
89 if (Dist
.empty() == false && Dist
[Dist
.size() - 1] != '/')
90 Res
.append(Ver
.Arch()).append(" ");
94 uint8_t debPackagesIndex::GetIndexFlags() const
99 // Translation-* Index /*{{{*/
100 debTranslationsIndex::debTranslationsIndex(IndexTarget
const &Target
) :
101 pkgDebianIndexTargetFile(Target
, true), d(NULL
)
103 bool debTranslationsIndex::HasPackages() const
107 bool debTranslationsIndex::OpenListFile(FileFd
&Pkg
, std::string
const &FileName
)
109 if (FileExists(FileName
))
110 return pkgDebianIndexTargetFile::OpenListFile(Pkg
, FileName
);
113 uint8_t debTranslationsIndex::GetIndexFlags() const
115 return pkgCache::Flag::NotSource
| pkgCache::Flag::NoPackages
;
117 std::string
debTranslationsIndex::GetArchitecture() const
119 return std::string();
121 pkgCacheListParser
* debTranslationsIndex::CreateListParser(FileFd
&Pkg
)
123 if (Pkg
.IsOpen() == false)
125 _error
->PushToStack();
126 pkgCacheListParser
* const Parser
= new debTranslationsParser(&Pkg
);
127 bool const newError
= _error
->PendingError();
128 _error
->MergeWithStack();
138 // dpkg/status Index /*{{{*/
139 debStatusIndex::debStatusIndex(std::string
const &File
) : pkgDebianIndexRealFile(File
, true), d(NULL
)
142 std::string
debStatusIndex::GetArchitecture() const
144 return std::string();
146 std::string
debStatusIndex::GetComponent() const
150 uint8_t debStatusIndex::GetIndexFlags() const
152 return pkgCache::Flag::NotSource
;
155 pkgCacheListParser
* debStatusIndex::CreateListParser(FileFd
&Pkg
)
157 if (Pkg
.IsOpen() == false)
159 _error
->PushToStack();
160 pkgCacheListParser
* const Parser
= new debStatusListParser(&Pkg
);
161 bool const newError
= _error
->PendingError();
162 _error
->MergeWithStack();
172 // DebPkgFile Index - a single .deb file as an index /*{{{*/
173 debDebPkgFileIndex::debDebPkgFileIndex(std::string
const &DebFile
)
174 : pkgDebianIndexRealFile(DebFile
, true), d(NULL
), DebFile(DebFile
)
177 bool debDebPkgFileIndex::GetContent(std::ostream
&content
, std::string
const &debfile
)
180 if (stat(debfile
.c_str(), &Buf
) != 0)
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");
185 std::vector
<const char *> Args
;
186 Args
.push_back(dpkg
.c_str());
187 Args
.push_back("-I");
188 Args
.push_back(debfile
.c_str());
189 Args
.push_back("control");
190 Args
.push_back(NULL
);
193 if(Popen((const char**)&Args
[0], PipeFd
, Child
, FileFd::ReadOnly
) == false)
194 return _error
->Error("Popen failed");
196 content
<< "Filename: " << debfile
<< "\n";
197 content
<< "Size: " << std::to_string(Buf
.st_size
) << "\n";
198 bool first_line_seen
= false;
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");
206 buffer
[actual
] = '\0';
207 char const * b
= buffer
;
208 if (first_line_seen
== false)
210 for (; *b
!= '\0' && (*b
== '\n' || *b
== '\r'); ++b
)
211 /* skip over leading newlines */;
214 first_line_seen
= true;
218 ExecWait(Child
, "Popen");
222 bool debDebPkgFileIndex::OpenListFile(FileFd
&Pkg
, std::string
const &FileName
)
224 // write the control data to a tempfile
225 if (GetTempFile("deb-file-" + flNotDir(FileName
), true, &Pkg
) == NULL
)
227 std::ostringstream content
;
228 if (GetContent(content
, FileName
) == false)
230 std::string
const contentstr
= content
.str();
231 if (contentstr
.empty())
233 if (Pkg
.Write(contentstr
.c_str(), contentstr
.length()) == false || Pkg
.Seek(0) == false)
237 pkgCacheListParser
* debDebPkgFileIndex::CreateListParser(FileFd
&Pkg
)
239 if (Pkg
.IsOpen() == false)
241 _error
->PushToStack();
242 pkgCacheListParser
* const Parser
= new debDebFileParser(&Pkg
, DebFile
);
243 bool const newError
= _error
->PendingError();
244 _error
->MergeWithStack();
253 uint8_t debDebPkgFileIndex::GetIndexFlags() const
255 return pkgCache::Flag::LocalSource
;
257 std::string
debDebPkgFileIndex::GetArchitecture() const
259 return std::string();
261 std::string
debDebPkgFileIndex::GetComponent() const
265 pkgCache::PkgFileIterator
debDebPkgFileIndex::FindInCache(pkgCache
&Cache
) const
267 std::string
const FileName
= IndexFileName();
268 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
269 for (; File
.end() == false; ++File
)
271 if (File
.FileName() == NULL
|| FileName
!= File
.FileName())
273 // we can't do size checks here as file size != content size
279 std::string
debDebPkgFileIndex::ArchiveInfo_impl(pkgCache::VerIterator
const &Ver
) const
281 std::string Res
= IndexFileName() + " ";
282 Res
.append(Ver
.ParentPkg().Name()).append(" ");
283 Res
.append(Ver
.Arch()).append(" ");
284 Res
.append(Ver
.VerStr());
288 // DscFile Index - a single .dsc file as an index /*{{{*/
289 debDscFileIndex::debDscFileIndex(std::string
const &DscFile
)
290 : pkgDebianIndexRealFile(DscFile
, true), d(NULL
)
293 pkgSrcRecords::Parser
*debDscFileIndex::CreateSrcParser() const
295 if (Exists() == false)
297 return new debDscRecordParser(File
, this);
299 std::string
debDscFileIndex::GetComponent() const
303 std::string
debDscFileIndex::GetArchitecture() const
307 uint8_t debDscFileIndex::GetIndexFlags() const
309 return pkgCache::Flag::LocalSource
;
312 // ControlFile Index - a directory with a debian/control file /*{{{*/
313 std::string
debDebianSourceDirIndex::GetComponent() const
315 return "local-control";
318 // String Package Index - a string of Packages file content /*{{{*/
319 std::string
debStringPackageIndex::GetArchitecture() const
321 return std::string();
323 std::string
debStringPackageIndex::GetComponent() const
325 return "apt-tmp-index";
327 uint8_t debStringPackageIndex::GetIndexFlags() const
329 return pkgCache::Flag::NotSource
;
331 const pkgIndexFile::Type
*debStringPackageIndex::GetType() const
333 return pkgIndexFile::Type::GetType("Debian Package Index");
335 debStringPackageIndex::debStringPackageIndex(std::string
const &content
) :
336 pkgDebianIndexRealFile("", false), d(NULL
)
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
);
343 FileFd::Write(fd
, content
.data(), content
.length());
346 debStringPackageIndex::~debStringPackageIndex()
348 RemoveFile("~debStringPackageIndex", File
);
352 // Index File types for Debian /*{{{*/
353 class APT_HIDDEN debIFTypeSrc
: public pkgIndexFile::Type
356 debIFTypeSrc() {Label
= "Debian Source Index";};
358 class APT_HIDDEN debIFTypePkg
: public pkgIndexFile::Type
361 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator
const &File
) const APT_OVERRIDE
363 return new debRecordParser(File
.FileName(),*File
.Cache());
365 debIFTypePkg() {Label
= "Debian Package Index";};
367 class APT_HIDDEN debIFTypeTrans
: public debIFTypePkg
370 debIFTypeTrans() {Label
= "Debian Translation Index";};
372 class APT_HIDDEN debIFTypeStatus
: public pkgIndexFile::Type
375 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator
const &File
) const APT_OVERRIDE
377 return new debRecordParser(File
.FileName(),*File
.Cache());
379 debIFTypeStatus() {Label
= "Debian dpkg status file";};
381 class APT_HIDDEN debIFTypeDebPkgFile
: public pkgIndexFile::Type
384 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator
const &File
) const APT_OVERRIDE
386 return new debDebFileRecordParser(File
.FileName());
388 debIFTypeDebPkgFile() {Label
= "Debian deb file";};
390 class APT_HIDDEN debIFTypeDscFile
: public pkgIndexFile::Type
393 virtual pkgSrcRecords::Parser
*CreateSrcPkgParser(std::string
const &DscFile
) const APT_OVERRIDE
395 return new debDscRecordParser(DscFile
, NULL
);
397 debIFTypeDscFile() {Label
= "Debian dsc file";};
399 class APT_HIDDEN debIFTypeDebianSourceDir
: public pkgIndexFile::Type
402 virtual pkgSrcRecords::Parser
*CreateSrcPkgParser(std::string
const &SourceDir
) const APT_OVERRIDE
404 return new debDscRecordParser(SourceDir
+ std::string("/debian/control"), NULL
);
406 debIFTypeDebianSourceDir() {Label
= "Debian control file";};
409 APT_HIDDEN debIFTypeSrc _apt_Src
;
410 APT_HIDDEN debIFTypePkg _apt_Pkg
;
411 APT_HIDDEN debIFTypeTrans _apt_Trans
;
412 APT_HIDDEN debIFTypeStatus _apt_Status
;
413 APT_HIDDEN debIFTypeDebPkgFile _apt_DebPkgFile
;
414 // file based pseudo indexes
415 APT_HIDDEN debIFTypeDscFile _apt_DscFile
;
416 APT_HIDDEN debIFTypeDebianSourceDir _apt_DebianSourceDir
;
418 const pkgIndexFile::Type
*debSourcesIndex::GetType() const
422 const pkgIndexFile::Type
*debPackagesIndex::GetType() const
426 const pkgIndexFile::Type
*debTranslationsIndex::GetType() const
430 const pkgIndexFile::Type
*debStatusIndex::GetType() const
434 const pkgIndexFile::Type
*debDebPkgFileIndex::GetType() const
436 return &_apt_DebPkgFile
;
438 const pkgIndexFile::Type
*debDscFileIndex::GetType() const
440 return &_apt_DscFile
;
442 const pkgIndexFile::Type
*debDebianSourceDirIndex::GetType() const
444 return &_apt_DebianSourceDir
;
448 debStatusIndex::~debStatusIndex() {}
449 debPackagesIndex::~debPackagesIndex() {}
450 debTranslationsIndex::~debTranslationsIndex() {}
451 debSourcesIndex::~debSourcesIndex() {}
453 debDebPkgFileIndex::~debDebPkgFileIndex() {}
454 debDscFileIndex::~debDscFileIndex() {}