apt  2.2.4
About: Apt (Advanced Package Tool) is a management system for software packages (Debian/Ubuntu). Release series 2.2.
  Fossies Dox: apt-2.2.4.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

indexfile.cc
Go to the documentation of this file.
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 /* ######################################################################
4 
5  Index File - Abstraction for an index of archive/source file.
6 
7  ##################################################################### */
8  /*}}}*/
9 // Include Files /*{{{*/
10 #include <config.h>
11 
12 #include <apt-pkg/acquire.h>
14 #include <apt-pkg/configuration.h>
15 #include <apt-pkg/deblistparser.h>
16 #include <apt-pkg/error.h>
17 #include <apt-pkg/fileutl.h>
18 #include <apt-pkg/indexfile.h>
19 #include <apt-pkg/macros.h>
20 #include <apt-pkg/pkgcache.h>
21 #include <apt-pkg/pkgcachegen.h>
22 #include <apt-pkg/progress.h>
23 #include <apt-pkg/srcrecords.h>
24 #include <apt-pkg/strutl.h>
25 
26 #include <apt-pkg/debindexfile.h>
27 
28 #include <sys/stat.h>
29 
30 #include <clocale>
31 #include <cstring>
32 #include <memory>
33 #include <string>
34 #include <vector>
35  /*}}}*/
36 
37 // Global list of Item supported
40 unsigned long pkgIndexFile::Type::GlobalListLen = 0;
41 
42 // Type::Type - Constructor /*{{{*/
43 // ---------------------------------------------------------------------
44 /* */
46 {
47  ItmList[GlobalListLen] = this;
48  GlobalListLen++;
49  Label = NULL;
50 }
51  /*}}}*/
52 // Type::GetType - Locate the type by name /*{{{*/
53 // ---------------------------------------------------------------------
54 /* */
56 {
57  for (unsigned I = 0; I != GlobalListLen; I++)
58  if (strcmp(GlobalList[I]->Label,Type) == 0)
59  return GlobalList[I];
60  return 0;
61 }
62  /*}}}*/
63 pkgIndexFile::pkgIndexFile(bool const Trusted) : /*{{{*/
64  d(NULL), Trusted(Trusted)
65 {
66 }
67  /*}}}*/
68 // IndexFile::ArchiveInfo - Stub /*{{{*/
70 {
71  return std::string();
72 }
73  /*}}}*/
74 // IndexFile::FindInCache - Stub /*{{{*/
76 {
77  return pkgCache::PkgFileIterator(Cache);
78 }
79  /*}}}*/
80 // IndexFile::SourceIndex - Stub /*{{{*/
81 std::string pkgIndexFile::SourceInfo(pkgSrcRecords::Parser const &/*Record*/,
82  pkgSrcRecords::File const &/*File*/) const
83 {
84  return std::string();
85 }
86  /*}}}*/
87 
88 // IndexTarget - Constructor /*{{{*/
89 IndexTarget::IndexTarget(std::string const &MetaKey, std::string const &ShortDesc,
90  std::string const &LongDesc, std::string const &URI, bool const IsOptional,
91  bool const KeepCompressed, std::map<std::string, std::string> const &Options) :
92  URI(URI), Description(LongDesc), ShortDesc(ShortDesc), MetaKey(MetaKey),
93  IsOptional(IsOptional), KeepCompressed(KeepCompressed), Options(Options)
94 {
95 }
96  /*}}}*/
97 std::string IndexTarget::Option(OptionKeys const EnumKey) const /*{{{*/
98 {
99  std::string Key;
100  switch (EnumKey)
101  {
102 #define APT_CASE(X) case X: Key = #X; break
103  APT_CASE(SITE);
104  APT_CASE(RELEASE);
105  APT_CASE(COMPONENT);
106  APT_CASE(LANGUAGE);
107  APT_CASE(ARCHITECTURE);
108  APT_CASE(BASE_URI);
109  APT_CASE(REPO_URI);
110  APT_CASE(IDENTIFIER);
111  APT_CASE(TARGET_OF);
112  APT_CASE(CREATED_BY);
113  APT_CASE(FALLBACK_OF);
114  APT_CASE(PDIFFS);
115  APT_CASE(DEFAULTENABLED);
116  APT_CASE(COMPRESSIONTYPES);
117  APT_CASE(SOURCESENTRY);
118  APT_CASE(BY_HASH);
119  APT_CASE(KEEPCOMPRESSEDAS);
120  APT_CASE(ALLOW_INSECURE);
121  APT_CASE(ALLOW_WEAK);
122  APT_CASE(ALLOW_DOWNGRADE_TO_INSECURE);
123  APT_CASE(INRELEASE_PATH);
124 #undef APT_CASE
125  case FILENAME:
126  {
127  auto const M = Options.find("FILENAME");
128  if (M == Options.end())
129  return _config->FindDir("Dir::State::lists") + URItoFileName(URI);
130  return M->second;
131  }
132  case EXISTING_FILENAME:
133  std::string const filename = Option(FILENAME);
134  std::vector<std::string> const types = VectorizeString(Option(COMPRESSIONTYPES), ' ');
135  for (std::vector<std::string>::const_iterator t = types.begin(); t != types.end(); ++t)
136  {
137  if (t->empty())
138  continue;
139  std::string const file = (*t == "uncompressed") ? filename : (filename + "." + *t);
140  if (FileExists(file))
141  return file;
142  }
143  return "";
144  }
145  std::map<std::string,std::string>::const_iterator const M = Options.find(Key);
146  if (M == Options.end())
147  return "";
148  return M->second;
149 }
150  /*}}}*/
151 bool IndexTarget::OptionBool(OptionKeys const EnumKey) const /*{{{*/
152 {
153  return StringToBool(Option(EnumKey), false);
154 }
155  /*}}}*/
156 std::string IndexTarget::Format(std::string format) const /*{{{*/
157 {
158  for (std::map<std::string, std::string>::const_iterator O = Options.begin(); O != Options.end(); ++O)
159  {
160  format = SubstVar(format, std::string("$(") + O->first + ")", O->second);
161  }
162  format = SubstVar(format, "$(METAKEY)", MetaKey);
163  format = SubstVar(format, "$(SHORTDESC)", ShortDesc);
164  format = SubstVar(format, "$(DESCRIPTION)", Description);
165  format = SubstVar(format, "$(URI)", URI);
166  format = SubstVar(format, "$(FILENAME)", Option(IndexTarget::FILENAME));
167  return format;
168 }
169  /*}}}*/
170 
171 pkgDebianIndexTargetFile::pkgDebianIndexTargetFile(IndexTarget const &Target, bool const Trusted) :/*{{{*/
172  pkgDebianIndexFile(Trusted), d(NULL), Target(Target)
173 {
174 }
175  /*}}}*/
176 std::string pkgDebianIndexTargetFile::ArchiveURI(std::string const &File) const/*{{{*/
177 {
178  return Target.Option(IndexTarget::REPO_URI) + pkgAcquire::URIEncode(File);
179 }
180  /*}}}*/
181 std::string pkgDebianIndexTargetFile::Describe(bool const Short) const /*{{{*/
182 {
183  if (Short)
184  return Target.Description;
185  return Target.Description + " (" + IndexFileName() + ")";
186 }
187  /*}}}*/
188 std::string pkgDebianIndexTargetFile::IndexFileName() const /*{{{*/
189 {
190  std::string const s = Target.Option(IndexTarget::FILENAME);
191  if (FileExists(s))
192  return s;
193 
194  std::vector<std::string> const types = VectorizeString(Target.Option(IndexTarget::COMPRESSIONTYPES), ' ');
195  for (std::vector<std::string>::const_iterator t = types.begin(); t != types.end(); ++t)
196  {
197  std::string p = s + '.' + *t;
198  if (FileExists(p))
199  return p;
200  }
201  return s;
202 }
203  /*}}}*/
204 unsigned long pkgDebianIndexTargetFile::Size() const /*{{{*/
205 {
206  unsigned long size = 0;
207 
208  /* we need to ignore errors here; if the lists are absent, just return 0 */
209  _error->PushToStack();
210 
212  if (!f.Failed())
213  size = f.Size();
214 
215  if (_error->PendingError() == true)
216  size = 0;
217  _error->RevertToStack();
218 
219  return size;
220 }
221  /*}}}*/
223 {
224  return FileExists(IndexFileName());
225 }
226  /*}}}*/
227 std::string pkgDebianIndexTargetFile::GetArchitecture() const /*{{{*/
228 {
230 }
231  /*}}}*/
232 std::string pkgDebianIndexTargetFile::GetComponent() const /*{{{*/
233 {
235 }
236  /*}}}*/
237 bool pkgDebianIndexTargetFile::OpenListFile(FileFd &Pkg, std::string const &FileName)/*{{{*/
238 {
239  if (Pkg.Open(FileName, FileFd::ReadOnly, FileFd::Extension) == false)
240  return _error->Error("Problem opening %s",FileName.c_str());
241  return true;
242 }
243  /*}}}*/
245 {
246  return Target.Description;
247 }
249 {
250  return Target;
251 }
252 
253 pkgDebianIndexRealFile::pkgDebianIndexRealFile(std::string const &pFile, bool const Trusted) :/*{{{*/
254  pkgDebianIndexFile(Trusted), d(NULL)
255 {
256  if (pFile.empty())
257  ;
258  else if (pFile == "/nonexistent/stdin")
259  File = pFile;
260  else
261  File = flAbsPath(pFile);
262 }
263  /*}}}*/
264 // IndexRealFile::Size - Return the size of the index /*{{{*/
265 unsigned long pkgDebianIndexRealFile::Size() const
266 {
267  struct stat S;
268  if (stat(File.c_str(),&S) != 0)
269  return 0;
270  return S.st_size;
271 }
272  /*}}}*/
273 bool pkgDebianIndexRealFile::Exists() const /*{{{*/
274 {
275  return FileExists(File);
276 }
277  /*}}}*/
278 std::string pkgDebianIndexRealFile::Describe(bool const /*Short*/) const/*{{{*/
279 {
280  return File;
281 }
282  /*}}}*/
283 std::string pkgDebianIndexRealFile::ArchiveURI(std::string const &/*File*/) const/*{{{*/
284 {
285  return "file:" + pkgAcquire::URIEncode(File);
286 }
287  /*}}}*/
288 std::string pkgDebianIndexRealFile::IndexFileName() const /*{{{*/
289 {
290  return File;
291 }
292  /*}}}*/
294 {
295  return File;
296 }
297 bool pkgDebianIndexRealFile::OpenListFile(FileFd &Pkg, std::string const &FileName)/*{{{*/
298 {
299  if (Pkg.Open(FileName, FileFd::ReadOnly, FileFd::Extension) == false)
300  return _error->Error("Problem opening %s",FileName.c_str());
301  return true;
302 }
303  /*}}}*/
304 
306 {
307 }
309 {
310 }
312 {
313  if (Pkg.IsOpen() == false)
314  return nullptr;
315  _error->PushToStack();
316  std::unique_ptr<pkgCacheListParser> Parser(new debListParser(&Pkg));
317  bool const newError = _error->PendingError();
318  _error->MergeWithStack();
319  return newError ? nullptr : Parser.release();
320 }
322 {
323  std::string const PackageFile = IndexFileName();
324  FileFd Pkg;
325  if (OpenListFile(Pkg, PackageFile) == false)
326  return false;
327  _error->PushToStack();
328  std::unique_ptr<pkgCacheListParser> Parser(CreateListParser(Pkg));
329  bool const newError = _error->PendingError();
330  _error->MergeWithStack();
331  if (newError == false && Parser == nullptr)
332  return true;
333  if (Parser == NULL)
334  return false;
335 
336  if (Prog != NULL)
338 
339  if (Gen.SelectFile(PackageFile, *this, GetArchitecture(), GetComponent(), GetIndexFlags()) == false)
340  return _error->Error("Problem with SelectFile %s",PackageFile.c_str());
341 
342  // Store the IMS information
345  File->Size = Pkg.FileSize();
346  File->mtime = Pkg.ModificationTime();
347 
348  if (Gen.MergeList(*Parser) == false)
349  return _error->Error("Problem with MergeList %s",PackageFile.c_str());
350  return true;
351 }
353 {
354  std::string const FileName = IndexFileName();
355  pkgCache::PkgFileIterator File = Cache.FileBegin();
356  for (; File.end() == false; ++File)
357  {
358  if (File.FileName() == NULL || FileName != File.FileName())
359  continue;
360 
361  struct stat St;
362  if (stat(File.FileName(),&St) != 0)
363  {
364  if (_config->FindB("Debug::pkgCacheGen", false))
365  std::clog << "DebianIndexFile::FindInCache - stat failed on " << File.FileName() << std::endl;
366  return pkgCache::PkgFileIterator(Cache);
367  }
368  if ((map_filesize_t)St.st_size != File->Size || St.st_mtime != File->mtime)
369  {
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);
375  }
376  return File;
377  }
378 
379  return File;
380 }
381 
static bool std::string const metaIndex const *const pkgAcqMetaClearSig *const pkgAcquire::Item *const I
bool FindB(const char *Name, bool const &Default=false) const
std::string FindDir(const char *Name, const char *Default=0) const
Definition: fileutl.h:39
bool IsOpen()
Definition: fileutl.h:150
@ Extension
Definition: fileutl.h:80
@ ReadOnly
Definition: fileutl.h:59
unsigned long long FileSize()
Definition: fileutl.cc:2943
bool Failed()
Definition: fileutl.h:151
bool Open(std::string FileName, unsigned int const Mode, CompressMode Compress, unsigned long const AccessMode=0666)
Definition: fileutl.cc:2415
time_t ModificationTime()
Definition: fileutl.cc:2955
unsigned long long Size()
Definition: fileutl.cc:2967
Information about an index file.
Definition: indexfile.h:39
std::string Format(std::string format) const
Definition: indexfile.cc:156
std::string Description
A description of the index file.
Definition: indexfile.h:45
std::string Option(OptionKeys const Key) const
Definition: indexfile.cc:97
@ ARCHITECTURE
Definition: indexfile.h:75
@ COMPRESSIONTYPES
Definition: indexfile.h:83
bool OptionBool(OptionKeys const Key) const
Definition: indexfile.cc:151
IndexTarget(std::string const &MetaKey, std::string const &ShortDesc, std::string const &LongDesc, std::string const &URI, bool const IsOptional, bool const KeepCompressed, std::map< std::string, std::string > const &Options)
Definition: indexfile.cc:89
void SubProgress(unsigned long long SubTotal, const std::string &Op="", float const Percent=-1)
Definition: progress.cc:73
Definition: strutl.h:193
static APT_HIDDEN std::string URIEncode(std::string const &part)
Definition: acquire.cc:54
bool MergeList(ListParser &List, pkgCache::VerIterator *Ver=0)
Definition: pkgcachegen.cc:241
pkgCache::PkgFileIterator GetCurFile()
Definition: pkgcachegen.h:138
bool SelectFile(const std::string &File, pkgIndexFile const &Index, std::string const &Architecture, std::string const &Component, unsigned long Flags=0)
PkgFileIterator FileBegin()
Definition: pkgcache.h:814
virtual ~pkgDebianIndexFile()
Definition: indexfile.cc:308
virtual bool OpenListFile(FileFd &Pkg, std::string const &FileName)=0
virtual std::string IndexFileName() const =0
virtual bool Merge(pkgCacheGenerator &Gen, OpProgress *const Prog) APT_OVERRIDE
Definition: indexfile.cc:321
virtual APT_HIDDEN pkgCacheListParser * CreateListParser(FileFd &Pkg)
Definition: indexfile.cc:311
pkgDebianIndexFile(bool const Trusted)
Definition: indexfile.cc:305
virtual pkgCache::PkgFileIterator FindInCache(pkgCache &Cache) const APT_OVERRIDE
Definition: indexfile.cc:352
virtual std::string GetArchitecture() const =0
virtual std::string GetComponent() const =0
virtual uint8_t GetIndexFlags() const =0
virtual std::string GetProgressDescription() const =0
virtual unsigned long Size() const APT_OVERRIDE
Definition: indexfile.cc:265
virtual std::string IndexFileName() const APT_OVERRIDE
Definition: indexfile.cc:288
virtual bool Exists() const APT_OVERRIDE
Definition: indexfile.cc:273
virtual std::string Describe(bool const =false) const APT_OVERRIDE
Definition: indexfile.cc:278
virtual ~pkgDebianIndexRealFile()
Definition: indexfile.cc:384
virtual std::string ArchiveURI(std::string const &) const APT_OVERRIDE
Definition: indexfile.cc:283
virtual bool OpenListFile(FileFd &Pkg, std::string const &FileName) APT_OVERRIDE
Definition: indexfile.cc:297
pkgDebianIndexRealFile(std::string const &File, bool const Trusted)
Definition: indexfile.cc:253
virtual std::string GetProgressDescription() const APT_OVERRIDE
Definition: indexfile.cc:293
virtual unsigned long Size() const APT_OVERRIDE
Definition: indexfile.cc:204
virtual std::string ArchiveURI(std::string const &File) const APT_OVERRIDE
Definition: indexfile.cc:176
virtual std::string IndexFileName() const APT_OVERRIDE
Definition: indexfile.cc:188
virtual bool Exists() const APT_OVERRIDE
Definition: indexfile.cc:222
pkgDebianIndexTargetFile(IndexTarget const &Target, bool const Trusted)
Definition: indexfile.cc:171
virtual std::string GetArchitecture() const APT_OVERRIDE
Definition: indexfile.cc:227
virtual bool OpenListFile(FileFd &Pkg, std::string const &FileName) APT_OVERRIDE
Definition: indexfile.cc:237
IndexTarget GetIndexTarget() const APT_HIDDEN
Definition: indexfile.cc:248
virtual std::string Describe(bool const Short=false) const APT_OVERRIDE
Definition: indexfile.cc:181
virtual ~pkgDebianIndexTargetFile()
Definition: indexfile.cc:383
virtual std::string GetComponent() const APT_OVERRIDE
Definition: indexfile.cc:232
IndexTarget const Target
Definition: indexfile.h:176
virtual std::string GetProgressDescription() const APT_OVERRIDE
Definition: indexfile.cc:244
const char * Label
Definition: indexfile.h:118
static Type ** GlobalList
Definition: indexfile.h:114
static unsigned long GlobalListLen
Definition: indexfile.h:115
static Type * GetType(const char *const Type) APT_PURE
Definition: indexfile.cc:55
bool Trusted
Definition: indexfile.h:105
virtual ~pkgIndexFile()
Definition: indexfile.cc:382
virtual std::string SourceInfo(pkgSrcRecords::Parser const &Record, pkgSrcRecords::File const &File) const
Definition: indexfile.cc:81
virtual std::string ArchiveInfo(pkgCache::VerIterator const &Ver) const
Definition: indexfile.cc:69
pkgIndexFile(bool const Trusted)
Definition: indexfile.cc:63
virtual pkgCache::PkgFileIterator FindInCache(pkgCache &Cache) const
Definition: indexfile.cc:75
void *const d
Definition: indexfile.h:103
Configuration * _config
bool FileExists(string File)
Definition: fileutl.cc:326
string flAbsPath(string File)
Takes a file path and returns the absolute path.
Definition: fileutl.cc:757
static pkgIndexFile::Type * ItmList[10]
Definition: indexfile.cc:38
#define APT_CASE(X)
pkgCache - Structure definitions for the cache file
uint64_t map_filesize_t
Definition: pkgcache.h:89
int StringToBool(const string &Text, int Default)
Definition: strutl.cc:820
string SubstVar(const string &Str, const string &Subst, const string &Contents)
Definition: strutl.cc:502
string URItoFileName(const string &URI)
Definition: strutl.cc:553
vector< string > VectorizeString(string const &haystack, char const &split)
Definition: strutl.cc:1308