]>
git.saurik.com Git - apt.git/blob - apt-pkg/indexfile.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: indexfile.cc,v 1.2.2.1 2003/12/24 23:09:17 mdz Exp $
4 /* ######################################################################
6 Index File - Abstraction for an index of archive/souce file.
8 ##################################################################### */
10 // Include Files /*{{{*/
13 #include <apt-pkg/configuration.h>
14 #include <apt-pkg/indexfile.h>
15 #include <apt-pkg/error.h>
16 #include <apt-pkg/fileutl.h>
17 #include <apt-pkg/aptconfiguration.h>
18 #include <apt-pkg/pkgcache.h>
19 #include <apt-pkg/pkgcachegen.h>
20 #include <apt-pkg/cacheiterators.h>
21 #include <apt-pkg/srcrecords.h>
22 #include <apt-pkg/progress.h>
23 #include <apt-pkg/macros.h>
25 #include <apt-pkg/deblistparser.h>
36 // Global list of Item supported
37 static pkgIndexFile::Type
*ItmList
[10];
38 pkgIndexFile::Type
**pkgIndexFile::Type::GlobalList
= ItmList
;
39 unsigned long pkgIndexFile::Type::GlobalListLen
= 0;
41 // Type::Type - Constructor /*{{{*/
42 // ---------------------------------------------------------------------
44 pkgIndexFile::Type::Type()
46 ItmList
[GlobalListLen
] = this;
51 // Type::GetType - Locate the type by name /*{{{*/
52 // ---------------------------------------------------------------------
54 pkgIndexFile::Type
*pkgIndexFile::Type::GetType(const char *Type
)
56 for (unsigned I
= 0; I
!= GlobalListLen
; I
++)
57 if (strcmp(GlobalList
[I
]->Label
,Type
) == 0)
62 pkgIndexFile::pkgIndexFile(bool const Trusted
) : /*{{{*/
63 d(NULL
), Trusted(Trusted
)
67 // IndexFile::ArchiveInfo - Stub /*{{{*/
68 std::string
pkgIndexFile::ArchiveInfo(pkgCache::VerIterator
const &/*Ver*/) const
73 // IndexFile::FindInCache - Stub /*{{{*/
74 pkgCache::PkgFileIterator
pkgIndexFile::FindInCache(pkgCache
&Cache
) const
76 return pkgCache::PkgFileIterator(Cache
);
79 // IndexFile::SourceIndex - Stub /*{{{*/
80 std::string
pkgIndexFile::SourceInfo(pkgSrcRecords::Parser
const &/*Record*/,
81 pkgSrcRecords::File
const &/*File*/) const
86 // IndexFile::TranslationsAvailable - Check if will use Translation /*{{{*/
87 // ---------------------------------------------------------------------
89 bool pkgIndexFile::TranslationsAvailable() {
90 return (APT::Configuration::getLanguages().empty() != true);
93 // IndexFile::CheckLanguageCode - Check the Language Code /*{{{*/
94 // ---------------------------------------------------------------------
95 /* No intern need for this method anymore as the check for correctness
96 is already done in getLanguages(). Note also that this check is
97 rather bad (doesn't take three character like ast into account).
98 TODO: Remove method with next API break */
99 APT_DEPRECATED
bool pkgIndexFile::CheckLanguageCode(const char * const Lang
)
101 if (strlen(Lang
) == 2 || (strlen(Lang
) == 5 && Lang
[2] == '_'))
104 if (strcmp(Lang
,"C") != 0)
105 _error
->Warning("Wrong language code %s", Lang
);
110 // IndexFile::LanguageCode - Return the Language Code /*{{{*/
111 // ---------------------------------------------------------------------
112 /* As we have now possibly more than one LanguageCode this method is
113 supersided by a) private classmembers or b) getLanguages().
114 TODO: Remove method with next API break */
115 APT_DEPRECATED
std::string
pkgIndexFile::LanguageCode() {
116 if (TranslationsAvailable() == false)
118 return APT::Configuration::getLanguages()[0];
122 // IndexTarget - Constructor /*{{{*/
123 IndexTarget::IndexTarget(std::string
const &MetaKey
, std::string
const &ShortDesc
,
124 std::string
const &LongDesc
, std::string
const &URI
, bool const IsOptional
,
125 bool const KeepCompressed
, std::map
<std::string
, std::string
> const &Options
) :
126 URI(URI
), Description(LongDesc
), ShortDesc(ShortDesc
), MetaKey(MetaKey
),
127 IsOptional(IsOptional
), KeepCompressed(KeepCompressed
), Options(Options
)
131 std::string
IndexTarget::Option(OptionKeys
const EnumKey
) const /*{{{*/
136 #define APT_CASE(X) case X: Key = #X; break
141 APT_CASE(ARCHITECTURE
);
145 APT_CASE(CREATED_BY
);
147 case FILENAME
: return _config
->FindDir("Dir::State::lists") + URItoFileName(URI
);
148 case EXISTING_FILENAME
:
149 std::string
const filename
= Option(FILENAME
);
150 std::vector
<std::string
> const types
= APT::Configuration::getCompressionTypes();
151 for (std::vector
<std::string
>::const_iterator t
= types
.begin(); t
!= types
.end(); ++t
)
155 std::string
const file
= (*t
== "uncompressed") ? filename
: (filename
+ "." + *t
);
156 if (FileExists(file
))
161 std::map
<std::string
,std::string
>::const_iterator
const M
= Options
.find(Key
);
162 if (M
== Options
.end())
167 std::string
IndexTarget::Format(std::string format
) const /*{{{*/
169 for (std::map
<std::string
, std::string
>::const_iterator O
= Options
.begin(); O
!= Options
.end(); ++O
)
171 format
= SubstVar(format
, std::string("$(") + O
->first
+ ")", O
->second
);
173 format
= SubstVar(format
, "$(METAKEY)", MetaKey
);
174 format
= SubstVar(format
, "$(SHORTDESC)", ShortDesc
);
175 format
= SubstVar(format
, "$(DESCRIPTION)", Description
);
176 format
= SubstVar(format
, "$(URI)", URI
);
177 format
= SubstVar(format
, "$(FILENAME)", Option(IndexTarget::FILENAME
));
182 pkgDebianIndexTargetFile::pkgDebianIndexTargetFile(IndexTarget
const &Target
, bool const Trusted
) :/*{{{*/
183 pkgDebianIndexFile(Trusted
), d(NULL
), Target(Target
)
187 std::string
pkgDebianIndexTargetFile::ArchiveURI(std::string
const &File
) const/*{{{*/
189 return Target
.Option(IndexTarget::REPO_URI
) + File
;
192 std::string
pkgDebianIndexTargetFile::Describe(bool const Short
) const /*{{{*/
195 return Target
.Description
;
196 return Target
.Description
+ " (" + IndexFileName() + ")";
199 std::string
pkgDebianIndexTargetFile::IndexFileName() const /*{{{*/
201 std::string
const s
= Target
.Option(IndexTarget::FILENAME
);
205 std::vector
<std::string
> types
= APT::Configuration::getCompressionTypes();
206 for (std::vector
<std::string
>::const_iterator t
= types
.begin(); t
!= types
.end(); ++t
)
208 std::string p
= s
+ '.' + *t
;
215 unsigned long pkgDebianIndexTargetFile::Size() const /*{{{*/
217 unsigned long size
= 0;
219 /* we need to ignore errors here; if the lists are absent, just return 0 */
220 _error
->PushToStack();
222 FileFd
f(IndexFileName(), FileFd::ReadOnly
, FileFd::Extension
);
226 if (_error
->PendingError() == true)
228 _error
->RevertToStack();
233 bool pkgDebianIndexTargetFile::Exists() const /*{{{*/
235 return FileExists(IndexFileName());
238 std::string
pkgDebianIndexTargetFile::GetArchitecture() const /*{{{*/
240 return Target
.Option(IndexTarget::ARCHITECTURE
);
243 std::string
pkgDebianIndexTargetFile::GetComponent() const /*{{{*/
245 return Target
.Option(IndexTarget::COMPONENT
);
248 bool pkgDebianIndexTargetFile::OpenListFile(FileFd
&Pkg
, std::string
const &FileName
)/*{{{*/
250 if (Pkg
.Open(FileName
, FileFd::ReadOnly
, FileFd::Extension
) == false)
251 return _error
->Error("Problem opening %s",FileName
.c_str());
255 std::string
pkgDebianIndexTargetFile::GetProgressDescription() const
257 return Target
.Description
;
260 pkgDebianIndexRealFile::pkgDebianIndexRealFile(std::string
const &File
, bool const Trusted
) :/*{{{*/
261 pkgDebianIndexFile(Trusted
), d(NULL
), File(flAbsPath(File
))
265 // IndexRealFile::Size - Return the size of the index /*{{{*/
266 unsigned long pkgDebianIndexRealFile::Size() const
269 if (stat(File
.c_str(),&S
) != 0)
274 bool pkgDebianIndexRealFile::Exists() const /*{{{*/
276 return FileExists(File
);
279 std::string
pkgDebianIndexRealFile::Describe(bool const /*Short*/) const/*{{{*/
284 std::string
pkgDebianIndexRealFile::ArchiveURI(std::string
const &/*File*/) const/*{{{*/
286 return "file:" + File
;
289 std::string
pkgDebianIndexRealFile::IndexFileName() const /*{{{*/
294 std::string
pkgDebianIndexRealFile::GetProgressDescription() const
298 bool pkgDebianIndexRealFile::OpenListFile(FileFd
&Pkg
, std::string
const &FileName
)/*{{{*/
300 if (Pkg
.Open(FileName
, FileFd::ReadOnly
, FileFd::None
) == false)
301 return _error
->Error("Problem opening %s",FileName
.c_str());
306 pkgDebianIndexFile::pkgDebianIndexFile(bool const Trusted
) : pkgIndexFile(Trusted
)
309 pkgDebianIndexFile::~pkgDebianIndexFile()
312 pkgCacheListParser
* pkgDebianIndexFile::CreateListParser(FileFd
&Pkg
)
314 if (Pkg
.IsOpen() == false)
316 _error
->PushToStack();
317 pkgCacheListParser
* const Parser
= new debListParser(&Pkg
);
318 bool const newError
= _error
->PendingError();
319 _error
->MergeWithStack();
320 return newError
? NULL
: Parser
;
322 bool pkgDebianIndexFile::Merge(pkgCacheGenerator
&Gen
,OpProgress
* const Prog
)
324 std::string
const PackageFile
= IndexFileName();
326 if (OpenListFile(Pkg
, PackageFile
) == false)
328 _error
->PushToStack();
329 std::unique_ptr
<pkgCacheListParser
> Parser(CreateListParser(Pkg
));
330 bool const newError
= _error
->PendingError();
331 if (newError
== false && Parser
== nullptr)
337 Prog
->SubProgress(0, GetProgressDescription());
339 if (Gen
.SelectFile(PackageFile
, *this, GetArchitecture(), GetComponent(), GetIndexFlags()) == false)
340 return _error
->Error("Problem with SelectFile %s",PackageFile
.c_str());
342 // Store the IMS information
343 pkgCache::PkgFileIterator File
= Gen
.GetCurFile();
344 pkgCacheGenerator::Dynamic
<pkgCache::PkgFileIterator
> DynFile(File
);
345 File
->Size
= Pkg
.FileSize();
346 File
->mtime
= Pkg
.ModificationTime();
348 if (Gen
.MergeList(*Parser
) == false)
349 return _error
->Error("Problem with MergeList %s",PackageFile
.c_str());
352 pkgCache::PkgFileIterator
pkgDebianIndexFile::FindInCache(pkgCache
&Cache
) const
354 std::string
const FileName
= IndexFileName();
355 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
356 for (; File
.end() == false; ++File
)
358 if (File
.FileName() == NULL
|| FileName
!= File
.FileName())
362 if (stat(File
.FileName(),&St
) != 0)
364 if (_config
->FindB("Debug::pkgCacheGen", false))
365 std::clog
<< "DebianIndexFile::FindInCache - stat failed on " << File
.FileName() << std::endl
;
366 return pkgCache::PkgFileIterator(Cache
);
368 if ((map_filesize_t
)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
370 if (_config
->FindB("Debug::pkgCacheGen", false))
371 std::clog
<< "DebianIndexFile::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
372 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
373 << ") doesn't match for " << File
.FileName() << std::endl
;
374 return pkgCache::PkgFileIterator(Cache
);
382 APT_CONST
pkgIndexFile::~pkgIndexFile() {}
383 APT_CONST
pkgDebianIndexTargetFile::~pkgDebianIndexTargetFile() {}
384 APT_CONST
pkgDebianIndexRealFile::~pkgDebianIndexRealFile() {}