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();
129 return newError
? NULL
: Parser
;
132 // dpkg/status Index /*{{{*/
133 debStatusIndex::debStatusIndex(std::string
const &File
) : pkgDebianIndexRealFile(File
, true), d(NULL
)
136 std::string
debStatusIndex::GetArchitecture() const
138 return std::string();
140 std::string
debStatusIndex::GetComponent() const
144 uint8_t debStatusIndex::GetIndexFlags() const
146 return pkgCache::Flag::NotSource
;
149 pkgCacheListParser
* debStatusIndex::CreateListParser(FileFd
&Pkg
)
151 if (Pkg
.IsOpen() == false)
153 _error
->PushToStack();
154 pkgCacheListParser
* const Parser
= new debStatusListParser(&Pkg
);
155 bool const newError
= _error
->PendingError();
156 _error
->MergeWithStack();
157 return newError
? NULL
: Parser
;
160 // DebPkgFile Index - a single .deb file as an index /*{{{*/
161 debDebPkgFileIndex::debDebPkgFileIndex(std::string
const &DebFile
)
162 : pkgDebianIndexRealFile(DebFile
, true), d(NULL
), DebFile(DebFile
)
165 bool debDebPkgFileIndex::GetContent(std::ostream
&content
, std::string
const &debfile
)
168 if (stat(debfile
.c_str(), &Buf
) != 0)
171 // get the control data out of the deb file via dpkg-deb -I
172 std::string dpkg
= _config
->Find("Dir::Bin::dpkg","dpkg-deb");
173 std::vector
<const char *> Args
;
174 Args
.push_back(dpkg
.c_str());
175 Args
.push_back("-I");
176 Args
.push_back(debfile
.c_str());
177 Args
.push_back("control");
178 Args
.push_back(NULL
);
181 if(Popen((const char**)&Args
[0], PipeFd
, Child
, FileFd::ReadOnly
) == false)
182 return _error
->Error("Popen failed");
186 unsigned long long actual
= 0;
187 if (PipeFd
.Read(buffer
, sizeof(buffer
)-1, &actual
) == false)
188 return _error
->Errno("read", "Failed to read dpkg pipe");
191 buffer
[actual
] = '\0';
194 ExecWait(Child
, "Popen");
196 content
<< "Filename: " << debfile
<< "\n";
197 content
<< "Size: " << Buf
.st_size
<< "\n";
201 bool debDebPkgFileIndex::OpenListFile(FileFd
&Pkg
, std::string
const &FileName
)
203 // write the control data to a tempfile
204 if (GetTempFile("deb-file-" + flNotDir(FileName
), true, &Pkg
) == NULL
)
206 std::ostringstream content
;
207 if (GetContent(content
, FileName
) == false)
209 std::string
const contentstr
= content
.str();
210 if (contentstr
.empty())
212 if (Pkg
.Write(contentstr
.c_str(), contentstr
.length()) == false || Pkg
.Seek(0) == false)
216 pkgCacheListParser
* debDebPkgFileIndex::CreateListParser(FileFd
&Pkg
)
218 if (Pkg
.IsOpen() == false)
220 _error
->PushToStack();
221 pkgCacheListParser
* const Parser
= new debDebFileParser(&Pkg
, DebFile
);
222 bool const newError
= _error
->PendingError();
223 _error
->MergeWithStack();
224 return newError
? NULL
: Parser
;
226 uint8_t debDebPkgFileIndex::GetIndexFlags() const
228 return pkgCache::Flag::LocalSource
;
230 std::string
debDebPkgFileIndex::GetArchitecture() const
232 return std::string();
234 std::string
debDebPkgFileIndex::GetComponent() const
238 pkgCache::PkgFileIterator
debDebPkgFileIndex::FindInCache(pkgCache
&Cache
) const
240 std::string
const FileName
= IndexFileName();
241 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
242 for (; File
.end() == false; ++File
)
244 if (File
.FileName() == NULL
|| FileName
!= File
.FileName())
246 // we can't do size checks here as file size != content size
252 std::string
debDebPkgFileIndex::ArchiveInfo_impl(pkgCache::VerIterator
const &Ver
) const
254 std::string Res
= IndexFileName() + " ";
255 Res
.append(Ver
.ParentPkg().Name()).append(" ");
256 Res
.append(Ver
.Arch()).append(" ");
257 Res
.append(Ver
.VerStr());
261 // DscFile Index - a single .dsc file as an index /*{{{*/
262 debDscFileIndex::debDscFileIndex(std::string
const &DscFile
)
263 : pkgDebianIndexRealFile(DscFile
, true), d(NULL
)
266 pkgSrcRecords::Parser
*debDscFileIndex::CreateSrcParser() const
268 if (Exists() == false)
270 return new debDscRecordParser(File
, this);
274 // Index File types for Debian /*{{{*/
275 class APT_HIDDEN debIFTypeSrc
: public pkgIndexFile::Type
278 debIFTypeSrc() {Label
= "Debian Source Index";};
280 class APT_HIDDEN debIFTypePkg
: public pkgIndexFile::Type
283 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator
const &File
) const APT_OVERRIDE
285 return new debRecordParser(File
.FileName(),*File
.Cache());
287 debIFTypePkg() {Label
= "Debian Package Index";};
289 class APT_HIDDEN debIFTypeTrans
: public debIFTypePkg
292 debIFTypeTrans() {Label
= "Debian Translation Index";};
294 class APT_HIDDEN debIFTypeStatus
: public pkgIndexFile::Type
297 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator
const &File
) const APT_OVERRIDE
299 return new debRecordParser(File
.FileName(),*File
.Cache());
301 debIFTypeStatus() {Label
= "Debian dpkg status file";};
303 class APT_HIDDEN debIFTypeDebPkgFile
: public pkgIndexFile::Type
306 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator
const &File
) const APT_OVERRIDE
308 return new debDebFileRecordParser(File
.FileName());
310 debIFTypeDebPkgFile() {Label
= "Debian deb file";};
312 class APT_HIDDEN debIFTypeDscFile
: public pkgIndexFile::Type
315 virtual pkgSrcRecords::Parser
*CreateSrcPkgParser(std::string
const &DscFile
) const APT_OVERRIDE
317 return new debDscRecordParser(DscFile
, NULL
);
319 debIFTypeDscFile() {Label
= "Debian dsc file";};
321 class APT_HIDDEN debIFTypeDebianSourceDir
: public pkgIndexFile::Type
324 virtual pkgSrcRecords::Parser
*CreateSrcPkgParser(std::string
const &SourceDir
) const APT_OVERRIDE
326 return new debDscRecordParser(SourceDir
+ std::string("/debian/control"), NULL
);
328 debIFTypeDebianSourceDir() {Label
= "Debian control file";};
331 APT_HIDDEN debIFTypeSrc _apt_Src
;
332 APT_HIDDEN debIFTypePkg _apt_Pkg
;
333 APT_HIDDEN debIFTypeTrans _apt_Trans
;
334 APT_HIDDEN debIFTypeStatus _apt_Status
;
335 APT_HIDDEN debIFTypeDebPkgFile _apt_DebPkgFile
;
336 // file based pseudo indexes
337 APT_HIDDEN debIFTypeDscFile _apt_DscFile
;
338 APT_HIDDEN debIFTypeDebianSourceDir _apt_DebianSourceDir
;
340 const pkgIndexFile::Type
*debSourcesIndex::GetType() const
344 const pkgIndexFile::Type
*debPackagesIndex::GetType() const
348 const pkgIndexFile::Type
*debTranslationsIndex::GetType() const
352 const pkgIndexFile::Type
*debStatusIndex::GetType() const
356 const pkgIndexFile::Type
*debDebPkgFileIndex::GetType() const
358 return &_apt_DebPkgFile
;
360 const pkgIndexFile::Type
*debDscFileIndex::GetType() const
362 return &_apt_DscFile
;
364 const pkgIndexFile::Type
*debDebianSourceDirIndex::GetType() const
366 return &_apt_DebianSourceDir
;
370 debStatusIndex::~debStatusIndex() {}
371 debPackagesIndex::~debPackagesIndex() {}
372 debTranslationsIndex::~debTranslationsIndex() {}
373 debSourcesIndex::~debSourcesIndex() {}
375 debDebPkgFileIndex::~debDebPkgFileIndex() {}
376 debDscFileIndex::~debDscFileIndex() {}