]>
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>
27 #include <sys/types.h>
38 // Global list of Item supported
39 static pkgIndexFile::Type
*ItmList
[10];
40 pkgIndexFile::Type
**pkgIndexFile::Type::GlobalList
= ItmList
;
41 unsigned long pkgIndexFile::Type::GlobalListLen
= 0;
43 // Type::Type - Constructor /*{{{*/
44 // ---------------------------------------------------------------------
46 pkgIndexFile::Type::Type()
48 ItmList
[GlobalListLen
] = this;
53 // Type::GetType - Locate the type by name /*{{{*/
54 // ---------------------------------------------------------------------
56 pkgIndexFile::Type
*pkgIndexFile::Type::GetType(const char *Type
)
58 for (unsigned I
= 0; I
!= GlobalListLen
; I
++)
59 if (strcmp(GlobalList
[I
]->Label
,Type
) == 0)
64 pkgIndexFile::pkgIndexFile(bool const Trusted
) : /*{{{*/
65 d(NULL
), Trusted(Trusted
)
69 // IndexFile::ArchiveInfo - Stub /*{{{*/
70 std::string
pkgIndexFile::ArchiveInfo(pkgCache::VerIterator
const &/*Ver*/) const
75 // IndexFile::FindInCache - Stub /*{{{*/
76 pkgCache::PkgFileIterator
pkgIndexFile::FindInCache(pkgCache
&Cache
) const
78 return pkgCache::PkgFileIterator(Cache
);
81 // IndexFile::SourceIndex - Stub /*{{{*/
82 std::string
pkgIndexFile::SourceInfo(pkgSrcRecords::Parser
const &/*Record*/,
83 pkgSrcRecords::File
const &/*File*/) const
88 // IndexFile::TranslationsAvailable - Check if will use Translation /*{{{*/
89 // ---------------------------------------------------------------------
91 bool pkgIndexFile::TranslationsAvailable() {
92 return (APT::Configuration::getLanguages().empty() != true);
95 // IndexFile::CheckLanguageCode - Check the Language Code /*{{{*/
96 // ---------------------------------------------------------------------
97 /* No intern need for this method anymore as the check for correctness
98 is already done in getLanguages(). Note also that this check is
99 rather bad (doesn't take three character like ast into account).
100 TODO: Remove method with next API break */
101 APT_DEPRECATED
bool pkgIndexFile::CheckLanguageCode(const char * const Lang
)
103 if (strlen(Lang
) == 2 || (strlen(Lang
) == 5 && Lang
[2] == '_'))
106 if (strcmp(Lang
,"C") != 0)
107 _error
->Warning("Wrong language code %s", Lang
);
112 // IndexFile::LanguageCode - Return the Language Code /*{{{*/
113 // ---------------------------------------------------------------------
114 /* As we have now possibly more than one LanguageCode this method is
115 supersided by a) private classmembers or b) getLanguages().
116 TODO: Remove method with next API break */
117 APT_DEPRECATED
std::string
pkgIndexFile::LanguageCode() {
118 if (TranslationsAvailable() == false)
120 return APT::Configuration::getLanguages()[0];
124 // IndexTarget - Constructor /*{{{*/
125 IndexTarget::IndexTarget(std::string
const &MetaKey
, std::string
const &ShortDesc
,
126 std::string
const &LongDesc
, std::string
const &URI
, bool const IsOptional
,
127 bool const KeepCompressed
, std::map
<std::string
, std::string
> const &Options
) :
128 URI(URI
), Description(LongDesc
), ShortDesc(ShortDesc
), MetaKey(MetaKey
),
129 IsOptional(IsOptional
), KeepCompressed(KeepCompressed
), Options(Options
)
133 std::string
IndexTarget::Option(OptionKeys
const EnumKey
) const /*{{{*/
138 #define APT_CASE(X) case X: Key = #X; break
143 APT_CASE(ARCHITECTURE
);
147 APT_CASE(CREATED_BY
);
149 case FILENAME
: return _config
->FindDir("Dir::State::lists") + URItoFileName(URI
);
150 case EXISTING_FILENAME
:
151 std::string
const filename
= Option(FILENAME
);
152 std::vector
<std::string
> const types
= APT::Configuration::getCompressionTypes();
153 for (std::vector
<std::string
>::const_iterator t
= types
.begin(); t
!= types
.end(); ++t
)
157 std::string
const file
= (*t
== "uncompressed") ? filename
: (filename
+ "." + *t
);
158 if (FileExists(file
))
163 std::map
<std::string
,std::string
>::const_iterator
const M
= Options
.find(Key
);
164 if (M
== Options
.end())
169 std::string
IndexTarget::Format(std::string format
) const /*{{{*/
171 for (std::map
<std::string
, std::string
>::const_iterator O
= Options
.begin(); O
!= Options
.end(); ++O
)
173 format
= SubstVar(format
, std::string("$(") + O
->first
+ ")", O
->second
);
175 format
= SubstVar(format
, "$(METAKEY)", MetaKey
);
176 format
= SubstVar(format
, "$(SHORTDESC)", ShortDesc
);
177 format
= SubstVar(format
, "$(DESCRIPTION)", Description
);
178 format
= SubstVar(format
, "$(URI)", URI
);
179 format
= SubstVar(format
, "$(FILENAME)", Option(IndexTarget::FILENAME
));
184 pkgDebianIndexTargetFile::pkgDebianIndexTargetFile(IndexTarget
const &Target
, bool const Trusted
) :/*{{{*/
185 pkgDebianIndexFile(Trusted
), d(NULL
), Target(Target
)
189 std::string
pkgDebianIndexTargetFile::ArchiveURI(std::string
const &File
) const/*{{{*/
191 return Target
.Option(IndexTarget::REPO_URI
) + File
;
194 std::string
pkgDebianIndexTargetFile::Describe(bool const Short
) const /*{{{*/
197 return Target
.Description
;
198 return Target
.Description
+ " (" + IndexFileName() + ")";
201 std::string
pkgDebianIndexTargetFile::IndexFileName() const /*{{{*/
203 std::string
const s
= Target
.Option(IndexTarget::FILENAME
);
207 std::vector
<std::string
> types
= APT::Configuration::getCompressionTypes();
208 for (std::vector
<std::string
>::const_iterator t
= types
.begin(); t
!= types
.end(); ++t
)
210 std::string p
= s
+ '.' + *t
;
217 unsigned long pkgDebianIndexTargetFile::Size() const /*{{{*/
219 unsigned long size
= 0;
221 /* we need to ignore errors here; if the lists are absent, just return 0 */
222 _error
->PushToStack();
224 FileFd
f(IndexFileName(), FileFd::ReadOnly
, FileFd::Extension
);
228 if (_error
->PendingError() == true)
230 _error
->RevertToStack();
235 bool pkgDebianIndexTargetFile::Exists() const /*{{{*/
237 return FileExists(IndexFileName());
240 std::string
pkgDebianIndexTargetFile::GetArchitecture() const /*{{{*/
242 return Target
.Option(IndexTarget::ARCHITECTURE
);
245 std::string
pkgDebianIndexTargetFile::GetComponent() const /*{{{*/
247 return Target
.Option(IndexTarget::COMPONENT
);
250 bool pkgDebianIndexTargetFile::OpenListFile(FileFd
&Pkg
, std::string
const &FileName
)/*{{{*/
252 if (Pkg
.Open(FileName
, FileFd::ReadOnly
, FileFd::Extension
) == false)
253 return _error
->Error("Problem opening %s",FileName
.c_str());
257 std::string
pkgDebianIndexTargetFile::GetProgressDescription() const
259 return Target
.Description
;
262 pkgDebianIndexRealFile::pkgDebianIndexRealFile(std::string
const &File
, bool const Trusted
) :/*{{{*/
263 pkgDebianIndexFile(Trusted
), d(NULL
), File(flAbsPath(File
))
267 // IndexRealFile::Size - Return the size of the index /*{{{*/
268 unsigned long pkgDebianIndexRealFile::Size() const
271 if (stat(File
.c_str(),&S
) != 0)
276 bool pkgDebianIndexRealFile::Exists() const /*{{{*/
278 return FileExists(File
);
281 std::string
pkgDebianIndexRealFile::Describe(bool const /*Short*/) const/*{{{*/
286 std::string
pkgDebianIndexRealFile::ArchiveURI(std::string
const &/*File*/) const/*{{{*/
288 return "file:" + File
;
291 std::string
pkgDebianIndexRealFile::IndexFileName() const /*{{{*/
296 std::string
pkgDebianIndexRealFile::GetProgressDescription() const
300 bool pkgDebianIndexRealFile::OpenListFile(FileFd
&Pkg
, std::string
const &FileName
)/*{{{*/
302 if (Pkg
.Open(FileName
, FileFd::ReadOnly
, FileFd::None
) == false)
303 return _error
->Error("Problem opening %s",FileName
.c_str());
308 pkgDebianIndexFile::pkgDebianIndexFile(bool const Trusted
) : pkgIndexFile(Trusted
)
311 pkgDebianIndexFile::~pkgDebianIndexFile()
314 pkgCacheListParser
* pkgDebianIndexFile::CreateListParser(FileFd
&Pkg
)
316 if (Pkg
.IsOpen() == false)
318 _error
->PushToStack();
319 pkgCacheListParser
* const Parser
= new debListParser(&Pkg
);
320 bool const newError
= _error
->PendingError();
321 _error
->MergeWithStack();
322 return newError
? NULL
: Parser
;
324 bool pkgDebianIndexFile::Merge(pkgCacheGenerator
&Gen
,OpProgress
* const Prog
)
326 std::string
const PackageFile
= IndexFileName();
328 if (OpenListFile(Pkg
, PackageFile
) == false)
330 _error
->PushToStack();
331 std::unique_ptr
<pkgCacheListParser
> Parser(CreateListParser(Pkg
));
332 bool const newError
= _error
->PendingError();
333 if (newError
== false && Parser
== nullptr)
339 Prog
->SubProgress(0, GetProgressDescription());
341 if (Gen
.SelectFile(PackageFile
, *this, GetArchitecture(), GetComponent(), GetIndexFlags()) == false)
342 return _error
->Error("Problem with SelectFile %s",PackageFile
.c_str());
344 // Store the IMS information
345 pkgCache::PkgFileIterator File
= Gen
.GetCurFile();
346 pkgCacheGenerator::Dynamic
<pkgCache::PkgFileIterator
> DynFile(File
);
347 File
->Size
= Pkg
.FileSize();
348 File
->mtime
= Pkg
.ModificationTime();
350 if (Gen
.MergeList(*Parser
) == false)
351 return _error
->Error("Problem with MergeList %s",PackageFile
.c_str());
354 pkgCache::PkgFileIterator
pkgDebianIndexFile::FindInCache(pkgCache
&Cache
) const
356 std::string
const FileName
= IndexFileName();
357 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
358 for (; File
.end() == false; ++File
)
360 if (File
.FileName() == NULL
|| FileName
!= File
.FileName())
364 if (stat(File
.FileName(),&St
) != 0)
366 if (_config
->FindB("Debug::pkgCacheGen", false))
367 std::clog
<< "DebianIndexFile::FindInCache - stat failed on " << File
.FileName() << std::endl
;
368 return pkgCache::PkgFileIterator(Cache
);
370 if ((map_filesize_t
)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
372 if (_config
->FindB("Debug::pkgCacheGen", false))
373 std::clog
<< "DebianIndexFile::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
374 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
375 << ") doesn't match for " << File
.FileName() << std::endl
;
376 return pkgCache::PkgFileIterator(Cache
);
384 APT_CONST
pkgIndexFile::~pkgIndexFile() {}
385 APT_CONST
pkgDebianIndexTargetFile::~pkgDebianIndexTargetFile() {}
386 APT_CONST
pkgDebianIndexRealFile::~pkgDebianIndexRealFile() {}