]> git.saurik.com Git - apt.git/blame - apt-pkg/indexfile.cc
CMake: Add coverage build type
[apt.git] / apt-pkg / indexfile.cc
CommitLineData
b2e465d6
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
7db98ffc 3// $Id: indexfile.cc,v 1.2.2.1 2003/12/24 23:09:17 mdz Exp $
b2e465d6
AL
4/* ######################################################################
5
6 Index File - Abstraction for an index of archive/souce file.
7
8 ##################################################################### */
9 /*}}}*/
10// Include Files /*{{{*/
ea542140
DK
11#include<config.h>
12
e3c1cfc7 13#include <apt-pkg/configuration.h>
b2e465d6
AL
14#include <apt-pkg/indexfile.h>
15#include <apt-pkg/error.h>
e3c1cfc7 16#include <apt-pkg/fileutl.h>
45df0ad2 17#include <apt-pkg/aptconfiguration.h>
453b82a3 18#include <apt-pkg/pkgcache.h>
c9443c01 19#include <apt-pkg/pkgcachegen.h>
453b82a3
DK
20#include <apt-pkg/cacheiterators.h>
21#include <apt-pkg/srcrecords.h>
d7a51997 22#include <apt-pkg/strutl.h>
c9443c01 23#include <apt-pkg/progress.h>
64b66a46 24#include <apt-pkg/deblistparser.h>
453b82a3 25#include <apt-pkg/macros.h>
a52f938b 26
64b66a46 27#include <apt-pkg/debindexfile.h>
c9443c01 28
c9443c01 29#include <sys/stat.h>
c9443c01 30
453b82a3
DK
31#include <string>
32#include <vector>
a52f938b 33#include <clocale>
4f333a8b 34#include <cstring>
c9443c01 35#include <memory>
b2e465d6
AL
36 /*}}}*/
37
38// Global list of Item supported
39static pkgIndexFile::Type *ItmList[10];
40pkgIndexFile::Type **pkgIndexFile::Type::GlobalList = ItmList;
41unsigned long pkgIndexFile::Type::GlobalListLen = 0;
42
43// Type::Type - Constructor /*{{{*/
44// ---------------------------------------------------------------------
45/* */
46pkgIndexFile::Type::Type()
47{
48 ItmList[GlobalListLen] = this;
dd688285
DK
49 GlobalListLen++;
50 Label = NULL;
b2e465d6
AL
51}
52 /*}}}*/
53// Type::GetType - Locate the type by name /*{{{*/
54// ---------------------------------------------------------------------
55/* */
56pkgIndexFile::Type *pkgIndexFile::Type::GetType(const char *Type)
57{
58 for (unsigned I = 0; I != GlobalListLen; I++)
59 if (strcmp(GlobalList[I]->Label,Type) == 0)
60 return GlobalList[I];
61 return 0;
e3c1cfc7
DK
62}
63 /*}}}*/
c9443c01 64pkgIndexFile::pkgIndexFile(bool const Trusted) : /*{{{*/
6c55f07a 65 d(NULL), Trusted(Trusted)
e3c1cfc7 66{
b2e465d6
AL
67}
68 /*}}}*/
b2e465d6 69// IndexFile::ArchiveInfo - Stub /*{{{*/
64b66a46 70std::string pkgIndexFile::ArchiveInfo(pkgCache::VerIterator const &Ver) const
b2e465d6 71{
64b66a46
DK
72 debDebPkgFileIndex const * const debfile = dynamic_cast<debDebPkgFileIndex const*>(this);
73 if (debfile != nullptr)
74 return debfile->ArchiveInfo_impl(Ver);
8f3ba4e8 75 return std::string();
b2e465d6
AL
76}
77 /*}}}*/
78// IndexFile::FindInCache - Stub /*{{{*/
b2e465d6
AL
79pkgCache::PkgFileIterator pkgIndexFile::FindInCache(pkgCache &Cache) const
80{
81 return pkgCache::PkgFileIterator(Cache);
82}
83 /*}}}*/
84// IndexFile::SourceIndex - Stub /*{{{*/
65512241
DK
85std::string pkgIndexFile::SourceInfo(pkgSrcRecords::Parser const &/*Record*/,
86 pkgSrcRecords::File const &/*File*/) const
b2e465d6 87{
8f3ba4e8 88 return std::string();
b2e465d6
AL
89}
90 /*}}}*/
45df0ad2 91// IndexFile::TranslationsAvailable - Check if will use Translation /*{{{*/
45df0ad2
DK
92bool pkgIndexFile::TranslationsAvailable() {
93 return (APT::Configuration::getLanguages().empty() != true);
a52f938b
OS
94}
95 /*}}}*/
45df0ad2 96// IndexFile::CheckLanguageCode - Check the Language Code /*{{{*/
5dd00edb 97bool pkgIndexFile::CheckLanguageCode(const char * const Lang)
a52f938b
OS
98{
99 if (strlen(Lang) == 2 || (strlen(Lang) == 5 && Lang[2] == '_'))
100 return true;
101
102 if (strcmp(Lang,"C") != 0)
103 _error->Warning("Wrong language code %s", Lang);
104
105 return false;
106}
107 /*}}}*/
45df0ad2 108// IndexFile::LanguageCode - Return the Language Code /*{{{*/
5dd00edb 109std::string pkgIndexFile::LanguageCode() {
668ae33a 110APT_IGNORE_DEPRECATED_PUSH
45df0ad2
DK
111 if (TranslationsAvailable() == false)
112 return "";
113 return APT::Configuration::getLanguages()[0];
668ae33a 114APT_IGNORE_DEPRECATED_POP
a52f938b
OS
115}
116 /*}}}*/
e3c1cfc7
DK
117
118// IndexTarget - Constructor /*{{{*/
119IndexTarget::IndexTarget(std::string const &MetaKey, std::string const &ShortDesc,
120 std::string const &LongDesc, std::string const &URI, bool const IsOptional,
653ef26c
DK
121 bool const KeepCompressed, std::map<std::string, std::string> const &Options) :
122 URI(URI), Description(LongDesc), ShortDesc(ShortDesc), MetaKey(MetaKey),
123 IsOptional(IsOptional), KeepCompressed(KeepCompressed), Options(Options)
e3c1cfc7
DK
124{
125}
126 /*}}}*/
001c76fe 127std::string IndexTarget::Option(OptionKeys const EnumKey) const /*{{{*/
e3c1cfc7 128{
001c76fe
DK
129 std::string Key;
130 switch (EnumKey)
131 {
132#define APT_CASE(X) case X: Key = #X; break
133 APT_CASE(SITE);
134 APT_CASE(RELEASE);
135 APT_CASE(COMPONENT);
136 APT_CASE(LANGUAGE);
137 APT_CASE(ARCHITECTURE);
138 APT_CASE(BASE_URI);
139 APT_CASE(REPO_URI);
39c724b4 140 APT_CASE(IDENTIFIER);
8881b11e 141 APT_CASE(TARGET_OF);
001c76fe 142 APT_CASE(CREATED_BY);
7f2d1eef 143 APT_CASE(FALLBACK_OF);
1a3a14ac 144 APT_CASE(PDIFFS);
9adb9778 145 APT_CASE(DEFAULTENABLED);
d7a51997 146 APT_CASE(COMPRESSIONTYPES);
3090ae69 147 APT_CASE(SOURCESENTRY);
24e8f24e 148 APT_CASE(BY_HASH);
0179cfa8 149 APT_CASE(KEEPCOMPRESSEDAS);
d03b947b
DK
150 APT_CASE(ALLOW_INSECURE);
151 APT_CASE(ALLOW_WEAK);
152 APT_CASE(ALLOW_DOWNGRADE_TO_INSECURE);
001c76fe 153#undef APT_CASE
8bd823d0
DK
154 case FILENAME:
155 {
156 auto const M = Options.find("FILENAME");
157 if (M == Options.end())
158 return _config->FindDir("Dir::State::lists") + URItoFileName(URI);
159 return M->second;
160 }
3fd89e62
DK
161 case EXISTING_FILENAME:
162 std::string const filename = Option(FILENAME);
d7a51997 163 std::vector<std::string> const types = VectorizeString(Option(COMPRESSIONTYPES), ' ');
3fd89e62
DK
164 for (std::vector<std::string>::const_iterator t = types.begin(); t != types.end(); ++t)
165 {
166 if (t->empty())
167 continue;
168 std::string const file = (*t == "uncompressed") ? filename : (filename + "." + *t);
169 if (FileExists(file))
170 return file;
171 }
172 return "";
001c76fe 173 }
e3c1cfc7
DK
174 std::map<std::string,std::string>::const_iterator const M = Options.find(Key);
175 if (M == Options.end())
176 return "";
177 return M->second;
178}
179 /*}}}*/
1a3a14ac
DK
180bool IndexTarget::OptionBool(OptionKeys const EnumKey) const /*{{{*/
181{
d03b947b 182 return StringToBool(Option(EnumKey), false);
1a3a14ac
DK
183}
184 /*}}}*/
8881b11e
DK
185std::string IndexTarget::Format(std::string format) const /*{{{*/
186{
187 for (std::map<std::string, std::string>::const_iterator O = Options.begin(); O != Options.end(); ++O)
188 {
189 format = SubstVar(format, std::string("$(") + O->first + ")", O->second);
190 }
191 format = SubstVar(format, "$(METAKEY)", MetaKey);
192 format = SubstVar(format, "$(SHORTDESC)", ShortDesc);
193 format = SubstVar(format, "$(DESCRIPTION)", Description);
194 format = SubstVar(format, "$(URI)", URI);
195 format = SubstVar(format, "$(FILENAME)", Option(IndexTarget::FILENAME));
196 return format;
197}
198 /*}}}*/
e3c1cfc7 199
c9443c01
DK
200pkgDebianIndexTargetFile::pkgDebianIndexTargetFile(IndexTarget const &Target, bool const Trusted) :/*{{{*/
201 pkgDebianIndexFile(Trusted), d(NULL), Target(Target)
e3c1cfc7
DK
202{
203}
204 /*}}}*/
c9443c01 205std::string pkgDebianIndexTargetFile::ArchiveURI(std::string const &File) const/*{{{*/
e3c1cfc7 206{
001c76fe 207 return Target.Option(IndexTarget::REPO_URI) + File;
e3c1cfc7
DK
208}
209 /*}}}*/
c9443c01 210std::string pkgDebianIndexTargetFile::Describe(bool const Short) const /*{{{*/
e3c1cfc7
DK
211{
212 if (Short)
213 return Target.Description;
214 return Target.Description + " (" + IndexFileName() + ")";
215}
216 /*}}}*/
c9443c01 217std::string pkgDebianIndexTargetFile::IndexFileName() const /*{{{*/
e3c1cfc7 218{
8881b11e 219 std::string const s = Target.Option(IndexTarget::FILENAME);
e3c1cfc7
DK
220 if (FileExists(s))
221 return s;
222
d7a51997 223 std::vector<std::string> const types = VectorizeString(Target.Option(IndexTarget::COMPRESSIONTYPES), ' ');
e3c1cfc7
DK
224 for (std::vector<std::string>::const_iterator t = types.begin(); t != types.end(); ++t)
225 {
226 std::string p = s + '.' + *t;
227 if (FileExists(p))
228 return p;
229 }
230 return s;
231}
232 /*}}}*/
c9443c01 233unsigned long pkgDebianIndexTargetFile::Size() const /*{{{*/
e3c1cfc7
DK
234{
235 unsigned long size = 0;
236
237 /* we need to ignore errors here; if the lists are absent, just return 0 */
238 _error->PushToStack();
239
240 FileFd f(IndexFileName(), FileFd::ReadOnly, FileFd::Extension);
241 if (!f.Failed())
242 size = f.Size();
243
244 if (_error->PendingError() == true)
245 size = 0;
246 _error->RevertToStack();
247
248 return size;
249}
250 /*}}}*/
c9443c01 251bool pkgDebianIndexTargetFile::Exists() const /*{{{*/
e3c1cfc7
DK
252{
253 return FileExists(IndexFileName());
254}
255 /*}}}*/
c9443c01
DK
256std::string pkgDebianIndexTargetFile::GetArchitecture() const /*{{{*/
257{
258 return Target.Option(IndexTarget::ARCHITECTURE);
259}
260 /*}}}*/
261std::string pkgDebianIndexTargetFile::GetComponent() const /*{{{*/
262{
263 return Target.Option(IndexTarget::COMPONENT);
264}
265 /*}}}*/
266bool pkgDebianIndexTargetFile::OpenListFile(FileFd &Pkg, std::string const &FileName)/*{{{*/
267{
268 if (Pkg.Open(FileName, FileFd::ReadOnly, FileFd::Extension) == false)
269 return _error->Error("Problem opening %s",FileName.c_str());
270 return true;
271}
272 /*}}}*/
273std::string pkgDebianIndexTargetFile::GetProgressDescription() const
274{
275 return Target.Description;
276}
277
7f58427b
DK
278pkgDebianIndexRealFile::pkgDebianIndexRealFile(std::string const &pFile, bool const Trusted) :/*{{{*/
279 pkgDebianIndexFile(Trusted), d(NULL)
c9443c01 280{
a249b3e6
DK
281 if (pFile.empty())
282 ;
283 else if (pFile == "/nonexistent/stdin")
7f58427b
DK
284 File = pFile;
285 else
286 File = flAbsPath(pFile);
c9443c01
DK
287}
288 /*}}}*/
289// IndexRealFile::Size - Return the size of the index /*{{{*/
290unsigned long pkgDebianIndexRealFile::Size() const
291{
292 struct stat S;
293 if (stat(File.c_str(),&S) != 0)
294 return 0;
295 return S.st_size;
296}
297 /*}}}*/
298bool pkgDebianIndexRealFile::Exists() const /*{{{*/
299{
300 return FileExists(File);
301}
302 /*}}}*/
303std::string pkgDebianIndexRealFile::Describe(bool const /*Short*/) const/*{{{*/
304{
305 return File;
306}
307 /*}}}*/
308std::string pkgDebianIndexRealFile::ArchiveURI(std::string const &/*File*/) const/*{{{*/
309{
310 return "file:" + File;
311}
312 /*}}}*/
313std::string pkgDebianIndexRealFile::IndexFileName() const /*{{{*/
314{
315 return File;
316}
317 /*}}}*/
318std::string pkgDebianIndexRealFile::GetProgressDescription() const
319{
320 return File;
321}
322bool pkgDebianIndexRealFile::OpenListFile(FileFd &Pkg, std::string const &FileName)/*{{{*/
323{
f41352ad 324 if (Pkg.Open(FileName, FileFd::ReadOnly, FileFd::Extension) == false)
c9443c01
DK
325 return _error->Error("Problem opening %s",FileName.c_str());
326 return true;
327}
328 /*}}}*/
329
330pkgDebianIndexFile::pkgDebianIndexFile(bool const Trusted) : pkgIndexFile(Trusted)
331{
332}
333pkgDebianIndexFile::~pkgDebianIndexFile()
334{
335}
336pkgCacheListParser * pkgDebianIndexFile::CreateListParser(FileFd &Pkg)
337{
338 if (Pkg.IsOpen() == false)
c48ef894 339 return nullptr;
c9443c01 340 _error->PushToStack();
7f8c0eed 341 pkgCacheListParser * const Parser = new debListParser(&Pkg);
c9443c01
DK
342 bool const newError = _error->PendingError();
343 _error->MergeWithStack();
c48ef894
DK
344 if (newError)
345 {
346 delete Parser;
347 return nullptr;
348 }
349 else
350 return Parser;
c9443c01
DK
351}
352bool pkgDebianIndexFile::Merge(pkgCacheGenerator &Gen,OpProgress * const Prog)
353{
354 std::string const PackageFile = IndexFileName();
355 FileFd Pkg;
356 if (OpenListFile(Pkg, PackageFile) == false)
357 return false;
358 _error->PushToStack();
359 std::unique_ptr<pkgCacheListParser> Parser(CreateListParser(Pkg));
360 bool const newError = _error->PendingError();
95278287 361 _error->MergeWithStack();
c9443c01
DK
362 if (newError == false && Parser == nullptr)
363 return true;
364 if (Parser == NULL)
365 return false;
366
367 if (Prog != NULL)
368 Prog->SubProgress(0, GetProgressDescription());
369
370 if (Gen.SelectFile(PackageFile, *this, GetArchitecture(), GetComponent(), GetIndexFlags()) == false)
371 return _error->Error("Problem with SelectFile %s",PackageFile.c_str());
372
373 // Store the IMS information
374 pkgCache::PkgFileIterator File = Gen.GetCurFile();
375 pkgCacheGenerator::Dynamic<pkgCache::PkgFileIterator> DynFile(File);
376 File->Size = Pkg.FileSize();
377 File->mtime = Pkg.ModificationTime();
378
379 if (Gen.MergeList(*Parser) == false)
380 return _error->Error("Problem with MergeList %s",PackageFile.c_str());
381 return true;
382}
383pkgCache::PkgFileIterator pkgDebianIndexFile::FindInCache(pkgCache &Cache) const
384{
385 std::string const FileName = IndexFileName();
386 pkgCache::PkgFileIterator File = Cache.FileBegin();
387 for (; File.end() == false; ++File)
388 {
389 if (File.FileName() == NULL || FileName != File.FileName())
390 continue;
391
392 struct stat St;
393 if (stat(File.FileName(),&St) != 0)
394 {
395 if (_config->FindB("Debug::pkgCacheGen", false))
396 std::clog << "DebianIndexFile::FindInCache - stat failed on " << File.FileName() << std::endl;
397 return pkgCache::PkgFileIterator(Cache);
398 }
399 if ((map_filesize_t)St.st_size != File->Size || St.st_mtime != File->mtime)
400 {
401 if (_config->FindB("Debug::pkgCacheGen", false))
402 std::clog << "DebianIndexFile::FindInCache - size (" << St.st_size << " <> " << File->Size
403 << ") or mtime (" << St.st_mtime << " <> " << File->mtime
404 << ") doesn't match for " << File.FileName() << std::endl;
405 return pkgCache::PkgFileIterator(Cache);
406 }
407 return File;
408 }
409
410 return File;
411}
c8a4ce6c
DK
412
413APT_CONST pkgIndexFile::~pkgIndexFile() {}
c9443c01
DK
414APT_CONST pkgDebianIndexTargetFile::~pkgDebianIndexTargetFile() {}
415APT_CONST pkgDebianIndexRealFile::~pkgDebianIndexRealFile() {}