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