]> git.saurik.com Git - apt.git/blame - apt-pkg/indexfile.cc
./prepare-release pre-export
[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() {
45df0ad2
DK
110 if (TranslationsAvailable() == false)
111 return "";
112 return APT::Configuration::getLanguages()[0];
a52f938b
OS
113}
114 /*}}}*/
e3c1cfc7
DK
115
116// IndexTarget - Constructor /*{{{*/
117IndexTarget::IndexTarget(std::string const &MetaKey, std::string const &ShortDesc,
118 std::string const &LongDesc, std::string const &URI, bool const IsOptional,
653ef26c
DK
119 bool const KeepCompressed, std::map<std::string, std::string> const &Options) :
120 URI(URI), Description(LongDesc), ShortDesc(ShortDesc), MetaKey(MetaKey),
121 IsOptional(IsOptional), KeepCompressed(KeepCompressed), Options(Options)
e3c1cfc7
DK
122{
123}
124 /*}}}*/
001c76fe 125std::string IndexTarget::Option(OptionKeys const EnumKey) const /*{{{*/
e3c1cfc7 126{
001c76fe
DK
127 std::string Key;
128 switch (EnumKey)
129 {
130#define APT_CASE(X) case X: Key = #X; break
131 APT_CASE(SITE);
132 APT_CASE(RELEASE);
133 APT_CASE(COMPONENT);
134 APT_CASE(LANGUAGE);
135 APT_CASE(ARCHITECTURE);
136 APT_CASE(BASE_URI);
137 APT_CASE(REPO_URI);
8881b11e 138 APT_CASE(TARGET_OF);
001c76fe 139 APT_CASE(CREATED_BY);
1a3a14ac 140 APT_CASE(PDIFFS);
9adb9778 141 APT_CASE(DEFAULTENABLED);
d7a51997 142 APT_CASE(COMPRESSIONTYPES);
3090ae69 143 APT_CASE(SOURCESENTRY);
24e8f24e 144 APT_CASE(BY_HASH);
001c76fe 145#undef APT_CASE
8881b11e 146 case FILENAME: return _config->FindDir("Dir::State::lists") + URItoFileName(URI);
3fd89e62
DK
147 case EXISTING_FILENAME:
148 std::string const filename = Option(FILENAME);
d7a51997 149 std::vector<std::string> const types = VectorizeString(Option(COMPRESSIONTYPES), ' ');
3fd89e62
DK
150 for (std::vector<std::string>::const_iterator t = types.begin(); t != types.end(); ++t)
151 {
152 if (t->empty())
153 continue;
154 std::string const file = (*t == "uncompressed") ? filename : (filename + "." + *t);
155 if (FileExists(file))
156 return file;
157 }
158 return "";
001c76fe 159 }
e3c1cfc7
DK
160 std::map<std::string,std::string>::const_iterator const M = Options.find(Key);
161 if (M == Options.end())
162 return "";
163 return M->second;
164}
165 /*}}}*/
1a3a14ac
DK
166bool IndexTarget::OptionBool(OptionKeys const EnumKey) const /*{{{*/
167{
168 return StringToBool(Option(EnumKey));
169}
170 /*}}}*/
8881b11e
DK
171std::string IndexTarget::Format(std::string format) const /*{{{*/
172{
173 for (std::map<std::string, std::string>::const_iterator O = Options.begin(); O != Options.end(); ++O)
174 {
175 format = SubstVar(format, std::string("$(") + O->first + ")", O->second);
176 }
177 format = SubstVar(format, "$(METAKEY)", MetaKey);
178 format = SubstVar(format, "$(SHORTDESC)", ShortDesc);
179 format = SubstVar(format, "$(DESCRIPTION)", Description);
180 format = SubstVar(format, "$(URI)", URI);
181 format = SubstVar(format, "$(FILENAME)", Option(IndexTarget::FILENAME));
182 return format;
183}
184 /*}}}*/
e3c1cfc7 185
c9443c01
DK
186pkgDebianIndexTargetFile::pkgDebianIndexTargetFile(IndexTarget const &Target, bool const Trusted) :/*{{{*/
187 pkgDebianIndexFile(Trusted), d(NULL), Target(Target)
e3c1cfc7
DK
188{
189}
190 /*}}}*/
c9443c01 191std::string pkgDebianIndexTargetFile::ArchiveURI(std::string const &File) const/*{{{*/
e3c1cfc7 192{
001c76fe 193 return Target.Option(IndexTarget::REPO_URI) + File;
e3c1cfc7
DK
194}
195 /*}}}*/
c9443c01 196std::string pkgDebianIndexTargetFile::Describe(bool const Short) const /*{{{*/
e3c1cfc7
DK
197{
198 if (Short)
199 return Target.Description;
200 return Target.Description + " (" + IndexFileName() + ")";
201}
202 /*}}}*/
c9443c01 203std::string pkgDebianIndexTargetFile::IndexFileName() const /*{{{*/
e3c1cfc7 204{
8881b11e 205 std::string const s = Target.Option(IndexTarget::FILENAME);
e3c1cfc7
DK
206 if (FileExists(s))
207 return s;
208
d7a51997 209 std::vector<std::string> const types = VectorizeString(Target.Option(IndexTarget::COMPRESSIONTYPES), ' ');
e3c1cfc7
DK
210 for (std::vector<std::string>::const_iterator t = types.begin(); t != types.end(); ++t)
211 {
212 std::string p = s + '.' + *t;
213 if (FileExists(p))
214 return p;
215 }
216 return s;
217}
218 /*}}}*/
c9443c01 219unsigned long pkgDebianIndexTargetFile::Size() const /*{{{*/
e3c1cfc7
DK
220{
221 unsigned long size = 0;
222
223 /* we need to ignore errors here; if the lists are absent, just return 0 */
224 _error->PushToStack();
225
226 FileFd f(IndexFileName(), FileFd::ReadOnly, FileFd::Extension);
227 if (!f.Failed())
228 size = f.Size();
229
230 if (_error->PendingError() == true)
231 size = 0;
232 _error->RevertToStack();
233
234 return size;
235}
236 /*}}}*/
c9443c01 237bool pkgDebianIndexTargetFile::Exists() const /*{{{*/
e3c1cfc7
DK
238{
239 return FileExists(IndexFileName());
240}
241 /*}}}*/
c9443c01
DK
242std::string pkgDebianIndexTargetFile::GetArchitecture() const /*{{{*/
243{
244 return Target.Option(IndexTarget::ARCHITECTURE);
245}
246 /*}}}*/
247std::string pkgDebianIndexTargetFile::GetComponent() const /*{{{*/
248{
249 return Target.Option(IndexTarget::COMPONENT);
250}
251 /*}}}*/
252bool pkgDebianIndexTargetFile::OpenListFile(FileFd &Pkg, std::string const &FileName)/*{{{*/
253{
254 if (Pkg.Open(FileName, FileFd::ReadOnly, FileFd::Extension) == false)
255 return _error->Error("Problem opening %s",FileName.c_str());
256 return true;
257}
258 /*}}}*/
259std::string pkgDebianIndexTargetFile::GetProgressDescription() const
260{
261 return Target.Description;
262}
263
7f58427b
DK
264pkgDebianIndexRealFile::pkgDebianIndexRealFile(std::string const &pFile, bool const Trusted) :/*{{{*/
265 pkgDebianIndexFile(Trusted), d(NULL)
c9443c01 266{
7f58427b
DK
267 if (pFile == "/nonexistent/stdin")
268 File = pFile;
269 else
270 File = flAbsPath(pFile);
c9443c01
DK
271}
272 /*}}}*/
273// IndexRealFile::Size - Return the size of the index /*{{{*/
274unsigned long pkgDebianIndexRealFile::Size() const
275{
276 struct stat S;
277 if (stat(File.c_str(),&S) != 0)
278 return 0;
279 return S.st_size;
280}
281 /*}}}*/
282bool pkgDebianIndexRealFile::Exists() const /*{{{*/
283{
284 return FileExists(File);
285}
286 /*}}}*/
287std::string pkgDebianIndexRealFile::Describe(bool const /*Short*/) const/*{{{*/
288{
289 return File;
290}
291 /*}}}*/
292std::string pkgDebianIndexRealFile::ArchiveURI(std::string const &/*File*/) const/*{{{*/
293{
294 return "file:" + File;
295}
296 /*}}}*/
297std::string pkgDebianIndexRealFile::IndexFileName() const /*{{{*/
298{
299 return File;
300}
301 /*}}}*/
302std::string pkgDebianIndexRealFile::GetProgressDescription() const
303{
304 return File;
305}
306bool pkgDebianIndexRealFile::OpenListFile(FileFd &Pkg, std::string const &FileName)/*{{{*/
307{
308 if (Pkg.Open(FileName, FileFd::ReadOnly, FileFd::None) == false)
309 return _error->Error("Problem opening %s",FileName.c_str());
310 return true;
311}
312 /*}}}*/
313
314pkgDebianIndexFile::pkgDebianIndexFile(bool const Trusted) : pkgIndexFile(Trusted)
315{
316}
317pkgDebianIndexFile::~pkgDebianIndexFile()
318{
319}
320pkgCacheListParser * pkgDebianIndexFile::CreateListParser(FileFd &Pkg)
321{
322 if (Pkg.IsOpen() == false)
323 return NULL;
324 _error->PushToStack();
7f8c0eed 325 pkgCacheListParser * const Parser = new debListParser(&Pkg);
c9443c01
DK
326 bool const newError = _error->PendingError();
327 _error->MergeWithStack();
328 return newError ? NULL : Parser;
329}
330bool pkgDebianIndexFile::Merge(pkgCacheGenerator &Gen,OpProgress * const Prog)
331{
332 std::string const PackageFile = IndexFileName();
333 FileFd Pkg;
334 if (OpenListFile(Pkg, PackageFile) == false)
335 return false;
336 _error->PushToStack();
337 std::unique_ptr<pkgCacheListParser> Parser(CreateListParser(Pkg));
338 bool const newError = _error->PendingError();
95278287 339 _error->MergeWithStack();
c9443c01
DK
340 if (newError == false && Parser == nullptr)
341 return true;
342 if (Parser == NULL)
343 return false;
344
345 if (Prog != NULL)
346 Prog->SubProgress(0, GetProgressDescription());
347
348 if (Gen.SelectFile(PackageFile, *this, GetArchitecture(), GetComponent(), GetIndexFlags()) == false)
349 return _error->Error("Problem with SelectFile %s",PackageFile.c_str());
350
351 // Store the IMS information
352 pkgCache::PkgFileIterator File = Gen.GetCurFile();
353 pkgCacheGenerator::Dynamic<pkgCache::PkgFileIterator> DynFile(File);
354 File->Size = Pkg.FileSize();
355 File->mtime = Pkg.ModificationTime();
356
357 if (Gen.MergeList(*Parser) == false)
358 return _error->Error("Problem with MergeList %s",PackageFile.c_str());
359 return true;
360}
361pkgCache::PkgFileIterator pkgDebianIndexFile::FindInCache(pkgCache &Cache) const
362{
363 std::string const FileName = IndexFileName();
364 pkgCache::PkgFileIterator File = Cache.FileBegin();
365 for (; File.end() == false; ++File)
366 {
367 if (File.FileName() == NULL || FileName != File.FileName())
368 continue;
369
370 struct stat St;
371 if (stat(File.FileName(),&St) != 0)
372 {
373 if (_config->FindB("Debug::pkgCacheGen", false))
374 std::clog << "DebianIndexFile::FindInCache - stat failed on " << File.FileName() << std::endl;
375 return pkgCache::PkgFileIterator(Cache);
376 }
377 if ((map_filesize_t)St.st_size != File->Size || St.st_mtime != File->mtime)
378 {
379 if (_config->FindB("Debug::pkgCacheGen", false))
380 std::clog << "DebianIndexFile::FindInCache - size (" << St.st_size << " <> " << File->Size
381 << ") or mtime (" << St.st_mtime << " <> " << File->mtime
382 << ") doesn't match for " << File.FileName() << std::endl;
383 return pkgCache::PkgFileIterator(Cache);
384 }
385 return File;
386 }
387
388 return File;
389}
c8a4ce6c
DK
390
391APT_CONST pkgIndexFile::~pkgIndexFile() {}
c9443c01
DK
392APT_CONST pkgDebianIndexTargetFile::~pkgDebianIndexTargetFile() {}
393APT_CONST pkgDebianIndexRealFile::~pkgDebianIndexRealFile() {}