]> git.saurik.com Git - apt.git/blame_incremental - apt-pkg/indexfile.cc
run update post-invokes even on (partial) failures
[apt.git] / apt-pkg / indexfile.cc
... / ...
CommitLineData
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3// $Id: indexfile.cc,v 1.2.2.1 2003/12/24 23:09:17 mdz Exp $
4/* ######################################################################
5
6 Index File - Abstraction for an index of archive/souce file.
7
8 ##################################################################### */
9 /*}}}*/
10// Include Files /*{{{*/
11#include<config.h>
12
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/strutl.h>
23#include <apt-pkg/progress.h>
24#include <apt-pkg/deblistparser.h>
25#include <apt-pkg/macros.h>
26
27#include <apt-pkg/debindexfile.h>
28
29#include <sys/stat.h>
30
31#include <string>
32#include <vector>
33#include <clocale>
34#include <cstring>
35#include <memory>
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;
49 GlobalListLen++;
50 Label = NULL;
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;
62}
63 /*}}}*/
64pkgIndexFile::pkgIndexFile(bool const Trusted) : /*{{{*/
65 d(NULL), Trusted(Trusted)
66{
67}
68 /*}}}*/
69// IndexFile::ArchiveInfo - Stub /*{{{*/
70std::string pkgIndexFile::ArchiveInfo(pkgCache::VerIterator const &Ver) const
71{
72 debDebPkgFileIndex const * const debfile = dynamic_cast<debDebPkgFileIndex const*>(this);
73 if (debfile != nullptr)
74 return debfile->ArchiveInfo_impl(Ver);
75 return std::string();
76}
77 /*}}}*/
78// IndexFile::FindInCache - Stub /*{{{*/
79pkgCache::PkgFileIterator pkgIndexFile::FindInCache(pkgCache &Cache) const
80{
81 return pkgCache::PkgFileIterator(Cache);
82}
83 /*}}}*/
84// IndexFile::SourceIndex - Stub /*{{{*/
85std::string pkgIndexFile::SourceInfo(pkgSrcRecords::Parser const &/*Record*/,
86 pkgSrcRecords::File const &/*File*/) const
87{
88 return std::string();
89}
90 /*}}}*/
91// IndexFile::TranslationsAvailable - Check if will use Translation /*{{{*/
92bool pkgIndexFile::TranslationsAvailable() {
93 return (APT::Configuration::getLanguages().empty() != true);
94}
95 /*}}}*/
96// IndexFile::CheckLanguageCode - Check the Language Code /*{{{*/
97bool pkgIndexFile::CheckLanguageCode(const char * const Lang)
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 /*}}}*/
108// IndexFile::LanguageCode - Return the Language Code /*{{{*/
109std::string pkgIndexFile::LanguageCode() {
110APT_IGNORE_DEPRECATED_PUSH
111 if (TranslationsAvailable() == false)
112 return "";
113 return APT::Configuration::getLanguages()[0];
114APT_IGNORE_DEPRECATED_POP
115}
116 /*}}}*/
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,
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)
124{
125}
126 /*}}}*/
127std::string IndexTarget::Option(OptionKeys const EnumKey) const /*{{{*/
128{
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);
140 APT_CASE(IDENTIFIER);
141 APT_CASE(TARGET_OF);
142 APT_CASE(CREATED_BY);
143 APT_CASE(FALLBACK_OF);
144 APT_CASE(PDIFFS);
145 APT_CASE(DEFAULTENABLED);
146 APT_CASE(COMPRESSIONTYPES);
147 APT_CASE(SOURCESENTRY);
148 APT_CASE(BY_HASH);
149 APT_CASE(KEEPCOMPRESSEDAS);
150#undef APT_CASE
151 case FILENAME: return _config->FindDir("Dir::State::lists") + URItoFileName(URI);
152 case EXISTING_FILENAME:
153 std::string const filename = Option(FILENAME);
154 std::vector<std::string> const types = VectorizeString(Option(COMPRESSIONTYPES), ' ');
155 for (std::vector<std::string>::const_iterator t = types.begin(); t != types.end(); ++t)
156 {
157 if (t->empty())
158 continue;
159 std::string const file = (*t == "uncompressed") ? filename : (filename + "." + *t);
160 if (FileExists(file))
161 return file;
162 }
163 return "";
164 }
165 std::map<std::string,std::string>::const_iterator const M = Options.find(Key);
166 if (M == Options.end())
167 return "";
168 return M->second;
169}
170 /*}}}*/
171bool IndexTarget::OptionBool(OptionKeys const EnumKey) const /*{{{*/
172{
173 return StringToBool(Option(EnumKey));
174}
175 /*}}}*/
176std::string IndexTarget::Format(std::string format) const /*{{{*/
177{
178 for (std::map<std::string, std::string>::const_iterator O = Options.begin(); O != Options.end(); ++O)
179 {
180 format = SubstVar(format, std::string("$(") + O->first + ")", O->second);
181 }
182 format = SubstVar(format, "$(METAKEY)", MetaKey);
183 format = SubstVar(format, "$(SHORTDESC)", ShortDesc);
184 format = SubstVar(format, "$(DESCRIPTION)", Description);
185 format = SubstVar(format, "$(URI)", URI);
186 format = SubstVar(format, "$(FILENAME)", Option(IndexTarget::FILENAME));
187 return format;
188}
189 /*}}}*/
190
191pkgDebianIndexTargetFile::pkgDebianIndexTargetFile(IndexTarget const &Target, bool const Trusted) :/*{{{*/
192 pkgDebianIndexFile(Trusted), d(NULL), Target(Target)
193{
194}
195 /*}}}*/
196std::string pkgDebianIndexTargetFile::ArchiveURI(std::string const &File) const/*{{{*/
197{
198 return Target.Option(IndexTarget::REPO_URI) + File;
199}
200 /*}}}*/
201std::string pkgDebianIndexTargetFile::Describe(bool const Short) const /*{{{*/
202{
203 if (Short)
204 return Target.Description;
205 return Target.Description + " (" + IndexFileName() + ")";
206}
207 /*}}}*/
208std::string pkgDebianIndexTargetFile::IndexFileName() const /*{{{*/
209{
210 std::string const s = Target.Option(IndexTarget::FILENAME);
211 if (FileExists(s))
212 return s;
213
214 std::vector<std::string> const types = VectorizeString(Target.Option(IndexTarget::COMPRESSIONTYPES), ' ');
215 for (std::vector<std::string>::const_iterator t = types.begin(); t != types.end(); ++t)
216 {
217 std::string p = s + '.' + *t;
218 if (FileExists(p))
219 return p;
220 }
221 return s;
222}
223 /*}}}*/
224unsigned long pkgDebianIndexTargetFile::Size() const /*{{{*/
225{
226 unsigned long size = 0;
227
228 /* we need to ignore errors here; if the lists are absent, just return 0 */
229 _error->PushToStack();
230
231 FileFd f(IndexFileName(), FileFd::ReadOnly, FileFd::Extension);
232 if (!f.Failed())
233 size = f.Size();
234
235 if (_error->PendingError() == true)
236 size = 0;
237 _error->RevertToStack();
238
239 return size;
240}
241 /*}}}*/
242bool pkgDebianIndexTargetFile::Exists() const /*{{{*/
243{
244 return FileExists(IndexFileName());
245}
246 /*}}}*/
247std::string pkgDebianIndexTargetFile::GetArchitecture() const /*{{{*/
248{
249 return Target.Option(IndexTarget::ARCHITECTURE);
250}
251 /*}}}*/
252std::string pkgDebianIndexTargetFile::GetComponent() const /*{{{*/
253{
254 return Target.Option(IndexTarget::COMPONENT);
255}
256 /*}}}*/
257bool pkgDebianIndexTargetFile::OpenListFile(FileFd &Pkg, std::string const &FileName)/*{{{*/
258{
259 if (Pkg.Open(FileName, FileFd::ReadOnly, FileFd::Extension) == false)
260 return _error->Error("Problem opening %s",FileName.c_str());
261 return true;
262}
263 /*}}}*/
264std::string pkgDebianIndexTargetFile::GetProgressDescription() const
265{
266 return Target.Description;
267}
268
269pkgDebianIndexRealFile::pkgDebianIndexRealFile(std::string const &pFile, bool const Trusted) :/*{{{*/
270 pkgDebianIndexFile(Trusted), d(NULL)
271{
272 if (pFile.empty())
273 ;
274 else if (pFile == "/nonexistent/stdin")
275 File = pFile;
276 else
277 File = flAbsPath(pFile);
278}
279 /*}}}*/
280// IndexRealFile::Size - Return the size of the index /*{{{*/
281unsigned long pkgDebianIndexRealFile::Size() const
282{
283 struct stat S;
284 if (stat(File.c_str(),&S) != 0)
285 return 0;
286 return S.st_size;
287}
288 /*}}}*/
289bool pkgDebianIndexRealFile::Exists() const /*{{{*/
290{
291 return FileExists(File);
292}
293 /*}}}*/
294std::string pkgDebianIndexRealFile::Describe(bool const /*Short*/) const/*{{{*/
295{
296 return File;
297}
298 /*}}}*/
299std::string pkgDebianIndexRealFile::ArchiveURI(std::string const &/*File*/) const/*{{{*/
300{
301 return "file:" + File;
302}
303 /*}}}*/
304std::string pkgDebianIndexRealFile::IndexFileName() const /*{{{*/
305{
306 return File;
307}
308 /*}}}*/
309std::string pkgDebianIndexRealFile::GetProgressDescription() const
310{
311 return File;
312}
313bool pkgDebianIndexRealFile::OpenListFile(FileFd &Pkg, std::string const &FileName)/*{{{*/
314{
315 if (Pkg.Open(FileName, FileFd::ReadOnly, FileFd::Extension) == false)
316 return _error->Error("Problem opening %s",FileName.c_str());
317 return true;
318}
319 /*}}}*/
320
321pkgDebianIndexFile::pkgDebianIndexFile(bool const Trusted) : pkgIndexFile(Trusted)
322{
323}
324pkgDebianIndexFile::~pkgDebianIndexFile()
325{
326}
327pkgCacheListParser * pkgDebianIndexFile::CreateListParser(FileFd &Pkg)
328{
329 if (Pkg.IsOpen() == false)
330 return nullptr;
331 _error->PushToStack();
332 pkgCacheListParser * const Parser = new debListParser(&Pkg);
333 bool const newError = _error->PendingError();
334 _error->MergeWithStack();
335 if (newError)
336 {
337 delete Parser;
338 return nullptr;
339 }
340 else
341 return Parser;
342}
343bool pkgDebianIndexFile::Merge(pkgCacheGenerator &Gen,OpProgress * const Prog)
344{
345 std::string const PackageFile = IndexFileName();
346 FileFd Pkg;
347 if (OpenListFile(Pkg, PackageFile) == false)
348 return false;
349 _error->PushToStack();
350 std::unique_ptr<pkgCacheListParser> Parser(CreateListParser(Pkg));
351 bool const newError = _error->PendingError();
352 _error->MergeWithStack();
353 if (newError == false && Parser == nullptr)
354 return true;
355 if (Parser == NULL)
356 return false;
357
358 if (Prog != NULL)
359 Prog->SubProgress(0, GetProgressDescription());
360
361 if (Gen.SelectFile(PackageFile, *this, GetArchitecture(), GetComponent(), GetIndexFlags()) == false)
362 return _error->Error("Problem with SelectFile %s",PackageFile.c_str());
363
364 // Store the IMS information
365 pkgCache::PkgFileIterator File = Gen.GetCurFile();
366 pkgCacheGenerator::Dynamic<pkgCache::PkgFileIterator> DynFile(File);
367 File->Size = Pkg.FileSize();
368 File->mtime = Pkg.ModificationTime();
369
370 if (Gen.MergeList(*Parser) == false)
371 return _error->Error("Problem with MergeList %s",PackageFile.c_str());
372 return true;
373}
374pkgCache::PkgFileIterator pkgDebianIndexFile::FindInCache(pkgCache &Cache) const
375{
376 std::string const FileName = IndexFileName();
377 pkgCache::PkgFileIterator File = Cache.FileBegin();
378 for (; File.end() == false; ++File)
379 {
380 if (File.FileName() == NULL || FileName != File.FileName())
381 continue;
382
383 struct stat St;
384 if (stat(File.FileName(),&St) != 0)
385 {
386 if (_config->FindB("Debug::pkgCacheGen", false))
387 std::clog << "DebianIndexFile::FindInCache - stat failed on " << File.FileName() << std::endl;
388 return pkgCache::PkgFileIterator(Cache);
389 }
390 if ((map_filesize_t)St.st_size != File->Size || St.st_mtime != File->mtime)
391 {
392 if (_config->FindB("Debug::pkgCacheGen", false))
393 std::clog << "DebianIndexFile::FindInCache - size (" << St.st_size << " <> " << File->Size
394 << ") or mtime (" << St.st_mtime << " <> " << File->mtime
395 << ") doesn't match for " << File.FileName() << std::endl;
396 return pkgCache::PkgFileIterator(Cache);
397 }
398 return File;
399 }
400
401 return File;
402}
403
404APT_CONST pkgIndexFile::~pkgIndexFile() {}
405APT_CONST pkgDebianIndexTargetFile::~pkgDebianIndexTargetFile() {}
406APT_CONST pkgDebianIndexRealFile::~pkgDebianIndexRealFile() {}