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
;
84 auto const space
= Target
.Description
.rfind(' ');
85 if (space
!= std::string::npos
)
90 Res
+= Ver
.ParentPkg().Name();
92 std::string
const Dist
= Target
.Option(IndexTarget::RELEASE
);
93 if (Dist
.empty() == false && Dist
[Dist
.size() - 1] != '/')
94 Res
.append(Ver
.Arch()).append(" ");
98 uint8_t debPackagesIndex::GetIndexFlags() const
103 // Translation-* Index /*{{{*/
104 debTranslationsIndex::debTranslationsIndex(IndexTarget
const &Target
) :
105 pkgDebianIndexTargetFile(Target
, true), d(NULL
)
107 bool debTranslationsIndex::HasPackages() const
111 bool debTranslationsIndex::OpenListFile(FileFd
&Pkg
, std::string
const &FileName
)
113 if (FileExists(FileName
))
114 return pkgDebianIndexTargetFile::OpenListFile(Pkg
, FileName
);
117 uint8_t debTranslationsIndex::GetIndexFlags() const
119 return pkgCache::Flag::NotSource
| pkgCache::Flag::NoPackages
;
121 std::string
debTranslationsIndex::GetArchitecture() const
123 return std::string();
125 pkgCacheListParser
* debTranslationsIndex::CreateListParser(FileFd
&Pkg
)
127 if (Pkg
.IsOpen() == false)
129 _error
->PushToStack();
130 pkgCacheListParser
* const Parser
= new debTranslationsParser(&Pkg
);
131 bool const newError
= _error
->PendingError();
132 _error
->MergeWithStack();
142 // dpkg/status Index /*{{{*/
143 debStatusIndex::debStatusIndex(std::string
const &File
) : pkgDebianIndexRealFile(File
, true), d(NULL
)
146 std::string
debStatusIndex::GetArchitecture() const
148 return std::string();
150 std::string
debStatusIndex::GetComponent() const
154 uint8_t debStatusIndex::GetIndexFlags() const
156 return pkgCache::Flag::NotSource
;
159 pkgCacheListParser
* debStatusIndex::CreateListParser(FileFd
&Pkg
)
161 if (Pkg
.IsOpen() == false)
163 _error
->PushToStack();
164 pkgCacheListParser
* const Parser
= new debStatusListParser(&Pkg
);
165 bool const newError
= _error
->PendingError();
166 _error
->MergeWithStack();
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
)
181 bool debDebPkgFileIndex::GetContent(std::ostream
&content
, std::string
const &debfile
)
184 if (stat(debfile
.c_str(), &Buf
) != 0)
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");
189 std::vector
<const char *> Args
;
190 Args
.push_back(dpkg
.c_str());
191 Args
.push_back("-I");
192 Args
.push_back(debfile
.c_str());
193 Args
.push_back("control");
194 Args
.push_back(NULL
);
197 if(Popen((const char**)&Args
[0], PipeFd
, Child
, FileFd::ReadOnly
) == false)
198 return _error
->Error("Popen failed");
200 content
<< "Filename: " << debfile
<< "\n";
201 content
<< "Size: " << std::to_string(Buf
.st_size
) << "\n";
202 bool first_line_seen
= false;
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");
210 buffer
[actual
] = '\0';
211 char const * b
= buffer
;
212 if (first_line_seen
== false)
214 for (; *b
!= '\0' && (*b
== '\n' || *b
== '\r'); ++b
)
215 /* skip over leading newlines */;
218 first_line_seen
= true;
222 ExecWait(Child
, "Popen");
226 bool debDebPkgFileIndex::OpenListFile(FileFd
&Pkg
, std::string
const &FileName
)
228 // write the control data to a tempfile
229 if (GetTempFile("deb-file-" + flNotDir(FileName
), true, &Pkg
) == NULL
)
231 std::ostringstream content
;
232 if (GetContent(content
, FileName
) == false)
234 std::string
const contentstr
= content
.str();
235 if (contentstr
.empty())
237 if (Pkg
.Write(contentstr
.c_str(), contentstr
.length()) == false || Pkg
.Seek(0) == false)
241 pkgCacheListParser
* debDebPkgFileIndex::CreateListParser(FileFd
&Pkg
)
243 if (Pkg
.IsOpen() == false)
245 _error
->PushToStack();
246 pkgCacheListParser
* const Parser
= new debDebFileParser(&Pkg
, DebFile
);
247 bool const newError
= _error
->PendingError();
248 _error
->MergeWithStack();
257 uint8_t debDebPkgFileIndex::GetIndexFlags() const
259 return pkgCache::Flag::LocalSource
;
261 std::string
debDebPkgFileIndex::GetArchitecture() const
263 return std::string();
265 std::string
debDebPkgFileIndex::GetComponent() const
269 pkgCache::PkgFileIterator
debDebPkgFileIndex::FindInCache(pkgCache
&Cache
) const
271 std::string
const FileName
= IndexFileName();
272 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
273 for (; File
.end() == false; ++File
)
275 if (File
.FileName() == NULL
|| FileName
!= File
.FileName())
277 // we can't do size checks here as file size != content size
283 std::string
debDebPkgFileIndex::ArchiveInfo_impl(pkgCache::VerIterator
const &Ver
) const
285 std::string Res
= IndexFileName() + " ";
286 Res
.append(Ver
.ParentPkg().Name()).append(" ");
287 Res
.append(Ver
.Arch()).append(" ");
288 Res
.append(Ver
.VerStr());
292 // DscFile Index - a single .dsc file as an index /*{{{*/
293 debDscFileIndex::debDscFileIndex(std::string
const &DscFile
)
294 : pkgDebianIndexRealFile(DscFile
, true), d(NULL
)
297 pkgSrcRecords::Parser
*debDscFileIndex::CreateSrcParser() const
299 if (Exists() == false)
301 return new debDscRecordParser(File
, this);
303 std::string
debDscFileIndex::GetComponent() const
307 std::string
debDscFileIndex::GetArchitecture() const
311 uint8_t debDscFileIndex::GetIndexFlags() const
313 return pkgCache::Flag::LocalSource
;
316 // ControlFile Index - a directory with a debian/control file /*{{{*/
317 std::string
debDebianSourceDirIndex::GetComponent() const
319 return "local-control";
322 // String Package Index - a string of Packages file content /*{{{*/
323 std::string
debStringPackageIndex::GetArchitecture() const
325 return std::string();
327 std::string
debStringPackageIndex::GetComponent() const
329 return "apt-tmp-index";
331 uint8_t debStringPackageIndex::GetIndexFlags() const
333 return pkgCache::Flag::NotSource
;
335 const pkgIndexFile::Type
*debStringPackageIndex::GetType() const
337 return pkgIndexFile::Type::GetType("Debian Package Index");
339 debStringPackageIndex::debStringPackageIndex(std::string
const &content
) :
340 pkgDebianIndexRealFile("", false), d(NULL
)
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
);
347 FileFd::Write(fd
, content
.data(), content
.length());
350 debStringPackageIndex::~debStringPackageIndex()
352 RemoveFile("~debStringPackageIndex", File
);
356 // Index File types for Debian /*{{{*/
357 class APT_HIDDEN debIFTypeSrc
: public pkgIndexFile::Type
360 debIFTypeSrc() {Label
= "Debian Source Index";};
362 class APT_HIDDEN debIFTypePkg
: public pkgIndexFile::Type
365 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator
const &File
) const APT_OVERRIDE
367 return new debRecordParser(File
.FileName(),*File
.Cache());
369 debIFTypePkg() {Label
= "Debian Package Index";};
371 class APT_HIDDEN debIFTypeTrans
: public debIFTypePkg
374 debIFTypeTrans() {Label
= "Debian Translation Index";};
376 class APT_HIDDEN debIFTypeStatus
: public pkgIndexFile::Type
379 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator
const &File
) const APT_OVERRIDE
381 return new debRecordParser(File
.FileName(),*File
.Cache());
383 debIFTypeStatus() {Label
= "Debian dpkg status file";};
385 class APT_HIDDEN debIFTypeDebPkgFile
: public pkgIndexFile::Type
388 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator
const &File
) const APT_OVERRIDE
390 return new debDebFileRecordParser(File
.FileName());
392 debIFTypeDebPkgFile() {Label
= "Debian deb file";};
394 class APT_HIDDEN debIFTypeDscFile
: public pkgIndexFile::Type
397 virtual pkgSrcRecords::Parser
*CreateSrcPkgParser(std::string
const &DscFile
) const APT_OVERRIDE
399 return new debDscRecordParser(DscFile
, NULL
);
401 debIFTypeDscFile() {Label
= "Debian dsc file";};
403 class APT_HIDDEN debIFTypeDebianSourceDir
: public pkgIndexFile::Type
406 virtual pkgSrcRecords::Parser
*CreateSrcPkgParser(std::string
const &SourceDir
) const APT_OVERRIDE
408 return new debDscRecordParser(SourceDir
+ std::string("/debian/control"), NULL
);
410 debIFTypeDebianSourceDir() {Label
= "Debian control file";};
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
;
418 // file based pseudo indexes
419 APT_HIDDEN debIFTypeDscFile _apt_DscFile
;
420 APT_HIDDEN debIFTypeDebianSourceDir _apt_DebianSourceDir
;
422 const pkgIndexFile::Type
*debSourcesIndex::GetType() const
426 const pkgIndexFile::Type
*debPackagesIndex::GetType() const
430 const pkgIndexFile::Type
*debTranslationsIndex::GetType() const
434 const pkgIndexFile::Type
*debStatusIndex::GetType() const
438 const pkgIndexFile::Type
*debDebPkgFileIndex::GetType() const
440 return &_apt_DebPkgFile
;
442 const pkgIndexFile::Type
*debDscFileIndex::GetType() const
444 return &_apt_DscFile
;
446 const pkgIndexFile::Type
*debDebianSourceDirIndex::GetType() const
448 return &_apt_DebianSourceDir
;
452 debStatusIndex::~debStatusIndex() {}
453 debPackagesIndex::~debPackagesIndex() {}
454 debTranslationsIndex::~debTranslationsIndex() {}
455 debSourcesIndex::~debSourcesIndex() {}
457 debDebPkgFileIndex::~debDebPkgFileIndex() {}
458 debDscFileIndex::~debDscFileIndex() {}